-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhieracy_text.py
405 lines (362 loc) · 14.1 KB
/
hieracy_text.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import numpy as np
import pandas
import h5py
import json
import os
### classes to proces sensor descriptions
class AliasDict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.aliases = {}
def __getitem__(self, key):
return dict.__getitem__(self, self.aliases.get(key, key))
def __setitem__(self, key, value):
return dict.__setitem__(self, self.aliases.get(key, key), value)
def add_alias(self, key, alias):
self.aliases[alias] = key
class ChannelDescription:
def __init__(self, CHID):
"""
Parameters
----------
CHID : intger
ID of the channel startig with 1.
Returns
-------
None.
"""
self.Description = {
"CHID": CHID,
"PHYSICAL_QUANTITY": None,
"UNIT": None,
"RESOLUTION": None,
"MIN_SCALE": None,
"MAX_SCALE": None,
"HIERARCHY": None,
}
self._complete = False
def __getitem__(self, key):
# if key='SpecialKey':
# self.Description['SpecialKey']
return self.Description[key]
def __setitem__(self, key, item):
# if key='SpecialKey':
# self.Description['SpecialKey']
self.Description[key] = item
def __repr__(self):
"""
Prints the quantity and unit of the channel.
"""
return (
"Channel: "
+ str(self.Description["CHID"])
+ " ==>"
+ str(self.Description["PHYSICAL_QUANTITY"])
+ " in "
+ str(self.Description["UNIT"])
)
# todo override set methode
def setDescription(self, key, value):
"""
Sets an spefic key of an channel description.
Parameters
----------
key : string
PHYSICAL_QUANTITY",UNIT,RESOLUTION,MIN_SCALE, MAX_SCALE,HIERACY.
value : string or intger
valuie coresponding to the key.
Returns
-------
None.
"""
self.Description[key] = value
if (
self.Description["PHYSICAL_QUANTITY"] != None
and self.Description["UNIT"] != None
and self.Description["RESOLUTION"] != None
and self.Description["MIN_SCALE"] != None
and self.Description["MAX_SCALE"] != None
and self.Description["HIERARCHY"] != None
):
self._complete = True
class SensorDescription:
"""
this class is holding the Sensor description.
It's subscriptable by :
1. inter number of the channel eg.g. SensorDescription[1]
2. Name of The physical quantity SensorDescription["Temperature"]
3. Name of the data field SensorDescription["Data_01"]
"""
def __init__(self, ID=0x00000000, SensorName="undefined", fromDict=None):
"""
Parameters
----------
ID : uint32
ID of the Sensor.The default is 0x00000000
SensorName : sting
Name of the sensor.The default is "undefined".
fromDict : dict
If an Description dict is passed the Channel params will be set accordingly.
Returns
-------
None.
"""
self.ID = ID
self.SensorName = SensorName
self._complete = False
self.Channels = AliasDict([])
self.ChannelCount = 0
self._ChannelsComplte = 0
if type(fromDict) is dict:
try:
self.ID = fromDict["ID"]
except KeyError:
warnings.warn("ID not in Dict", RuntimeWarning)
try:
self.SensorName = fromDict["Name"]
except KeyError:
warnings.warn("Name not in Dict", RuntimeWarning)
for i in range(16):
try:
channelDict = fromDict[i]
for key in channelDict.keys():
if key == "CHID":
pass
else:
self.setChannelParam(
channelDict["CHID"], key, channelDict[key]
)
print("Channel " + str(i) + " read from dict")
except KeyError:
# ok maybe the channels are coded as string
try:
channelDict = fromDict[str(i)]
for key in channelDict.keys():
if key == "CHID":
pass
else:
self.setChannelParam(
channelDict["CHID"], key, channelDict[key]
)
print("Channel " + str(i) + " read from dict")
except KeyError:
pass
def setChannelParam(self, CHID, key, value):
"""
Set parametes for an specific channel.
Parameters
----------
CHID : intger
ID of the channel startig with 1.
key : string
PHYSICAL_QUANTITY",UNIT,RESOLUTION,MIN_SCALE or MAX_SCALE.
value : string or intger
valuie coresponding to the key.
Returns
-------
None.
"""
wasComplete = False
if CHID in self.Channels:
wasComplete = self.Channels[
CHID
]._complete # read if channel was completed before
self.Channels[CHID].setDescription(key, value)
if key == "PHYSICAL_QUANTITY":
self.Channels.add_alias(
CHID, value
) # make channels callable by their Quantity
else:
if key == "PHYSICAL_QUANTITY":
self.Channels.add_alias(
CHID, value
) # make channels callable by their Quantity
self.Channels[CHID] = ChannelDescription(CHID)
self.Channels[CHID].setDescription(key, value)
self.Channels.add_alias(
CHID, "Data_" + "{:02d}".format(CHID)
) # make channels callable by ther Data_xx name
self.ChannelCount = self.ChannelCount + 1
if wasComplete == False and self.Channels[CHID]._complete:
self._ChannelsComplte = self._ChannelsComplte + 1
if self._ChannelsComplte == self.ChannelCount:
self._complete = True
print("Description completed")
def __getitem__(self, key):
"""
Reutrns the description for an channel callable by Channel ID eg 1, Channel name eg. Data_01 or Physical PHYSICAL_QUANTITY eg. Acceleration_x.
Parameters
----------
key : sting or int
Channel ID eg 1, Channel name eg. "Data_01" or Physical PHYSICAL_QUANTITY eg. "X Acceleration".
Returns
-------
ChannelDescription
The description of the channel.
"""
# if key='SpecialKey':
# self.Description['SpecialKey']
return self.Channels[key]
def __repr__(self):
return "Descripton of" + self.SensorName + hex(self.ID)
def asDict(self):
"""
ChannelDescription as dict.
ReturnsDR.
-------
ReturnDict : dict
ChannelDescription as dict.
"""
ReturnDict = {"Name": self.SensorName, "ID": self.ID}
for key in self.Channels:
print(self.Channels[key].Description)
ReturnDict.update(
{self.Channels[key]["CHID"]: self.Channels[key].Description}
)
return ReturnDict
def getUnits(self):
units = {}
for Channel in self.Channels:
if self.Channels[Channel]["UNIT"] in units:
units[self.Channels[Channel]["UNIT"]].append(Channel)
else:
units[self.Channels[Channel]["UNIT"]] = [Channel]
return units
def getActiveChannelsIDs(self):
return self.Channels.keys()
def gethieracyasdict(self):
self.hiracydict = {}
channelsperdatasetcount = {}
# loop over all channels to extract gropus and count elemnts perfomance dosent matter since only a few channels (16 max) are expected per description
for Channel in self.Channels:
splittedhieracy = self.Channels[Channel]["HIERARCHY"].split("/")
if len(splittedhieracy) != 2:
raise ValueError(
"HIERACY "
+ Channel["HIERARCHY"]
+ " is invalide since it was no split in two parts"
)
try:
splittedhieracy[1] = int(splittedhieracy[1])
except ValueError:
raise ValueError(
"HIERACY "
+ Channel["HIERARCHY"]
+ "is invalide since last part is not an integer"
)
if splittedhieracy[0] in channelsperdatasetcount:
channelsperdatasetcount[splittedhieracy[0]] = (
channelsperdatasetcount[splittedhieracy[0]] + 1
)
else:
channelsperdatasetcount[splittedhieracy[0]] = 1
print(channelsperdatasetcount)
for key in channelsperdatasetcount.keys():
self.hiracydict[key] = {
"copymask": np.zeros(channelsperdatasetcount[key]).astype(int)
}
self.hiracydict[key]["PHYSICAL_QUANTITY"] = [
None
] * channelsperdatasetcount[key]
self.hiracydict[key]["RESOLUTION"] = np.zeros(channelsperdatasetcount[key])
self.hiracydict[key]["MIN_SCALE"] = np.zeros(channelsperdatasetcount[key])
self.hiracydict[key]["MAX_SCALE"] = np.zeros(channelsperdatasetcount[key])
# print(self.hiracydict)
# loop a second time infecient but don't care error check no nessary since done before
# no align chann
for Channel in self.Channels:
splittedhieracy = self.Channels[Channel]["HIERARCHY"].split("/")
self.hiracydict[splittedhieracy[0]]["copymask"][int(splittedhieracy[1])] = (
self.Channels[Channel]["CHID"] - 1
)
self.hiracydict[splittedhieracy[0]]["MIN_SCALE"][
int(splittedhieracy[1])
] = self.Channels[Channel]["MIN_SCALE"]
self.hiracydict[splittedhieracy[0]]["MAX_SCALE"][
int(splittedhieracy[1])
] = self.Channels[Channel]["MAX_SCALE"]
self.hiracydict[splittedhieracy[0]]["RESOLUTION"][
int(splittedhieracy[1])
] = self.Channels[Channel]["RESOLUTION"]
self.hiracydict[splittedhieracy[0]]["PHYSICAL_QUANTITY"][
int(splittedhieracy[1])
] = self.Channels[Channel]["PHYSICAL_QUANTITY"]
self.hiracydict[splittedhieracy[0]]["UNIT"] = self.Channels[Channel][
"UNIT"
] # tehy ned to have the same unit by definition so we will over write it mybe some times but will not change anny thing
print(self.hiracydict)
return self.hiracydict
def convertdumpfile(dumpfile):
with open(dumpfile) as f:
jsonstring = f.readline()
dscp = json.loads(jsonstring)
print(dscp)
df = pandas.read_csv(dumpfile, sep=";", header=1)
hdf5filename = dumpfile.replace("csv", "hdf5")
# hdffile = h5py.File(hdf5filename, 'a')
# group=hdffile.create_group(hex(dscp[ID]))
return dscp, df
if __name__ == "__main__":
dscpstr, df = convertdumpfile(
r"D:\datareceiver\data\20201012133232_MPU_9250_0x60ad0100_00000.dump"
)
dscp = SensorDescription(fromDict=dscpstr)
hieracy = dscp.gethieracyasdict()
os.remove("test2.hdf5")
f = h5py.File(r"test2.hdf5", "a")
rawvalues = df.values.transpose()
dataframindexoffset = 5
Datasets = {}
chunksize = 1000
group = f.create_group(hex(dscp.ID) + "_" + dscp.SensorName.replace(" ", "_"))
group.attrs["Data_description_json"] = json.dumps(dscp.asDict())
group.attrs["Sensor_name"] = dscp.SensorName
group.attrs["Sensor_ID"] = dscp.ID
group.attrs["Data_description_json"] = json.dumps(dscp.asDict())
Datasets["Absolutetime"] = group.create_dataset(
"Absolutetime",
([1, chunksize]),
maxshape=(1, None),
dtype="float64",
compression="gzip",
shuffle=True,
)
Datasets["Absolutetime"].resize([1, rawvalues.shape[1]])
time = rawvalues[2] + rawvalues[3] * 1e-9
Datasets["Absolutetime"][...] = time.astype(np.float64)
Datasets["Absolutetime"].make_scale("Absoluitetime")
Datasets["Sample_number"] = group.create_dataset(
"Sample_number",
([1, chunksize]),
maxshape=(1, None),
dtype="float64",
compression="gzip",
shuffle=True,
)
Datasets["Sample_number"].resize([1, rawvalues.shape[1]])
Datasets["Sample_number"][...] = rawvalues[1].astype(np.uint32)
for Groupname in hieracy:
vectorlength = len(hieracy[Groupname]["copymask"])
Datasets[Groupname] = group.create_dataset(
Groupname,
([vectorlength, chunksize]),
maxshape=(3, None),
dtype="float32",
compression="gzip",
shuffle=True,
) # compression="gzip",shuffle=True,
Datasets[Groupname].resize([vectorlength, rawvalues.shape[1]])
Datasets[Groupname][...] = rawvalues[
hieracy[Groupname]["copymask"] + dataframindexoffset
].astype("float32")
Datasets[Groupname].dims[0].label = "Absoluitetime"
Datasets[Groupname].dims[0].attach_scale(Datasets["Absolutetime"])
Datasets[Groupname].attrs["Unit"] = hieracy[Groupname]["UNIT"]
Datasets[Groupname].attrs["Physical_quantity"] = hieracy[Groupname][
"PHYSICAL_QUANTITY"
]
Datasets[Groupname].attrs["Resolution"] = hieracy[Groupname]["RESOLUTION"]
Datasets[Groupname].attrs["Max_scale"] = hieracy[Groupname]["MAX_SCALE"]
Datasets[Groupname].attrs["Min_scale"] = hieracy[Groupname]["MIN_SCALE"]
f.flush()
f.close()