-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5pyTools.py
87 lines (75 loc) · 3.02 KB
/
h5pyTools.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
#import ezodf
import numpy as np
import pdb
import h5py
class h5pyTools:
def __init__(self):
pass
############################################################
# creates a new data-set if it doesn't exist, otherwise overwrites
############################################################
def createOverwriteDS(self,grp,dsname,data,at=None):
try:
rec = grp.require_dataset(dsname, shape = np.shape(data), dtype = data.dtype, exact=False )
# either different shape or not exisiting
except TypeError:
try:
del grp[dsname]
except KeyError:
try:
grp.create_dataset(dsname,data=data,dtype = data.dtype)
except TypeError:
string_dt = h5py.special_dtype(vlen=str)
data=np.array(data, dtype=string_dt)
grp.create_dataset(dsname, data=data, dtype=string_dt)
else:
grp.create_dataset(dsname,data=data,dtype = data.dtype)
else:
rec[:] = data
# save attributes
if at:
#print('evaluating list shape')
# print('list shape :', at, self.listShape(at))
if len(self.listShape(at))==1:
try:
grp[dsname].attrs[at[0]]=at[1]
except:
grp[dsname].attrs[at[0]] = at[1]['.'][at[0]]
else:
for i in range(len(at)):
try:
grp[dsname].attrs[at[i][0]]=at[i][1]
except:
pdb.set_trace()
############################################################
def getH5GroupName(self,f,groupNames):
current_group = ''
for i in range(len(groupNames)):
if i == 0:
try:
grpHandle = f.require_group(groupNames[i])
except KeyError:
groupNames[i] = groupNames[i] + '_new'
grpHandle = f.require_group(groupNames[i])
else:
try:
grpHandle = f[current_group].require_group(groupNames[i])
except KeyError:
groupNames[i] = groupNames[i] + '_new'
grpHandle = f[current_group].require_group(groupNames[i])
current_group += groupNames[i] + '/'
return (current_group[:-1],grpHandle)
############################################################
def deleteElement(self,f,elementName):
del f[elementName]
############################################################
#def list_shape(self,inputList):
def listShape(self,lst):
def ishape(lst):
shapes = [ishape(x) if isinstance(x, list) else [] for x in lst]
shape = shapes[0]
if shapes.count(shape) != len(shapes):
raise ValueError('Ragged list')
shape.append(len(lst))
return shape
return tuple(reversed(ishape(lst)))