Mercurial > pylearn
annotate pylearn/io/pmat.py @ 1531:88f361283a19 tip
Fix url/name to pylearn2.
author | Frederic Bastien <nouiz@nouiz.org> |
---|---|
date | Mon, 09 Sep 2013 10:08:05 -0400 |
parents | 3f44379177b2 |
children |
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 |
829
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
39 try: |
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
40 import fpconst |
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
41 except ImportError: |
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
42 print 'Python library fpconst is needed but not installed,' |
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
43 print 'please install it using your favorite package manager.' |
3f44379177b2
More descriptive error message when fpconst is missing.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
537
diff
changeset
|
44 raise |
114
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
45 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
46 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
|
47 indices = None |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
48 if isinstance( cols, int ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
49 indices = [ cols ] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
50 elif isinstance( cols, slice ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
51 #print cols |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
52 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
|
53 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
54 indices = list( cols ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
55 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
56 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
|
57 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
58 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
|
59 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
|
60 formatstr = s[0:64] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
61 datastr = s[64:] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
62 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
|
63 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
64 if data_type=='DOUBLE': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
65 elemtype = 'd' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
66 elif data_type=='FLOAT': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
67 elemtype = 'f' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
68 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
69 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
|
70 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
71 if endianness=='LITTLE_ENDIAN': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
72 byteorder = 'little' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
73 elif endianness=='BIG_ENDIAN': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
74 byteorder = 'big' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
75 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
76 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
|
77 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
78 l = int(l) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
79 w = int(w) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
80 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
|
81 if byteorder!=sys.byteorder: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
82 X.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
83 return X |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
84 |
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
|
85 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
|
86 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
|
87 |
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 #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
|
89 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
|
90 |
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 #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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 |
9330d941fa1f
added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
115
diff
changeset
|
104 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
|
105 |
380
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
106 def load_amat_as_array_dataset(fname): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
107 import dataset,lookup_list |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
108 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
109 #load the amat as array |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
110 (a,fieldnames)=readAMat(fname) |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
111 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
112 #load the fieldnames |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
113 if len(fieldnames)==0: |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
114 self.fieldnames = [ "field_"+str(i) for i in range(a.shape[1]) ] |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
115 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
116 return dataset.ArrayDataSet(a,lookup_list.LookupList(fieldnames,[x for x in range(a.shape[1])])) |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
117 |
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
|
118 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
|
119 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
|
120 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
|
121 |
114
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
122 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
|
123 s = file(fname,'wb') |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
124 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
125 length, width = ar.shape |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
126 if fieldnames: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
127 assert len(fieldnames) == width |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
128 metadatadir = fname+'.metadata' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
129 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
|
130 os.mkdir(metadatadir) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
131 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
|
132 f = open(fieldnamefile,'wb') |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
133 for name in fieldnames: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
134 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
|
135 f.close() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
136 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
137 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
|
138 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
|
139 header += 'DOUBLE ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
140 elemsize = 8 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
141 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
142 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
|
143 header += 'FLOAT ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
144 elemsize = 4 |
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 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
147 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
|
148 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
149 rowsize = elemsize*width |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
150 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
151 if sys.byteorder=='little': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
152 header += 'LITTLE_ENDIAN ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
153 elif sys.byteorder=='big': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
154 header += 'BIG_ENDIAN ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
155 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
156 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
|
157 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
158 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
|
159 s.write( header ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
160 s.write( ar.tostring() ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
161 s.close() |
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 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
164 ####### Iterators ########################################################### |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
165 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
166 class VMatIt: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
167 def __init__(self, vmat): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
168 self.vmat = vmat |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
169 self.cur_row = 0 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
170 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
171 def __iter__(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
172 return self |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
173 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
174 def next(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
175 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
|
176 raise StopIteration |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
177 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
|
178 self.cur_row += 1 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
179 return row |
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 ColumnIt: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
182 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
|
183 self.vmat = vmat |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
184 self.col = col |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
185 self.cur_row = 0 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
186 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
187 def __iter__(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
188 return self |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
189 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
190 def next(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
191 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
|
192 raise StopIteration |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
193 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
|
194 self.cur_row += 1 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
195 return val |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
196 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
197 ####### VMat classes ######################################################## |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
198 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
199 class VMat: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
200 def __iter__(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
201 return VMatIt(self) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
202 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
203 def __getitem__( self, key ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
204 if isinstance( key, slice ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
205 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
|
206 if step!=None: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
207 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
|
208 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
209 if start is None: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
210 start = 0 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
211 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
212 l = self.length |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
213 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
|
214 stop = l |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
215 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
216 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
|
217 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
218 elif isinstance( key, tuple ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
219 # Basically returns a SubVMatrix |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
220 assert len(key) == 2 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
221 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
|
222 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
223 shape = rows.shape |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
224 if len(shape) == 1: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
225 return rows[ key[1] ] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
226 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
227 cols = key[1] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
228 if isinstance(cols, slice): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
229 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
|
230 if start is None: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
231 start = 0 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
232 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
233 if stop is None: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
234 stop = self.width |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
235 elif stop < 0: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
236 stop = self.width+stop |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
237 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
238 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
|
239 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
240 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
|
241 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
242 elif isinstance( key, str ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
243 # 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
|
244 # returned. |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
245 try: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
246 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
|
247 self.fieldnames.index(key) ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
248 except ValueError: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
249 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
|
250 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
|
251 raise |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
252 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
253 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
254 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
|
255 return self.getRow(key) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
256 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
257 def getFieldIndex(self, fieldname): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
258 try: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
259 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
|
260 except ValueError: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
261 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
|
262 %(fieldname, ','.join(self.fieldnames)) ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
263 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
264 class PMat( VMat ): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
265 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
266 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
|
267 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
|
268 self.fname = fname |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
269 self.inputsize = inputsize |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
270 self.targetsize = targetsize |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
271 self.weightsize = weightsize |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
272 if openmode=='r': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
273 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
|
274 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
|
275 self.load_fieldnames() |
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 elif openmode=='w': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
278 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
|
279 self.fieldnames = fieldnames |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
280 self.save_fieldnames() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
281 self.length = 0 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
282 self.width = len(fieldnames) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
283 self.elemtype = elemtype |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
284 self.swap_bytes = False |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
285 self.write_header() |
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 elif openmode=='a': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
288 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
|
289 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
|
290 self.load_fieldnames() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
291 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
292 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
293 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
|
294 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
295 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
|
296 shape = array.shape |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
297 if len(shape) == 1: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
298 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
|
299 elif len(shape) == 2: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
300 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
|
301 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
302 for row in array: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
303 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
|
304 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
305 def __del__(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
306 self.close() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
307 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
308 def write_header(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
309 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
|
310 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
311 if self.elemtype=='d': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
312 header += 'DOUBLE ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
313 self.elemsize = 8 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
314 elif self.elemtype=='f': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
315 header += 'FLOAT ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
316 self.elemsize = 4 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
317 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
318 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
|
319 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
|
320 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
321 if sys.byteorder=='little': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
322 header += 'LITTLE_ENDIAN ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
323 elif sys.byteorder=='big': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
324 header += 'BIG_ENDIAN ' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
325 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
326 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
|
327 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
328 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
|
329 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
330 self.f.seek(0) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
331 self.f.write(header) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
332 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
333 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
|
334 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
|
335 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
|
336 if mat_type!='MATRIX': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
337 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
|
338 self.length = int(l) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
339 self.width = int(w) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
340 if endianness=='LITTLE_ENDIAN': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
341 byteorder = 'little' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
342 elif endianness=='BIG_ENDIAN': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
343 byteorder = 'big' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
344 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
345 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
|
346 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
|
347 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
348 if data_type=='DOUBLE': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
349 self.elemtype = 'd' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
350 self.elemsize = 8 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
351 elif data_type=='FLOAT': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
352 self.elemtype = 'f' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
353 self.elemsize = 4 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
354 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
355 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
|
356 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
|
357 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
358 def load_fieldnames(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
359 self.fieldnames = [] |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
360 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
|
361 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
|
362 f = open(fieldnamefile) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
363 for row in f: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
364 row = row.split() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
365 if len(row)>0: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
366 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
|
367 f.close() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
368 else: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
369 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
|
370 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
371 def save_fieldnames(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
372 metadatadir = self.fname+'.metadata' |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
373 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
|
374 os.mkdir(metadatadir) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
375 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
|
376 f = open(fieldnamefile,'wb') |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
377 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
|
378 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
|
379 f.close() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
380 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
381 def getRow(self,i): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 if self.swap_bytes: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
388 ar.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
389 return ar |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
390 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 if self.swap_bytes: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
398 ar.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
399 return ar |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
400 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
401 def checkzerorow(self,i): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 if self.swap_bytes: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
408 ar.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
409 for elem in ar: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
410 if elem!=0: |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
411 return False |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
412 return True |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
413 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 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
|
418 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
|
419 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
|
420 raise IndexError |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
421 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
|
422 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
|
423 ar.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
424 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
|
425 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
|
426 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
|
427 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
|
428 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
429 def appendRow(self,row): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
430 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
|
431 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
|
432 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
|
433 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
|
434 ar.byteswap(True) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
435 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
|
436 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
|
437 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
438 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
|
439 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
|
440 self.length += 1 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
441 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
|
442 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
443 def flush(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
444 self.f.flush() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
445 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
446 def close(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
447 if hasattr(self, 'f'): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
448 self.f.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 def append(self,row): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
451 self.appendRow(row) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
452 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
453 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
|
454 l = self.length |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
455 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
|
456 self.putRow(i,row) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
457 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
458 def __len__(self): |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
459 return self.length |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
460 |
380
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
461 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
462 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
463 #copied from PLEARNDIR:python_modules/plearn/vmat/readAMat.py |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
464 def safefloat(str): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
465 """Convert the given string to its float value. It is 'safe' in the sense |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
466 that missing values ('nan') will be properly converted to the corresponding |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
467 float value under all platforms, contrarily to 'float(str)'. |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
468 """ |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
469 if str.lower() == 'nan': |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
470 return fpconst.NaN |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
471 else: |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
472 return float(str) |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
473 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
474 #copied from PLEARNDIR:python_modules/plearn/vmat/readAMat.py |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
475 def readAMat(amatname): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
476 """Read a PLearn .amat file and return it as a numarray Array. |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
477 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
478 Return a tuple, with as the first argument the array itself, and as |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
479 the second argument the fieldnames (list of strings). |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
480 """ |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
481 ### NOTE: this version is much faster than first creating the array and |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
482 ### updating each row as it is read... Bizarrely enough |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
483 f = open(amatname) |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
484 a = [] |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
485 fieldnames = [] |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
486 for line in f: |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
487 if line.startswith("#size:"): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
488 (length,width) = line[6:].strip().split() |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
489 elif line.startswith("#sizes:"): # ignore input/target/weight/extra sizes |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
490 continue |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
491 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
492 elif line.startswith("#:"): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
493 fieldnames = line[2:].strip().split() |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
494 pass |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
495 elif not line.startswith('#'): |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
496 # Add all non-comment lines. |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
497 row = [ safefloat(x) for x in line.strip().split() ] |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
498 if row: |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
499 a.append(row) |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
500 |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
501 f.close() |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
502 return numpy.numarray.array(a), fieldnames |
c2f17f231960
added function to load amat file
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
116
diff
changeset
|
503 |
114
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
504 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
505 if __name__ == '__main__': |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
506 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
|
507 pmat.append( [1, 2] ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
508 pmat.append( [3, 4] ) |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
509 pmat.close() |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
510 |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
511 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
|
512 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
|
513 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
|
514 |
9330d941fa1f
added function load_pmat_as_array_dataset and save_array_dataset_as_pmat
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
115
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 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
|
519 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
|
520 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
|
521 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
|
522 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
|
523 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
|
524 print i |
114
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
525 # print "+++ tmp.pmat contains: " |
d6d42a0c1275
file copied from PLearn/python_modules/plearn/vmat/PMat.py
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
diff
changeset
|
526 # 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 shutil.rmtree( fname+'.metadata' ) |