コード例 #1
0
ファイル: matrix_pool.py プロジェクト: sonofeft/M_Pool
 def read_from_pickle(self, fname=None):
     if fname==None:
         fname = '%s_matrix.pool'%(self.name)
         print 'Reading:', fname
         
     if os.path.exists(fname):            
         
         fInp = open(fname, 'rb')
         D = pickle.load( fInp )
         fInp.close()
         
         # reinit self
         self.axisPoolObj = AxisPool()
         self.matrixL = [] # list of Matrix objects
         self.matrixD = {} # cross ref by name
         
         self.descD = D.get('descD',{}) # if descriptive dictionary was saved, restore it
         
         for AD in D['axisL']:
             #print 'Adding',AD
             self.add_axis( AD )
         #print self.axisPoolObj
         #print
         #print self
             
         for MD in D['matrixL']:
             #print 'add Matrix',MD
             self.add_matrix( **MD )
     else:
         print '...WARNING... could not find:',fname
コード例 #2
0
ファイル: matrix_pool.py プロジェクト: sonofeft/M_Pool
 def __init__(self, name='Pool Data', descD=None):
     
     self.name = name
     self.descD = descD  # descriptive data in dictionary format
     
     self.axisPoolObj = AxisPool()
     
     self.matrixL = [] # list of Matrix objects
     self.matrixD = {} # cross ref by name
コード例 #3
0
ファイル: matrix_pool.py プロジェクト: sonofeft/M_Pool
    def read_from_hdf5(self, fname=None):
        if fname==None:
            fname = '%s_mpool.h5'%(self.name)
            print 'Reading:', fname
            
        if os.path.exists(fname):            
            h5file = tables.openFile(fname, mode='r')
               
            # reinit self
            self.axisPoolObj = AxisPool()
            self.matrixL = [] # list of Matrix objects
            self.matrixD = {} # cross ref by name
            self.descD = None
            
            root = h5file.root
            
            # First get the axes 
            axis_nameL = [_ for _ in root.axes_name_list]
            print 'axis_nameL =',axis_nameL

            for aname in axis_nameL:
                val_arr = getattr( root.axes, aname ).read()
                dname = 'desc_' + aname
                desc = getattr( root.axes, dname ).read()
                units, trans, alen = desc
                
                A = Axis({'name':aname, 'valueL':val_arr, 'units':units, 'transform':trans})
                self.add_axis( A )
            
            # Then get the matrices
            matrix_nameL = [_ for _ in root.matrix_name_list]
            print 'matrix_nameL =',matrix_nameL

            for mname in matrix_nameL:
                m = getattr( root.matrices, mname ).read()
                units = getattr( root.matrices, mname ).attrs.units
                a_list = getattr( root.matrices, mname+'_axis_list' ).read()
                self.add_matrix( name=mname, units=units, axisNameL=list(a_list), matValArr=m )
            
            h5file.close()
コード例 #4
0
ファイル: matrix_obj.py プロジェクト: sonofeft/M_Pool
        return M

if __name__=="__main__":

    epsAxis = Axis({'name':'eps', 'valueL':[10., 20., 30., 40.], 
            'units':'', 'transform':'log10'})
    
    # Just a dict, not an Axis obj
    pcAxis = {'name':'pc', 'valueL':[100.,200.,300], 'units':'psia', 'transform':'log10'}

    mrAxis = Axis({'name':'mr', 'valueL':[1,2,3,4,5], 
            'units':'', 'transform':''})

    axesDefL = [epsAxis, pcAxis, mrAxis]
    
    AP = AxisPool( {'axesDefL':axesDefL} )
        
    axisNameL = ['eps','pc','mr']
    shape = [len(AP.axisD[name])  for name in axisNameL]
    print 'shape =',shape
    matValArr = np.zeros( shape )
    n0,n1,n2 = axisNameL
    for i0,v0 in enumerate(AP.axisD[n0]):
        for i1,v1 in enumerate(AP.axisD[n1]):
            for i2,v2 in enumerate(AP.axisD[n2]):
                matValArr[i0,i1,i2] = v0+v1+v2

    M = Matrix( {'name':'isp_ode', 'matValArr':matValArr, 'units':'', 
        'axisNameL':axisNameL, 'axisPoolObj':AP} )
        
    #print M.axisL
