-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_analysis.py
executable file
·352 lines (320 loc) · 13 KB
/
signal_analysis.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
#!/usr/bin/env python3
import argparse
import json
import os
import csv
import pprint
import itertools
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
definitions = {}
datatypes = {'i_gear_t' : 32,
'i_safe_ramp_state_t': 32,
'i_safe_vehicle_motion_t': 32,
'i_safe_boolean_t': 32,
'i_hecu_op_mode_t': 32,
'i_esc_err_t': 32,
'i_light_blinking_request_t': 32,
'float': 32,
'i_vehicle_state_t': 32,
'i_power_mode_t': 32,
'uint16_t': 16,
'i_on_off_t': 32,
'i_ivi_avh_enable_t': 32,
'i_charger_state_t': 32,
'i_hecu_epb_act_req_t': 32,
'i_wiper_position_t': 32,
'int32_t': 32,
'i_hecu_prnd_t': 32,
'int8_t': 8,
'i_operating_mode_dt_t': 32,
'i_regen_control_request_t': 32,
'i_actual_avh_status_t': 32,
'Data type': 0,
'i_switch_state_t': 32,
'int16_t': 16,
'i_ivi_regen_intensity_t': 32,
'i_alarm_hazard_lights_t': 32,
'i_safe_sig_cnd_t': 32,
'bool': 8,
'i_hecu_avh_fail_t': 32,
'i_ccu_hlc_fsm_state_t': 32,
'i_speed_func_selection_t': 32,
'uint32_t':32,
'uint8_t':8,
'i_ivi_closures_lock_t':32
}
def file_path(string):
if os.path.isfile(string):
return string
else:
raise NotADirectoryError(string)
def type_summation(file):
with open(file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
variables = set()
for row in reader:
variables.add(row['variable'])
typeCount = {}
counter = 0
for variable in variables:
counter += 1
datatype = definitions[variable][0]
if datatype in typeCount:
typeCount[datatype] += 1
else:
typeCount[datatype] = 1
print("counter: " + str(counter))
print(typeCount)
print("typecount sum: " + str(sum(typeCount.values())))
def read_write_ratio(file):
pp = pprint.PrettyPrinter(indent=4)
with open(file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
ratios = {}
count = 0;
skipcount=0
for row in reader:
if row['task'] in ["gwy_receive_task", "gwy_transmit_task"]:
skipcount +=1
continue
count +=1
name = row['variable']
if name in ratios:
read, write = ratios[name]
if row['action'] == 'read':
ratios[name] = (read+1, write)
else:
ratios[name] = (read, write+1)
else:
if row['action'] == 'read':
ratios[name] = (1,0)
else:
ratios[name] = (0,1)
print("(read,write)")
for key, group in itertools.groupby(sorted(ratios.items(), key = lambda x : x[1]), lambda x: x[1]):
print(key, ": ", sum(1 for _ in group))
# pp.pprint(ratios)
print("sc ", skipcount)
print ("count: ", count)
def rate_table(file):
table = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
index = ['1', '10', '50', '100', '500']
pp = pprint.PrettyPrinter(indent=4)
with open(file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
count = 0
skipcount=0
producers = {}
consumers = []
for row in reader:
if row['task'] in ["gwy_receive_task", "gwy_transmit_task"]:
skipcount +=1
continue
count +=1
if row['action'] == 'write':
producers[row['variable']] = index.index(row['period (ms)'])
else:
consumers.append((row['variable'], index.index(row['period (ms)'])))
nf = 0
for variable, c_index in consumers:
p_index = producers.get(variable)
if p_index:
table[p_index][c_index] += 1
else:
nf += 1
print(table)
print("not found: ", nf)
def get_communication_type(producer, consumer):
if producer == consumer:
return 'local'
elif producer == 'safety_control_unit_primary' and consumer == 'central_gateway':
return 'direct'
elif producer == 'safety_control_unit_primary' and consumer == 'vehicle_control_unit':
return 'bridged'
elif producer == 'central_gateway':
return 'direct'
elif producer == 'vehicle_control_unit' and consumer == 'central_gateway':
return 'direct'
elif producer == 'vehicle_control_unit' and consumer == 'safety_control_unit_primary':
return 'bridged'
else:
print("oopsiedoodle")
return 'oopsiedoodle'
def communication_type(file):
# Get the #local, #direct, #bridged
producers = {} # {var: physical}
consumers = [] #[(var, physical)]
comm_type = {} # {var: {local: 5, direct:7, bridged: 42}}
with open(file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['task'] in ["gwy_receive_task", "gwy_transmit_task"]:
continue
if row['action'] == 'write':
producers[row['variable']] = row['physical']
else:
consumers.append((row['variable'], row['physical']))
print(len(producers.keys()))
for variable, physical_consumer in consumers:
if variable not in producers:
continue
communication = get_communication_type(producers[variable], physical_consumer)
if variable in comm_type:
comm_type[variable][communication] += 1
else:
comm_type[variable] = {'local':0, 'direct': 0, 'bridged': 0, 'oopsiedoodle': 0}
comm_type[variable][communication] += 1
local = 0
direct = 0
bridged = 0
local_direct = 0
local_bridged = 0
direct_bridged = 0
all = 0
error = 0
count = 0
for name, counts in comm_type.items():
count += 1
if counts['local'] != 0 and counts['direct'] == 0 and counts['bridged'] == 0:
local += 1
elif counts['direct'] != 0 and counts['local'] == 0 and counts['bridged'] == 0:
direct += 1
elif counts['bridged'] != 0 and counts['direct'] == 0 and counts['local'] == 0:
bridged += 1
elif counts['local'] != 0 and counts['direct'] != 0 and counts['bridged'] == 0:
local_direct += 1
elif counts['local'] != 0 and counts['direct'] == 0 and counts['bridged'] != 0:
local_bridged += 1
elif counts['local'] == 0 and counts['direct'] != 0 and counts['bridged'] != 0:
direct_bridged += 1
elif counts['local'] != 0 and counts['direct'] != 0 and counts['bridged'] != 0:
all += 1
else:
error +=1
print("error for ", name)
if counts['oopsiedoodle'] != 0:
error +=1
print("Thingamabob forgot to Majig the Thinga resulting in ", counts['oopsiedoodle'], ' oopsiedoodles for ', name)
print("local: ", local)
print("direct: ", direct)
print("bridged: ", bridged)
print("local_direct: ", local_direct)
print("local_bridged: ", local_bridged)
print("direct_bridged", direct_bridged)
print("all: ", all)
print("error: ", error)
def set_size(width_pt, fraction=1, subplots=(1, 1)):
"""Set figure dimensions to sit nicely in our document.
Parameters
----------
width_pt: float
Document width in points
fraction: float, optional
Fraction of the width which you wish the figure to occupy
subplots: array-like, optional
The number of rows and columns of subplots.
Returns
-------
fig_dim: tuple
Dimensions of figure in inches
"""
# Width of figure (in pts)
fig_width_pt = width_pt * fraction
# Convert from pt to inches
inches_per_pt = 1 / 72.27
# Golden ratio to set aesthetic figure height
golden_ratio = (5**.5 - 1) / 2
# Figure width in inches
fig_width_in = fig_width_pt * inches_per_pt
# Figure height in inches
fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])
return (fig_width_in, fig_height_in)
def produced_consumed_runnable(file):
runnable = {} #{runnable_name : {read: 4, write: 2}}
with open(file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name = '{}_{}'.format(row['task'], row['physical'])
if name in runnable:
runnable[name][row['action']] += 1
else:
runnable[name] = {'read': 0, 'write': 0}
runnable[name][row['action']] += 1
runnable_list = list(runnable.items())
fig, ax = plt.subplots(1,1,figsize=set_size(0.95*424.58624))
plt.grid(visible=True, alpha=0.7)
plt.scatter([r[1]['write'] for r in runnable_list if 'gwy' in r[0]],
[r[1]['read'] for r in runnable_list if 'gwy' in r[0]],
c='tab:orange', marker='.', label='CAN runnable')
plt.scatter([r[1]['write'] for r in runnable_list if 'gwy' not in r[0]],
[r[1]['read'] for r in runnable_list if 'gwy' not in r[0]],
c='tab:blue', marker='.', label='Other runnable')
plt.xlabel('Nr of produced data dictionaries')
plt.ylabel('Nr of consumed data dictionaries')
plt.legend()
plt.title('Data dictionaries consumed/produced per runnable')
plt.rcParams.update({"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False # don't setup fonts from rc parameters
})
fig.tight_layout()
fig.savefig('paper/images/Produce_Consume_graph.pgf', format='pgf')
plt.show()
def produced_consumed_can(file):
runnable = {} #{runnable_name: {'read': 4, 'write':5}}
with open(file, 'r') as jsonfile:
jsonarray = json.load(jsonfile)
for r in jsonarray:
name = '{}_{}'.format(r['physical'], r['name'])
if name in runnable:
print('oops')
runnable[name] = {'read': len(r['canIn']), 'write': len(r['canOut'])}
runnable_list = list(runnable.items())
fig, ax = plt.subplots(1,1,figsize=set_size(0.95*424.58624))
plt.grid(visible=True, alpha=0.7)
plt.scatter([r[1]['write'] for r in runnable_list if not any(node in r[0] for node in ['cgw', 'SCU', 'VCU'])],
[r[1]['read'] for r in runnable_list if not any(node in r[0] for node in ['cgw', 'SCU', 'VCU'])],
c='tab:purple', marker='.', label='Parametrizable end nodes')
plt.scatter([r[1]['write'] for r in runnable_list if 'gwy' in r[0]],
[r[1]['read'] for r in runnable_list if 'gwy' in r[0]],
c='tab:orange', marker='.', label='CAN runnable')
plt.scatter([r[1]['write'] for r in runnable_list if 'gwy' not in r[0] and any(node in r[0] for node in ['cgw', 'SCU', 'VCU'])],
[r[1]['read'] for r in runnable_list if 'gwy' not in r[0] and any(node in r[0] for node in ['cgw', 'SCU', 'VCU'])],
c='tab:blue', marker='.', label='Other Runnable')
plt.xlabel('Nr of produced CAN messages')
plt.ylabel('Nr of consumed CAN messages')
plt.legend()
plt.title('CAN messages consumed/produced')
plt.rcParams.update({"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False # don't setup fonts from rc parameters
})
fig.tight_layout()
fig.savefig('paper/images/Produce_Consume_CAN_graph.pgf', format='pgf')
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('path', type=file_path, help='path to the csv network file, can be absolute or relative')
parser.add_argument('centraldef', type=file_path, help='path to central.definitions file, can be relative or absolute')
parser.add_argument('canjson', type=file_path)
args = parser.parse_args()
file = args.path
# Create a lookup dictionary of central definitions of the Datadictionaries
with open(args.centraldef, 'r', newline='') as central_definitions:
reader = csv.DictReader(central_definitions)
for row in reader:
if row['With reliability'] == 'Yes':
# With reliability there are two datadicts generated, so add both
name = '{}_reliable'.format(row['Name'])
definitions[row['Name']] = (row['Data type'], True)
definitions[name] = (row['Data type'], True)
else:
definitions[row['Name']] = (row['Data type'], False)
type_summation(file)
read_write_ratio(file)
rate_table(file)
communication_type(file)
produced_consumed_runnable(file)
# produced_consumed_can(args.canjson)