-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.py
364 lines (319 loc) · 11.2 KB
/
data.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
from dataclasses import dataclass, fields
import json
from datetime import datetime
import faker
import random
from logger import logger
from __init__ import *
fake = faker.Faker()
class data_elaboration:
def __init__(self, **kwargs):
if len(kwargs) != 1 and len(kwargs) != len(fields(self)):
raise ValueError(f"Invalid number of arguments ({len(kwargs)} out of {len(fields(self))}). Please provide values for one or all attributes.")
for field in fields(self):
setattr(self, field.name, kwargs.get(field.name, self.generate_random_value(field)))
def generate_random_value(self, field):
if field.type == int:
return random.randint(1, 100)
elif field.type == float:
return round(random.uniform(0.0, 100.0), 5)
elif field.type == bool:
return random.choice([True, False])
elif field.type == str:
return ''.join(random.choices('abcdefghijklmnopqrstuvwxyz ', k=10))
elif field.type == list[str]:
return [''.join(random.choices('abcdefghijklmnopqrstuvwxyz ', k=5)) for _ in range(3)]
elif field.type == dict[str, str]:
return random.choice(PHASE_design).markers
elif field.type == dict[str, float]:
return random.choice(PHASE_design).anatomy
else:
logger.critical(f"generate_random_value: unsupported value type: {field.type}")
def dumps(self) -> str:
"dump data into json string"
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False)
def db_create_table(self, table) -> str:
"dump data into db table creation string"
s = []
for k, v in vars(self).items():
if k == "id":
s.append(f"`{k}` TEXT NOT NULL PRIMARY KEY")
elif k == "time_init":
s.append(f"`{k}` TIMESTAMP")
elif type(v) == int:
s.append(f"`{k}` INTEGER")
elif type(v) == float:
s.append(f"`{k}` REAL")
elif type(v) == bool:
s.append(f"`{k}` BOOL")
elif type(v) == str:
s.append(f"`{k}` TEXT")
elif type(v) == list:
s.append(f"`{k}` TEXT")
elif type(v) == dict:
if k == "anatomy":
for ECP_design in PHASE_design:
for kk, vv in ECP_design.anatomy.items():
s.append(f"`{kk.replace(" ", "_")}` REAL")
elif k == "markers":
for kk, vv in v.items():
s.append(f"`{kk}` TEXT")
else:
logger.critical(f"db_create_table: unsupported dict key: {type(v)} {k} {v}")
else:
logger.critical(f"db_create_table: unsupported value type: {type(v)} {k} {v}")
s = list(dict.fromkeys(s)) # remove duplicates
return f"CREATE TABLE IF NOT EXISTS {table} (" + ", ".join(s) + ")"
def db_insert_table(self, table) -> tuple[str, tuple]:
"dump data into db table insertion string"
dbkeys: list[str] = []
dbvals: list[any] = []
for k, v in vars(self).items():
if k == "id":
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif k == "time_init":
dbkeys.append(f"`{k}`")
dbvals.append(f"{datetime.fromtimestamp(round(v, 0))}")
elif type(v) == int:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == float:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == bool:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == str:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == list:
dbkeys.append(f"`{k}`")
dbvals.append(";".join(map(str, v)))
elif type(v) == dict:
dbkeys.extend([f"`{kk.replace(" ", "_")}`" for kk in v.keys()])
dbvals.extend(v.values())
else:
logger.critical(f"db_create_table: unsupported value type: {type(v)} {k} {v}")
return f"INSERT INTO {table} ({', '.join(dbkeys)}) VALUES ({','.join(['?'] * len(dbkeys))})", tuple(dbvals)
def db_update_table(self, table, id) -> str:
dbkeys: list[str] = []
dbvals: list[any] = []
for k, v in vars(self).items():
if k == "id":
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif k == "time_init":
dbkeys.append(f"`{k}`")
dbvals.append(f"{datetime.fromtimestamp(round(v, 0))}")
elif type(v) == int:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == float:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == bool:
dbkeys.append(f"`{k}`")
dbvals.append(1 if v else 0)
elif type(v) == str:
dbkeys.append(f"`{k}`")
dbvals.append(v)
elif type(v) == list:
dbkeys.append(f"`{k}`")
dbvals.append(";".join(map(str, v)))
elif type(v) == dict:
dbkeys.extend([f"`{kk.replace(" ", "_")}`" for kk in v.keys()])
dbvals.extend(v.values())
else:
logger.critical(f"db_create_table: unsupported value type: {type(v)} {k} {v}")
return f"UPDATE {table} SET {', '.join([f"{key} = '{value}'" for key, value in zip(dbkeys, dbvals)])} WHERE id = '{id}';"
@dataclass
class ECP_design:
ktarget: str
markers: dict[str, str]
anatomy: dict[str, float]
# relative to phantom v2.3.5
anatomy_eval: dict[str, str] = {
"basilic vein": -1.0,
"brachial artery": -1.0,
"brachial vein": -1.0,
"cephalic vein": -1.0,
"inferior ulnar collateral artery": -1.0,
"median antebrachial vein": -1.0,
"median cubital vein": -1.0,
"median cubital vein 1": -1.0,
"middle collateral artery": -1.0,
"radial artery": -1.0,
"radial collateral artery": -1.0,
"radial veins": -1.0,
"random artery 1": -1.0,
"random artery 2": -1.0,
"ulnar artery": -1.0,
"ulnar veins": -1.0,
"anterior branch of medial antebrachial cutaneous nerve": -1.0,
"anterior interosseous nerve of forearm": -1.0,
"deep branch of radial nerve": -1.0,
"lateral antebrachial cutaneous nerve": -1.0,
"median nerve": -1.0,
"muscular branches of radial nerve": -1.0,
"musculocutaneous nerve": -1.0,
"posterior antebrachial cutaneous nerve": -1.0,
"posterior branch of medial antebrachial cutaneous nerve": -1.0,
"radial nerve": -1.0,
"superficial branch of radial nerve": -1.0,
"ulnar nerve": -1.0,
}
PHASE_design: list[ECP_design] = [
ECP_design("ECP:1",
{"A": "M:2", "B": "M:3", "C": "M:7", "D": "M:8"},
anatomy_eval),
ECP_design("ECP:2",
{"A": "M:3", "B": "M:4", "C": "M:8", "D": "M:9"},
anatomy_eval),
ECP_design("ECP:3",
{"A": "M:2", "B": "M:3", "C": "M:7", "D": "M:8"},
anatomy_eval)
]
@dataclass
class PHASEdata(data_elaboration):
"test data"
def __init__(self, **kwargs):
super().__init__(**kwargs)
datatype: str
id: str
ECP_ids: list[str]
PA_ids: list[str]
comment: str; "used to mark data on database for later technical analysis"
time_init: float
phase: int
phantom_id: str
country_orig: str
country: str
city: str
institute: str
name: str
surname: str
gender: str
right_handed: bool
age: int
career: str
medicine_surge_year: int
specialization_year: int
surgeon_year: int
exp_operation_count: int
glasses: bool
glasses_type: str
glasses_power: float
exp_vr: int
exp_ar: int
exp_3D_editor: int
realism_xray: int
realism_ar: int
realism_phantom: int
sim_quality_xray: int
sim_quality_ar: int
sim_quality_phantom: int
comfort_xray: int
comfort_ar: int
PHASE_D: float
PHASE_RPC: int; "PHASE radiation picture count"
PHASE_PAC: int
PHASE_PACF: int
PHASE_ECPC: int
hit_count: int
@dataclass
class ECPdata(data_elaboration):
"estimated correct position data"
def __init__(self, **kwargs):
super().__init__(**kwargs)
datatype: str
PHASE_id: str
id: str
PA_ids: list[str]
time_init: float
ease_of_placement: int
phase: int
ECP_number: int
ECP_D: float; "ECP duration"
ECP_RPC: int; "ECP radiation picture count"
ECP_PAC: float
ECP_PACF: float
hit_count: int
@dataclass
class PAdata(data_elaboration):
"positioning attempt data"
def __init__(self, **kwargs):
super().__init__(**kwargs)
datatype: str
PHASE_id: str
ECP_id: str
id: str
comment: str; "used to mark data on database for later technical analysis"
time_init: float
phase: int
ECP_number: int
PA_number: int
success: bool
entered_articulation: int; "kwire entered articulation cavity (-1: not analyzed; 0: not entered; 1: entered)"
PA_D: float
PA_RPC: int; "PA radiation picture count"
values_from_unity: str
P1A: float; "P1 - marker: measured with caliper"
P1B: float
P1C: float
P1D: float
P2A: float
P2B: float
P2C: float
P2D: float
P1A_F: float; "P1 - marker: measured in fusion"
P1B_F: float
P1C_F: float
P1D_F: float
P2A_F: float
P2B_F: float
P2C_F: float
P2D_F: float
P2eA_F: float; "P2e - marker: measured in fusion"
P2eB_F: float
P2eC_F: float
P2eD_F: float
P1A_U: float; "P1A - marker: measured in unity"
P1B_U: float
P1C_U: float
P1D_U: float
P2eA_U: float
P2eB_U: float
P2eC_U: float
P2eD_U: float
P1_mean_max: float; "P1 triangulation distances divergence"
P1_mean: float
P2_mean_max: float; "P2 triangulation distances divergence"
P2_mean: float
confidence_position: float
confidence_angle: float
estimate_hit: bool
target: str; "k-wire target component name on fusion 360"
markers: dict[str, str]
fusion_computed: bool; "analyzed by fusion 360"
anatomy: dict[str, float]
hit_count: int; "how many anatomy structure hits the PA did"
angle_PA_target: float
distance_P1_PA_target: float; "distance P1"
distance_P1_PA_target_X: float
distance_P1_PA_target_Y: float
distance_P1_PA_target_Z: float
distance_P2_PA_target: float; "distance P2"
distance_P2_PA_target_X: float
distance_P2_PA_target_Y: float
distance_P2_PA_target_Z: float
distance_P2e_PA_target: float; "distance P2 estimated"
distance_P2e_PA_target_X: float
distance_P2e_PA_target_Y: float
distance_P2e_PA_target_Z: float
delta_id_PA_target: float; "delta insertion depth"
PHASE_toinsert: PHASEdata = None
ECPs_toinsert: list[ECPdata] = []
PAs_toinsert: list[PAdata] = []
fusion360_imports: list[str] = []
companion_imports: list[str] = []