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