PySVD.SVDPack

Package PySVD :: Module SVDPack |
[hide private]
[frames] | no frames]

# Source Code for Module PySVD.SVDPack

      1  #
      2  #  Copyright (C) 2004 Rational Discovery LLC
      3  #    All Rights Reserved
      4  #
      5  import RDConfig
      6  import os
      7  from Numeric import *
      8  import sys
      9
     10 -def HBFOut(calc,outF):
     11    vects = calc._vects
     12    nRows = len(vects)
     13    nCols = len(calc._idMap)
     14    vals = calc._vals
     15    nVals = len(vals)
     16    outF.write('% 72s%s\n'%('svdpack input','rd'))
     17    outF.write('blank\n')
     18    outF.write('rra %d %d %d 0\n'%(nCols,nRows,nVals))
     19    outF.write('X X X X\n')
     20    pos = 1
     21    for i in range(nRows):
     22      outF.write('%d '%pos)
     23      pos += len(vects[i])
     24      if not (i+1)%20: outF.write('\n')
     25    outF.write('%d\n'%(nVals+1))
     26
     27    for i in range(nRows):
     28      row = vects[i]
     29      nHere = len(row)
     30      for entry in row:
     31        outF.write('%d '%(entry+1))
     32      outF.write('\n')
     33    for i in range(nVals):
     34      outF.write('%d '%vals[i])
     35      if not (i+1)%20:
     36        outF.write('\n')
     37
     38 -def ReadSVDPackOutputFile(inF):
     39    import re
     40    inL = inF.readline()
     41    while inL and inL.find('(ROWS)')==-1:
     42      inL = inF.readline()
     43    if inL.index('(ROWS)')<0:
     44      raise ValueError('input file contains no (ROWS) line')
     45    txt = (inL.split('=')[-1]).strip()
     46    nCols = int(txt)
     47    inL = inF.readline()
     48    if not inL:
     49      raise ValueError('premature EOF hit')
     50    txt = (inL.split('=')[-1]).strip()
     51    nRows = int(txt)
     52
     53    while inL and inL.find('COMPUTED SINGULAR VALUES')==-1:
     54      inL = inF.readline()
     55    if inL.index('COMPUTED SINGULAR VALUES')<0:
     56      raise ValueError('input file contains no singular vals')
     57    inL = inF.readline()
     58    inL = inF.readline()
     59    blankExpr = re.compile(' +')
     60    vals = []
     61    inL = inL.strip()
     62    while inL and inL[0]=='.':
     63      splitL = blankExpr.split(inL)
     64      idx = int(splitL[1])
     65      val = float(splitL[2])
     66      vals.append(val)
     67      inL = inF.readline()
     68      inL = inL.strip()
     69    return nRows,nCols,vals
     70
     71 -def ReadSVDPackArrays(inF,nRows,nCols,k):
     72    import struct
     73    T = []
     74    D = []
     75    for i in range(k):
     76      d = inF.read(nCols*8)
     77      if not d:
     78        raise ValueError('premature EOF hit')
     79      tmp = struct.unpack('%dd'%nCols,d)
     80      T.append(tmp)
     81
     82      d = inF.read(nRows*8)
     83      if not d:
     84        raise ValueError('premature EOF hit')
     85      tmp = struct.unpack('%dd'%nRows,d)
     86      D.append(tmp)
     87    T = transpose(array(T))
     88    D = transpose(array(D))
     89    return T,D
     90
     91 -def DoSVD(calc,k,exe=None,tol=1e-8):
     92    if exe is None:
     93      exe = os.path.join(RDConfig.RDBinDir,'sis2-rd')
     94    parmFilename = "sis2-parms"
     95    matFilename = "sis2-mat"
     96
     97    nRows = len(calc._vects)
     98    nCols = len(calc._idMap)
     99
    100    nExtras = max(2,.1*k)
    101    if (k+nExtras)>nRows:
    102      nExtras = nRows-k
    103    maxIts = max(nRows,nCols)
    104    parmText="'AutoGen' %d %d %d %g TRUE\n"%(k,nExtras,maxIts,tol)
    105    open(parmFilename,'w+').write(parmText)
    106    matF=open(matFilename,'w+')
    107    HBFOut(calc,matF)
    108    matF=None
    109    res = os.spawnl(os.P_WAIT,exe,exe,parmFilename,matFilename)
    110    if not res:
    111      nRows,nCols,vals = ReadSVDPackOutputFile(open('sio2','r'))
    112      T,D = ReadSVDPackArrays(open('siv2','rb'),nRows,nCols,k)
    113
    114      # update the lengths:
    115      while vals[-1]<=tol:
    116        vals.pop(-1)
    117      k = len(vals)
    118      T = T[:,:k]
    119      D = D[:,:k]
    120      calc.ForceSingularValues(k,T,D,array(vals))
    121
    122
    123
    124  if __name__ == '__main__':
    125    import SVDSimilarity
    126    m = [[0,1,2],
    127         [2,3,4,5,6,8],
    128         [1,3,4,7],
    129         [0,4,4,7],
    130         [3,5,6],
    131         [9],
    132         [9,10],
    133         [9,10,11],
    134         [8,10,11]]
    135    calc = SVDSimilarity.SimilarityCalculator()
    136    calc.SetVects(m)
    137    DoSVD(calc,2)
    138    print calc.ScorePoint(m[0])
    139