-
Notifications
You must be signed in to change notification settings - Fork 2
/
myfilemanager.py
104 lines (80 loc) · 3.03 KB
/
myfilemanager.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
import numpy as np
class obj_from_dict:
def __init__(self, dictto):
for kk in list(dictto.keys()):
setattr(self, kk, dictto[kk])
def obj_to_dict(obj):
dict_out={}
members = dir(obj)
for member in members:
dict_out[member] = getattr(obj, member)
return dict_out
def myloadmat(filename, squeeze = True):
import scipy.io as sio
dict_var=sio.loadmat(filename)
if squeeze:
for kk in list(dict_var.keys()):
try:
dict_var[kk]=np.squeeze(dict_var[kk])
except:
pass
return dict_var
def myloadmat_to_obj(filename, squeeze = True):
return obj_from_dict(myloadmat(filename, squeeze=squeeze))
def dict_of_arrays_and_scalar_from_h5(filename):
import h5py
with h5py.File(filename, 'r') as fid:
f_dict = {}
for kk in list(fid.keys()):
f_dict[kk] = np.array(fid[kk]).copy()
if f_dict[kk].shape == ():
f_dict[kk] = f_dict[kk].tolist()
return f_dict
def object_with_arrays_and_scalar_from_h5(filename):
return obj_from_dict(dict_of_arrays_and_scalar_from_h5(filename))
def monitorh5_to_dict(filename, key= 'Bunch'):
import h5py
with h5py.File(filename, 'r') as monitor_ev:
monitor = monitor_ev[key]
monitor_dict = {}
for kk in list(monitor.keys()):
monitor_dict[kk] = np.array(monitor[kk]).copy()
return monitor_dict
def monitorh5_to_obj(filename, key= 'Bunch'):
return obj_from_dict(monitorh5_to_dict(filename, key))
def monitorh5list_to_dict(filename_list, key='Bunch', permissive=False):
monitor_dict = monitorh5_to_dict(filename_list[0], key=key)
for i_file in range(1,len(filename_list)):
print(('Loading '+filename_list[i_file]))
try:
monitor_dict_curr = monitorh5_to_dict(filename_list[i_file])
for kk in list(monitor_dict.keys()):
monitor_dict[kk] = np.array(list(monitor_dict[kk])+list(monitor_dict_curr[kk]))
except IOError as err:
print('Got:')
print(err)
if not permissive:
raise err
return monitor_dict
def monitorh5list_to_obj(filename_list, key= 'Bunch', permissive=False):
return obj_from_dict(monitorh5list_to_dict(filename_list, key, permissive))
def dict_to_h5(dict_save, filename):
import h5py
with h5py.File(filename, 'w') as fid:
for kk in list(dict_save.keys()):
fid[kk] = dict_save[kk]
# Only works for not nested h5 files
def h5_to_obj(filename):
import h5py
d = {}
with h5py.File(filename, 'r') as f:
for key in f:
d[key] = np.array(f[key])
return obj_from_dict(d)
# Only works for not nested attributes of object
def aligned_obj_to_h5(obj, h5):
import h5py
with h5py.File(h5, 'w') as h5_handle:
h5_handle.create_dataset('timestamps', data=obj.timestamps)
h5_handle.create_dataset('variables', data=np.string_(obj.variables))
h5_handle.create_dataset('data', data=obj.data)