annotate pmat.py @ 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 c2f17f231960
rev   line source
114
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
1 ## Automatically adapted for numpy.numarray Jun 13, 2007 by python_numarray_to_numpy (-xsm)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
2
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
3 # PMat.py
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
4 # Copyright (C) 2005 Pascal Vincent
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
5 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
7 # modification, are permitted provided that the following conditions are met:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
8 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer.
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
11 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
12 # 2. Redistributions in binary form must reproduce the above copyright
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
13 # notice, this list of conditions and the following disclaimer in the
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
14 # documentation and/or other materials provided with the distribution.
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
15 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
16 # 3. The name of the authors may not be used to endorse or promote
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
17 # products derived from this software without specific prior written
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
18 # permission.
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
19 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
22 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
23 # NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
25 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
26 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
27 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
28 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
29 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
30 #
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
31 # This file is part of the PLearn library. For more information on the PLearn
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
32 # library, go to the PLearn Web site at www.plearn.org
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
33
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
34
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
35 # Author: Pascal Vincent
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
36
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
37 #import numarray, sys, os, os.path
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
38 import numpy.numarray, sys, os, os.path
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
39
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
40 def array_columns( a, cols ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
41 indices = None
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
42 if isinstance( cols, int ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
43 indices = [ cols ]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
44 elif isinstance( cols, slice ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
45 #print cols
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
46 indices = range( *cols.indices(cols.stop) )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
47 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
48 indices = list( cols )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
49
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
50 return numpy.numarray.take(a, indices, axis=1)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
51
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
52 def load_pmat_as_array(fname):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
53 s = file(fname,'rb').read()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
54 formatstr = s[0:64]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
55 datastr = s[64:]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
56 structuretype, l, w, data_type, endianness = formatstr.split()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
57
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
58 if data_type=='DOUBLE':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
59 elemtype = 'd'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
60 elif data_type=='FLOAT':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
61 elemtype = 'f'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
62 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
63 raise ValueError('Invalid data type in file header: '+data_type)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
64
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
65 if endianness=='LITTLE_ENDIAN':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
66 byteorder = 'little'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
67 elif endianness=='BIG_ENDIAN':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
68 byteorder = 'big'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
69 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
70 raise ValueError('Invalid endianness in file header: '+endianness)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
71
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
72 l = int(l)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
73 w = int(w)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
74 X = numpy.numarray.fromstring(datastr,elemtype, shape=(l,w) )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
75 if byteorder!=sys.byteorder:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
76 X.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
77 return X
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
78
116
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
79 def load_pmat_as_array_dataset(fname):
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
80 import dataset,lookup_list
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
81
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
82 #load the pmat as array
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
83 a=load_pmat_as_array(fname)
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
84
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
85 #load the fieldnames
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
86 fieldnames = []
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
87 fieldnamefile = os.path.join(fname+'.metadata','fieldnames')
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
88 if os.path.isfile(fieldnamefile):
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
89 f = open(fieldnamefile)
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
90 for row in f:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
91 row = row.split()
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
92 if len(row)>0:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
93 fieldnames.append(row[0])
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
94 f.close()
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
95 else:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
96 self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ]
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
97
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
98 return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])]))
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
99
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
100 def save_array_dataset_as_pmat(fname,ds):
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
101 ar=ds.data
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
102 save_array_as_pmat(fname,ar,ds.fieldNames())
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
103
114
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
104 def save_array_as_pmat( fname, ar, fieldnames=[] ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
105 s = file(fname,'wb')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
106
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
107 length, width = ar.shape
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
108 if fieldnames:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
109 assert len(fieldnames) == width
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
110 metadatadir = fname+'.metadata'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
111 if not os.path.isdir(metadatadir):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
112 os.mkdir(metadatadir)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
113 fieldnamefile = os.path.join(metadatadir,'fieldnames')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
114 f = open(fieldnamefile,'wb')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
115 for name in fieldnames:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
116 f.write(name+'\t0\n')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
117 f.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
118
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
119 header = 'MATRIX ' + str(length) + ' ' + str(width) + ' '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
120 if ar.dtype.char=='d':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
121 header += 'DOUBLE '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
122 elemsize = 8
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
123
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
124 elif ar.dtype.char=='f':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
125 header += 'FLOAT '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
126 elemsize = 4
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
127
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
128 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
129 raise TypeError('Unsupported typecode: %s' % ar.dtype.char)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
130
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
131 rowsize = elemsize*width
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
132
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
133 if sys.byteorder=='little':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
134 header += 'LITTLE_ENDIAN '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
135 elif sys.byteorder=='big':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
136 header += 'BIG_ENDIAN '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
137 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
138 raise TypeError('Unsupported sys.byteorder: '+repr(sys.byteorder))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
139
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
140 header += ' '*(63-len(header))+'\n'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
141 s.write( header )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
142 s.write( ar.tostring() )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
143 s.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
144
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
145
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
146 ####### Iterators ###########################################################
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
147
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
148 class VMatIt:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
149 def __init__(self, vmat):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
150 self.vmat = vmat
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
151 self.cur_row = 0
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
152
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
153 def __iter__(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
154 return self
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
155
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
156 def next(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
157 if self.cur_row==self.vmat.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
158 raise StopIteration
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
159 row = self.vmat.getRow(self.cur_row)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
160 self.cur_row += 1
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
161 return row
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
162
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
163 class ColumnIt:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
164 def __init__(self, vmat, col):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
165 self.vmat = vmat
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
166 self.col = col
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
167 self.cur_row = 0
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
168
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
169 def __iter__(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
170 return self
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
171
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
172 def next(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
173 if self.cur_row==self.vmat.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
174 raise StopIteration
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
175 val = self.vmat[self.cur_row, self.col]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
176 self.cur_row += 1
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
177 return val
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
178
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
179 ####### VMat classes ########################################################
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
180
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
181 class VMat:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
182 def __iter__(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
183 return VMatIt(self)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
184
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
185 def __getitem__( self, key ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
186 if isinstance( key, slice ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
187 start, stop, step = key.start, key.stop, key.step
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
188 if step!=None:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
189 raise IndexError('Extended slice with step not currently supported')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
190
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
191 if start is None:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
192 start = 0
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
193
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
194 l = self.length
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
195 if stop is None or stop > l:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
196 stop = l
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
197
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
198 return self.getRows(start,stop-start)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
199
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
200 elif isinstance( key, tuple ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
201 # Basically returns a SubVMatrix
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
202 assert len(key) == 2
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
203 rows = self.__getitem__( key[0] )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
204
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
205 shape = rows.shape
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
206 if len(shape) == 1:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
207 return rows[ key[1] ]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
208
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
209 cols = key[1]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
210 if isinstance(cols, slice):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
211 start, stop, step = cols.start, cols.stop, cols.step
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
212 if start is None:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
213 start = 0
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
214
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
215 if stop is None:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
216 stop = self.width
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
217 elif stop < 0:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
218 stop = self.width+stop
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
219
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
220 cols = slice(start, stop, step)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
221
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
222 return array_columns(rows, cols)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
223
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
224 elif isinstance( key, str ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
225 # The key is considered to be a fieldname and a column is
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
226 # returned.
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
227 try:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
228 return array_columns( self.getRows(0,self.length),
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
229 self.fieldnames.index(key) )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
230 except ValueError:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
231 print >>sys.stderr, "Key is '%s' while fieldnames are:" % key
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
232 print >>sys.stderr, self.fieldnames
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
233 raise
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
234
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
235 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
236 if key<0: key+=self.length
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
237 return self.getRow(key)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
238
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
239 def getFieldIndex(self, fieldname):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
240 try:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
241 return self.fieldnames.index(fieldname)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
242 except ValueError:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
243 raise ValueError( "VMat has no field named %s. Field names: %s"
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
244 %(fieldname, ','.join(self.fieldnames)) )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
245
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
246 class PMat( VMat ):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
247
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
248 def __init__(self, fname, openmode='r', fieldnames=[], elemtype='d',
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
249 inputsize=-1, targetsize=-1, weightsize=-1, array = None):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
250 self.fname = fname
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
251 self.inputsize = inputsize
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
252 self.targetsize = targetsize
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
253 self.weightsize = weightsize
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
254 if openmode=='r':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
255 self.f = open(fname,'rb')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
256 self.read_and_parse_header()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
257 self.load_fieldnames()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
258
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
259 elif openmode=='w':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
260 self.f = open(fname,'w+b')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
261 self.fieldnames = fieldnames
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
262 self.save_fieldnames()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
263 self.length = 0
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
264 self.width = len(fieldnames)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
265 self.elemtype = elemtype
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
266 self.swap_bytes = False
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
267 self.write_header()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
268
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
269 elif openmode=='a':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
270 self.f = open(fname,'r+b')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
271 self.read_and_parse_header()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
272 self.load_fieldnames()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
273
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
274 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
275 raise ValueError("Currently only supported openmodes are 'r', 'w' and 'a': "+repr(openmode)+" is not supported")
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
276
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
277 if array is not None:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
278 shape = array.shape
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
279 if len(shape) == 1:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
280 row_format = lambda r: [ r ]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
281 elif len(shape) == 2:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
282 row_format = lambda r: r
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
283
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
284 for row in array:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
285 self.appendRow( row_format(row) )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
286
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
287 def __del__(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
288 self.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
289
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
290 def write_header(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
291 header = 'MATRIX ' + str(self.length) + ' ' + str(self.width) + ' '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
292
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
293 if self.elemtype=='d':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
294 header += 'DOUBLE '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
295 self.elemsize = 8
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
296 elif self.elemtype=='f':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
297 header += 'FLOAT '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
298 self.elemsize = 4
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
299 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
300 raise TypeError('Unsupported elemtype: '+repr(elemtype))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
301 self.rowsize = self.elemsize*self.width
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
302
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
303 if sys.byteorder=='little':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
304 header += 'LITTLE_ENDIAN '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
305 elif sys.byteorder=='big':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
306 header += 'BIG_ENDIAN '
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
307 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
308 raise TypeError('Unsupported sys.byteorder: '+repr(sys.byteorder))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
309
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
310 header += ' '*(63-len(header))+'\n'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
311
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
312 self.f.seek(0)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
313 self.f.write(header)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
314
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
315 def read_and_parse_header(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
316 header = self.f.read(64)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
317 mat_type, l, w, data_type, endianness = header.split()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
318 if mat_type!='MATRIX':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
319 raise ValueError('Invalid file header (should start with MATRIX)')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
320 self.length = int(l)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
321 self.width = int(w)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
322 if endianness=='LITTLE_ENDIAN':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
323 byteorder = 'little'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
324 elif endianness=='BIG_ENDIAN':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
325 byteorder = 'big'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
326 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
327 raise ValueError('Invalid endianness in file header: '+endianness)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
328 self.swap_bytes = (byteorder!=sys.byteorder)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
329
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
330 if data_type=='DOUBLE':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
331 self.elemtype = 'd'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
332 self.elemsize = 8
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
333 elif data_type=='FLOAT':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
334 self.elemtype = 'f'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
335 self.elemsize = 4
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
336 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
337 raise ValueError('Invalid data type in file header: '+data_type)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
338 self.rowsize = self.elemsize*self.width
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
339
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
340 def load_fieldnames(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
341 self.fieldnames = []
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
342 fieldnamefile = os.path.join(self.fname+'.metadata','fieldnames')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
343 if os.path.isfile(fieldnamefile):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
344 f = open(fieldnamefile)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
345 for row in f:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
346 row = row.split()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
347 if len(row)>0:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
348 self.fieldnames.append(row[0])
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
349 f.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
350 else:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
351 self.fieldnames = [ "field_"+str(i) for i in range(self.width) ]
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
352
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
353 def save_fieldnames(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
354 metadatadir = self.fname+'.metadata'
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
355 if not os.path.isdir(metadatadir):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
356 os.mkdir(metadatadir)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
357 fieldnamefile = os.path.join(metadatadir,'fieldnames')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
358 f = open(fieldnamefile,'wb')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
359 for name in self.fieldnames:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
360 f.write(name+'\t0\n')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
361 f.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
362
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
363 def getRow(self,i):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
364 if i<0 or i>=self.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
365 raise IndexError('PMat index out of range')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
366 self.f.seek(64+i*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
367 data = self.f.read(self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
368 ar = numpy.numarray.fromstring(data, self.elemtype, (self.width,))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
369 if self.swap_bytes:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
370 ar.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
371 return ar
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
372
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
373 def getRows(self,i,l):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
374 if i<0 or l<0 or i+l>self.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
375 raise IndexError('PMat index out of range')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
376 self.f.seek(64+i*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
377 data = self.f.read(l*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
378 ar = numpy.numarray.fromstring(data, self.elemtype, (l,self.width))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
379 if self.swap_bytes:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
380 ar.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
381 return ar
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
382
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
383 def checkzerorow(self,i):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
384 if i<0 or i>self.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
385 raise IndexError('PMat index out of range')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
386 self.f.seek(64+i*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
387 data = self.f.read(self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
388 ar = numpy.numarray.fromstring(data, self.elemtype, (len(data)/self.elemsize,))
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
389 if self.swap_bytes:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
390 ar.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
391 for elem in ar:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
392 if elem!=0:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
393 return False
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
394 return True
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
395
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
396 def putRow(self,i,row):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
397 if i<0 or i>=self.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
398 raise IndexError('PMat index out of range')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
399 if len(row)!=self.width:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
400 raise TypeError('length of row ('+str(len(row))+ ') differs from matrix width ('+str(self.width)+')')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
401 if i<0 or i>=self.length:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
402 raise IndexError
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
403 if self.swap_bytes: # must make a copy and swap bytes
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
404 ar = numpy.numarray.numarray(row,type=self.elemtype)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
405 ar.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
406 else: # asarray makes a copy if not already a numarray of the right type
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
407 ar = numpy.numarray.asarray(row,type=self.elemtype)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
408 self.f.seek(64+i*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
409 self.f.write(ar.tostring())
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
410
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
411 def appendRow(self,row):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
412 if len(row)!=self.width:
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
413 raise TypeError('length of row ('+str(len(row))+ ') differs from matrix width ('+str(self.width)+')')
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
414 if self.swap_bytes: # must make a copy and swap bytes
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
415 ar = numpy.numarray.numarray(row,type=self.elemtype)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
416 ar.byteswap(True)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
417 else: # asarray makes a copy if not already a numarray of the right type
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
418 ar = numpy.numarray.asarray(row,type=self.elemtype)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
419
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
420 self.f.seek(64+self.length*self.rowsize)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
421 self.f.write(ar.tostring())
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
422 self.length += 1
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
423 self.write_header() # update length in header
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
424
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
425 def flush(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
426 self.f.flush()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
427
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
428 def close(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
429 if hasattr(self, 'f'):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
430 self.f.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
431
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
432 def append(self,row):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
433 self.appendRow(row)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
434
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
435 def __setitem__(self, i, row):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
436 l = self.length
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
437 if i<0: i+=l
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
438 self.putRow(i,row)
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
439
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
440 def __len__(self):
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
441 return self.length
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
442
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
443
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
444 if __name__ == '__main__':
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
445 pmat = PMat( 'tmp.pmat', 'w', fieldnames=['F1', 'F2'] )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
446 pmat.append( [1, 2] )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
447 pmat.append( [3, 4] )
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
448 pmat.close()
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
449
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
450 pmat = PMat( 'tmp.pmat', 'r' )
116
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
451 ar=load_pmat_as_array('tmp.pmat')
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
452 ds=load_pmat_as_array_dataset('tmp.pmat')
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
453
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
454 print "PMat",pmat
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
455 print "PMat",pmat[:]
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
456 print "array",ar
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
457 print "ArrayDataSet",ds
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
458 for i in ds:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
459 print i
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
460 save_array_dataset_as_pmat("tmp2.pmat",ds)
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
461 ds2=load_pmat_as_array_dataset('tmp2.pmat')
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
462 for i in ds2:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
463 print i
114
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
464 # print "+++ tmp.pmat contains: "
d6d42a0c1275 file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff changeset
465 # os.system( 'plearn vmat cat tmp.pmat' )
116
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
466 import shutil
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
467 for fname in ["tmp.pmat", "tmp2.pmat"]:
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
468 os.remove( fname )
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
469 if os.path.exists( fname+'.metadata' ):
9330d941fa1f added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents: 115
diff changeset
470 shutil.rmtree( fname+'.metadata' )