This is a mess...

Nick nleioatt at gmail.com
Thu Jul 16 10:15:52 EDT 2009


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[i][i]
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[i] = float(fields[i])
        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[i]
            for j in range(self.length()):
                newdot += v1.dot(self.vectors[j])
               # root = self.e_val[i] * other.e_val[j]
                print self.vectors[i]
                print self.vectors[j]
                print self.vectors[i][i]
#####################<<------------------------This is line 66, I'm
trying to figure out how to call "e_val" from the Eigen_set class

                root = self.vectors[i][i] * other.vectors[i][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....



More information about the Python-list mailing list