-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_proc_func.py
359 lines (323 loc) · 18.9 KB
/
data_proc_func.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
import collections
import queue
import numpy as np
import kafka_helper
import plot_func
HEADER_STRING = "ffffffffffffffff0"
END_HEADER = "efffffffffffffff0"
def kafka_frame_decoder(start_time, end_time):
processed_data = ""
current_data = ""
i = 0
all_data = kafka_helper.get_data_between(start_time, end_time)
list_length = len(all_data)
for i in range(0, list_length):
current_data = (all_data[i].get("packet")).replace(HEADER_STRING, "\n" + HEADER_STRING) \
.replace(END_HEADER, "\n" + END_HEADER)
processed_data = processed_data + current_data
return processed_data
#######Gets historic data from kafka and returns separate dictionaries entry for each line ####################
def kafka_frame_decoder_ip_nest_dict(start_time, end_time):
current_data = {'packet': "", 'packet_info': ""}
all_data = kafka_helper.get_data_between(start_time, end_time)
list_length = len(all_data)
processed_data = [{'packet': "", 'packet_info': ""} for i in range(list_length)]
for i in range(0, list_length):
current_data['packet'] = (all_data[i].get("packet")).replace(HEADER_STRING, "\n" + HEADER_STRING) \
.replace(END_HEADER, "\n" + END_HEADER)
current_data['packet_info'] = all_data[i].get("packet_info")
processed_data[i].update(current_data)
return processed_data
#############Gets historic data from kafka and returns separate dictionaries of each IP addresses#######
def kafka_frame_decoder_ip_dict(start_time, end_time):
current_data = {'packet': "", 'packet_info': ""}
all_data = kafka_helper.get_data_between(start_time, end_time)
list_length = len(all_data)
ip_data = {}
for i in range(0, list_length):
current_data['packet'] = (all_data[i].get("packet")).replace(HEADER_STRING, "\n" + HEADER_STRING) \
.replace(END_HEADER, "\n" + END_HEADER)
current_data['packet_info'] = all_data[i].get("packet_info")
if current_data.get("packet_info") in ip_data:
ip_data[current_data.get("packet_info")] = ip_data[current_data.get("packet_info")] \
+ current_data.get("packet")
else:
ip_data[current_data.get("packet_info")] = current_data.get("packet")
return ip_data
#############Gets historic data from kafka and returns separate dictionaries of each IP addresses#######
def kafka_frame_decoder_ip_dict_split_line(start_time, end_time):
current_data = {'packet': "", 'packet_info': ""}
all_data = kafka_helper.get_data_between(start_time, end_time)
list_length = len(all_data)
ip_data = {}
for i in range(0, list_length):
current_data['packet'] = ((all_data[i].get("packet")).replace(HEADER_STRING, "\n" + HEADER_STRING) \
.replace(END_HEADER, "\n" + END_HEADER))
current_data['packet_info'] = all_data[i].get("packet_info")
current_data['packet'] = current_data.get("packet").splitlines() # turn carriage return string into a list
current_data['packet'] = [e[128:] for e in current_data.get("packet")] # remove the header from the list
if current_data.get("packet_info") in ip_data:
ip_data[current_data.get("packet_info")] = ip_data[current_data.get("packet_info")] \
+ current_data.get("packet")
else:
ip_data[current_data.get("packet_info")] = current_data.get("packet")
return ip_data
#############Works on data live from kafka and returns separate dictionaries of each IP addresses#######
def kafka_data_decoder_ip_dict(all_data):
current_data = {'packet': "", 'packet_info': ""}
ip_data = {}
current_data['packet'] = (all_data[0].get("packet")).replace(HEADER_STRING, "\n" + HEADER_STRING) \
.replace(END_HEADER, "\n" + END_HEADER)
current_data['packet'] = current_data.get("packet").splitlines() # turn carriage return string into a list
current_data['packet'] = [e[128:] for e in current_data.get("packet")] # remove the header from the list
current_data['packet_info'] = all_data[0].get("packet_info")
return current_data
def data_split_string(kafka_data):
position, PulseHeight, StartSig, Misplace, MaxSlope, AreaData = [], [], [], [], [], []
Time = []
kafka_data = kafka_data.splitlines() # turn carriage return string into a list
kafka_data = [e[128:] for e in kafka_data] # remove the header from the list
for line in kafka_data:
kafka_data_length = (len(line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
position.append(int(line[i + 29:i + 32], 16))
PulseHeight.append(int(line[i + 26:i + 29], 16))
StartSig.append(int(line[i + 23:i + 26], 16))
Misplace.append(int(line[i + 20:i + 23], 16))
MaxSlope.append(int(line[i + 17:i + 20], 16))
AreaData.append(int(line[i + 14:i + 17], 16))
Time.append(int(line[i + 3:i + 8], 16))
return position, PulseHeight, StartSig, Misplace, MaxSlope, AreaData, Time
def data_split_dict(kafka_data):
position, PulseHeight, StartSig, Misplace, MaxSlope, AreaData = [], [], [], [], [], []
kafka_data_dict = {'position': collections.Counter(), 'PulseHeight': collections.Counter(),
'StartSig': collections.Counter(),
'Misplace': collections.Counter(), 'MaxSlope': collections.Counter(),
'AreaData': collections.Counter()}
# if not kafka_data:
kafka_data = kafka_data.splitlines() # turn carriage return string into a list
kafka_data = [e[128:] for e in kafka_data] # remove the header from the list
for line in kafka_data:
kafka_data_length = (len(line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
position.append(int(line[i + 29:i + 32], 16))
PulseHeight.append(int(line[i + 26:i + 29], 16))
StartSig.append(int(line[i + 23:i + 26], 16))
Misplace.append(int(line[i + 20:i + 23], 16))
MaxSlope.append(int(line[i + 17:i + 20], 16))
AreaData.append(int(line[i + 14:i + 17], 16))
kafka_data_dict['position'] = collections.Counter(position)
kafka_data_dict['PulseHeight'] = collections.Counter(PulseHeight)
kafka_data_dict['StartSig'] = collections.Counter(StartSig)
kafka_data_dict['Misplace'] = collections.Counter(Misplace)
kafka_data_dict['MaxSlope'] = collections.Counter(MaxSlope)
kafka_data_dict['AreaData'] = collections.Counter(AreaData)
return kafka_data_dict
def data_split_dict_channel(kafka_data):
position = [[] for i in range(6)]
PulseHeight = [[] for i in range(6)]
StartSig = [[] for i in range(6)]
Misplace = [[] for i in range(6)]
MaxSlope = [[] for i in range(6)]
AreaData = [[] for i in range(6)]
kafka_dict_list = [{'position': collections.Counter(), 'PulseHeight': collections.Counter(),
'StartSig': collections.Counter(),
'Misplace': collections.Counter(), 'MaxSlope': collections.Counter(),
'AreaData': collections.Counter()} for i in range(6)]
kafka_data = kafka_data.splitlines() # turn carriage return string into a list
kafka_data = [e[128:] for e in kafka_data] # remove the header from the list
for line in kafka_data:
if line:
kafka_data_length = (len(line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
channel = int(((bin(int('1' + (line[i + 11:i + 13]), 16))[3:])[3:6]), 2)
# print(line)
# print(channel)
position[channel].append(int(line[i + 29:i + 32], 16))
PulseHeight[channel].append(int(line[i + 26:i + 29], 16))
StartSig[channel].append(int(line[i + 23:i + 26], 16))
Misplace[channel].append(int(line[i + 20:i + 23], 16))
MaxSlope[channel].append(int(line[i + 17:i + 20], 16))
AreaData[channel].append(int(line[i + 14:i + 17], 16))
for i in range(0, 6, 1):
kafka_dict_list[i]['position'] = collections.Counter(position[i])
kafka_dict_list[i]['PulseHeight'] = collections.Counter(PulseHeight[i])
kafka_dict_list[i]['StartSig'] = collections.Counter(StartSig[i])
kafka_dict_list[i]['Misplace'] = collections.Counter(Misplace[i])
kafka_dict_list[i]['MaxSlope'] = collections.Counter(MaxSlope[i])
kafka_dict_list[i]['AreaData'] = collections.Counter(AreaData[i])
return kafka_dict_list
def data_split_dict_channel_ip_combine(kafka_data, ch_list):
position = [[] for i in range(24)]
PulseHeight = [[] for i in range(24)]
StartSig = [[] for i in range(24)]
Misplace = [[] for i in range(24)]
MaxSlope = [[] for i in range(24)]
AreaData = [[] for i in range(24)]
kafka_dict_list = [{'position': collections.Counter(), 'PulseHeight': collections.Counter(),
'StartSig': collections.Counter(),
'Misplace': collections.Counter(), 'MaxSlope': collections.Counter(),
'AreaData': collections.Counter()} for i in range(24)]
l = 0
for line in kafka_data.values():
channel_offset = (int(
list(kafka_data.keys())[l][11:]) - 1) * 6 # create and offset for channel based on ipaddress
l = l + 1
for list_line in line:
kafka_data_length = (len(list_line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
if list_line[i:i + 32] != '00000000000000000000000000000000': # to remove empty packets
channel = int(((bin(int('1' + (list_line[i + 11:i + 13]), 16))[3:])[3:6]), 2) + channel_offset
if channel in ch_list:
position[channel].append(int(int(list_line[i + 29:i + 32], 16) / 16))
PulseHeight[channel].append(int(int(list_line[i + 26:i + 29], 16) / 16))
StartSig[channel].append(int(int(list_line[i + 23:i + 26], 16) / 16))
Misplace[channel].append(int(int(list_line[i + 20:i + 23], 16) / 16))
MaxSlope[channel].append(int(int(list_line[i + 17:i + 20], 16) / 16))
AreaData[channel].append(int(int(list_line[i + 14:i + 17], 16) / 16))
for i in ch_list: # range(0, 24, 1):
kafka_dict_list[i]['position'] = collections.Counter(position[i])
kafka_dict_list[i]['PulseHeight'] = collections.Counter(PulseHeight[i])
kafka_dict_list[i]['StartSig'] = collections.Counter(StartSig[i])
kafka_dict_list[i]['Misplace'] = collections.Counter(Misplace[i])
kafka_dict_list[i]['MaxSlope'] = collections.Counter(MaxSlope[i])
kafka_dict_list[i]['AreaData'] = collections.Counter(AreaData[i])
return kafka_dict_list
def data_split_dict_channel_ip_combine_time(kafka_data, ch_list):
time_bins = 1024
position = [[[] for i in range(time_bins)] for t in range(24)]
PulseHeight = [[[] for i in range(time_bins)] for t in range(24)]
StartSig = [[[] for i in range(time_bins)] for t in range(24)]
Misplace = [[[] for i in range(time_bins)] for t in range(24)]
MaxSlope = [[[] for i in range(time_bins)] for t in range(24)]
AreaData = [[[] for i in range(time_bins)] for t in range(24)]
kafka_time_dict_list = [[{'position': collections.Counter(), 'PulseHeight': collections.Counter(),
'StartSig': collections.Counter(),
'Misplace': collections.Counter(), 'MaxSlope': collections.Counter(),
'AreaData': collections.Counter()} for i in range(time_bins)] for t in range(24)]
l = 0
for line in kafka_data.values():
channel_offset = (int(
list(kafka_data.keys())[l][11:]) - 1) * 6 # create and offset for channel based on ipaddress
l = l + 1
for list_line in line:
kafka_data_length = (len(list_line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
channel = int(((bin(int('1' + (list_line[i + 11:i + 13]), 16))[3:])[3:6]), 2) + channel_offset
if channel in ch_list:
time_split = list_line[i + 2:i + 8]
time = (int(time_split[:3], 16)) # only using the top three bits for development
# time = (int(list_line[i + 2:i + 8], 16))
position[channel][time].append(int(list_line[i + 29:i + 32], 16))
PulseHeight[channel][time].append(int(list_line[i + 26:i + 29], 16))
StartSig[channel][time].append(int(list_line[i + 23:i + 26], 16))
Misplace[channel][time].append(int(list_line[i + 20:i + 23], 16))
MaxSlope[channel][time].append(int(list_line[i + 17:i + 20], 16))
AreaData[channel][time].append(int(list_line[i + 14:i + 17], 16))
for i in ch_list: # range(0, 24, 1):
for t in range(time_bins):
kafka_time_dict_list[i][t]['position'] = collections.Counter(position[i][t])
kafka_time_dict_list[i][t]['PulseHeight'] = collections.Counter(PulseHeight[i][t])
kafka_time_dict_list[i][t]['StartSig'] = collections.Counter(StartSig[i][t])
kafka_time_dict_list[i][t]['Misplace'] = collections.Counter(Misplace[i][t])
kafka_time_dict_list[i][t]['MaxSlope'] = collections.Counter(MaxSlope[i][t])
kafka_time_dict_list[i][t]['AreaData'] = collections.Counter(AreaData[i][t])
return kafka_time_dict_list
def data_split_dict_channel_ip_combine_PosTime(kafka_data, ch_list):
time_bins = int(1024)
time_factor = 16777215 / time_bins
position = np.zeros((24, 256, time_bins), dtype=int)
l = 0
for line in kafka_data.values():
channel_offset = (int(
list(kafka_data.keys())[l][11:]) - 1) * 6 # create and offset for channel based on ipaddress
l = l + 1
for list_line in line:
kafka_data_length = (len(list_line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
if list_line[i:i + 32] != '00000000000000000000000000000000':
channel = int(((bin(int('1' + (list_line[i + 11:i + 13]), 16))[3:])[3:6]), 2) + channel_offset
if channel in ch_list:
time_split = int(list_line[i + 2:i + 8], 16)
time = int(time_split / 1000) # set to 5000 for TS2
# if time_split != 0:
# time = int(time_factor / time_split)
# else:
# time = 0
pos = int(list_line[i + 29:i + 32], 16)
scaled_pos = int(pos / 16)
# pos = int(int(list_line[i + 29:i + 32], 16)/16)
position[channel, scaled_pos, time] += 1
return position
def data_split_dict_channel_ip_combine_Pos_PulseTime(kafka_data, ch_list):
time_bins = int(1024)
time_factor = 16777215 / time_bins
position = np.zeros((24, 256, time_bins), dtype=int)
pulseheight = np.zeros((24, 256, time_bins), dtype=int)
l = 0
for line in kafka_data.values():
channel_offset = (int(
list(kafka_data.keys())[l][11:]) - 1) * 6 # create and offset for channel based on ipaddress
l = l + 1
for list_line in line:
kafka_data_length = (len(list_line))
for i in range(0, kafka_data_length, 32): # return 32 character chunks
if list_line[i:i + 32] != '00000000000000000000000000000000':
channel = int(((bin(int('1' + (list_line[i + 11:i + 13]), 16))[3:])[3:6]), 2) + channel_offset
if channel in ch_list:
time_split = int(list_line[i + 2:i + 8], 16)
time = int(time_split / 1000) # set to 5000 for TS2
# if time_split != 0:
# time = int(time_factor / time_split)
# else:
# time = 0
pos = int(list_line[i + 29:i + 32], 16)
plh = (int(list_line[i + 26:i + 29], 16))
scaled_pos = int(pos / 16)
scaled_plh = int(plh / 16)
# pos = int(int(list_line[i + 29:i + 32], 16)/16)
position[channel, scaled_pos, time] += 1
pulseheight[channel, scaled_plh, time] += 1
return position, pulseheight
###################combines two dictionaries with matching keys#####################
def dict_add(dicta, dictb):
for key in dicta:
if key in dictb:
dicta[key] = dicta[key] + dictb[key]
else:
pass
return dicta
def kafka_live_data_proc(*args):
procdata = kafka_data_decoder_ip_dict(args)
# for i in procdata:
procdata = data_split_live_dict(procdata)
plot_func.dict_create(procdata, procdata.get("packet_info")) # [i]), i)
return procdata
def live_data_test(*args):
# print(args)
return args
def data_split_live_dict(kafka_data):
current_data = {'packet': "", 'packet_info': ""}
position, PulseHeight, StartSig, Misplace, MaxSlope, AreaData = [], [], [], [], [], []
kafka_data_dict = {'position': collections.Counter(), 'PulseHeight': collections.Counter(),
'StartSig': collections.Counter(),
'Misplace': collections.Counter(), 'MaxSlope': collections.Counter(),
'AreaData': collections.Counter(), 'packet_info': ""}
# for line in kafka_data.get("packet"):
line = kafka_data.get("packet")
kafka_data_length = (len(line))
for i in range(1, kafka_data_length, 32): # return 32 character chunks
position.append(int(line[i + 29:i + 32], 16))
PulseHeight.append(int(line[i + 26:i + 29], 16))
StartSig.append(int(line[i + 23:i + 26], 16))
Misplace.append(int(line[i + 20:i + 23], 16))
MaxSlope.append(int(line[i + 17:i + 20], 16))
AreaData.append(int(line[i + 14:i + 17], 16))
kafka_data_dict['position'] = collections.Counter(position)
kafka_data_dict['PulseHeight'] = collections.Counter(PulseHeight)
kafka_data_dict['StartSig'] = collections.Counter(StartSig)
kafka_data_dict['Misplace'] = collections.Counter(Misplace)
kafka_data_dict['MaxSlope'] = collections.Counter(MaxSlope)
kafka_data_dict['AreaData'] = collections.Counter(AreaData)
kafka_data_dict['packet_info'] = kafka_data.get("packet_info")
return kafka_data_dict