-
Notifications
You must be signed in to change notification settings - Fork 6
/
mat2h5.py
executable file
·318 lines (294 loc) · 15 KB
/
mat2h5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/local/python-3.5.2/bin/python3
#
# Copyright (C) 2016 by Howard Hughes Medical Institute.
# Author: Gennady Denisov
#
import scipy.io
import numpy
import h5py
import os
import time
import shutil, numpy
import sys, re, optparse
import scipy.sparse as sps
__version__ = "0.1"
def mat2hdf5_command_line_parser(parser):
parser.add_option("-o", "--outfolder", dest="output_folder", help="parent directory for the output tracker folder",metavar="output_folder",default=".")
parser.add_option("-i", "--inputfolder",dest="input_folder", help="folder containing choreography results", metavar="input_folder", default=".")
parser.add_option("-m", "--metadata", dest="metadata", help="(comma-separated list of) metadata file(s) to be combined with data file(s)",metavar="metadata",default="")
parser.add_option("-r", "--replace", action="store_true", dest="replace", help="if the output file already exists, replace/overwrite it", default=False)
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="increase the verbosity level of output", default=False)
return parser
# ----------------------------------------------------------------------
def get_array_type(array):
len_shape = len(array.shape)
if len_shape == 0:
array_type = type(array)
elif array.shape[0] == 0:
array_type = 'None'
elif len_shape == 1:
array_type = type(array[0])
else:
array_type = type(array[0][0])
return array_type
# ----------------------------------------------------------------------
def item_type(item_value):
item_type = ""
# if sps.issparse(sps.coo_matrix(item_value)):
if type(item_value).__name__ in ["csc_matrix", "csr_matrix"]:
return "sparse_matrix"
if hasattr(item_value , '__dict__'):
return "dictionary"
if hasattr(item_value , '__array__') and len(item_value.shape) > 0:
return "array"
try:
if isinstance(item_value, basestring):
return "string"
if isinstance(item_value, unicode):
return "unicode"
except:
if isinstance(item_value, bytes):
return "string"
if isinstance(item_value, str):
return "unicode"
if isinstance(item_value, int):
return "int"
if isinstance(item_value, float):
return "float"
if item_value.dtype in ["uint8", "uint16"]:
return "uint"
if isinstance(item_value, complex):
return "complex"
if item_value.dtype == "object":
return "object"
return item_type
# ----------------------------------------------------------------------
def parse_item(item_name, item_value, hdf5_group, level, upper_folder, options):
# if item_name in ('timeSeriesArrayHash', 'descr'):
# import pdb; pdb.set_trace()
if item_type(item_value) == "sparse_matrix":
if options.verbose:
print ("...Handling1 sparse_matrix ", item_name, \
" of dtype=", item_value.dtype, " and shape=", item_value.shape)
array = numpy.zeros(item_value.shape, dtype = item_value.dtype)
array[:] = item_value.todense()
dset = hdf5_group.create_dataset(item_name, item_value.shape, \
dtype=item_value.dtype, data=array)
elif item_type(item_value) == "dictionary": # is folder
# if re.search("scipy.sparse", type(item_value)):
# if sps.issparse(sps.coo_matrix(item_value)):
# print "type(", item_name, ")=", type(item_value).__name__
# try:
# print "class_name=", item_value._class_.__name
# except:
# print item_name, " has no class attribute"
keys = item_value.__dict__.keys()
if options.verbose:
print ("...Handling2 item ", item_name, \
" of type 'dictionary' at level ", level, \
" in parent folder ", upper_folder, " keys =", keys)
if len(keys) > 0:
for k in sorted(keys):
if not re.search('__', k) and not k[0]=='_':
hdf5_subfolder = hdf5_group.create_group(k)
parse_item(k, item_value.__dict__[k], hdf5_subfolder, \
level+1, item_name, options)
elif item_type(item_value) == "array":
if options.verbose:
print ("...Handling3 item ", item_name, " in parent folder ", upper_folder, \
" item type 'array' of shape ", item_value.shape, \
"at level ", level, " item_value=", item_value)
if item_value.shape[0] > 0:
print (" item type=", item_type(item_value[0]))
len_shape = len(item_value.shape)
itype = get_array_type(item_value )
idtype = item_value.dtype
# if not item_type(item_value[0]) == "string":
ishape = item_value.shape
# else:
# ishape = tuple([len(i) for i in item_value])
# print "ishape=", ishape
# if not itype == 'None':
if options.verbose:
print (" array len=", item_name.__len__(), \
" shape=", ishape, " type=", itype, " dtype=", idtype)
if not str(idtype) == 'object':
if options.verbose:
print ("item_name=", item_name, " ishape=", ishape, " idtype=", idtype.type)
if len_shape > 0:
# dset = hdf5_group.create_dataset(item_name, ishape, dtype=idtype, data=item_value )
try:
dset = hdf5_group.create_dataset(item_name, ishape, data=item_value )
except:
data = numpy.array([a.encode('utf8') for a in item_value]) # explicitly encoding
dset = hdf5_group.create_dataset(item_name, ishape, dtype=data.dtype, data=data)
else: # variable-length string
dt = h5py.special_dtype(vlen=str)
dset = hdf5_group.create_dataset(item_name, (1,1), dtype=dt, data=str(item_value ))
elif len_shape ==1 and item_type(item_value[0]) == "array" and item_value[0].dtype.type is 'numpy.unicode_':
if options.verbose:
print (" ...Handling4 cellular array ", item_name, \
" of strings, shape=", ishape, \
" type(item_value[0])=", item_value[0].dtype.type, \
" item_value[0]=", item_value[0], " at level ", level)
dset = hdf5_group.create_dataset(item_name, ishape[0], dtype='numpy.unicode_', data=item_value )
elif len_shape ==1 and item_type(item_value[0]) == "array":
if options.verbose:
print (" ...Handling5 cellular array ", item_name, \
" of objects of type array", " at level ", level)
for d in range(0, int(ishape[0])):
hdf5_subfolder = hdf5_group.create_group(str(d+1))
parse_item(str(d+1), item_value[d], hdf5_subfolder, level+1, \
item_name, options)
elif len_shape ==1 and item_type(item_value[0]) == "dictionary":
if options.verbose:
print (" ...Handling6 cellular array of structures ", item_name, \
" ishape=", ishape, " at level ", level)
for d in range(0, int(ishape[0])):
hdf5_subfolder = hdf5_group.create_group(str(d+1))
if item_type(item_value[d]) == "dictionary":
keys = item_value[d].__dict__.keys()
if options.verbose:
print (" d=", str(d), " keys=", keys)
if len(keys) > 0:
for k in sorted(keys):
if not re.search('__', k) and not k[0]=='_':
hdf5_subfolder2 = hdf5_subfolder.create_group(k)
value = item_value[d].__dict__[k]
value_str = str(value)
value_len = len(value_str)
if value_len > 0 and not value_str=='[]' \
and not value_str=='[[] []]':
if options.verbose:
print (" item with name=", k, \
" value=", value , " value_len=", value_len)
parse_item(k, item_value[d].__dict__[k], \
hdf5_subfolder2, level+1,item_name, options)
elif item_type(item_value[d]) == "array":
ishape_d = item_value[d].shape
itype_d = item_type(item_value[d][0])
if options.verbose:
print (" ...Handling7 tricky array ", d, " of shape ", ishape_d, " and dtype", item_value[d].dtype,\
" in parent folder ", upper_folder + "/" + item_name, " at level ", level)
dset = hdf5_subfolder.create_dataset(str(d), ishape_d, dtype=item_value[d].dtype, data=item_value[d])
else:
print (" ...Cannot1 handle item ", item_name, "of type ", item_type(item_value[d]), \
" in upper_folder", upper_folder)
elif item_type(item_value[0]) in ["string", "unicode"]:
# ishape = tuple([len(i) for i in item_value ])
if options.verbose:
print ("...Handling8 array of strings ", item_name, "item_value.dtype=", item_value.dtype, " ishape=", ishape)
print ("item_value=", item_value, " at level ", level)
# data = numpy.chararray(ishape, itemsize=100)
# data[:] = item_value
data = numpy.array(item_value, dtype=object)
if options.verbose:
print ("data=", data)
if item_type(item_value[0]) == "unicode":
dt = h5py.special_dtype(vlen=str)
else:
dt = h5py.special_dtype(vlen=bytes)
dset = hdf5_group.create_dataset(item_name, data.shape, dtype=dt, data=data)
elif item_type(item_value[0]) == "int":
if options.verbose:
print ("...Handling9 array of type int and size", len(item_value))
data = numpy.array(numpy.int32(item_value))
if options.verbose:
print ("data=", data, " ishape=", ishape)
dset = hdf5_group.create_dataset(item_name, ishape, dtype = numpy.int32, data=data)
else:
print (" ...Cannot2 handle array ",item_name, "of idtype ",idtype,\
" and type ", item_type(item_value[0]), "in upper_folder", upper_folder)
elif item_type(item_value) in ["string", "unicode"]:
if options.verbose:
print ("...Handling10 string2 item ", item_name, " in parent folder ", upper_folder, \
" item type ", item_type(item_value), \
"at level ", level, " item_value=", item_value)
# data = numpy.chararray((1,), itemsize=100)
data = numpy.chararray((1,), itemsize=len(item_value)) # -jt
data[:] = item_value
if options.verbose:
print ("data=", data)
try:
dt = h5py.special_dtype(vlen=str)
except:
dt = h5py.special_dtype(vlen=unicode)
dset = hdf5_group.create_dataset(item_name, (1,), dtype=dt, data=data)
elif item_type(item_value) == "float":
if options.verbose:
print ("...Handling11 item ", item_name, " in parent folder ", upper_folder, \
" item type ", item_type(item_value), \
"at level ", level, " item_value=", item_value)
dset = hdf5_group.create_dataset(item_name, dtype=numpy.float_, \
data=numpy.float_(item_value ), \
shape=(1,))
elif item_type(item_value) == "int":
if options.verbose:
print ("...Handling12 item ", item_name, " in parent folder ", upper_folder, \
" item type ", item_type(item_value), \
"at level ", level, " item_value=", item_value)
dset = hdf5_group.create_dataset(item_name, dtype=numpy.int_, \
data=numpy.int_(item_value ), \
shape=(1,))
else:
# Create annotation
print ("...Cannot3 handle item ", item_name, \
" of type ", item_type(item_value), " at level=", str(level), \
" in upper_folder=", upper_folder, " item_value=", item_value )
# ----------------------------------------------------------------------
def create_hdf5_file(mat_file_name, hdf5_file_name, options):
# Read the input file and initialize hdf5 object
# print "opening mat file ..."
mat = scipy.io.loadmat(mat_file_name,squeeze_me=True,chars_as_strings=True,struct_as_record=False)
if options.verbose:
print("Opening the output hdf5 file " + hdf5_file_name + " ...")
if options.replace:
os.system("rm -f " + hdf5_file_name)
try:
f = h5py.File(hdf5_file_name, 'w')
except:
sys.exit("\nPlease, make sure the output file does not exist or can be overwritten, or use option -r")
# Parse mat file and store its contents in hdf5 object
print ("parsing ...")
header = mat['__header__']
version = mat['__version__']
# print "Attributes=", dir(mat)
keys = mat.keys()
if options.verbose:
print ("keys=", keys, " groups:", f.keys())
if len(keys) > 0:
for k in sorted(keys):
if not re.search('__', k):
parse_item(k, mat[k], f, 1, 'top', options)
# Add informations from metadata file
if len(options.metadata) > 0:
mat2 = scipy.io.loadmat(options.metadata,squeeze_me=True,struct_as_record=False)
metadata = f.create_group('metadata')
keys2 = mat2.keys()
if options.verbose:
print ("metadata keys=", keys, " groups:", metadata.keys())
for k2 in sorted(keys2):
if not re.search('__', k2):
parse_item(k2, mat2[k2], metadata, 1, 'metadata', options)
print ("...Done")
# ----------------------------------------------------------------------
if __name__ == "__main__":
usage = "Usage: \n\
%prog mat_file_name [options (-h to list)]"
parser = optparse.OptionParser(usage=usage, version="%%prog %s" % __version__)
parser = mat2hdf5_command_line_parser(parser)
(options, args) = parser.parse_args()
if options.verbose:
print ("len(args)=", len(args))
if len(args) >= 1:
# Extract the name of an input file
mat_file_name = str(args[0:1][0])
if not mat_file_name.split(".")[1] == "mat":
sys.exit("\nNot a MAT file")
hdf5_file_name = mat_file_name[0:len(mat_file_name)-3] + "h5"
print ("Input: ", mat_file_name)
print ("Output: ", hdf5_file_name)
create_hdf5_file(mat_file_name, hdf5_file_name, options)
else:
parser.print_usage()
sys.exit(2)