Mercurial > pylearn
changeset 116:9330d941fa1f
added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Wed, 07 May 2008 13:07:33 -0400 |
parents | 01aa97a2212d |
children | 3ef569b92fba 7ffecde9dadc d3c72e412065 |
files | pmat.py test_dataset.py |
diffstat | 2 files changed, 47 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- 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' )