-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckbd5.py
133 lines (126 loc) · 5.91 KB
/
checkbd5.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
# checkbd5.py
import h5py
import numpy as np
def checkbd5group(datahandle):
if isinstance(datahandle, h5py.Group) == False:
errormsg = 'error: no data Group found'
return False, errormsg
else:
errormsg = 'error: none'
return True, errormsg
def checkbd5dataset(datahandle):
if isinstance(datahandle, h5py.Dataset) == False:
errormsg = 'error: no data set found'
return False, errormsg
else:
errormsg = 'error: none'
return True, errormsg
def checkbd5attribute(datahandle, attribute, typedef):
# print("running checkbd5attribute")
if isinstance(datahandle, h5py.Dataset) == False:
errormsg = 'error: no Dataset found'
return False, errormsg
if (attribute in datahandle.dtype.names) == False:
errormsg = 'error: '+attribute+'not found'
return False, errormsg
elif (attribute in datahandle.dtype.names) == True:
# print(attribute+"is found!!!!")
if (typedef == 'float'):
if ((isinstance(datahandle[attribute][0], np.float32)) or isinstance(datahandle[attribute][0], np.float64)) == True:
# print("checkingfloat %s, %s" % (attribute, type(datahandle[attribute][0])))
# errormsg = 'error: none'
# return True, "float"
return True, str(datahandle[attribute][0].dtype)
else:
errormsg = "error: not a float type"
# print(" %s, %s" % (attribute, type(datahandle[attribute][0])))
print(errormsg)
return False, errormsg
elif (typedef == 'hybridfloatstring'):
if ((isinstance(datahandle[attribute][0], np.float32) or
isinstance(datahandle[attribute][0], np.float64) or
isinstance(datahandle[attribute][0], np.bytes_) or
isinstance(datahandle[attribute][0], np.integer))) == True:
# print("checkingfloat %s, %s" % (attribute, type(datahandle[attribute][0])))
# errormsg = 'error: none'
# return True, "float"
return True, str(datahandle[attribute][0].dtype)
else:
errormsg = "error: not a float or string type"
# print(" %s, %s" % (attribute, type(datahandle[attribute][0])))
print(errormsg)
return False, errormsg
elif (typedef == 'string'):
# print("checkbd5 - isinstance a np.string")
# print("checkbd5 - the type of %s, %s" % (attribute, type(datahandle[attribute][0])))
# print("isinstance np.bytes_ is %s" % (isinstance(datahandle[attribute][0], np.bytes_)))
if (isinstance(datahandle[attribute][0], np.bytes_)):
# print("checkbd5 - isinstance is a np.bytes_")
# print("Pass bytes %s, %s" % (attribute, type(datahandle[attribute][0])))
# errormsg = 'error: none'
# return True, "string"
return True, str(datahandle[attribute][0].dtype)
# print("this shouldn't run!")
elif (isinstance(datahandle[attribute][0], np.string_)) == True:
# print("Pass string %s, %s" % (attribute, type(datahandle[attribute][0])))
# errormsg = 'error: none'
return True, str(datahandle[attribute][0].dtype)
else:
errormsg = "error: not a string type"
# print("error part! %s, %s" % (attribute, type(datahandle[attribute][0])))
print(errormsg)
return False, errormsg
elif (typedef == 'integer'):
if (isinstance(datahandle[attribute][0], np.integer)):
# print(" %s, %s" % (attribute, type(datahandle[attribute][0])))
# errormsg = 'error: none'
# return True, "integer"
return True, str(datahandle[attribute][0].dtype)
else:
errormsg = "error: not a integer type"
# print(" %s, %s" % (attribute, type(datahandle[attribute][0])))
print(errormsg)
return False, errormsg
else:
errormsg = "error: undefined type"
# print(" %s, %s" % (attribute, type(datahandle[attribute][0])))
print(errormsg)
return False, errormsg
else:
print(errormsg)
return False, errormsg
def checkbd5entity(datahandle, entity=None):
if isinstance(datahandle, h5py.Group) == False:
errormsg = 'error: no Group found'
return False, errormsg
# check each entry on the validity of the entity tag.
if entity == None:
# print("Checking whether entities are eligible")
entitylist = ['point', 'line', 'face', 'circle', 'sphere']
else:
# check each entry and see whether it has the same entity tag.
entitylist = [entity]
for i in datahandle: # for i in f['data/'+t+'/object/']
if isinstance(datahandle[i], h5py.Dataset) == False:
errormsg = 'error: no Dataset found'
return False, errormsg
for j in datahandle[i]:
data_entity = np.char.decode(j['entity'])
entity_check = False
for k in entitylist:
# check whether the entity is one that is allowed in the entitylist
#print(k, data_entity)
if data_entity == k:
entity_check = True
if entity_check == False:
errormsg = "entity %s is not eligible or differ, ID=%s" % (data_entity,np.char.decode(j['ID']))
return False, errormsg
return True
def checkbd5file(filename):
f = h5py.File(filename, "r")
if isinstance(f, h5py.File) == False:
errormsg = "error: cannot find h5file"
print(errormsg)
return False, errormsg
else:
return True, f