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