This is a mess...

N

Nick

I've been coding python for about a week now, and i'm trying to make
an object oriented version of a program i just wrote. in this post is
the original program. the next post will include the new programs and
the traceback. i'm sure there are many underlying problems, but have
been stuck on this for 2 days now...

email me for input files since they are big and i don't see a way to
upload a file here(~5 mB)
this program works, and inputing the same file twice you should get a
"Q" values of ~1 (.99...)

##############CODE#####################

#9July09
#Compute the covariance overlap of two results
#Read in _U.asc files for both ensembles to find e-vectors
#Read in _ .asc files for both ensembles to find sqrt e-values
#strip out matrix info
#compute corvariance


import sys
import math
from math import sqrt


if len(sys.argv) != 3:
print " "
print "The corrent usage is 'python covariance.py file1 file2'"
print "where the _U.asc and _s.asc will be appended when needed"
print " "
exit(1)


####Input the first prefix1_U.asc file:
####this is the first eigen vector matrix
prefix1 = sys.argv[1] + "_U.asc"
fileholder = open(prefix1)
text = fileholder.readlines()
fields = text[1].split()
num_rows = int(fields[1])
num_cols = int(fields[2])

U1_matrix = []
for line in text[2: num_rows+2]:
fields = line.split()
for i in range(len(fields)):
fields = float(fields)
U1_matrix.append(fields)

####Input the second prefix2_U.asc file:
####this is the 2nd eigen vector matrix
prefix2 = sys.argv[2] + "_U.asc"
fileholder = open(prefix2)
text = fileholder.readlines()
fields = text[1].split()
num_rows = int(fields[1])
num_cols = int(fields[2])
#print "look here nick:", fields

U2_matrix = []
for line in text[2: num_rows+2]:
fields = line.split()
for i in range(len(fields)):
fields = float(fields)
U2_matrix.append(fields)

####Then Read in the first eigen values
####1st 2 lines are header and need to be stripped away
prefix3 = sys.argv[1] + "_s.asc"
fileholder = open(prefix3)
text = fileholder.readlines()
fields = text[1].split()
num_vals1 = int(fields[1]) #add check to see if correct # of values
added
print "square if", len(U1_matrix), "=", len(U2_matrix), "=", len
(U1_matrix[1]), "=", len(U2_matrix[1])
#the list of sqrt e vals
sqrt_s1 = [float(line) for line in text[2: num_vals1+2]]
s1 = [float(line) * float(line) for line in text[2: num_vals1+2]]

####Finally, read in the 2nd set of eigen vals
prefix4 = sys.argv[2] + "_s.asc"
fileholder = open(prefix4)
text = fileholder.readlines()
fields = text[1].split()
num_vals2 = int(fields[1]) #add check to see if correct # of values
added
#the list of sqrt e vals
sqrt_s2 = [float(line) for line in text[2: num_vals1+2]]
s2 = [float(line) * float(line) for line in text[2: num_vals1+2]]


#=============================================================
####double summation (the 2nd term)
doublesum = 0.0
for i in range(len(U1_matrix[1])):
#print "i = ", i
v1 = U1_matrix
sum = 0
for j in range(len(U2_matrix)):
v2 = U2_matrix[j]
dot = 0
for k in range(len(U1_matrix)):
dot += v1[k] * v2[k]
root = sqrt_s1 * sqrt_s2[j]
sum += root * dot * dot
doublesum += sum


print "double sum: ", doublesum

####single summation (the 1st term and denominator)
singsum = 0.0
for k in range(len(s1)):
singsum += s1[k] + s2[k]
print "sing sum:", singsum


####Put it all together
Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum))
print "your Q:"
print Q

############################# end of code covariance.py
 
N

Nick

this is the new oop version, its pretty messy currently, and i do
understand it is a simple routine, but i'm using it as an exercise to
learn oop python...