コード例 #5
0
ファイル: matrix_pool.py プロジェクト: sonofeft/M_Pool
class MatrixPool(object):
    """
    A wrapper for numpy arrays providing named axes, interpolation, iteration, disk persistence and numerical calcs

    A MatrixPool object is a Collection of Axis and Matrix objects. 
    Like AxisPool, it is used to define a collection of Matrix objects
         
    *** Structured to easily pickle via a dictionary of named values for properties. ***
    """
    
    def __init__(self, name='Pool Data', descD=None):
        
        self.name = name
        self.descD = descD  # descriptive data in dictionary format
        
        self.axisPoolObj = AxisPool()
        
        self.matrixL = [] # list of Matrix objects
        self.matrixD = {} # cross ref by name
        
    def add_axis(self, axOrD):
        self.axisPoolObj.add_axis( axOrD )
    
    def rename_matrix(self, old_name, new_name ):
        M = self.matrixD[old_name]
        M.name = new_name
        del self.matrixD[old_name]
        self.matrixD[new_name] = M
    
    def add_matrix(self, name='matrixName', units='', axisNameL=None,
        matValArr=None):
        
        D={'name':name,  'units':units, 'matValArr':matValArr,
            'axisNameL':axisNameL, 'axisPoolObj':self.axisPoolObj}
        self.matrixL.append( Matrix(D) )
        
        self.matrixD[name] = self.matrixL[-1]
    
        return self.matrixL[-1]

    def get_matrix_by_name(self, matName):
        return self.matrixD.get(matName,None)

    def get_axis_by_name(self, axName):
        return self.axisPoolObj[axName]

    def __str__(self):
        sL = ['MatrixPool: %s'%self.name ]
        for M in self.matrixL:
            sL.append( '  Matrix:%s, shape=%s, units=%s, %%full=%i'%(M.name, M.shape(), M.units, M.iPercentFull()) )
        return '\n'.join( sL )
    
    def __len__(self):
        return len( self.matrixL )
    
    def __iter__(self):
        for value in self.matrixL:
            yield value
    
    def __getitem__(self, i): # retrieve as: A[i]
        return self.matrixL[i]
        
    def save_to_hdf5(self, fname=None):
        if fname==None:
            fname = '%s_mpool.h5'%(self.name)
            
        h5file = tables.openFile(fname, mode='w')
        h5file.createArray("/", 'axes_name_list', [A.name for A in self.axisPoolObj], "String array")
        h5file.createArray("/", 'matrix_name_list', [M.name for M in self.matrixL], "String array")
        axes_group = h5file.createGroup("/", 'axes', 'All the Axes used in Matrix Pool')
        mat_group = h5file.createGroup("/", 'matrices', 'All the Matrices used in Matrix Pool')
        
        '''  {'name':self.name, 'valueL':self.valueL, 'units':self.units, 
            'transform':self.transform, 'roundDigits':self.roundDigits}'''
        for A in self.axisPoolObj:
            d = h5file.createArray(axes_group, A.name, A.valueArr) 
            h5file.createArray(axes_group, 'transform_%s'%A.name, A.transArr) 
            h5file.createArray(axes_group, 'desc_%s'%A.name, 
                [str(A.units), str(A.transform), str(A.roundDigits)], "String array")
            #d.transform_desc = A.transform
            #d.roundDigits_desc = A.roundDigits
            #d.units_desc = A.units
        
        # Just use last axis "A" for atom creation
        atom = tables.Atom.from_dtype(A.valueArr.dtype)
        filters = tables.Filters(complib='blosc', complevel=5)    
        
        for M in self.matrixL:    
            ds = h5file.createCArray(mat_group, M.name, atom, M.matValArr.shape, filters=filters)
            ds.attrs.units = M.units
            ds[:] = M.matValArr
                
            h5file.createArray(mat_group, M.name+'_axis_list', [aname for aname in M.axisNameL], "String array")
            
            
        h5file.close()
    
    def read_from_hdf5(self, fname=None):
        if fname==None:
            fname = '%s_mpool.h5'%(self.name)
            print 'Reading:', fname
            
        if os.path.exists(fname):            
            h5file = tables.openFile(fname, mode='r')
               
            # reinit self
            self.axisPoolObj = AxisPool()
            self.matrixL = [] # list of Matrix objects
            self.matrixD = {} # cross ref by name
            self.descD = None
            
            root = h5file.root
            
            # First get the axes 
            axis_nameL = [_ for _ in root.axes_name_list]
            print 'axis_nameL =',axis_nameL

            for aname in axis_nameL:
                val_arr = getattr( root.axes, aname ).read()
                dname = 'desc_' + aname
                desc = getattr( root.axes, dname ).read()
                units, trans, alen = desc
                
                A = Axis({'name':aname, 'valueL':val_arr, 'units':units, 'transform':trans})
                self.add_axis( A )
            
            # Then get the matrices
            matrix_nameL = [_ for _ in root.matrix_name_list]
            print 'matrix_nameL =',matrix_nameL

            for mname in matrix_nameL:
                m = getattr( root.matrices, mname ).read()
                units = getattr( root.matrices, mname ).attrs.units
                a_list = getattr( root.matrices, mname+'_axis_list' ).read()
                self.add_matrix( name=mname, units=units, axisNameL=list(a_list), matValArr=m )
            
            h5file.close()

    def save_to_pickle(self, fname=None):
        if fname==None:
            fname = '%s_matrix.pool'%(self.name)
        D = {}
        D['axisL'] = [A.get_pickleable_dict() for A in self.axisPoolObj]
        D['matrixL'] = [M.get_pickleable_dict() for M in self.matrixL]
        D['descD'] = self.descD
        
        fOut = open(fname, 'wb')
        pickle.dump( D, fOut )
        fOut.close()
        
    def read_from_pickle(self, fname=None):
        if fname==None:
            fname = '%s_matrix.pool'%(self.name)
            print 'Reading:', fname
            
        if os.path.exists(fname):            
            
            fInp = open(fname, 'rb')
            D = pickle.load( fInp )
            fInp.close()
            
            # reinit self
            self.axisPoolObj = AxisPool()
            self.matrixL = [] # list of Matrix objects
            self.matrixD = {} # cross ref by name
            
            self.descD = D.get('descD',{}) # if descriptive dictionary was saved, restore it
            
            for AD in D['axisL']:
                #print 'Adding',AD
                self.add_axis( AD )
            #print self.axisPoolObj
            #print
            #print self
                
            for MD in D['matrixL']:
                #print 'add Matrix',MD
                self.add_matrix( **MD )
        else:
            print '...WARNING... could not find:',fname