-
Notifications
You must be signed in to change notification settings - Fork 25
/
TotalSegmentator.py
1276 lines (1047 loc) · 62.7 KB
/
TotalSegmentator.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
import logging
import os
import re
import vtk
import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin
#
# TotalSegmentator
#
#
class TotalSegmentator(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "Total Segmentator"
self.parent.categories = ["Segmentation"]
self.parent.dependencies = []
self.parent.contributors = ["Andras Lasso (PerkLab, Queen's University)"]
self.parent.helpText = """
3D Slicer extension for fully automatic whole body CT segmentation using "TotalSegmentator" AI model.
See more information in the <a href="https://github.com/lassoan/SlicerTotalSegmentator">extension documentation</a>.
"""
self.parent.acknowledgementText = """
This file was originally developed by Andras Lasso (PerkLab, Queen's University).
The module uses <a href="https://github.com/wasserth/TotalSegmentator">TotalSegmentator</a>.
If you use the TotalSegmentator nn-Unet function from this software in your research, please cite:
Wasserthal J., Meyer M., , Hanns-Christian Breit H.C., Cyriac J., Shan Y., Segeroth, M.:
TotalSegmentator: robust segmentation of 104 anatomical structures in CT images.
https://arxiv.org/abs/2208.05868
"""
slicer.app.connect("startupCompleted()", self.configureDefaultTerminology)
def configureDefaultTerminology(self):
moduleDir = os.path.dirname(self.parent.path)
totalSegmentatorTerminologyFilePath = os.path.join(moduleDir, 'Resources', 'SegmentationCategoryTypeModifier-TotalSegmentator.term.json')
tlogic = slicer.modules.terminologies.logic()
self.terminologyName = tlogic.LoadTerminologyFromFile(totalSegmentatorTerminologyFilePath)
#
# TotalSegmentatorWidget
#
class TotalSegmentatorWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent=None):
"""
Called when the user opens the module the first time and the widget is initialized.
"""
ScriptedLoadableModuleWidget.__init__(self, parent)
VTKObservationMixin.__init__(self) # needed for parameter node observation
self.logic = None
self._parameterNode = None
self._updatingGUIFromParameterNode = False
def setup(self):
"""
Called when the user opens the module the first time and the widget is initialized.
"""
ScriptedLoadableModuleWidget.setup(self)
# Load widget from .ui file (created by Qt Designer).
# Additional widgets can be instantiated manually and added to self.layout.
uiWidget = slicer.util.loadUI(self.resourcePath('UI/TotalSegmentator.ui'))
self.layout.addWidget(uiWidget)
self.ui = slicer.util.childWidgetVariables(uiWidget)
# Set scene in MRML widgets. Make sure that in Qt designer the top-level qMRMLWidget's
# "mrmlSceneChanged(vtkMRMLScene*)" signal in is connected to each MRML widget's.
# "setMRMLScene(vtkMRMLScene*)" slot.
uiWidget.setMRMLScene(slicer.mrmlScene)
# Create logic class. Logic implements all computations that should be possible to run
# in batch mode, without a graphical user interface.
self.logic = TotalSegmentatorLogic()
self.logic.logCallback = self.addLog
for task in self.logic.tasks:
taskTitle = self.logic.tasks[task]['title']
if self.logic.isLicenseRequiredForTask(task):
taskTitle += " [license required]"
self.ui.taskComboBox.addItem(taskTitle, task)
# Connections
# These connections ensure that we update parameter node when scene is closed
self.addObserver(slicer.mrmlScene, slicer.mrmlScene.StartCloseEvent, self.onSceneStartClose)
self.addObserver(slicer.mrmlScene, slicer.mrmlScene.EndCloseEvent, self.onSceneEndClose)
# These connections ensure that whenever user changes some settings on the GUI, that is saved in the MRML scene
# (in the selected parameter node).
self.ui.inputVolumeSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.updateParameterNodeFromGUI)
self.ui.fastCheckBox.connect('toggled(bool)', self.updateParameterNodeFromGUI)
self.ui.cpuCheckBox.connect('toggled(bool)', self.updateParameterNodeFromGUI)
self.ui.useStandardSegmentNamesCheckBox.connect('toggled(bool)', self.updateParameterNodeFromGUI)
self.ui.taskComboBox.currentTextChanged.connect(self.updateParameterNodeFromGUI)
self.ui.outputSegmentationSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.updateParameterNodeFromGUI)
self.ui.outputSegmentationSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.ui.segmentationShow3DButton.setSegmentationNode)
# Buttons
self.ui.packageInfoUpdateButton.connect('clicked(bool)', self.onPackageInfoUpdate)
self.ui.packageUpgradeButton.connect('clicked(bool)', self.onPackageUpgrade)
self.ui.setLicenseButton.connect('clicked(bool)', self.onSetLicense)
self.ui.applyButton.connect('clicked(bool)', self.onApplyButton)
# Make sure parameter node is initialized (needed for module reload)
self.initializeParameterNode()
def cleanup(self):
"""
Called when the application closes and the module widget is destroyed.
"""
self.removeObservers()
def enter(self):
"""
Called each time the user opens this module.
"""
# Make sure parameter node exists and observed
self.initializeParameterNode()
def exit(self):
"""
Called each time the user opens a different module.
"""
# Do not react to parameter node changes (GUI wlil be updated when the user enters into the module)
self.removeObserver(self._parameterNode, vtk.vtkCommand.ModifiedEvent, self.updateGUIFromParameterNode)
def onSceneStartClose(self, caller, event):
"""
Called just before the scene is closed.
"""
# Parameter node will be reset, do not use it anymore
self.setParameterNode(None)
def onSceneEndClose(self, caller, event):
"""
Called just after the scene is closed.
"""
# If this module is shown while the scene is closed then recreate a new parameter node immediately
if self.parent.isEntered:
self.initializeParameterNode()
def initializeParameterNode(self):
"""
Ensure parameter node exists and observed.
"""
# Parameter node stores all user choices in parameter values, node selections, etc.
# so that when the scene is saved and reloaded, these settings are restored.
self.setParameterNode(self.logic.getParameterNode())
# Select default input nodes if nothing is selected yet to save a few clicks for the user
if not self._parameterNode.GetNodeReference("InputVolume"):
firstVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
if firstVolumeNode:
self._parameterNode.SetNodeReferenceID("InputVolume", firstVolumeNode.GetID())
def setParameterNode(self, inputParameterNode):
"""
Set and observe parameter node.
Observation is needed because when the parameter node is changed then the GUI must be updated immediately.
"""
if inputParameterNode:
self.logic.setDefaultParameters(inputParameterNode)
# Unobserve previously selected parameter node and add an observer to the newly selected.
# Changes of parameter node are observed so that whenever parameters are changed by a script or any other module
# those are reflected immediately in the GUI.
if self._parameterNode is not None:
self.removeObserver(self._parameterNode, vtk.vtkCommand.ModifiedEvent, self.updateGUIFromParameterNode)
self._parameterNode = inputParameterNode
if self._parameterNode is not None:
self.addObserver(self._parameterNode, vtk.vtkCommand.ModifiedEvent, self.updateGUIFromParameterNode)
# Initial GUI update
self.updateGUIFromParameterNode()
def updateGUIFromParameterNode(self, caller=None, event=None):
"""
This method is called whenever parameter node is changed.
The module GUI is updated to show the current state of the parameter node.
"""
if self._parameterNode is None or self._updatingGUIFromParameterNode:
return
# Make sure GUI changes do not call updateParameterNodeFromGUI (it could cause infinite loop)
self._updatingGUIFromParameterNode = True
# Update node selectors and sliders
self.ui.inputVolumeSelector.setCurrentNode(self._parameterNode.GetNodeReference("InputVolume"))
task = self._parameterNode.GetParameter("Task")
self.ui.taskComboBox.setCurrentIndex(self.ui.taskComboBox.findData(task))
self.ui.fastCheckBox.checked = self._parameterNode.GetParameter("Fast") == "true"
self.ui.cpuCheckBox.checked = self._parameterNode.GetParameter("CPU") == "true"
self.ui.useStandardSegmentNamesCheckBox.checked = self._parameterNode.GetParameter("UseStandardSegmentNames") == "true"
self.ui.outputSegmentationSelector.setCurrentNode(self._parameterNode.GetNodeReference("OutputSegmentation"))
# Update buttons states and tooltips
inputVolume = self._parameterNode.GetNodeReference("InputVolume")
if inputVolume:
self.ui.applyButton.toolTip = "Start segmentation"
self.ui.applyButton.enabled = True
else:
self.ui.applyButton.toolTip = "Select input volume"
self.ui.applyButton.enabled = False
if inputVolume:
self.ui.outputSegmentationSelector.baseName = inputVolume.GetName() + " segmentation"
fastModeSupported = self.logic.isFastModeSupportedForTask(task)
self.ui.fastCheckBox.visible = fastModeSupported
self.ui.fastNotAvailableLabel.visible = not fastModeSupported
# All the GUI updates are done
self._updatingGUIFromParameterNode = False
def updateParameterNodeFromGUI(self, caller=None, event=None):
"""
This method is called when the user makes any change in the GUI.
The changes are saved into the parameter node (so that they are restored when the scene is saved and loaded).
"""
if self._parameterNode is None or self._updatingGUIFromParameterNode:
return
wasModified = self._parameterNode.StartModify() # Modify all properties in a single batch
self._parameterNode.SetNodeReferenceID("InputVolume", self.ui.inputVolumeSelector.currentNodeID)
self._parameterNode.SetParameter("Task", self.ui.taskComboBox.currentData)
self._parameterNode.SetParameter("Fast", "true" if self.ui.fastCheckBox.checked else "false")
self._parameterNode.SetParameter("CPU", "true" if self.ui.cpuCheckBox.checked else "false")
self._parameterNode.SetParameter("UseStandardSegmentNames", "true" if self.ui.useStandardSegmentNamesCheckBox.checked else "false")
self._parameterNode.SetNodeReferenceID("OutputSegmentation", self.ui.outputSegmentationSelector.currentNodeID)
self._parameterNode.EndModify(wasModified)
def addLog(self, text):
"""Append text to log window
"""
self.ui.statusLabel.appendPlainText(text)
slicer.app.processEvents() # force update
def onApplyButton(self):
"""
Run processing when user clicks "Apply" button.
"""
self.ui.statusLabel.plainText = ''
import qt
sequenceBrowserNode = slicer.modules.sequences.logic().GetFirstBrowserNodeForProxyNode(self.ui.inputVolumeSelector.currentNode())
if sequenceBrowserNode:
if not slicer.util.confirmYesNoDisplay("The input volume you provided are part of a sequence. Do you want to segment all frames of that sequence?"):
sequenceBrowserNode = None
try:
slicer.app.setOverrideCursor(qt.Qt.WaitCursor)
self.logic.setupPythonRequirements()
slicer.app.restoreOverrideCursor()
except Exception as e:
slicer.app.restoreOverrideCursor()
import traceback
traceback.print_exc()
self.ui.statusLabel.appendPlainText("\nApplication restart required.")
if slicer.util.confirmOkCancelDisplay(
"Application is required to complete installation of required Python packages.\nPress OK to restart.",
"Confirm application restart"
):
slicer.util.restart()
else:
return
with slicer.util.tryWithErrorDisplay("Failed to compute results.", waitCursor=True):
# Create new segmentation node, if not selected yet
if not self.ui.outputSegmentationSelector.currentNode():
self.ui.outputSegmentationSelector.addNode()
self.logic.useStandardSegmentNames = self.ui.useStandardSegmentNamesCheckBox.checked
# Compute output
self.logic.process(self.ui.inputVolumeSelector.currentNode(), self.ui.outputSegmentationSelector.currentNode(),
self.ui.fastCheckBox.checked, self.ui.cpuCheckBox.checked, self.ui.taskComboBox.currentData, interactive = True, sequenceBrowserNode = sequenceBrowserNode)
self.ui.statusLabel.appendPlainText("\nProcessing finished.")
def onPackageInfoUpdate(self):
self.ui.packageInfoTextBrowser.plainText = ''
with slicer.util.tryWithErrorDisplay("Failed to get TotalSegmentator package version information", waitCursor=True):
self.ui.packageInfoTextBrowser.plainText = self.logic.installedTotalSegmentatorPythonPackageInfo().rstrip()
def onPackageUpgrade(self):
with slicer.util.tryWithErrorDisplay("Failed to upgrade TotalSegmentator", waitCursor=True):
self.logic.setupPythonRequirements(upgrade=True)
self.onPackageInfoUpdate()
if not slicer.util.confirmOkCancelDisplay(f"This TotalSegmentator update requires a 3D Slicer restart.","Press OK to restart."):
raise ValueError('Restart was cancelled.')
else:
slicer.util.restart()
def onSetLicense(self):
import qt
licenseText = qt.QInputDialog.getText(slicer.util.mainWindow(), "Set TotalSegmentator license key", "License key:")
success = False
with slicer.util.tryWithErrorDisplay("Failed to set TotalSegmentator license.", waitCursor=True):
if not licenseText:
raise ValueError("License is not specified.")
self.logic.setupPythonRequirements()
self.logic.setLicense(licenseText)
success = True
if success:
slicer.util.infoDisplay("License key is set. You can now use TotalSegmentator tasks that require a license.")
#
# TotalSegmentatorLogic
#
class TotalSegmentatorLogic(ScriptedLoadableModuleLogic):
"""This class should implement all the actual
computation done by your module. The interface
should be such that other python code can import
this class and make use of the functionality without
requiring an instance of the Widget.
Uses ScriptedLoadableModuleLogic base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self):
"""
Called when the logic class is instantiated. Can be used for initializing member variables.
"""
from collections import OrderedDict
ScriptedLoadableModuleLogic.__init__(self)
self.totalSegmentatorPythonPackageDownloadUrl = "https://github.com/wasserth/TotalSegmentator/archive/65d0859c893badaeedaf39600613b73bb0865efe.zip" # tag: 2.2.1
# Custom applications can set custom location for weights.
# For example, it could be set to `sysconfig.get_path('scripts')` to have an independent copy of
# the weights for each Slicer installation. However, setting such custom path would result in extra downloads and
# storage space usage if there were multiple Slicer installations on the same computer.
self.totalSegmentatorWeightsPath = None
self.logCallback = None
self.clearOutputFolder = True
self.useStandardSegmentNames = True
self.pullMaster = False
# List of property type codes that are specified by in the TotalSegmentator terminology.
#
# # Codes are stored as a list of strings containing coding scheme designator and code value of the property type,
# separated by "^" character. For example "SCT^123456".
#
# If property the code is found in this list then the TotalSegmentator terminology will be used,
# otherwise the DICOM terminology will be used. This is necessary because the DICOM terminology
# does not contain all the necessary items and some items are incomplete (e.g., don't have color or 3D Slicer label).
#
self.totalSegmentatorTerminologyPropertyTypes = []
# Map from TotalSegmentator structure name to terminology string.
# Terminology string uses Slicer terminology entry format - see specification at
# https://slicer.readthedocs.io/en/latest/developer_guide/modules/segmentations.html#terminologyentry-tag
self.totalSegmentatorLabelTerminology = {}
# Segmentation tasks specified by TotalSegmentator
# Ideally, this information should be provided by TotalSegmentator itself.
self.tasks = OrderedDict()
# Main
self.tasks['total'] = {'title': 'total', 'supportsFast': True, 'supportsMultiLabel': True}
self.tasks['total_mr'] = {'title': 'total_mr', 'supportsFast': True, 'supportsMultiLabel': True}
self.tasks['body'] = {'title': 'body', 'supportsFast': True}
self.tasks['vertebrae_body'] = {'title': 'vertebrae body'}
self.tasks['lung_vessels'] = {'title': 'lung vessels', 'requiresPreSegmentation': True}
# Trained on reduced data set
self.tasks['cerebral_bleed'] = {'title': 'cerebral bleed', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
self.tasks['hip_implant'] = {'title': 'hip implant', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
self.tasks['coronary_arteries'] = {'title': 'coronary arteries', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
self.tasks['pleural_pericard_effusion'] = {'title': 'pleural and pericardial effusion', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# Requires license
self.tasks['appendicular_bones'] = {'title': 'appendicular bones', 'requiresPreSegmentation': True, 'supportsMultiLabel': True, 'requiresLicense': True}
self.tasks['tissue_types'] = {'title': 'tissue types', 'requiresPreSegmentation': True, 'supportsMultiLabel': True, 'requiresLicense': True}
self.tasks['heartchambers_highres'] = {'title': 'heartchambers highres' , 'requiresPreSegmentation': True, 'supportsMultiLabel': True, 'requiresLicense': True}
self.tasks['face'] = {'title': 'face', 'requiresPreSegmentation': True, 'supportsMultiLabel': True, 'requiresLicense': True}
self.tasks['tissue_types_mr'] = {'title': 'tissue_types_mr', 'supportsMultiLabel': True, 'requiresLicense': True}
self.tasks['face_mr'] = {'title': 'face_mr', 'supportsFast': False, 'supportsMultiLabel': True, 'requiresLicense': True}
# Experimental
# self.tasks['liver_vessels'] = {'title': 'liver vessels', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['aortic_branches'] = {'title': 'aortic branches', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['head'] = {'title': 'head', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['covid'] = {'title': 'covid', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# Testing
# self.tasks['heartchambers_test'] = {'title': 'heartchambers test', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['aortic_branches_test'] = {'title': 'aortic branches test', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['bones_tissue_test'] = {'title': 'bones tissue test', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['test'] = {'title': 'test', 'requiresPreSegmentation': True, 'supportsMultiLabel': True}
# self.tasks['covid'] = {'title': 'pleural and pericardial effusion'}
self.loadTotalSegmentatorLabelTerminology()
def loadTotalSegmentatorLabelTerminology(self):
"""Load label terminology from totalsegmentator_snomed_mapping.csv file.
Terminology entries are either in DICOM or TotalSegmentator "Segmentation category and type".
"""
moduleDir = os.path.dirname(slicer.util.getModule('TotalSegmentator').path)
totalSegmentatorTerminologyMappingFilePath = os.path.join(moduleDir, 'Resources', 'totalsegmentator_snomed_mapping.csv')
terminologiesLogic = slicer.util.getModuleLogic('Terminologies')
totalSegmentatorTerminologyName = "Segmentation category and type - Total Segmentator"
anatomicalStructureCategory = slicer.vtkSlicerTerminologyCategory()
numberOfCategories = terminologiesLogic.GetNumberOfCategoriesInTerminology(totalSegmentatorTerminologyName)
for i in range(numberOfCategories):
terminologiesLogic.GetNthCategoryInTerminology(totalSegmentatorTerminologyName, i, anatomicalStructureCategory)
if anatomicalStructureCategory.GetCodingSchemeDesignator() == 'SCT' and anatomicalStructureCategory.GetCodeValue() == '123037004':
# Found the (123037004, SCT, "Anatomical Structure") category within DICOM master list
break
# Retrieve all property type codes from the TotalSegmentator terminology
self.totalSegmentatorTerminologyPropertyTypes = []
terminologyType = slicer.vtkSlicerTerminologyType()
numberOfTypes = terminologiesLogic.GetNumberOfTypesInTerminologyCategory(totalSegmentatorTerminologyName, anatomicalStructureCategory)
for i in range(numberOfTypes):
if terminologiesLogic.GetNthTypeInTerminologyCategory(totalSegmentatorTerminologyName, anatomicalStructureCategory, i, terminologyType):
self.totalSegmentatorTerminologyPropertyTypes.append(terminologyType.GetCodingSchemeDesignator() + "^" + terminologyType.GetCodeValue())
# Helper function to get code string from CSV file row
def getCodeString(field, columnNames, row):
columnValues = []
for fieldName in ["CodingSchemeDesignator", "CodeValue", "CodeMeaning"]:
columnIndex = columnNames.index(f"{field}.{fieldName}")
try:
columnValue = row[columnIndex]
except IndexError:
# Probably the line in the CSV file was not terminated by multiple commas (,)
columnValue = ''
columnValues.append(columnValue)
return columnValues
import csv
with open(totalSegmentatorTerminologyMappingFilePath, "r") as f:
reader = csv.reader(f)
columnNames = next(reader)
data = {}
# Loop through the rows of the csv file
for row in reader:
# Determine segmentation category (DICOM or TotalSegmentator)
terminologyEntryStrWithoutCategoryName = (
"~"
# Property category: "SCT^123037004^Anatomical Structure" or "SCT^49755003^Morphologically Altered Structure"
+ '^'.join(getCodeString("SegmentedPropertyCategoryCodeSequence", columnNames, row))
+ '~'
# Property type: "SCT^23451007^Adrenal gland", "SCT^367643001^Cyst", ...
+ '^'.join(getCodeString("SegmentedPropertyTypeCodeSequence", columnNames, row))
+ '~'
# Property type modifier: "SCT^7771000^Left", ...
+ '^'.join(getCodeString("SegmentedPropertyTypeModifierCodeSequence", columnNames, row))
+ '~Anatomic codes - DICOM master list'
+ '~'
# Anatomic region (set if category is not anatomical structure): "SCT^64033007^Kidney", ...
+ '^'.join(getCodeString("AnatomicRegionSequence", columnNames, row))
+ '~'
# Anatomic region modifier: "SCT^7771000^Left", ...
+ '^'.join(getCodeString("AnatomicRegionModifierSequence", columnNames, row))
+ '|')
terminologyEntry = slicer.vtkSlicerTerminologyEntry()
terminologyPropertyTypeStr = ( # Example: SCT^23451007
row[columnNames.index("SegmentedPropertyTypeCodeSequence.CodingSchemeDesignator")]
+ "^" + row[columnNames.index("SegmentedPropertyTypeCodeSequence.CodeValue")])
if terminologyPropertyTypeStr in self.totalSegmentatorTerminologyPropertyTypes:
terminologyEntryStr = "Segmentation category and type - Total Segmentator" + terminologyEntryStrWithoutCategoryName
else:
terminologyEntryStr = "Segmentation category and type - DICOM master list" + terminologyEntryStrWithoutCategoryName
# Store the terminology string for this structure
totalSegmentatorStructureName = row[columnNames.index("Structure")] # TotalSegmentator structure name, such as "adrenal_gland_left"
self.totalSegmentatorLabelTerminology[totalSegmentatorStructureName] = terminologyEntryStr
def isFastModeSupportedForTask(self, task):
return (task in self.tasks) and ('supportsFast' in self.tasks[task]) and self.tasks[task]['supportsFast']
def isMultiLabelSupportedForTask(self, task):
return (task in self.tasks) and ('supportsMultiLabel' in self.tasks[task]) and self.tasks[task]['supportsMultiLabel']
def isPreSegmentationRequiredForTask(self, task):
return (task in self.tasks) and ('requiresPreSegmentation' in self.tasks[task]) and self.tasks[task]['requiresPreSegmentation']
def isLicenseRequiredForTask(self, task):
return (task in self.tasks) and ('requiresLicense' in self.tasks[task]) and self.tasks[task]['requiresLicense']
def getSegmentLabelColor(self, terminologyEntryStr):
"""Get segment label and color from terminology"""
def labelColorFromTypeObject(typeObject):
"""typeObject is a terminology type or type modifier"""
label = typeObject.GetSlicerLabel() if typeObject.GetSlicerLabel() else typeObject.GetCodeMeaning()
rgb = typeObject.GetRecommendedDisplayRGBValue()
return label, (rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0)
tlogic = slicer.modules.terminologies.logic()
terminologyEntry = slicer.vtkSlicerTerminologyEntry()
if not tlogic.DeserializeTerminologyEntry(terminologyEntryStr, terminologyEntry):
raise RuntimeError(f"Failed to deserialize terminology string: {terminologyEntryStr}")
numberOfTypes = tlogic.GetNumberOfTypesInTerminologyCategory(terminologyEntry.GetTerminologyContextName(), terminologyEntry.GetCategoryObject())
foundTerminologyEntry = slicer.vtkSlicerTerminologyEntry()
for typeIndex in range(numberOfTypes):
tlogic.GetNthTypeInTerminologyCategory(terminologyEntry.GetTerminologyContextName(), terminologyEntry.GetCategoryObject(), typeIndex, foundTerminologyEntry.GetTypeObject())
if terminologyEntry.GetTypeObject().GetCodingSchemeDesignator() != foundTerminologyEntry.GetTypeObject().GetCodingSchemeDesignator():
continue
if terminologyEntry.GetTypeObject().GetCodeValue() != foundTerminologyEntry.GetTypeObject().GetCodeValue():
continue
if terminologyEntry.GetTypeModifierObject() and terminologyEntry.GetTypeModifierObject().GetCodeValue():
# Type has a modifier, get the color from there
numberOfModifiers = tlogic.GetNumberOfTypeModifiersInTerminologyType(terminologyEntry.GetTerminologyContextName(), terminologyEntry.GetCategoryObject(), terminologyEntry.GetTypeObject())
foundMatchingModifier = False
for modifierIndex in range(numberOfModifiers):
tlogic.GetNthTypeModifierInTerminologyType(terminologyEntry.GetTerminologyContextName(), terminologyEntry.GetCategoryObject(), terminologyEntry.GetTypeObject(),
modifierIndex, foundTerminologyEntry.GetTypeModifierObject())
if terminologyEntry.GetTypeModifierObject().GetCodingSchemeDesignator() != foundTerminologyEntry.GetTypeModifierObject().GetCodingSchemeDesignator():
continue
if terminologyEntry.GetTypeModifierObject().GetCodeValue() != foundTerminologyEntry.GetTypeModifierObject().GetCodeValue():
continue
return labelColorFromTypeObject(foundTerminologyEntry.GetTypeModifierObject())
continue
return labelColorFromTypeObject(foundTerminologyEntry.GetTypeObject())
raise RuntimeError(f"Color was not found for terminology {terminologyEntryStr}")
def log(self, text):
logging.info(text)
if self.logCallback:
self.logCallback(text)
def installedTotalSegmentatorPythonPackageDownloadUrl(self):
"""Get package download URL of the installed TotalSegmentator Python package"""
import importlib.metadata
import json
try:
metadataPath = [p for p in importlib.metadata.files('TotalSegmentator') if 'direct_url.json' in str(p)][0]
with open(metadataPath.locate()) as json_file:
data = json.load(json_file)
return data['url']
except:
# Failed to get version information, probably not installed from download URL
return None
def installedTotalSegmentatorPythonPackageInfo(self):
import shutil
import subprocess
versionInfo = subprocess.check_output([shutil.which('PythonSlicer'), "-m", "pip", "show", "TotalSegmentator"]).decode()
# Get download URL, as the version information does not contain the github hash
downloadUrl = self.installedTotalSegmentatorPythonPackageDownloadUrl()
if downloadUrl:
versionInfo += "Download URL: " + downloadUrl
return versionInfo
def simpleITKPythonPackageVersion(self):
"""Utility function to get version of currently installed SimpleITK.
Currently not used, but it can be useful for diagnostic purposes.
"""
import shutil
import subprocess
versionInfo = subprocess.check_output([shutil.which('PythonSlicer'), "-m", "pip", "show", "SimpleITK"]).decode()
# versionInfo looks something like this:
#
# Name: SimpleITK
# Version: 2.2.0rc2.dev368
# Summary: SimpleITK is a simplified interface to the Insight Toolkit (ITK) for image registration and segmentation
# ...
#
# Get version string (second half of the second line):
version = versionInfo.split('\n')[1].split(' ')[1].strip()
return version
def pipInstallSelective(self, packageToInstall, installCommand, packagesToSkip):
"""Installs a Python package, skipping a list of packages.
Return the list of skipped requirements (package name with version requirement).
"""
slicer.util.pip_install(f"{installCommand} --no-deps")
skippedRequirements = [] # list of all missed packages and their version
# Get path to site-packages\nnunetv2-2.2.dist-info\METADATA
import importlib.metadata
metadataPath = [p for p in importlib.metadata.files(packageToInstall) if 'METADATA' in str(p)][0]
metadataPath.locate()
# Remove line: `Requires-Dist: SimpleITK (==2.0.2)`
# User Latin-1 encoding to read the file, as it may contain non-ASCII characters and not necessarily in UTF-8 encoding.
filteredMetadata = ""
with open(metadataPath.locate(), "r+", encoding="latin1") as file:
for line in file:
skipThisPackage = False
requirementPrefix = 'Requires-Dist: '
if line.startswith(requirementPrefix):
for packageToSkip in packagesToSkip:
if packageToSkip in line:
skipThisPackage = True
break
if skipThisPackage:
# skip SimpleITK requirement
skippedRequirements.append(line.removeprefix(requirementPrefix))
continue
filteredMetadata += line
# Update file content with filtered result
file.seek(0)
file.write(filteredMetadata)
file.truncate()
# Install all dependencies but the ones listed in packagesToSkip
import importlib.metadata
requirements = importlib.metadata.requires(packageToInstall)
for requirement in requirements:
skipThisPackage = False
for packageToSkip in packagesToSkip:
if requirement.startswith(packageToSkip):
# Do not install
skipThisPackage = True
break
match = False
if not match:
# ruff ; extra == 'dev' -> rewrite to: ruff[extra]
match = re.match(r"([\S]+)[\s]*; extra == '([^']+)'", requirement)
if match:
requirement = f"{match.group(1)}[{match.group(2)}]"
if not match:
# nibabel >=2.3.0 -> rewrite to: nibabel>=2.3.0
match = re.match("([\S]+)[\s](.+)", requirement)
if match:
requirement = f"{match.group(1)}{match.group(2)}"
if skipThisPackage:
self.log(f'- Skip {requirement}')
else:
self.log(f'- Installing {requirement}...')
slicer.util.pip_install(requirement)
return skippedRequirements
def setupPythonRequirements(self, upgrade=False):
import importlib.metadata
import importlib.util
import packaging
# TotalSegmentator requires this, yet it is not listed among its dependencies
try:
import pandas
except ModuleNotFoundError as e:
slicer.util.pip_install("pandas")
# pillow version that is installed in Slicer (10.1.0) is too new,
# it is incompatible with several TotalSegmentator dependencies.
# Attempt to uninstall and install an older version before any of the packages import it.
needToInstallPillow = True
try:
if packaging.version.parse(importlib.metadata.version("pillow")) < packaging.version.parse("10.1"):
# A suitable pillow version is already installed
needToInstallPillow = False
except Exception as e:
pass
if needToInstallPillow:
slicer.util.pip_install("pillow<10.1")
# These packages come preinstalled with Slicer and should remain unchanged
packagesToSkip = [
'SimpleITK', # Slicer's SimpleITK uses a special IO class, which should not be replaced
'torch', # needs special installation using SlicerPyTorch
'requests', # TotalSegmentator would want to force a specific version of requests, which would require a restart of Slicer and it is unnecessary
]
# Install PyTorch
try:
import PyTorchUtils
except ModuleNotFoundError as e:
raise RuntimeError("This module requires PyTorch extension. Install it from the Extensions Manager.")
minimumTorchVersion = "1.12"
torchLogic = PyTorchUtils.PyTorchUtilsLogic()
if not torchLogic.torchInstalled():
self.log('PyTorch Python package is required. Installing... (it may take several minutes)')
torch = torchLogic.installTorch(askConfirmation=True, torchVersionRequirement = f">={minimumTorchVersion}")
if torch is None:
raise ValueError('PyTorch extension needs to be installed to use this module.')
else:
# torch is installed, check version
from packaging import version
if version.parse(torchLogic.torch.__version__) < version.parse(minimumTorchVersion):
raise ValueError(f'PyTorch version {torchLogic.torch.__version__} is not compatible with this module.'
+ f' Minimum required version is {minimumTorchVersion}. You can use "PyTorch Util" module to install PyTorch'
+ f' with version requirement set to: >={minimumTorchVersion}')
# Install TotalSegmentator with selected dependencies only
# (it would replace Slicer's "requests")
needToInstallSegmenter = False
try:
import totalsegmentator
if not upgrade:
# Check if we need to update TotalSegmentator Python package version
downloadUrl = self.installedTotalSegmentatorPythonPackageDownloadUrl()
if downloadUrl and (downloadUrl != self.totalSegmentatorPythonPackageDownloadUrl):
# TotalSegmentator have been already installed from GitHub, from a different URL that this module needs
if not slicer.util.confirmOkCancelDisplay(
f"This module requires TotalSegmentator Python package update.",
detailedText=f"Currently installed: {downloadUrl}\n\nRequired: {self.totalSegmentatorPythonPackageDownloadUrl}"):
raise ValueError('TotalSegmentator update was cancelled.')
upgrade = True
except ModuleNotFoundError as e:
needToInstallSegmenter = True
if needToInstallSegmenter or upgrade:
self.log(f'TotalSegmentator Python package is required. Installing it from {self.totalSegmentatorPythonPackageDownloadUrl}... (it may take several minutes)')
if upgrade:
# TotalSegmentator version information is usually not updated with each git revision, therefore we must uninstall it to force the upgrade
slicer.util.pip_uninstall("TotalSegmentator")
# Update TotalSegmentator and all its dependencies
skippedRequirements = self.pipInstallSelective(
"TotalSegmentator",
self.totalSegmentatorPythonPackageDownloadUrl + (" --upgrade" if upgrade else ""),
packagesToSkip + ["nnunetv2"])
# Install nnunetv2 with selected dependencies only
# (it would replace Slicer's "SimpleITK")
try:
nnunetRequirement = next(requirement for requirement in skippedRequirements if requirement.startswith('nnunetv2'))
except StopIteration:
# nnunetv2 requirement was not found in TotalSegmentator - this must be an error, so let's report it
raise ValueError("nnunetv2 requirement was not found in TotalSegmentator")
# Remove spaces and parentheses from version requirement (convert from "nnunetv2 (==2.1)" to "nnunetv2==2.1")
nnunetRequirement = re.sub('[ \(\)]', '', nnunetRequirement)
self.log(f'nnunetv2 Python package is required. Installing {nnunetRequirement} ...')
self.pipInstallSelective('nnunetv2', nnunetRequirement, packagesToSkip)
# Workaround: fix incompatibility of dynamic_network_architectures==0.4 with totalsegmentator==2.0.5.
# Revert to the last working version: dynamic_network_architectures==0.2
from packaging import version
if version.parse(importlib.metadata.version("dynamic_network_architectures")) == version.parse("0.4"):
self.log(f'dynamic_network_architectures package version is incompatible. Installing working version...')
slicer.util.pip_install("dynamic_network_architectures==0.2.0")
self.log('TotalSegmentator installation completed successfully.')
def setDefaultParameters(self, parameterNode):
"""
Initialize parameter node with default settings.
"""
if not parameterNode.GetParameter("Fast"):
parameterNode.SetParameter("Fast", "True")
if not parameterNode.GetParameter("Task"):
parameterNode.SetParameter("Task", "total")
if not parameterNode.GetParameter("UseStandardSegmentNames"):
parameterNode.SetParameter("UseStandardSegmentNames", "true")
def logProcessOutput(self, proc, returnOutput=False):
# Wait for the process to end and forward output to the log
output = ""
from subprocess import CalledProcessError
while True:
try:
line = proc.stdout.readline()
if not line:
break
if returnOutput:
output += line
self.log(line.rstrip())
except UnicodeDecodeError as e:
# Code page conversion happens because `universal_newlines=True` sets process output to text mode,
# and it fails because probably system locale is not UTF8. We just ignore the error and discard the string,
# as we only guarantee correct behavior if an UTF8 locale is used.
pass
proc.wait()
retcode = proc.returncode
if retcode != 0:
raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
return output if returnOutput else None
def check_zip_extension(self, file_path):
_, ext = os.path.splitext(file_path)
if ext.lower() != '.zip':
raise ValueError(f"The selected file '{file_path}' is not a .zip file!")
@staticmethod
def executableName(name):
return name + ".exe" if os.name == "nt" else name
def setLicense(self, licenseStr):
"""
Import weights.
Weights are provided in ZIP format.
This function can be used without GUI widget.
"""
# Get totalseg_import_weights command
# totalseg_import_weights (.py file, without extension) is installed in Python Scripts folder
if not licenseStr:
raise ValueError(f"The license string is empty.")
self.log('Setting license...')
# Get Python executable path
import shutil
pythonSlicerExecutablePath = shutil.which('PythonSlicer')
if not pythonSlicerExecutablePath:
raise RuntimeError("Python was not found")
# Get arguments
import sysconfig
totalSegmentatorLicenseToolExecutablePath = os.path.join(sysconfig.get_path('scripts'), TotalSegmentatorLogic.executableName("totalseg_set_license"))
cmd = [pythonSlicerExecutablePath, totalSegmentatorLicenseToolExecutablePath, "-l", licenseStr]
# Launch command
logging.debug(f"Launch TotalSegmentator license tool: {cmd}")
proc = slicer.util.launchConsoleProcess(cmd)
licenseToolOutput = self.logProcessOutput(proc, returnOutput=True)
if "ERROR: Invalid license number" in licenseToolOutput:
raise ValueError('Invalid license number. Please check your license number or contact support.')
self.log('License has been successfully set.')
if slicer.util.confirmOkCancelDisplay(f"This license update requires a 3D Slicer restart.","Press OK to restart."):
slicer.util.restart()
else:
raise ValueError('Restart was cancelled.')
def process(self, inputVolume, outputSegmentation, fast=True, cpu=False, task=None, subset=None, interactive=False, sequenceBrowserNode=None):
"""
Run the processing algorithm on a volume or a sequence of volumes.
Can be used without GUI widget.
:param inputVolume: volume to be thresholded
:param outputVolume: thresholding result
:param fast: faster and less accurate output
:param task: one of self.tasks, default is "total"
:param subset: a list of structures (TotalSegmentator classe names https://github.com/wasserth/TotalSegmentator#class-detailsTotalSegmentator) to segment.
Default is None, which means that all available structures will be segmented."
:param interactive: set to True to enable warning popups to be shown to users
:param sequenceBrowserNode: if specified then all frames of the inputVolume sequence will be segmented
"""
if not inputVolume:
raise ValueError("Input or output volume is invalid")
if task == None and not subset:
task = "total"
import time
startTime = time.time()
self.log('Processing started')
if self.totalSegmentatorWeightsPath:
os.environ["TOTALSEG_WEIGHTS_PATH"] = self.totalSegmentatorWeightsPath
# Create new empty folder
tempFolder = slicer.util.tempDirectory()
inputFile = tempFolder+"/total-segmentator-input.nii"
outputSegmentationFolder = tempFolder + "/segmentation"
# print (outputSegmentationFolder)
outputSegmentationFile = tempFolder + "/segmentation.nii"
# Recommend the user to switch to fast mode if no GPU or not enough memory is available
import torch
cuda = torch.cuda if torch.backends.cuda.is_built() and torch.cuda.is_available() else None
if not fast and not cuda and interactive:
import ctk
import qt
mbox = ctk.ctkMessageBox(slicer.util.mainWindow())
mbox.text = "No GPU is detected. Switch to 'fast' mode to get low-resolution result in a few minutes or compute full-resolution result which may take 5 to 50 minutes (depending on computer configuration)?"
mbox.addButton("Fast (~2 minutes)", qt.QMessageBox.AcceptRole)
mbox.addButton("Full-resolution (~5 to 50 minutes)", qt.QMessageBox.RejectRole)
# Windows 10 peek feature in taskbar shows all hidden but not destroyed windows
# (after creating and closing a messagebox, hovering over the mouse on Slicer icon, moving up the
# mouse to the peek thumbnail would show it again).
mbox.deleteLater()
fast = (mbox.exec_() == qt.QMessageBox.AcceptRole)
if not fast and cuda and cuda.get_device_properties(cuda.current_device()).total_memory < 7e9 and interactive:
if slicer.util.confirmYesNoDisplay("You have less than 7 GB of GPU memory available. Enable 'fast' mode to ensure segmentation can be completed successfully?"):
fast = True
# Get TotalSegmentator launcher command
# TotalSegmentator (.py file, without extension) is installed in Python Scripts folder
import sysconfig
totalSegmentatorExecutablePath = os.path.join(sysconfig.get_path('scripts'), TotalSegmentatorLogic.executableName("TotalSegmentator"))
# Get Python executable path
import shutil
pythonSlicerExecutablePath = shutil.which('PythonSlicer')
if not pythonSlicerExecutablePath:
raise RuntimeError("Python was not found")
totalSegmentatorCommand = [ pythonSlicerExecutablePath, totalSegmentatorExecutablePath]
inputVolumeSequence = None
if sequenceBrowserNode:
inputVolumeSequence = sequenceBrowserNode.GetSequenceNode(inputVolume)
if inputVolumeSequence is not None:
# If the volume already has a sequence in the current browser node then use that
segmentationSequence = sequenceBrowserNode.GetSequenceNode(outputSegmentation)
if not segmentationSequence:
segmentationSequence = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSequenceNode", outputSegmentation.GetName())
sequenceBrowserNode.AddProxyNode(outputSegmentation, segmentationSequence, False)
selectedItemNumber = sequenceBrowserNode.GetSelectedItemNumber()
sequenceBrowserNode.PlaybackActiveOff()
sequenceBrowserNode.SelectFirstItem()
sequenceBrowserNode.SetRecording(segmentationSequence, True)
sequenceBrowserNode.SetSaveChanges(segmentationSequence, True)
numberOfItems = sequenceBrowserNode.GetNumberOfItems()
for i in range(numberOfItems):
self.log(f"Segmenting item {i+1}/{numberOfItems} of sequence")
self.processVolume(inputFile, inputVolume,
outputSegmentationFolder, outputSegmentation, outputSegmentationFile,
task, subset, cpu, totalSegmentatorCommand, fast)
sequenceBrowserNode.SelectNextItem()
sequenceBrowserNode.SetSelectedItemNumber(selectedItemNumber)
else:
# Segment a single volume
self.processVolume(inputFile, inputVolume,
outputSegmentationFolder, outputSegmentation, outputSegmentationFile,
task, subset, cpu, totalSegmentatorCommand, fast)
stopTime = time.time()
self.log(f"Processing completed in {stopTime-startTime:.2f} seconds")
if self.clearOutputFolder:
self.log("Cleaning up temporary folder...")
if os.path.isdir(tempFolder):
shutil.rmtree(tempFolder)
else:
self.log(f"Not cleaning up temporary folder: {tempFolder}")
def processVolume(self, inputFile, inputVolume, outputSegmentationFolder, outputSegmentation, outputSegmentationFile, task, subset, cpu, totalSegmentatorCommand, fast):
"""Segment a single volume
"""
# Write input volume to file
# TotalSegmentator requires NIFTI
self.log(f"Writing input file to {inputFile}")
volumeStorageNode = slicer.mrmlScene.CreateNodeByClass("vtkMRMLVolumeArchetypeStorageNode")
volumeStorageNode.SetFileName(inputFile)
volumeStorageNode.UseCompressionOff()
volumeStorageNode.WriteData(inputVolume)
volumeStorageNode.UnRegister(None)
# Get options
options = ["-i", inputFile, "-o", outputSegmentationFolder]
# Launch TotalSegmentator in fast mode to get initial segmentation, if needed
#options.extend(["--nr_thr_saving", "1"])
#options.append("--force_split")