first the (current) traceback:
[:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test
<Eigen.Eigen_vect instance at 0x7fa26c404ab8>
<Eigen.Eigen_vect instance at 0x7fa26c404ab8>
Traceback (most recent call last):
File "oop_covariance.py", line 24, in <module>
cov = set1.covariance(set2, Eigen_vect.dot)
File "/home/nleioatts/python/Eigen.py", line 66, in covariance
print self.vectors
AttributeError: Eigen_vect instance has no attribute '__getitem__'

and a quick explaination: the file structures aare a 2d list of lists
of numbers corresponding to eginvectors and a list of numbers
corresponding to eigenvalues

#########################33##Here is the main body
#!/usr/bin/env python
import sys
import Eigen
from Eigen import *

if len(sys.argv) != 3:
print " "
print "The corrent usage is 'python covariance.py file1 file2'"
print "where the _U.asc and _s.asc will be appended when needed"
print " "
exit(1)
file1 = sys.argv[1]
file2 = sys.argv[2]
set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc")
set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc")
cov = set1.covariance(set2, Eigen_vect.dot)
print cov


###############and here are the classes:

#!/usr/bin/env python
import sys
import math
from math import sqrt

class Eigen_vect:
def __init__(self, e_val, e_vect):
self.e_val = e_val
self.e_vect = e_vect
def length(self):
return len(self.e_vect)

def dot(self, other):
d = 0.0
if other.length() != self.length():
raise ValueError, "Eigen Vectors not same Length"
for k in range(self.length()):
# print "HI NICK", self.e_vect[k], other.e_vect[k]
d += float(self.e_vect[k]) * float(other.e_vect[k])
return d


class Eigen_set:
def __init__(self, vec_filename, val_filename):
self.vec_filename = vec_filename
self.val_filename = val_filename
# open two files
# loop through them, skipping lines that begin with #
# for each row, extract eigen vector and eigen values
fileholder = open(self.vec_filename)
text = fileholder.readlines()
fields = text[2].split()
# print "len of fields", len(fields)
self.vectors = []
for line in text[2: len(fields)]:
fields = line.split()
# print "len of fields", len(fields)
for i in range(len(fields)):
fields = float(fields)
e_vect = fields
fileholder = open(self.val_filename)
text = fileholder.readlines()
e_val = [float(line) for line in text[2: self.length()]]
self.vectors.append(Eigen_vect(e_val, e_vect))
# print "this is self.vectors"
# print self.vectors(e_val)

def length(self):
return len(self.vectors)

def covariance(self, other, dot):
newdot = 0.0
# do a length check to make sure we're consistent
if other.length() != self.length():
raise ValueError, "Eigen Vectors not same Length"
#double loop over all my vectors and all of other's vectors
doublesum = 0.0
for i in range(self.length()):
sum = 0.0
v1 = self.vectors
for j in range(self.length()):
newdot += v1.dot(self.vectors[j])
# root = self.e_val * other.e_val[j]
print self.vectors
print self.vectors[j]
print self.vectors
#####################<<------------------------This is line 66, I'm
trying to figure out how to call "e_val" from the Eigen_set class

root = self.vectors * other.vectors[j]
sum += newdot * newdot * root
doublesum += sum
#########
singsum = 0.0
for k in range(self.length()):
singsum += self.e_val[k] * self.e_val[k] + other.e_val[k]
* other.e_val[k]
Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum))
print "your Q:"
print Q



and any additional help is great. thanks in advance, like the title
says this is really a mess....
 
P

Piet van Oostrum

Nick said:
N> this is the new oop version, its pretty messy currently, and i do
N> understand it is a simple routine, but i'm using it as an exercise to
N> learn oop python...
N> first the (current) traceback:
N> [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test
N> <Eigen.Eigen_vect instance at 0x7fa26c404ab8>
N> <Eigen.Eigen_vect instance at 0x7fa26c404ab8>
N> Traceback (most recent call last):
N> File "oop_covariance.py", line 24, in <module>
N> cov = set1.covariance(set2, Eigen_vect.dot)
N> File "/home/nleioatts/python/Eigen.py", line 66, in covariance
N> print self.vectors
N> AttributeError: Eigen_vect instance has no attribute '__getitem__'


self.vectors is a list of Eigen_vect objects. So self.vectors is an
Eigen_vect object. Now you do a subscript on this Eigen_vect object but
you have not defined what that means. Presumably you want it to do the
subscript on the self.e_vect in the Eigen_vect instance. Therefore you
have to define the __getitem__ method in the Eigen_vect class.


class Eigen_vect:
def __init__(self, e_val, e_vect):
self.e_val = e_val
self.e_vect = e_vect
def length(self):
return len(self.e_vect)

def __getitem__(self, indx):
return self.e_vect[indx]

I hope I did not make a mistake, I didn't check it because I don't want
to make test input files.
 
T

Terry Reedy

Nick said:
this is the new oop version, its pretty messy currently, and i do
understand it is a simple routine, but i'm using it as an exercise to
learn oop python...

You neglected to specify Python version.
first the (current) traceback:
[:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test
<Eigen.Eigen_vect instance at 0x7fa26c404ab8>
<Eigen.Eigen_vect instance at 0x7fa26c404ab8>
Traceback (most recent call last):
File "oop_covariance.py", line 24, in <module>
cov = set1.covariance(set2, Eigen_vect.dot)
File "/home/nleioatts/python/Eigen.py", line 66, in covariance
print self.vectors
AttributeError: Eigen_vect instance has no attribute '__getitem__'


self.vectors is apparently an Eigen_vect instance and you try to
subscript it, but, as the message said, there is no __getitem__ method.

So add one.

and a quick explaination: the file structures aare a 2d list of lists
of numbers corresponding to eginvectors and a list of numbers
corresponding to eigenvalues

#########################33##Here is the main body
#!/usr/bin/env python
import sys
import Eigen
from Eigen import *

if len(sys.argv) != 3:
print " "
print "The corrent usage is 'python covariance.py file1 file2'"
print "where the _U.asc and _s.asc will be appended when needed"
print " "
exit(1)
file1 = sys.argv[1]
file2 = sys.argv[2]
set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc")
set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc")
cov = set1.covariance(set2, Eigen_vect.dot)
print cov


###############and here are the classes:

#!/usr/bin/env python
import sys
import math
from math import sqrt

class Eigen_vect:
def __init__(self, e_val, e_vect):
self.e_val = e_val
self.e_vect = e_vect
def length(self):
return len(self.e_vect)

You should really call this __len__ so len(Eigen_vect()) will work.

I presume you need something like
def __getitem__(self, i): return self.e_vect

Before continuing, read the Language Reference / Data Model / Special
method names to understand how to write classes to integrate into syntax.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top