annotate pmat.py @ 115:01aa97a2212d

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