Repository navigation
Expand file tree
/
Copy pathrun_quality_control.py
More file actions
355 lines (314 loc) · 11.8 KB
/
Copy pathrun_quality_control.py
File metadata and controls
355 lines (314 loc) · 11.8 KB
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
# Executes the QualityControl class which runs a suite of tests on the current patient,
# case and treatment plan (including ROIs, beam sets, beams, objectives, etc).
#
# Authors:
# Christoffer Lervåg, Marit Funderud, Robert Hoggard
# Helse Møre og Romsdal HF
#
# System configuration:
from connect import *
import datetime
import sys
from tkinter import *
from tkinter import messagebox
import os
import json
import base64
import hashlib
import hmac
import tkinter.font as tkfont
# Log start time:
time_start = datetime.datetime.now()
# Add necessary folders to the system path:
sys.path.append("C:\\temp\\raystation-scripts\\def_regions")
sys.path.append("C:\\temp\\raystation-scripts\\functions")
sys.path.append("C:\\temp\\raystation-scripts\\gui_classes")
sys.path.append("C:\\temp\\raystation-scripts\\rt_classes")
sys.path.append("C:\\temp\\raystation-scripts\\settings")
sys.path.append("C:\\temp\\raystation-scripts\\ts_classes")
sys.path.append("C:\\temp\\raystation-scripts\\various_classes")
sys.path.append(r'C:\temp\raystation-scripts')
# Local script imports:
import quality_control as QC
# "Global" variables:
try:
patient = get_current("Patient")
except SystemError:
raise IOError("No patient loaded.")
try:
case = get_current("Case")
except SystemError:
raise IOError("No case loaded.")
try:
plan = get_current("Plan")
except SystemError:
raise IOError("No plan loaded.")
# Apply log file workflow?
run_log = 1
# Log file configuration:
output_dir = r"I:\HMR - Begrenset\Klinikk for kreftbehandling og rehabilitering - Stråleterapi-kontor - Doseplanlegging\Skript\Misc Scripts\QA logs"
file_prefix = "qc_results_"
file_suffix = ".txt"
cache_max_age_days = 30
# Encryption configuration:
encryption_version = 1
pbkdf2_iterations = 200000
# Get patient modification time:
def get_patient_modification_time_string(patient):
try:
if hasattr(patient, 'ModificationInfo') and patient.ModificationInfo is not None:
modification_time = patient.ModificationInfo.ModificationTime
if modification_time is None:
return None
if modification_time.Year < 1900:
return None
modification_time_py = datetime.datetime(
modification_time.Year,
modification_time.Month,
modification_time.Day,
modification_time.Hour,
modification_time.Minute,
modification_time.Second
)
return modification_time_py.strftime("%Y-%m-%d %H:%M:%S")
except Exception:
return None
return None
# Create key material:
def get_key_material(unique_plan_id, modification_time_str):
return (str(unique_plan_id) + "|" + str(modification_time_str)).encode("utf-8")
# Derive encryption key:
def derive_key(unique_plan_id, modification_time_str, salt):
return hashlib.pbkdf2_hmac(
"sha256",
get_key_material(unique_plan_id, modification_time_str),
salt,
pbkdf2_iterations,
dklen=32
)
# Create encryption stream:
def make_keystream(key, nonce, length):
stream = bytearray()
counter = 0
while len(stream) < length:
counter_bytes = counter.to_bytes(8, byteorder="big")
stream.extend(hmac.new(key, nonce + counter_bytes, hashlib.sha256).digest())
counter += 1
return bytes(stream[:length])
# Apply encryption stream:
def xor_bytes(data, key_stream):
return bytes([a ^ b for a, b in zip(data, key_stream)])
# Encrypt text:
def encrypt_text(text, unique_plan_id, modification_time_str):
salt = os.urandom(16)
nonce = os.urandom(16)
key = derive_key(unique_plan_id, modification_time_str, salt)
plain_bytes = text.encode("utf-8")
key_stream = make_keystream(key, nonce, len(plain_bytes))
cipher_bytes = xor_bytes(plain_bytes, key_stream)
tag = hmac.new(key, salt + nonce + cipher_bytes, hashlib.sha256).digest()
payload = {
"version": encryption_version,
"salt": base64.b64encode(salt).decode("ascii"),
"nonce": base64.b64encode(nonce).decode("ascii"),
"ciphertext": base64.b64encode(cipher_bytes).decode("ascii"),
"hmac": base64.b64encode(tag).decode("ascii")
}
return json.dumps(payload, sort_keys=True, indent=2)
# Decrypt text:
def decrypt_text(encrypted_text, unique_plan_id, modification_time_str):
payload = json.loads(encrypted_text)
if payload.get("version") != encryption_version:
return None
salt = base64.b64decode(payload["salt"])
nonce = base64.b64decode(payload["nonce"])
cipher_bytes = base64.b64decode(payload["ciphertext"])
stored_tag = base64.b64decode(payload["hmac"])
key = derive_key(unique_plan_id, modification_time_str, salt)
calculated_tag = hmac.new(key, salt + nonce + cipher_bytes, hashlib.sha256).digest()
if not hmac.compare_digest(stored_tag, calculated_tag):
return None
key_stream = make_keystream(key, nonce, len(cipher_bytes))
plain_bytes = xor_bytes(cipher_bytes, key_stream)
return plain_bytes.decode("utf-8")
# Clean old log files:
def clean_old_files(output_dir):
threshold = datetime.datetime.now() - datetime.timedelta(days=cache_max_age_days)
try:
if not os.path.exists(output_dir):
return
for filename in os.listdir(output_dir):
if filename.startswith(file_prefix) and filename.endswith(file_suffix):
full_path = os.path.join(output_dir, filename)
file_mtime = datetime.datetime.fromtimestamp(os.path.getmtime(full_path))
if file_mtime < threshold:
os.remove(full_path)
except Exception as e:
print("Could not clean old QC files: " + str(e))
# Read result from file:
def read_saved_result(file_path, unique_plan_id, modification_time_str):
if modification_time_str is None:
return None
if not os.path.exists(file_path):
return None
try:
with open(file_path, "r", encoding="utf-8") as file:
encrypted_text = file.read()
return decrypt_text(encrypted_text, unique_plan_id, modification_time_str)
except Exception as e:
print("Could not read QC file: " + str(e))
return None
# Write result to file:
def write_saved_result(file_path, unique_plan_id, modification_time_str, text):
if modification_time_str is None:
return
try:
if not os.path.exists(output_dir):
os.makedirs(output_dir)
encrypted_text = encrypt_text(text, unique_plan_id, modification_time_str)
with open(file_path, "w", encoding="utf-8", newline='\n') as file:
file.write(encrypted_text)
except Exception as e:
messagebox.showerror("Feil", "Kunne ikkje skrive til fil:\n" + file_path + "\n\nFeilmelding: " + str(e))
# Insert text with preserved indentation for wrapped lines:
def insert_indented_text(text_box, text):
# Get the font used by the text box:
font_obj = tkfont.Font(font=text_box.cget("font"))
# Measure the width of one space character:
space_width = font_obj.measure(" ")
# Create a dictionary to track indentation tags already created:
used_tags = {}
# Loop through each line in the result text:
for line in text.splitlines():
# Replace tab characters with spaces:
line = line.expandtabs(2)
# Preserve empty lines:
if line.strip() == "":
text_box.insert(END, "\n")
continue
# Count the number of leading spaces:
indent_chars = len(line) - len(line.lstrip(" "))
# Remove leading spaces from the text content:
content = line.lstrip(" ")
# Create a tag name for this indentation level:
tag_name = "indent_" + str(indent_chars)
# Create a new indentation tag if it does not already exist:
if tag_name not in used_tags:
margin = indent_chars * space_width
text_box.tag_configure(tag_name, lmargin1=margin, lmargin2=margin)
used_tags[tag_name] = True
# Insert the line using the correct indentation tag:
text_box.insert(END, content + "\n", tag_name)
# Display QC results in a custom popup:
def show_qc_result_window(title, text):
# Create the popup window:
root = Tk()
# Set the popup title:
root.title(title)
# Allow the popup to be resized:
root.resizable(True, True)
# Set the taskbar icon:
try:
root.iconbitmap(r'C:\temp\raystation-scripts\media\icons\qc.ico')
except Exception:
pass
# Allow the popup contents to resize with the window:
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
# Create the main frame:
frame = Frame(root, padx=18, pady=14)
frame.grid(row=0, column=0, sticky="nsew")
# Allow the text box to resize with the frame:
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
# Create the text box:
text_box = Text(frame, width=95, height=22, wrap=WORD, font=("Segoe UI", 10))
text_box.grid(row=0, column=0, sticky="nsew")
# Add a vertical scrollbar:
scrollbar = Scrollbar(frame, orient="vertical", command=text_box.yview)
scrollbar.grid(row=0, column=1, sticky="ns")
text_box.config(yscrollcommand=scrollbar.set)
# Insert the result text with indentation-aware wrapping:
insert_indented_text(text_box, text)
# Make the text box read-only:
text_box.config(state="disabled")
# Create the OK button:
ok_button = Button(frame, text="OK", width=10, command=root.destroy)
ok_button.grid(row=1, column=0, columnspan=2, sticky="e", pady=(12, 0))
# Close the popup with escape
root.bind("<Escape>", lambda event: root.destroy())
# Centre the popup on screen:
root.update_idletasks()
root.eval('tk::PlaceWindow . center')
# Bring the popup to the front:
root.lift()
root.attributes("-topmost", True)
root.after(250, lambda: root.attributes("-topmost", False))
# Run the popup:
root.mainloop()
# Run Quality Control
Log_file_used = 0
if run_log == 1:
# Set up the log file:
try:
unique_plan_id = plan.GetPlanUuid()
except Exception:
unique_plan_id = str(plan.Name)
file_path = os.path.join(output_dir, file_prefix + str(unique_plan_id) + file_suffix)
modification_time_str = get_patient_modification_time_string(patient)
clean_old_files(output_dir)
# Read saved result from file:
text = read_saved_result(file_path, unique_plan_id, modification_time_str)
if text is None:
# Set up and execute the quality control class:
qc = QC.QualityControl(patient, case, plan)
# Create title and body strings:
title = "Plan Quality Control"
summary = qc.result.failure_summary()
if qc.result.nr_failures() == 0:
# Zero failures:
text = "Ingen problemer ble funnet! :)\n\n"
else:
# One or more failures:
text = str(qc.result.nr_failures()) + " mulige problemer ble funnet:\n\n" + summary
# Write result to file:
write_saved_result(file_path, unique_plan_id, modification_time_str, text)
else:
# Create title string:
title = "Plan Quality Control"
Log_file_used = 1
else:
# Set up and execute the quality control class:
qc = QC.QualityControl(patient, case, plan)
# Create title and body strings:
title = "Plan Quality Control"
summary = qc.result.failure_summary()
if qc.result.nr_failures() == 0:
# Zero failures:
text = "Ingen problemer ble funnet! :)\n\n"
else:
# One or more failures:
text = str(qc.result.nr_failures()) + " mulige problemer ble funnet:\n\n" + summary
# Log finish time and format a time string:
time_end = datetime.datetime.now()
elapsed_time = time_end - time_start
if elapsed_time.seconds > 3600:
hours = elapsed_time.seconds // 3600 % 3600
minutes = (elapsed_time.seconds - hours * 3600) // 60 % 60
seconds = elapsed_time.seconds - hours * 3600 - minutes * 60
else:
hours = 0
minutes = elapsed_time.seconds // 60 % 60
seconds = elapsed_time.seconds - minutes * 60
# Append time string to result:
if hours > 0:
text += "\n\n" + "Tidsbruk: " +str(hours) + " time(r) " + str(minutes) + " min " + str(seconds) + " sek"
else:
if minutes > 0:
text += "\n\n" + "Tidsbruk: " + str(minutes) + " min " + str(seconds) + " sek"
else:
text += "\n\n" + "Tidsbruk: " + str(seconds) + " sek"
if Log_file_used == 1:
text += "\n\n" "QC Results read from log file"
# Display the QC results:
show_qc_result_window(title, text)