Mercurial > pylearn
comparison pmat.py @ 380:c2f17f231960
added function to load amat file
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Wed, 09 Jul 2008 16:55:27 -0400 |
parents | 9330d941fa1f |
children |
comparison
equal
deleted
inserted
replaced
379:74b402b5a81b | 380:c2f17f231960 |
---|---|
34 | 34 |
35 # Author: Pascal Vincent | 35 # Author: Pascal Vincent |
36 | 36 |
37 #import numarray, sys, os, os.path | 37 #import numarray, sys, os, os.path |
38 import numpy.numarray, sys, os, os.path | 38 import numpy.numarray, sys, os, os.path |
39 import fpconst | |
39 | 40 |
40 def array_columns( a, cols ): | 41 def array_columns( a, cols ): |
41 indices = None | 42 indices = None |
42 if isinstance( cols, int ): | 43 if isinstance( cols, int ): |
43 indices = [ cols ] | 44 indices = [ cols ] |
91 row = row.split() | 92 row = row.split() |
92 if len(row)>0: | 93 if len(row)>0: |
93 fieldnames.append(row[0]) | 94 fieldnames.append(row[0]) |
94 f.close() | 95 f.close() |
95 else: | 96 else: |
97 self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ] | |
98 | |
99 return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])])) | |
100 | |
101 def load_amat_as_array_dataset(fname): | |
102 import dataset,lookup_list | |
103 | |
104 #load the amat as array | |
105 (a,fieldnames)=readAMat(fname) | |
106 | |
107 #load the fieldnames | |
108 if len(fieldnames)==0: | |
96 self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ] | 109 self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ] |
97 | 110 |
98 return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])])) | 111 return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])])) |
99 | 112 |
100 def save_array_dataset_as_pmat(fname,ds): | 113 def save_array_dataset_as_pmat(fname,ds): |
438 self.putRow(i,row) | 451 self.putRow(i,row) |
439 | 452 |
440 def __len__(self): | 453 def __len__(self): |
441 return self.length | 454 return self.length |
442 | 455 |
456 | |
457 | |
458 #copied from PLEARNDIR:python_modules/plearn/vmat/readAMat.py | |
459 def safefloat(str): | |
460 """Convert the given string to its float value. It is 'safe' in the sense | |
461 that missing values ('nan') will be properly converted to the corresponding | |
462 float value under all platforms, contrarily to 'float(str)'. | |
463 """ | |
464 if str.lower() == 'nan': | |
465 return fpconst.NaN | |
466 else: | |
467 return float(str) | |
468 | |
469 #copied from PLEARNDIR:python_modules/plearn/vmat/readAMat.py | |
470 def readAMat(amatname): | |
471 """Read a PLearn .amat file and return it as a numarray Array. | |
472 | |
473 Return a tuple, with as the first argument the array itself, and as | |
474 the second argument the fieldnames (list of strings). | |
475 """ | |
476 ### NOTE: this version is much faster than first creating the array and | |
477 ### updating each row as it is read... Bizarrely enough | |
478 f = open(amatname) | |
479 a = [] | |
480 fieldnames = [] | |
481 for line in f: | |
482 if line.startswith("#size:"): | |
483 (length,width) = line[6:].strip().split() | |
484 elif line.startswith("#sizes:"): # ignore input/target/weight/extra sizes | |
485 continue | |
486 | |
487 elif line.startswith("#:"): | |
488 fieldnames = line[2:].strip().split() | |
489 pass | |
490 elif not line.startswith('#'): | |
491 # Add all non-comment lines. | |
492 row = [ safefloat(x) for x in line.strip().split() ] | |
493 if row: | |
494 a.append(row) | |
495 | |
496 f.close() | |
497 return numpy.numarray.array(a), fieldnames | |
498 | |
443 | 499 |
444 if __name__ == '__main__': | 500 if __name__ == '__main__': |
445 pmat = PMat( 'tmp.pmat', 'w', fieldnames=['F1', 'F2'] ) | 501 pmat = PMat( 'tmp.pmat', 'w', fieldnames=['F1', 'F2'] ) |
446 pmat.append( [1, 2] ) | 502 pmat.append( [1, 2] ) |
447 pmat.append( [3, 4] ) | 503 pmat.append( [3, 4] ) |