# HG changeset patch # User Frederic Bastien # Date 1210180053 14400 # Node ID 9330d941fa1f2e25e06df6b84b59c5290a629ffd # Parent 01aa97a2212d6c41370b8a9d8a1029a8c3a4fe92 added function load_pmat_as_array_dataset and save_array_dataset_as_pmat diff -r 01aa97a2212d -r 9330d941fa1f pmat.py --- a/pmat.py Wed May 07 12:19:36 2008 -0400 +++ b/pmat.py Wed May 07 13:07:33 2008 -0400 @@ -76,6 +76,31 @@ X.byteswap(True) return X +def load_pmat_as_array_dataset(fname): + import dataset,lookup_list + + #load the pmat as array + a=load_pmat_as_array(fname) + + #load the fieldnames + fieldnames = [] + fieldnamefile = os.path.join(fname+'.metadata','fieldnames') + if os.path.isfile(fieldnamefile): + f = open(fieldnamefile) + for row in f: + row = row.split() + if len(row)>0: + fieldnames.append(row[0]) + f.close() + else: + self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ] + + return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])])) + +def save_array_dataset_as_pmat(fname,ds): + ar=ds.data + save_array_as_pmat(fname,ar,ds.fieldNames()) + def save_array_as_pmat( fname, ar, fieldnames=[] ): s = file(fname,'wb') @@ -423,12 +448,23 @@ pmat.close() pmat = PMat( 'tmp.pmat', 'r' ) - print pmat - print pmat[:] + ar=load_pmat_as_array('tmp.pmat') + ds=load_pmat_as_array_dataset('tmp.pmat') + + print "PMat",pmat + print "PMat",pmat[:] + print "array",ar + print "ArrayDataSet",ds + for i in ds: + print i + save_array_dataset_as_pmat("tmp2.pmat",ds) + ds2=load_pmat_as_array_dataset('tmp2.pmat') + for i in ds2: + print i # print "+++ tmp.pmat contains: " # os.system( 'plearn vmat cat tmp.pmat' ) - - os.remove( 'tmp.pmat' ) - if os.path.exists( 'tmp.pmat.metadata' ): - import shutil - shutil.rmtree( 'tmp.pmat.metadata' ) + import shutil + for fname in ["tmp.pmat", "tmp2.pmat"]: + os.remove( fname ) + if os.path.exists( fname+'.metadata' ): + shutil.rmtree( fname+'.metadata' ) diff -r 01aa97a2212d -r 9330d941fa1f test_dataset.py --- a/test_dataset.py Wed May 07 12:19:36 2008 -0400 +++ b/test_dataset.py Wed May 07 13:07:33 2008 -0400 @@ -401,6 +401,10 @@ print "test_ArrayFieldsDataSet" raise NotImplementedError() +import pmat + +load_pmat_as_array_dataset("tmp.pmat") + test1() test_LookupList() test_ArrayDataSet()