-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
456 lines (374 loc) · 19.6 KB
/
main.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# compilation with auto-py-to-exe (as admin)
# C:\Users\Giacomo\AppData\Local\Programs\Python\Python311\Scripts\pyinstaller.exe --noconfirm --onefile --console --paths "." --name "AR kwire placement test companion" --icon ".\media\icon.ico" "main.py"
import sys
import time
import signal
from datetime import datetime
import pyperclip
import textwrap
from logger import logger
from __init__ import *
import db
import data
ci = custom_input()
# args
# python main.py --testdb
# python main.py --upddb
def main():
checkargv()
def handler(signum, frame):
logger.info("\nexiting. DATA NOT SAVED!")
exit(0)
signal.signal(signal.SIGINT, handler)
logger.info(f"# POSITIONING PHASE COMPANION ({version})")
logger.info(f"# What a beautiful day to stick some anti-pigeon spikes into plastic!\n")
data.PHASE_toinsert = phase()
# save on db at the end
db.db_save_all()
# save on fusion360 to import data
with open("logs/fusion360_imports.log", "a") as f:
f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {version}\n")
for s in data.fusion360_imports:
f.write(s+"\n")
# save on companion to import data
with open("logs/companion_imports.log", "a") as f:
f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {version} phase id: {data.PHASE_toinsert.id}\n")
for s in data.companion_imports:
f.write(s+"\n")
logger.info("bye!")
def checkargv():
logger.info(f"flags: {sys.argv[1:]}")
if len(sys.argv) <= 1:
return
if "--testdb" in sys.argv:
data.PHASE_toinsert = data.PHASEdata(datatype="--testdb")
data.ECPs_toinsert.append(data.ECPdata(datatype="--testdb"))
data.PAs_toinsert.append(data.PAdata(datatype="--testdb"))
db.db_save_all()
print(f"now check database and delete \"--testdb\" datatype entries")
elif "--upddb" in sys.argv:
db.db_update()
else:
print(f"unknown flags")
quit()
def phase():
"PHASE performs 3 ECPs with multiple PA"
id = db.db_newid(3)
logger.info(f"# BEGIN POSITIONING PHASE FOR A CANDIDATE, ONE SINGLE PHASE ({id})")
logger.info(f"# {datetime.now().strftime('%Y/%m/%d - %H:%M:%S')}")
logger.info(f"DATA COLLECTION - PHASE - ({id})")
while True:
PHASE_data = data.PHASEdata(
id=id,
ECP_ids=[], # to update
PA_ids=[], # to update
comment="",
datatype="PHASE",
time_init=time.time(),
phase= ci.int(" |-- phase [INTEGER]: ", 0, 9),
phantom_id= ci.str(" |-- phantom id [STRING]: ", 2),
country_orig= ci.str(" |-- country origin [STRING]: "),
country= ci.str(" |-- country [STRING]: "),
city= ci.str(" |-- city [STRING]: "),
institute= ci.str(" |-- institute [STRING]: "),
name= ci.str(" |-- name [STRING]: "),
surname= ci.str(" |-- surname [STRING]: "),
gender= ci.acc(" |-- gender ", ["m", "f"]),
right_handed= ci.boo(" |-- right-handed [Y/N]: "),
age= ci.int(" |-- age [INTEGER]: ", 0, 99),
career= ci.acc(" |-- career: ", ["st", "sp", "su"]),
medicine_surge_year= ci.int(" |-- medsurg year [-1 : 7]: ", -1, 7),
specialization_year= ci.int(" |-- specialization year [-1 : 5]: ", -1, 5),
surgeon_year= ci.int(" |-- surgeon year [-1 : 100]: ", -1, 100),
exp_operation_count= ci.int(" |-- exp operation count [INTEGER]: ", 0, 1000),
glasses= ci.boo(" |-- glasses [Y/N]: "),
glasses_type= ci.acc(" |-- glasses type [None [] / Myopia (Nearsightedness) [M] / Hyperopia (Farsightedness) [H] / Astigmatism [AS] / Presbyopia [P] / Strabismus [S] / Amblyopia (Lazy Eye) [AM] / Cataract [C]: ", ["", "m", "m+as", "h", "as", "p", "s", "am", "c"]),
glasses_power= ci.flo(" |-- glasses power [-1 : 100] ", min=-1, max=100),
exp_vr= ci.int(" |-- exp Virtual Reality [0 : 5]: ", 0, 5),
exp_ar= ci.int(" |-- exp Augmented Reality [0 : 5]: ", 0, 5),
exp_3D_editor= ci.int(" |-- exp 3D editors [0 : 5]: ", 0, 5),
realism_xray= -1.0, # update post test
realism_ar= -1.0, # update post test
realism_phantom= -1.0, # update post test
sim_quality_xray= -1.0, # update post test
sim_quality_ar= -1.0, # update post test
sim_quality_phantom=-1.0, # update post test
comfort_xray= -1.0, # update post test
comfort_ar= -1.0, # update post test
PHASE_D= 0, # update after each ECP
PHASE_RPC= 0, # update after each ECP
PHASE_PAC= 0, # update after each ECP
PHASE_PACF= 0, # update after each ECP
PHASE_ECPC= 0, # update after each ECP
hit_count= 0 # update after each ECP
)
if ci.boo("\tTECHNICAL:\t is data entered correct? [Y/N]: "):
break
for ECP_number in range(1, len(data.PHASE_design)+1):
ECP_data = ECP(PHASE_data.phase, PHASE_data.id, ECP_number)
# add ECP_data to ECPs
data.ECPs_toinsert.append(ECP_data)
# update test_data
PHASE_data.PHASE_D += ECP_data.ECP_D
PHASE_data.PHASE_RPC += ECP_data.ECP_RPC
PHASE_data.PHASE_PAC += ECP_data.ECP_PAC
PHASE_data.PHASE_PACF += ECP_data.ECP_PACF
PHASE_data.PHASE_ECPC = ECP_number
PHASE_data.ECP_ids.append(ECP_data.id)
PHASE_data.PA_ids.extend(ECP_data.PA_ids)
PHASE_data.hit_count += ECP_data.hit_count
logger.info(f"DATA COLLECTION - PHASE - ({id})")
PHASE_data.realism_xray= ci.int(" |-- realism xray [-1 : 5]: ", -1, 5)
PHASE_data.sim_quality_xray= ci.int(" |-- simulation quality xray [-1 : 5]: ", -1, 5)
PHASE_data.comfort_xray= ci.int(" |-- comfort xray [-1 : 5]: ", -1, 5)
PHASE_data.realism_ar= ci.int(" |-- realism AR [-1 : 5]: ", -1, 5)
PHASE_data.sim_quality_ar= ci.int(" |-- simulation quality AR [-1 : 5]: ", -1, 5)
PHASE_data.comfort_ar= ci.int(" |-- comfort AR [-1 : 5]: ", -1, 5)
PHASE_data.realism_phantom= ci.int(" |-- realism phantom [-1 : 5]: ", -1, 5)
PHASE_data.sim_quality_phantom=ci.int(" |-- simulation quality phantom [-1 : 5]: ", -1, 5)
PHASE_data.comment = ci.str(f"\tTECHNICAL:\t comment on PHASE ({id}) [STRING]: ")
# print next phase input-ready stats for same candidate
print(textwrap.dedent(f"""
copy-paste next phase ({PHASE_data.phase+1}) input-ready stats for SAME candidate:
-----
{PHASE_data.phase+1}
{PHASE_data.phantom_id}
{PHASE_data.country_orig}
{PHASE_data.country}
{PHASE_data.city}
{PHASE_data.institute}
{PHASE_data.name}
{PHASE_data.surname}
{PHASE_data.gender}
{PHASE_data.right_handed}
{PHASE_data.age}
{PHASE_data.career}
{PHASE_data.medicine_surge_year}
{PHASE_data.specialization_year}
{PHASE_data.surgeon_year}
{PHASE_data.exp_operation_count}
{PHASE_data.glasses}
{PHASE_data.glasses_type}
{PHASE_data.glasses_power}
{PHASE_data.exp_vr}
{PHASE_data.exp_ar}
{PHASE_data.exp_3D_editor}
-----
"""))
return PHASE_data
def ECP(phase, PHASE_id, ECP_number) -> data.ECPdata:
"ECP performs single ECP with multiple PA"
id = db.db_newid(4)
logger.info(f"\n########################")
logger.info(f"ECP{ECP_number} START! ({id})")
ECP_data = data.ECPdata(
PHASE_id= PHASE_id,
PA_ids= [], # update for each PA
id= id,
datatype= "ECP",
time_init= time.time(),
ease_of_placement= -1, # input at the end
phase= phase,
ECP_number= ECP_number,
ECP_D= 0.0, # update for each PA
ECP_RPC= 0,
ECP_PAC= 0, # update for each PA
ECP_PACF= 0, # update for each PA
hit_count= 0 # update for each PA
)
PA_number = 0
while True:
PA_number += 1
PA_data = PA(phase, PHASE_id, ECP_number, ECP_data.id, PA_number)
# add PA_data to PAs
data.PAs_toinsert.append(PA_data)
# update ECP_data
ECP_data.ECP_D += PA_data.PA_D
ECP_data.ECP_RPC += PA_data.PA_RPC
ECP_data.ECP_PAC = PA_number
ECP_data.ECP_PACF += 0 if PA_data.success else 1
ECP_data.PA_ids.append(PA_data.id)
ECP_data.hit_count += PA_data.hit_count
if PA_data.success:
break
logger.info(f"DATA COLLECTION - ECP{ECP_number} - ({id})")
# after multiple PA now the k-wire is in the estimated correct position
ECP_data.ease_of_placement=ci.int(" |-- ease of placement [0 difficult - 5 easy]: ", min=0, max=5)
return ECP_data
def PA(phase: int, test_id: str, ECP_number: int, ECP_id: str, PA_number: int) -> data.PAdata:
"PA performs single PA"
id = db.db_newid(5)
logger.info(f"\n------------------------")
logger.info(f"PA{ECP_number}.{PA_number} START! ({id})")
ci.str("PERFORM:\t reset x-ray machine [ENTER when done]: ")
ci.str("PERFORM:\t give instruction to insert k-wire [ENTER when instruction given]: ")
chrono = chronometer()
time_init = chrono.start()
# insertion of k-wire
while True:
i = ci.acc("CANDIDATE:\t inserting k-wire... -> checks it and declares it Failed or Successful (Restart timer)", ["f", "s", "r"])
if i.lower() == 'f': # PA FAILED
logger.info("\t\t \\_candidate has FAILED positioning attempt!")
success = False
break
elif i.lower() == 's': # PA SUCCESS
logger.info("\t\t \\_candidate has performed SUCCESSFUL positioning attempt!")
success = True
break
elif i.lower() == 'r': # restart timer
logger.info("\t\t \\_restart timer!")
_ = chrono.reset()
time_init = chrono.start()
# get PA data
chrono.pause()
logger.info(f"DATA COLLECTION - PA{ECP_number}.{PA_number} - ({id})")
# cycle until all PA data input is correct
while True:
PA_data = data.PAdata(
PHASE_id=test_id,
ECP_id=ECP_id,
id=id,
comment="",
datatype="PA",
time_init=time_init,
phase=phase,
ECP_number=ECP_number,
PA_number=PA_number,
success=success,
entered_articulation=-1, # set after PA fusion analysis
PA_D=-1.0, # set after PA conclusion
# confidence evaluation
confidence_position= ci.flo(" |-- CANDIDATE: entrance point distance from entrance point target? [FLOAT]: ", min=0),
confidence_angle= ci.flo(" |-- CANDIDATE: confidence on angle in deg? [FLOAT]: ", min=0),
estimate_hit= ci.boo(" |-- CANDIDATE: estimate structures hit [Y/N]?: "),
PA_RPC =ci.int(" |-- RADIATION picture count [INT] : ", min=0),
values_from_unity= ci.acc(f" |-- P from unity", ["", "1", "2", "12"]),
P1A=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['A']} [FLOAT]: "),
P1B=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['B']} [FLOAT]: "),
P1C=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['C']} [FLOAT]: "),
P1D=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['D']} [FLOAT]: "),
P2A=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['A']} [FLOAT]: "),
P2B=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['B']} [FLOAT]: "),
P2C=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['C']} [FLOAT]: "),
P2D=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['D']} [FLOAT]: "),
# computed by fusion
P1A_F=-1.0,
P1B_F=-1.0,
P1C_F=-1.0,
P1D_F=-1.0,
P2A_F=-1.0,
P2B_F=-1.0,
P2C_F=-1.0,
P2D_F=-1.0,
P2eA_F=-1.0,
P2eB_F=-1.0,
P2eC_F=-1.0,
P2eD_F=-1.0,
# computed by unity
P1A_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['A']} unity [FLOAT]: "),
P1B_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['B']} unity [FLOAT]: "),
P1C_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['C']} unity [FLOAT]: "),
P1D_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['D']} unity [FLOAT]: "),
P2eA_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['A']} unity [FLOAT]: "),
P2eB_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['B']} unity [FLOAT]: "),
P2eC_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['C']} unity [FLOAT]: "),
P2eD_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['D']} unity [FLOAT]: "),
# computed by fusion
P1_mean_max=1.0,
P1_mean=-1.0,
P2_mean_max=2.0,
P2_mean=-1.0,
# ECP info
target=data.PHASE_design[ECP_number-1].ktarget,
markers=data.PHASE_design[ECP_number-1].markers,
anatomy=data.PHASE_design[ECP_number-1].anatomy,
# computed by fusion
fusion_computed=False,
hit_count =-1.0,
angle_PA_target =-1.0,
distance_P1_PA_target =-1.0,
distance_P1_PA_target_X =-1.0,
distance_P1_PA_target_Y =-1.0,
distance_P1_PA_target_Z =-1.0,
distance_P2_PA_target =-1.0,
distance_P2_PA_target_X =-1.0,
distance_P2_PA_target_Y =-1.0,
distance_P2_PA_target_Z =-1.0,
distance_P2e_PA_target =-1.0,
distance_P2e_PA_target_X=-1.0,
distance_P2e_PA_target_Y=-1.0,
distance_P2e_PA_target_Z=-1.0,
delta_id_PA_target=-1.0
)
if ci.boo("\tTECHNICAL:\t is data entered correct? [Y/N]: "):
break
# cycle until fusion accepts measurement errors
while True:
# send to fusion 360 for computation
PA_data_str = PA_data.dumps()
pyperclip.copy(PA_data_str)
logger.info(f"PERFORM:\t test following string on fusion 360 (already copied in clipboard) -> \n{PA_data_str}")
# receive from fusion 360
PA_data = ci.PAdata_computed(f"PERFORM:\t enter data from fusion 360: ", PA_data.id)
def measurePmarkers():
PA_data.values_from_unity= ci.acc(f" |-- P from unity", ["", "1", "2", "12"])
PA_data.P1A=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['A']} [FLOAT, EMPTY for old value]: ", default=PA_data.P1A)
PA_data.P1B=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['B']} [FLOAT, EMPTY for old value]: ", default=PA_data.P1B)
PA_data.P1C=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['C']} [FLOAT, EMPTY for old value]: ", default=PA_data.P1C)
PA_data.P1D=ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['D']} [FLOAT, EMPTY for old value]: ", default=PA_data.P1D)
PA_data.P2A=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['A']} [FLOAT, EMPTY for old value]: ", default=PA_data.P2A)
PA_data.P2B=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['B']} [FLOAT, EMPTY for old value]: ", default=PA_data.P2B)
PA_data.P2C=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['C']} [FLOAT, EMPTY for old value]: ", default=PA_data.P2C)
PA_data.P2D=ci.flo(f" |-- P2{data.PHASE_design[ECP_number-1].markers['D']} [FLOAT, EMPTY for old value]: ", default=PA_data.P2D)
PA_data.P1A_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['A']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P1A_U),
PA_data.P1B_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['B']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P1B_U),
PA_data.P1C_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['C']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P1C_U),
PA_data.P1D_U= ci.flo(f" |-- P1{data.PHASE_design[ECP_number-1].markers['D']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P1D_U),
PA_data.P2eA_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['A']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P2eA_U),
PA_data.P2eB_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['B']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P2eB_U),
PA_data.P2eC_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['C']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P2eC_U),
PA_data.P2eD_U=ci.flo(f" |-- P2e{data.PHASE_design[ECP_number-1].markers['D']} unity [FLOAT, EMPTY for old value]: ", default=PA_data.P2eD_U),
if PA_data.P1_mean > PA_data.P1_mean_max or PA_data.P2_mean > PA_data.P2_mean_max:
if PA_data.P1_mean > PA_data.P1_mean_max:
logger.info("\tTECHNICAL:\t ATTENTION: P1 measurement error is above max allowed! Please remeasure or import from unity")
if PA_data.P2_mean > PA_data.P2_mean_max:
logger.info("\tTECHNICAL:\t ATTENTION: P2 measurement error is above max allowed! Please remeasure or import from unity")
measurePmarkers()
continue
elif ci.boo("\tTECHNICAL:\t want to remeasure P1 and/or P2? [Y/N]: "):
measurePmarkers()
continue
# P1 and P2 measurement errors are below max allowed
break
# k-wire extraction of PA is not counted in PA_D
PA_data.PA_D = chrono.reset()
PA_data.PA_D = ci.flo(f" |-- set PA duration manually [FLOAT, EMPTY for automatic ({PA_data.PA_D}s)]: ", default=PA_data.PA_D)
PA_data.entered_articulation = ci.int(f" |-- ENTERED ARTICULATION [-1 / 0 (not entered) / 1 (entered)] : ", min=-1, max=1)
PA_data.comment = ci.str(f"\tTECHNICAL:\t comment on PA ({id}) [STRING]: ")
ci.str(f"\tTECHNICAL:\t remove kwire from phanotm [ENTER when done]: ")
logger.info(f"PA{ECP_number}.{PA_number} FINISHED!")
# log to file
data.fusion360_imports.append(PA_data_str)
data.companion_imports.append(textwrap.dedent(f"""
###### PA{ECP_number}.{PA_number} - ({id}) ######
{PA_data.P1A}
{PA_data.P1B}
{PA_data.P1C}
{PA_data.P1D}
{PA_data.P2A}
{PA_data.P2B}
{PA_data.P2C}
{PA_data.P2D}
------
{PA_data.P1A_U}
{PA_data.P1B_U}
{PA_data.P1C_U}
{PA_data.P1D_U}
{PA_data.P2eA_U}
{PA_data.P2eB_U}
{PA_data.P2eC_U}
{PA_data.P2eD_U}
"""))
return PA_data
if __name__ == "__main__":
main()