-
Notifications
You must be signed in to change notification settings - Fork 0
/
hnDoseProtons.py
1391 lines (1171 loc) · 84.5 KB
/
hnDoseProtons.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This script is used for proton planning (using beam settings and objective function weights from an existing proton plan)
It expects
- the clinical plan to already be present in RStation (along with objectives)
- assets/objective-template-proton-kno.xml
- assets/eval-template-proton-robust.json
- assets/isodose.xml
Run the script from "__main__"
"""
# Import Raystation libraries
import connect
# Import private libraries
import hnDoseEvalHelpers as helpers
import hnDoseConfig as config
# Import public libraries
import re
import sys
import pdb
import time
import json
import shutil
import logging
import datetime
import traceback
import numpy as np
from pathlib import Path
DEBUG_PDB = True
def print(*args, **kwargs):
logging.info(" ".join(map(str, args)), **kwargs)
########################################################
# HELPERS #
########################################################
def checkIfRoiIgnore(planName, objectiveFType, roiName):
# Step 1 - Init
roisFullIgnore = []
roisPartialIgnore = []
roiIgnoreBool = False
planEUDSuffix = config.SUFFIX_PLAN_DFO.format(config.PREFIX_CLINICAL_CONTOURS) # R2
planEUDAutoSuffix = config.SUFFIX_PLAN_DFO.format(config.PREFIX_AUTOMATED_CONTOURS)
planEUD2Suffix = config.SUFFIX_PLAN_DFO2.format(config.PREFIX_CLINICAL_CONTOURS) # R3
planEUD2AutoSuffix = config.SUFFIX_PLAN_DFO2.format(config.PREFIX_AUTOMATED_CONTOURS)
planFinalSuffix = config.SUFFIX_PLAN_FINAL.format(config.PREFIX_CLINICAL_CONTOURS) # R5
planFinalAutoSuffix = config.SUFFIX_PLAN_FINAL.format(config.PREFIX_AUTOMATED_CONTOURS)
# Step 2 - Ignore check on certain ROIs
if roiName in [config.KEYNAME_BRAINSTEM, config.KEYNAME_BRAINSTEM + config.KEY_3MM_SUFFIX
, config.KEYNAME_SPINALCORD, config.KEYNAME_SPINALCORD + config.KEY_3MM_SUFFIX
, config.KEYNAME_GHOST_CRANIAL, config.KEYNAME_EAR_L_GHOST, config.KEYNAME_EAR_R_GHOST
]:
return roiIgnoreBool
# Step 2 - Get ROIs to ignore (while updating objectives)
roisPartialIgnoreBasic = [
config.REGEX_ROI_HOT, config.REGEX_ROI_HEET
, config.REGEX_KOUD, config.REGEX_COLD
, config.REGEX_ROI_MINDOSE, config.REGEX_ROI_MAXDOSE
, config.REGEX_ONDER, config.REGEX_OVER
, config.REGEX_DFO
, config.REGEX_ONDERDOSERING
, config.REGEX_ROI_HOSTPSOTS
, config.REGEX_CAUDAAL, config.REGEX_CAUD
]
if (planEUDSuffix in planName or planEUDAutoSuffix in planName) and objectiveFType == config.KEY_FTYPE_MAXEUD:
roisFullIgnore = [config.KEYNAME_BODY] # does not matter as it is Opt_Body in protons
roisPartialIgnore = roisPartialIgnoreBasic + [config.KEYNAME_3MM] # the extra part compared to EUD2 is KEYNAME_3MM
elif (planEUD2Suffix in planName or planEUD2AutoSuffix in planName) and objectiveFType == config.KEY_FTYPE_MAXEUD:
roisFullIgnore = [config.KEYNAME_BODY, config.KEYNAME_RING_PTV_DL2, config.KEYNAME_GHOST]
roisPartialIgnore = roisPartialIgnoreBasic
elif (planFinalSuffix in planName or planFinalAutoSuffix in planName) and objectiveFType == None:
roisFullIgnore = [config.KEYNAME_BODY]
roisPartialIgnore = []
# Step 3 - Check if ROI should be ignored
for roiFullIgnore in roisFullIgnore:
if roiName == roiFullIgnore:
roiIgnoreBool = True
break
for roiPartialIgnore in roisPartialIgnore:
if re.search(roiPartialIgnore, str(roiName).lower()) is not None:
roiIgnoreBool = True
break
return roiIgnoreBool
def updateObjectivesForProton(planName, objectivesFromRS, objectivesFromPath, objectiveFType):
"""
Called from uploadORUpdateObjectives()
Params:
-------
planName: str
objectivesFromRS: list
objectivesFromPath: list
objectiveFType: str, ['DoseFallOff', 'DoseFallOff', 'MaxEUD', None]
"""
updateObjectiveStatus = False
try:
# Step 1 - Init
patient, _, _, _ = helpers.getPatientAndPlan(planName)
planDFO2TermSuffix = config.SUFFIX_PLAN_DFO2.format(config.PREFIX_CLINICAL_CONTOURS)
planDFO2AutoTermSuffix = config.SUFFIX_PLAN_DFO2.format(config.PREFIX_AUTOMATED_CONTOURS)
planFinalTermSuffix = config.SUFFIX_PLAN_FINAL.format(config.PREFIX_CLINICAL_CONTOURS) # R5
planFinalAutoTermSuffix = config.SUFFIX_PLAN_FINAL.format(config.PREFIX_AUTOMATED_CONTOURS) # A5
# Step 2 - Get objectives from file (a.k.a path)
updatedFromPathObjectivesStatus = {}
for pathIdx, objectiveFromPath in enumerate(objectivesFromPath):
newRoiName = objectiveFromPath.roi_name
newFunctionType = objectiveFromPath.function_type
newWeight = objectiveFromPath.weight
newRobust = objectiveFromPath.is_robust
if newWeight == 0:
continue
newKey = f'{newRoiName}-{newFunctionType}-{newRobust}'
updatedFromPathObjectivesStatus[newKey] = {'exists': False, 'idx':pathIdx}
# Step 4 - Get existing objectives and update them
if len(objectivesFromRS) > 0:
print ('\n\n - [INFO][updateObjectivesForProton()][Patient={}] Updating Objectives ... '.format(helpers.getPatientIdentifier(patient)))
# Step 4.1 - Update weights of existing objectives (based on function-type and roi-name)
for rsIdx, objectiveFromRS in enumerate(objectivesFromRS):
try:
# Step 4.1.1 - Get roiName, weight, fType
existingRoiName = objectiveFromRS.ForRegionOfInterest.Name
existingWeight = objectiveFromRS.DoseFunctionParameters.Weight
existingRobust = objectiveFromRS.UseRobustness
try : existingFunctionType = objectiveFromRS.DoseFunctionParameters.FunctionType
except: existingFunctionType = config.KEY_FTYPE_DOSEFALLOFF
if existingRoiName == config.KEYNAME_BODY: continue
# [DEBUG]
if 0:
print (' - [DEBUG][updateObjectivesForProton()] \troi: {}, \tfunctionType: {}, \tweight: {}'.format(existingRoiName, existingFunctionType, existingWeight))
# Step 4.1.2 - Make note of existing objectives that are in new objectives
existingKey = f'{existingRoiName}-{existingFunctionType}-{existingRobust}'
if existingKey in updatedFromPathObjectivesStatus:
updatedFromPathObjectivesStatus[existingKey]['exists'] = True
# Step 4.1.3 - Check if existing objective is in new objectives
for objectiveFromPath in objectivesFromPath:
newRoiName = objectiveFromPath.roi_name
newFunctionType = objectiveFromPath.function_type
newWeight = objectiveFromPath.weight
newRobust = objectiveFromPath.is_robust
if newWeight == 0:
continue
functionMatchBool = newFunctionType == objectiveFType
if planFinalTermSuffix in planName or planFinalAutoTermSuffix in planName:
functionMatchBool = True # i.e update all rois
robustMatchBool = existingRobust == newRobust
if functionMatchBool and robustMatchBool:
if not checkIfRoiIgnore(planName, objectiveFType, newRoiName):
if existingRoiName == newRoiName and existingFunctionType == newFunctionType:
# Step 4.1.1 - Update weight
objectivesFromRS[rsIdx].DoseFunctionParameters.Weight = newWeight
print (f' -- [updateObjectivesForProton()] roi: {existingRoiName}, \tfType: {existingFunctionType}, \tweight: {existingWeight} --> {newWeight}')
# Step 4.1.2 - Update dose level(s)
if existingFunctionType == config.KEY_FTYPE_MAXEUD:
existingDoseLevel = objectivesFromRS[rsIdx].DoseFunctionParameters.DoseLevel
existingEudParameterA = objectivesFromRS[rsIdx].DoseFunctionParameters.EudParameterA
objectivesFromRS[rsIdx].DoseFunctionParameters.DoseLevel = objectiveFromPath.doselevel
objectivesFromRS[rsIdx].DoseFunctionParameters.EudParameterA = objectiveFromPath.eud_parameter_a
print (f' --- [updateObjectivesForProton()] \tDoseLevel: {existingDoseLevel} --> {objectiveFromPath.doselevel}')
print (f' --- [updateObjectivesForProton()] \tEudParameterA: {existingEudParameterA} --> {objectiveFromPath.eud_parameter_a}')
elif existingFunctionType == config.KEY_FTYPE_DOSEFALLOFF:
if planDFO2TermSuffix in planName or planDFO2AutoTermSuffix in planName: # [Ref: https://iprova.lumc.nl/Portal/#/document/1fc74366-40c1-4b84-a653-0455b6c891f8 (Vervolgens voor beide opties)]
print (f' --- [updateObjectivesForProton()] \tHighDoseLevel: {objectiveFromRS.DoseFunctionParameters.HighDoseLevel} --> {objectiveFromPath.high_doselevel}')
print (f' --- [updateObjectivesForProton()] \tLowDoseLevel : {objectiveFromRS.DoseFunctionParameters.LowDoseLevel} --> {objectiveFromPath.low_doselevel}')
objectivesFromRS[rsIdx].DoseFunctionParameters.HighDoseLevel = objectiveFromPath.high_doselevel
objectivesFromRS[rsIdx].DoseFunctionParameters.LowDoseLevel = objectiveFromPath.low_doselevel
elif existingFunctionType == config.KEY_FTYPE_MAXDOSE:
pass
# Step 4.1.3 - For debugging purposes
key = f'{newRoiName}-{newFunctionType}-{newRobust}'
updatedFromPathObjectivesStatus[key]['updated'] = True
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
else:
print (f' - [updateKNOObjectivesinRStation()] No objectives found in RS for plan {planName}')
# Step 5 - Add new objectives [if 1) they dont exist, and if they match 2) function-type and 3) roiName conditions]
print ('')
for key in updatedFromPathObjectivesStatus:
if updatedFromPathObjectivesStatus[key]['exists'] is False:
roiName = key.split('-')[0]
fType = key.split('-')[1]
isRobust = key.split('-')[2]
fTypeCondition = fType == objectiveFType # only if ftType in [DoseFallOff, MaxEUD]
roiIgnoreCondition = checkIfRoiIgnore(planName, objectiveFType, roiName) # this roiName comes from existing objectives of RS
if objectiveFType == None: # since this is the last plan, we want to add all (non-existent) objectives
fTypeCondition = True
if fTypeCondition and not roiIgnoreCondition:
print (f' - [updateObjectivesForProton()] Adding new objective for {roiName} of type {fType}')
newObjective = objectivesFromPath[updatedFromPathObjectivesStatus[key]['idx']]
print (f' --- [updateObjectivesForProton()] newObjective for roi: {newObjective.roi_name} and fType: {newObjective.function_type} with weight: {newObjective.weight}')
if newObjective.weight > 0:
newObjective.apply()
updateObjectiveStatus = True
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
return updateObjectiveStatus
def uploadOrUpdateProtonObjectives(planName, pathKNOProtonObjectives, uploadObjectivesBool, updateObjectivesBool, forceObjectives, objectiveFType):
# Step 1 - Init
objectiveStatus = False
# Step 2 - Boilerplate code for get patient and plans
_, case, plan, beamset = helpers.getPatientAndPlan(planName)
# Step 3 - Get objectives
objectivesFromPath = helpers.getObjectivesFromPath(plan, beamset, pathKNOProtonObjectives)
objectivesFromRS = helpers.getObjectivesFromPlan(plan, beamset)
# Step 3.1 - Filter an objective
if 1:
for objIdx, objectiveFromPath in enumerate(objectivesFromPath):
try:
if objectiveFromPath.roi_name == config.KEYNAME_OPT_BODY and objectiveFromPath.function_type == config.KEY_FTYPE_MAXDOSE:
if float(objectiveFromPath.doselevel) < 7000:
del objectivesFromPath[objIdx]
except:
pass
# Step 4 - Upload/Update Objectives
if uploadObjectivesBool:
if len(objectivesFromRS) == 0 or forceObjectives:
objectiveStatus = helpers.uploadObjectivesToRS(plan, beamset, objectivesFromPath)
else:
print(' - [uploadORUpdateObjectives()] Objectives already exist, not uploading')
elif updateObjectivesBool:
if len(objectivesFromRS) > 0:
objectiveStatus = updateObjectivesForProton(planName, objectivesFromRS, objectivesFromPath, objectiveFType)
else:
print(' - [uploadORUpdateObjectives()] No objectives found, not updating')
# Step 5 - Do sanity check
helpers.doPlanSanityCheck(case, planName)
return objectiveStatus
def robustEvaluationOld(planName, force=False):
try:
# Step 0 - Init
patientObj, caseObj, planObj, beamsetObj = helpers.getPatientAndPlan(planName)
if force:
radiationSetScenarioGroups = caseObj.TreatmentDelivery.RadiationSetScenarioGroups
for id_ in range(len(radiationSetScenarioGroups)):
if radiationSetScenarioGroups[id_].Name == planName:
print (f' - [robustEvaluation()] Deleting scenario group: {planName}')
radiationSetScenarioGroups[id_].DeleteRadiationSetScenarioGroup()
helpers.rayStationSave()
boolScenarioAvailable = False
for id_ in range(len(radiationSetScenarioGroups)):
if radiationSetScenarioGroups[id_].Name == planName:
boolScenarioAvailable = True
break
if boolScenarioAvailable is False:
print (f' - [robustEvaluation()] Creating scenario group: {planName}')
retval_0 = beamsetObj.CreateRadiationSetScenarioGroup(Name=planName
, UseIsotropicPositionUncertainty=False
, PositionUncertaintySuperior=0.3, PositionUncertaintyInferior=0.3
, PositionUncertaintyPosterior=0.3, PositionUncertaintyAnterior=0.3
, PositionUncertaintyLeft=0.3, PositionUncertaintyRight=0.3
, PositionUncertaintyFormation="AxesAndDiagonalEndPoints", PositionUncertaintyList=None
, DensityUncertainty=3, NumberOfDensityDiscretizationPoints=2
, ComputeScenarioDosesAfterGroupCreation=False)
print (f' - [robustEvaluation()] Computing dose using IonMonteCarlo algo')
beamsetObj.ComputeDose(ComputeBeamDoses=True, DoseAlgorithm="IonMonteCarlo", ForceRecompute=True)
print (f' - [robustEvaluation()] Computing scenario group dose values: {planName}')
retval_0.ComputeScenarioGroupDoseValues()
helpers.rayStationSave()
print (f' - [robustEvaluation()] Done computing scenario group dose values: {planName}')
else:
print (f' - [robustEvaluation()] Scenario group already exists: {planName}')
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
def robustEvaluationViaUI(planName, force=False):
try:
# Step 1 - Init
_, caseObj, _ , beamSetObj = helpers.getPatientAndPlan(planName, debug=True)
# Step 2 - Delete scenario group if force
if force:
radiationSetScenarioGroups = caseObj.TreatmentDelivery.RadiationSetScenarioGroups
for id_ in range(len(radiationSetScenarioGroups)):
try:
if radiationSetScenarioGroups[id_].Name == planName:
print (f' - [robustEvaluationViaUI()] Deleting scenario group: {planName}')
radiationSetScenarioGroups[id_].DeleteRadiationSetScenarioGroup()
helpers.rayStationSave()
break
except:
pass
# Step 3 - Confirm scenarioGroup does not exist
boolScenarioAvailable = False
radiationSetScenarioGroups = caseObj.TreatmentDelivery.RadiationSetScenarioGroups
for id_ in range(len(radiationSetScenarioGroups)):
if radiationSetScenarioGroups[id_].Name == planName:
boolScenarioAvailable = True
break
# Step 4 - Create scenario group if not exists (and do robust eval)
if boolScenarioAvailable is False:
print (f' - [robustEvaluationViaUI()] Creating scenario group: {planName}')
retval_0 = beamSetObj.CreateRadiationSetScenarioGroup(Name=planName
, UseIsotropicPositionUncertainty=False
, PositionUncertaintySuperior=0.3, PositionUncertaintyInferior=0.3
, PositionUncertaintyPosterior=0.3, PositionUncertaintyAnterior=0.3
, PositionUncertaintyLeft=0.3, PositionUncertaintyRight=0.3
, PositionUncertaintyFormation="AxesAndDiagonalEndPoints", PositionUncertaintyList=None
, DensityUncertainty=3, NumberOfDensityDiscretizationPoints=2
, ComputeScenarioDosesAfterGroupCreation=False)
print (f' - [robustEvaluationViaUI()] Computing dose using IonMonteCarlo algo')
beamSetObj.ComputeDose(ComputeBeamDoses=True, DoseAlgorithm="IonMonteCarlo", ForceRecompute=True)
print (f' - [robustEvaluationViaUI()] Computing scenario group dose values: {planName}')
retval_0.ComputeScenarioGroupDoseValues()
helpers.rayStationSave()
print (f' - [robustEvaluationViaUI()] Done computing scenario group dose values: {planName}')
else:
print (f' - [robustEvaluationViaUI()] Scenario group already exists: {planName}')
except:
traceback.print_exc()
def robustEvaluationViaSelf(planName, pathRobustTemplate, pathRobustResultsSave, verbose=False):
try:
def getClinicalGoalsFromDose(doseEvalObj):
"""
For evalFunc in planObj.TreatmentCourse.EvaluationSetup.EvaluationFunctions:
- evalFunc.ForRegionOfInterest.Name : self-explanatory
- evalFunc.PlanningGoal.Type : 'DoseAtVolume', 'DoseAtAbsoluteVolume', 'DoseAtRelativeVolume', 'VolumeAtDose', 'AbsoluteVolumeAtDose', 'RelativeVolumeAtDose'
- evalFunc.PlanningGoal.GoalCriteria : 'AtMost', 'AtLeast'
- evalFunc.PlanningGoal.AcceptanceLevel: float
"""
# Step 0 - Init
resObj = {}
try:
# Step 1 - Init
_, caseObj, planObj, beamSetObj = helpers.getPatientAndPlan(planName)
evalFuncs = planObj.TreatmentCourse.EvaluationSetup.EvaluationFunctions
# Step 2 - Loop over funcs
for evalFunc in evalFuncs:
try:
value = evalFunc.GetClinicalGoalValueForEvaluationDose(DoseDistribution=doseEvalObj, ScaleFractionDoseToBeamSet=True)
except:
value = evalFunc.GetClinicalGoalValue()
roiGoalkey = '-'.join([evalFunc.ForRegionOfInterest.Name, evalFunc.PlanningGoal.Type, evalFunc.PlanningGoal.GoalCriteria, str(evalFunc.PlanningGoal.AcceptanceLevel), str(evalFunc.PlanningGoal.ParameterValue)])
tmp = [value, evalFunc.PlanningGoal.GoalCriteria, evalFunc.PlanningGoal.AcceptanceLevel]
if evalFunc.PlanningGoal.AcceptanceLevel > 0:
if evalFunc.PlanningGoal.GoalCriteria == config.KEYNAME_ATMOST:
if value <= evalFunc.PlanningGoal.AcceptanceLevel: resObj[roiGoalkey] = tmp + [config.KEYNAME_PASS]
else : resObj[roiGoalkey] = tmp + [config.KEYNAME_FAIL]
elif evalFunc.PlanningGoal.GoalCriteria == config.KEYNAME_ATLEAST:
if value >= evalFunc.PlanningGoal.AcceptanceLevel: resObj[roiGoalkey] = tmp + [config.KEYNAME_PASS]
else : resObj[roiGoalkey] = tmp + [config.KEYNAME_FAIL]
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
return resObj
def getKeyMapping(robustTemplateObj, thisObj):
# Step 0 - Init
mapping = {}
try:
# Step 1 - Loop over keys
roiGoalValueKeys = list(thisObj.keys())
for roiGoalValueKey in roiGoalValueKeys:
thisRoiName = roiGoalValueKey.split('-')[0]
thisGoalType = roiGoalValueKey.split('-')[1]
thisGoalCriteria = roiGoalValueKey.split('-')[2]
thisAcceptanceValue = roiGoalValueKey.split('-')[3]
thisParameterValue = roiGoalValueKey.split('-')[4]
mapping[roiGoalValueKey] = ''
for templateKey in robustTemplateObj.keys():
templateKeyRoiName = templateKey.split(' ')[0]
templateKeyGoal = templateKey[templateKey.find('[')+1: templateKey.find(']')]
roiCheckBool = thisRoiName == templateKeyRoiName
if roiCheckBool:
if thisRoiName == 'CTV_DL1':
perc = int(round(float(thisParameterValue)/config.CTV_DL1_MAX,2) * 100)
if str(perc) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
elif thisRoiName == 'CTV_DL2':
perc = int(round(float(thisParameterValue)/config.CTV_DL2_MAX,2) * 100)
if str(perc) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
elif thisRoiName == 'Brainstem_Core':
if str(int(float(thisAcceptanceValue))) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
elif thisRoiName == 'Brainstem_Surf':
if str(int(float(thisAcceptanceValue))) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
elif thisRoiName == 'SpinalCord_Core':
if str(int(float(thisAcceptanceValue))) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
elif thisRoiName == 'SpinalCord_Surf':
if str(int(float(thisAcceptanceValue))) in templateKeyGoal:
mapping[roiGoalValueKey] = templateKey
break
else:
mapping[roiGoalValueKey] = templateKey
break
except:
traceback.print_exc()
return mapping
# Step 1 - Init
_, caseObj, planObj, beamSetObj = helpers.getPatientAndPlan(planName, debug=True)
radiationSetScenarioGroups = caseObj.TreatmentDelivery.RadiationSetScenarioGroups
res = {config.KEYNAME_NOMINAL_DOSE: {}, config.KEYNAME_MAX_DOSE: {}, config.KEYNAME_MIN_DOSE: {}, config.KEYNAME_SCENARIO_DOSE: {}}
dose3DMin = []
dose3DMax = []
# Step 2 - FInd idx of radiationSetScenarioGroups
groupIdx = -1
for id_ in range(len(radiationSetScenarioGroups)):
try:
if radiationSetScenarioGroups[id_].Name == planName:
groupIdx = id_
except:
pass
if groupIdx > -1:
print (f' - [robustEvaluationViaSelf()] Found scenario group: {planName} and doing robust eval on all scenarios')
for scenarioId, scenarioObj in enumerate(caseObj.TreatmentDelivery.RadiationSetScenarioGroups[groupIdx].DiscreteFractionDoseScenarios):
if verbose:
print (f' - [robustEvaluationViaSelf()] Computing dose for scenario: {scenarioId}')
# print (scenarioId, scenarioObj.PerturbedDoseProperties.IsoCenterShift, scenarioObj.PerturbedDoseProperties.RelativeDensityShift)
# Step 2.2 - Compute dose for scenario
beamSetObj.ComputePerturbedDose(DensityPerturbation=scenarioObj.PerturbedDoseProperties.RelativeDensityShift
, PatientShift=scenarioObj.PerturbedDoseProperties.IsoCenterShift
, OnlyOneDosePerImageSet=True, ExaminationNames=[caseObj.Examinations[0].Name], FractionNumbers=[0]
)
# Step 2.3 - Get doseObj and dose values
doseEvalObj = caseObj.TreatmentDelivery.FractionEvaluations[0].DoseOnExaminations[0].DoseEvaluations[0]
dose3Darray = doseEvalObj.DoseValues.DoseData
# Step 2.4 - Compute min and max dose
if len(dose3DMin) == 0: dose3DMin = dose3Darray
else : dose3DMin = np.minimum(dose3DMin, dose3Darray)
if len(dose3DMax) == 0: dose3DMax = dose3Darray
else : dose3DMax = np.maximum(dose3DMax, dose3Darray)
# Step 2.5 - Compute clinical goals for each perturbed dose
res[config.KEYNAME_SCENARIO_DOSE][scenarioId] = getClinicalGoalsFromDose(doseEvalObj)
# Step 2.99 - Debug
# if scenarioId > -1:
# break
# Step 3 - Set dose values to min and max dose
doseEvalObj.SetDoseValues(Array=dose3DMax.flatten(), CalculationInfo='Voxelwise max', DoseAlgorithm='Undefined')
res[config.KEYNAME_MAX_DOSE] = getClinicalGoalsFromDose(doseEvalObj)
doseEvalObj.SetDoseValues(Array=dose3DMin.flatten(), CalculationInfo='Voxelwise min', DoseAlgorithm='Undefined')
res[config.KEYNAME_MIN_DOSE] = getClinicalGoalsFromDose(doseEvalObj)
# Step 4 - Get norminal dose
doseEvalObj = planObj.TreatmentCourse.TotalDose # type=CompositeDose
res[config.KEYNAME_NOMINAL_DOSE] = getClinicalGoalsFromDose(doseEvalObj)
# Step 5 - Compute pass/fail (and save voxelwise-man/max values)
if Path(pathRobustTemplate).exists():
with open(str(pathRobustTemplate), 'r') as fp:
robustTemplate = json.load(fp)
# Step 5.0 - Init
keyMapper = getKeyMapping(robustTemplate, res[config.KEYNAME_SCENARIO_DOSE][0])
# Step 5.1 - Compute pass percentage
roiKeys = list(res[config.KEYNAME_SCENARIO_DOSE][0].keys())
for roiKey in roiKeys:
try:
roiTemplateKey = keyMapper[roiKey]
if len(roiTemplateKey) > 0:
# Step 5.1.1 - Collect for KEYNAME_SCENARIOS
roiPassCount = 0
for scenarioId in res[config.KEYNAME_SCENARIO_DOSE].keys():
if res[config.KEYNAME_SCENARIO_DOSE][scenarioId][roiKey][-1] == config.KEYNAME_PASS:
roiPassCount += 1
robustTemplate[roiTemplateKey][config.KEYNAME_SCENARIOS].append(round(res[config.KEYNAME_SCENARIO_DOSE][scenarioId][roiKey][0],4))
robustTemplate[roiTemplateKey][config.KEYNAME_PASSED] = round(roiPassCount/len(res[config.KEYNAME_SCENARIO_DOSE].keys()), 4)
# Step 5.1.2 - Collect for KEYNAME_NOMINAL_DOSE
robustTemplate[roiTemplateKey][config.KEYNAME_NOMINAL_DOSE] = round(res[config.KEYNAME_NOMINAL_DOSE][roiKey][0], 4)
# Step 5.1.3 - Collect for KEYNAME_MIN_DOSE and KEYNAME_MAX_DOSE
if roiTemplateKey in config.OBJECTIVES_ROIS_TUMOR:
robustTemplate[roiTemplateKey][config.KEYNAME_VOXELWISE_WORST] = round(res[config.KEYNAME_MIN_DOSE][roiKey][0], 4)
else:
robustTemplate[roiTemplateKey][config.KEYNAME_VOXELWISE_WORST] = round(res[config.KEYNAME_MAX_DOSE][roiKey][0], 4)
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
# Step 6 - Save
sys.stdout.write(f' - [robustEvaluationViaSelf()] Saving robustTemplate to: {pathRobustResultsSave}')
print (f' - [robustEvaluationViaSelf()] Saving robustTemplate to: {pathRobustResultsSave}')
with open(str(pathRobustResultsSave), 'w') as fp:
robustTemplateWrite = {planName: robustTemplate}
json.dump(robustTemplateWrite, fp, indent=4)
else:
print (f' - [robustEvaluationViaSelf()] Robust template not found: {pathRobustTemplate}')
else:
print (f' - [robustEvaluationViaSelf()] Scenario group not found for {planName}')
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
def robustEvaluation(planName, pathRobustTemplate, pathPatient, force=False, verbose=False):
try:
t0 = time.time()
# Step 0 - Init
pathRobustResultsSave = Path(pathPatient) / config.FILENAME_ROBUST_EVAL_RESULTS.format(planName)
# Step 1 - Do UI-based robust eval
robustEvaluationViaUI(planName, force=force)
# Step 2 - Do self-based robust eval
robustEvaluationViaSelf(planName, pathRobustTemplate, pathRobustResultsSave, verbose=verbose)
print (f' - [robustEvaluation()] Done robust eval for {planName} in {round(time.time() - t0, 2)} s')
except:
traceback.print_exc()
pdb.set_trace()
########################################################
# NTCP #
########################################################
def getNTCPVals(patientID, plans, pathPatient):
try:
print (f' - [getNTCPVals()] Getting NTCP values for patient: {patientID}')
# Step 0 - Plans and tumor types
patientObjOld = {
'HCAI-Dose-P1': ['1F PVFOTONEN', None] # None
, 'HCAI-Dose-P2': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P3': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P4': ['1A KNOKL', None] # None
, 'HCAI-Dose-P5': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P6': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P7': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P8': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P9': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P10': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P11': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P12': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P13': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P14': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P15': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P16': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P17': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P18': ['1A KNOKL', None] # None
, 'HCAI-Dose-P19': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P20': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P21': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P22': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P23': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P24': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P25': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P26': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P27': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P28': ['1A TONGKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P29': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P30': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P31': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
, 'HCAI-Dose-P32': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.ORAL_CAVITY]
}
patientObj = {
'HCAI-Dose-P1': ['1F PVFOTONEN', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX] # None
, 'HCAI-Dose-P2': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P3': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P4': ['1A KNOKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX] # None
, 'HCAI-Dose-P5': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P6': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P7': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P8': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P9': ['1A HYPOFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P10': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P11': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P12': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P13': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P14': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P15': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P16': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P17': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P18': ['1A KNOKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX] # None
, 'HCAI-Dose-P19': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P20': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P21': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P22': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P23': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P24': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P25': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P26': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P27': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P28': ['1A TONGKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P29': ['1A LARYNXKL', helpers.NtcpKnoDysphagia.TumorLocationType.LARYNX]
, 'HCAI-Dose-P30': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P31': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
, 'HCAI-Dose-P32': ['1A OROFARKL', helpers.NtcpKnoDysphagia.TumorLocationType.PHARYNX]
}
# Step 1 - Other params
params = {
config.KEY_NTCP_PLAN1 : None
, config.KEY_NTCP_PLAN2: None
, config.KEY_NTCP_TREATMENT_TYPE : helpers.TreatmentType.PRIMARY
, config.KEY_NTCP_TUMOR_LOCATION : patientObj[patientID][1] # NtcpKnoDysphagia.TumorLocationType.{ORAL_CAVITY, PHARYNX, LARYNX}
, config.KEY_NTCP_BASELINE_XEROSTOMIA: helpers.NtcpKnoXerostomia.BaselineType.NONE # NtcpKnoXerostomia.BaselineType.{NONE, LITTLE, SEVERE}
, config.KEY_NTCP_BASELINE_DYSPHAGIA : helpers.NtcpKnoDysphagia.BaselineType.GRADE_0_1 # NtcpKnoDysphagia.BaselineType.{GRADE_0_1, GRADE_2, GRADE_3_4}
, config.KEY_NTCP_IS_TOTAL_LARYNGECTOMY: False
, config.KEY_PAROTIDS_REMOVED : False
}
# Step 2 - main
res = {}
if params[config.KEY_NTCP_TUMOR_LOCATION] is not None:
for plan in plans:
# Step 2.1 - Init
params[config.KEY_NTCP_PLAN1] = plan
res[plan] = {}
objNTCP = helpers.KNONTCP(params)
# Step 2.2 - Calculate
tmp = objNTCP.ntcp_xerostomia_grade_2_values
if config.KEY_NTCP_PLAN_1 in tmp:
res[plan][tmp[config.KEY_NTCP_MODEL_NAME]] = round(tmp[config.KEY_NTCP_PLAN_1][config.KEY_NTCP_PLAN_VALUE], 5)
tmp = objNTCP.ntcp_xerostomia_grade_3_values
if config.KEY_NTCP_PLAN_1 in tmp:
res[plan][tmp[config.KEY_NTCP_MODEL_NAME]] = round(tmp[config.KEY_NTCP_PLAN_1][config.KEY_NTCP_PLAN_VALUE], 5)
tmp = objNTCP.ntcp_dysphagia_grade_2_values
if config.KEY_NTCP_PLAN_1 in tmp:
res[plan][tmp[config.KEY_NTCP_MODEL_NAME]] = round(tmp[config.KEY_NTCP_PLAN_1][config.KEY_NTCP_PLAN_VALUE], 5)
tmp = objNTCP.ntcp_dysphagia_grade_3_values
if config.KEY_NTCP_PLAN_1 in tmp:
res[plan][tmp[config.KEY_NTCP_MODEL_NAME]] = round(tmp[config.KEY_NTCP_PLAN_1][config.KEY_NTCP_PLAN_VALUE], 5)
# Step 3 - Save
pathPatientNTCP = Path(pathPatient) / config.FILENAME_NTCP_RESULTS
with open(str(pathPatientNTCP), 'w') as fp:
json.dump(res, fp, indent=4)
print (f' - [getNTCPVals()] Done NTCP calculations for {patientID}')
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
########################################################
# AUTO-HELPERS #
########################################################
def doAutoContouringForProton():
autoContourStatus = False
t0 = time.time()
try:
# Step 0 - Init
print (f' \n\n ===================== start autocontouring (for protons) ===================== \n\n')
patient = helpers.rayStationSave()
case = patient.Cases[0]
examinationName = case.Examinations[0].Name
# Step 1.1 - Get OARs to include
oarDuplicateStatus = helpers.checkOARDuplicateStatus(case)
oarsToInclude = []
for oar in oarDuplicateStatus:
if not oarDuplicateStatus[oar]:
oarsToInclude.append(oar)
print (' - [doAutoContouringForProton()] OARs pending to auto-contour: ', oarsToInclude)
# Step 1.2 - Run OAR segmentation
if len(oarsToInclude):
_ = helpers.rayStationSave()
case.SetCurrent() # Why == Potential Error: "The case to which the examination belongs mist be selected"
examination = case.Examinations[0]
_ = examination.RunOarSegmentation(ModelName="RSL Head and Neck CT", ExaminationsAndRegistrations={ examinationName: None }, RoisToInclude=oarsToInclude)
helpers.rayStationSave()
else:
print (' - [doAutoContouringForProton()] No OARs to auto-contour')
# Step 1.3 - Check aut--contours status
## If they dont have the config.KEY_AUTOCONTOUR_SUFFIX, then they were not initially present, so simply rename them
oarDuplicateStatus = helpers.checkOARDuplicateStatus(case)
for oar in oarDuplicateStatus:
if not oarDuplicateStatus[oar]:
print (' - [doAutoContouringForProton()] OAR auto-contour not present: ', oar)
if oar == config.KEYNAME_CAVITY_ORAL:
newName = config.KEYNAME_ORAL_CAVITY + config.KEY_AUTOCONTOUR_SUFFIX
case.PatientModel.RegionsOfInterest[oar].Name = newName
case.PatientModel.RegionsOfInterest[newName].Color = config.OAR_DUPLICATE_COLOR_RGB_STRING
print (' - [doAutoContouringForProton()] Renamed OAR: ', oar, ' --> ', newName)
elif oar == config.KEYNAME_ESOPHAGUS_S:
try:
newName = config.KEYNAME_ESOPHAGUS + config.KEY_AUTOCONTOUR_SUFFIX
case.PatientModel.RegionsOfInterest[oar].Name = newName
case.PatientModel.RegionsOfInterest[newName].Color = config.OAR_DUPLICATE_COLOR_RGB_STRING
print (' - [doAutoContouringForProton()] Renamed OAR: ', oar, ' --> ', newName)
except:
traceback.print_exc()
else:
case.PatientModel.RegionsOfInterest[oar].Name = oar + config.KEY_AUTOCONTOUR_SUFFIX
# Step 1.4 - Do ROI Algebra
helpers.doROIAlgebraForProtonAutoContours(case)
_ = helpers.rayStationSave()
timeTaken = round(time.time() - t0, 2)
print (f' \n\n ===================== end autocontouring (for protons) (in {timeTaken} s) ===================== \n\n')
return True, timeTaken
except:
traceback.print_exc()
if DEBUG_PDB: pdb.set_trace()
timeTaken = round(time.time() - t0, 2)
return False, timeTaken
########################################################
# MAIN(S) #
########################################################
# Func 1
def uploadRTAppsDataToRStation(pathPatient, planName, forceUpload=False, forceCurrentPatient=False):
"""
Params
------
pathPatient: Path, Path to patient folder containing CT, RTDose, RTPlan, RTStruct
- e.g. Path('H:\\').joinpath('RayStationData', 'LUMC-Dose', 'HCAI-Dose-1', '2.25.52093085334020578701550802539023326955')
- CT_{}
- RTDOSE_{}
- RTPLAN_{}
- RTSTRUCT_{}
planName: String, check if this plan exists in RayStation. If not, upload
forceUpload: Bool, If True, will upload data even if patient already exists. Useful when debugging
forceCurrentPatient: Bool, If True, will use patient currently open in RayStation
NOTE: Above folder structure is required for this function to work
"""
# Step 0 - Init
assert forceUpload + forceCurrentPatient < 2, ' - [uploadRTAppsDataToRStation] forceUpload and forceCurrentPatient cannot be both True!'
helpers.rayStationSave()
db = connect.get_current(config.KEYNAME_RS_PATIENTDB)
patientCTBool, patientRTStructBool, patientRTPlanBool, patientRTDoseBool = False, False, False, False
# Step 1 - Create patient
if pathPatient.exists():
# Step 2 - Get patient paths
pathPatientCTFolders = [path for path in pathPatient.iterdir() if path.is_dir() and path.name.startswith(config.KEYNAME_CT)]
pathPatientRTDoseFolders = [path for path in pathPatient.iterdir() if path.is_dir() and path.name.startswith(config.KEYNAME_RTDOSE)]
pathPatientRTPlanFolders = [path for path in pathPatient.iterdir() if path.is_dir() and path.name.startswith(config.KEYNAME_RTPLAN)]
pathPatientRTStructFolders = [path for path in pathPatient.iterdir() if path.is_dir() and path.name.startswith(config.KEYNAME_RTSTRUCT)]
# if len(pathPatientCTFolders) == 1 and len(pathPatientRTDoseFolders) == 1 and len(pathPatientRTPlanFolders) == 1 and len(pathPatientRTStructFolders) == 1:
if len(pathPatientCTFolders) == 1 and len(pathPatientRTStructFolders) == 1:
pathPatientCTFolder = pathPatientCTFolders[0]
pathPatientRTStructFolder = pathPatientRTStructFolders[0]
# Step 1.1 - Upload CT
patientIdCheck = Path(pathPatient).parts[-2]
if not forceCurrentPatient:
try:
print ('\n\n - [uploadRTAppsDataToRStation()] --------------------- Step 1: Checking for patientId={}, plan={} (forceUpload={}) \n\n'.format(patientIdCheck, planName, forceUpload))
if forceUpload: patient = None
else : patient = helpers.getPatientById(patientIdCheck, lastFind=True)
if patient is None:
print ('\n - [uploadRTAppsDataToRStation()] --------------------- Step 1.1: Uploading patient data for patientId={} \n\n'.format(patientIdCheck))
if Path(pathPatientCTFolder).exists():
patientID, studyUID, seriesUID = helpers.updateCTDicoms(pathPatientCTFolder)
print ('\n - [uploadRTAppsDataToRStation()] --------------------- Step 1.1: Uploading CT data for {} ... \n\n'.format(patientID))
warnings = db.ImportPatientFromPath(Path=str(pathPatientCTFolder), SeriesOrInstances=[{'PatientID': patientID, 'StudyInstanceUID': str(studyUID), 'SeriesInstanceUID': str(seriesUID)}], ImportFilter='', BrachyPlanImportOverrides={})
patient = helpers.rayStationSave()
# patient.Cases[0].SetCurrent()
print ('\n - [uploadRTAppsDataToRStation()] --------------------- Step 1.1: Uploaded CT data for {} \n\n'.format(helpers.getPatientIdentifier(patient)))
helpers.setEquipmentName()
patientCTBool = True
else:
print ('\n - [uploadRTAppsDataToRStation()][{}] --------------------- Step 1.1: No CT data found: {} \n\n'.format(patientID, pathPatientCTFolder))
else:
print ('\n - [uploadRTAppsDataToRStation()] --------------------- Step 1.1: Patient already exists \n\n')
patientCTBool = True
except:
traceback.print_exc()
print ('\n - [uploadRTAppsDataToRStation()] --------------------- Step 1.1: Issue with CT data upload: {} \n\n'.format(pathPatientCTFolder))
patientCTBool = False
else:
print ('\n\n [uploadRTAppsDataToRStation()][DEBUG] --------------------- Step 1: Not uploading for patientID={} (forceCurrentPatient={}) \n\n'.format(patientIdCheck, forceCurrentPatient))
patientCTBool = True
print ('\n ---------------------------------------------------------- ')
# Step 1.2 - Upload (existing) RTStruct/RTPLAN/RTDOSE
patient = helpers.rayStationSave()
if patient is not None and patientCTBool:
case = patient.Cases[0]
casename = case.CaseName
patientID = patient.PatientID
# Step 1.2 - Upload (existing) RTStruct
try:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: Upload RTStruct data \n\n'.format(helpers.getPatientIdentifier(patient)))
if not helpers.checkForRTStruct(case):
pathPatientRTStructFile = [each for each in pathPatientRTStructFolder.iterdir()][0]
if Path(pathPatientRTStructFile).exists():
studyUIDRTStruct, seriesUIDRTStruct = helpers.updateRTStructDicoms(pathPatientRTStructFile)
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: Uploading RTStruct data ... \n\n'.format(helpers.getPatientIdentifier(patient)))
warningsRTStruct = patient.ImportDataFromPath(Path=str(pathPatientRTStructFolder), CaseName=casename, SeriesOrInstances=[{'PatientID': patientID, 'StudyInstanceUID': str(studyUIDRTStruct), 'SeriesInstanceUID': str(seriesUIDRTStruct)}], ImportFilter='', BrachyPlanImportOverrides={}, AllowMismatchingPatientID=True)
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: Uploaded RTStruct data \n\n'.format(helpers.getPatientIdentifier(patient)))
patientRTStructBool = True
helpers.rayStationSave()
else:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: No RTStruct data found: {} \n\n'.format(helpers.getPatientIdentifier(patient), pathPatientRTStructFile))
else:
print ('\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: RTStruct data already exists \n\n'.format(helpers.getPatientIdentifier(patient)))
patientRTStructBool = True
except:
traceback.print_exc()
print ('\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: Issue with RTStruct data upload: {} \n\n'.format(helpers.getPatientIdentifier(patient), pathPatientRTStructFile))
patientRTStructBool = False
print ('\n ---------------------------------------------------------- ')
# Step 1.3 - Upload (existing) RTPlan/RTDose
if patientCTBool and patientRTStructBool:
if not helpers.checkForRTPlan(case, planName):
if len(pathPatientRTDoseFolders) == 1 and len(pathPatientRTPlanFolders) == 1:
pathPatientRTDoseFolder = pathPatientRTDoseFolders[0]
pathPatientRTPlanFolder = pathPatientRTPlanFolders[0]
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: Upload RTPlan/RTDose data \n\n'.format(helpers.getPatientIdentifier(patient)))
studyUIDRTPlan = Path(pathPatientRTPlanFolder).parts[-2]
seriesUIDRTPlan = Path(pathPatientRTPlanFolder).parts[-1].split('_')[-1]
studyUIDRTDose = Path(pathPatientRTDoseFolder).parts[-2]
seriesUIDRTDose = Path(pathPatientRTDoseFolder).parts[-1].split('_')[-1]
studyID = helpers.updateRTPlanDicoms(pathPatientRTPlanFolder)
# studyUIDRTPlan = studyID
# studyUIDRTDose = studyID
pathTempRTDoseAndRTPlanFolder = helpers.getTempRTDoseAndRTPlanFolder(pathPatientRTPlanFolder, pathPatientRTDoseFolder)
print ('\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: Uploading RTPlan/RTDose data ... \n\n'.format(helpers.getPatientIdentifier(patient)))
warningsRTPlan = patient.ImportDataFromPath(Path=str(pathTempRTDoseAndRTPlanFolder), CaseName=casename, SeriesOrInstances=[
{'PatientID': patientID, 'StudyInstanceUID': str(studyUIDRTPlan), 'SeriesInstanceUID': str(seriesUIDRTPlan)}
, {'PatientID': patientID, 'StudyInstanceUID': str(studyUIDRTDose), 'SeriesInstanceUID': str(seriesUIDRTDose)}
], ImportFilter='', BrachyPlanImportOverrides={}, AllowMismatchingPatientID=True)
shutil.rmtree(pathTempRTDoseAndRTPlanFolder)
patientRTPlanBool = True
patientRTDoseBool = True
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: Uploaded RTPlan/RTDose data \n\n'.format(helpers.getPatientIdentifier(patient)))
else:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: No RTPlan/RTDose .dcm found \n\n'.format(helpers.getPatientIdentifier(patient)))
else:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: RTPlan/RTDose already exists \n\n'.format(helpers.getPatientIdentifier(patient)))
patientRTPlanBool = True
patientRTDoseBool = True
else:
print ('\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.3: Not uploading RTPlan/RTDose due to CT/RTStruct issue \n\n'.format(patientID))
print ('\n ---------------------------------------------------------- ')
else:
if patientCTBool is False:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: No CT to be found for: \n\n'.format(pathPatientCTFolder))
else:
print ('\n\n [uploadRTAppsDataToRStation()][Patient={}] --------------------- Step 1.2: No patient to be found: \n\n'.format(patientID))
else:
print(' - [uploadRTAppsDataToRStation()] Patient folder does not contain one CT, RTDOSE, RTPLAN and RTSTRUCT folder: ', pathPatient)
else:
print(" - [uploadRTAppsDataToRStation()] Patient folder does not exist: ", pathPatient)
if len(pathPatientRTDoseFolders) == 1 and len(pathPatientRTPlanFolders) == 1:
if patientCTBool and patientRTStructBool and patientRTPlanBool and patientRTDoseBool:
return True
else:
return False
else:
if patientCTBool and patientRTStructBool:
return True
else:
return False
# Func 2
def copyProtonPlanAndOptimize(basePlanName, newPlanName
, pathKNOProtonObjectives, uploadObjectivesBool, updateObjectivesBool, forceObjectives, objectiveFType
, optSteps, optReset, pathIsoDoseXML=None
, debug=False):
t0 = time.time()
optimizeValue = -1
timeTaken = -1
try:
# Step 0 - for console printing (while debugging across multiple consoles)
try:
patient, _, _, _ = helpers.getPatientAndPlan(basePlanName)
sys.stdout.write(f' \n\n ===================== [{patient.Name}] start for {newPlanName} ===================== \n')
except:
traceback.print_exc()
# Step 1 - Copy Plan
print (f' \n\n ===================== start for {newPlanName} ===================== \n')
copyPlanStatus = helpers.copyPlan(basePlanName, newPlanName, createArcBeam=False, debug=debug)
if not copyPlanStatus:
return False, optimizeValue, timeTaken
# Step 2 - Upload/update objectives
objectiveStatus = uploadOrUpdateProtonObjectives(newPlanName, pathKNOProtonObjectives, uploadObjectivesBool, updateObjectivesBool, forceObjectives, objectiveFType)