SitkUtils missing import os
Summary
In Slicer 5.7 seems that sitkUtils.py file has been changed.
Functions PullFromSlicer and PushToSlicer requires os.remove, but the import os is missing at the beginning of the file, causing an Name error:
The error is raised at line 34 and line 56 in sitkUtil.py.
It can be easily solved by adding an import os at the beginning of the file.
Steps to reproduce
Detected with the following procedure:
- Create a custom selector for "vtkMRMLLabelMapVolumeNode" in a scripted python module
- get the labelmap from a custom callback, retrieving the image through
sitkUtils.PullFromSlicerfunction
Environment
- Slicer version: Slicer-5.7.0-2024-06-17
- Operating system: Windows 11 Pro 10.0.22631
Is the download suggestion above legitimate? Beware of malware.
To @Rufio2790 please specify what version of 5.7 and provide a short script to reproduce the issue.
Hi @pieper,
I see the comment you mentioned has already been deleted. It wasn't mine or event related to this, I guess something in github or some malware. probably it was automatically uploaded.
Here a small script that can be copypaste as a module named "temp":
import slicer
from slicer.i18n import tr as _
from slicer.i18n import translate
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin
import qt
import sitkUtils
#
# temp
#
class temp(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = _("temp") # TODO: make this more human readable by adding spaces
# TODO: set categories (folders where the module shows up in the module selector)
self.parent.categories = [translate("qSlicerAbstractCoreModule", "Examples")]
self.parent.dependencies = [] # TODO: add here list of module names that this module requires
self.parent.contributors = ["John Doe (AnyWare Corp.)"] # TODO: replace with "Firstname Lastname (Organization)"
# TODO: update with short description of the module and a link to online module documentation
# _() function marks text as translatable to other languages
self.parent.helpText = _("""
This is an example of scripted loadable module bundled in an extension.
See more information in <a href="https://github.com/organization/projectname#temp">module documentation</a>.
""")
# TODO: replace with organization, grant and thanks
self.parent.acknowledgementText = _("""
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc., Andras Lasso, PerkLab,
and Steve Pieper, Isomics, Inc. and was partially funded by NIH grant 3P41RR013218-12S1.
""")
# Additional initialization step after application startup is complete
slicer.app.connect("startupCompleted()", registerSampleData)
#
# tempWidget
#
def create_slicer_node_combo_box(
data_type_string,
tool_tip_string=None,
event_string=None,
callback=None,
mrmrl_scene=slicer.mrmlScene,
add_enable=True,
remove_enable=True,
none_enable=False,
show_hidden=False,
show_child_node_types=False,
):
"""
Combobox used specifically to select Slicer nodes
"""
combo_box = slicer.qMRMLNodeComboBox()
if not isinstance(data_type_string, list):
data_type_string = [data_type_string]
combo_box.nodeTypes = data_type_string
combo_box.selectNodeUponCreation = True
combo_box.addEnabled = add_enable
combo_box.removeEnabled = remove_enable
combo_box.noneEnabled = none_enable
combo_box.showHidden = show_hidden
combo_box.showChildNodeTypes = show_child_node_types
combo_box.setMRMLScene(mrmrl_scene)
combo_box.setToolTip(tool_tip_string)
if event_string is not None:
combo_box.connect(event_string, callback)
return combo_box
class tempWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent=None) -> 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._parameterNodeGuiTag = None
def setup(self) -> None:
"""Called when the user opens the module the first time and the widget is initialized."""
ScriptedLoadableModuleWidget.setup(self)
self.combobox = create_slicer_node_combo_box("vtkMRMLScalarVolumeNode")
self.button = qt.QPushButton("Get the volume selected")
self.button.connect("clicked(bool)", self.on_button_clicked)
self.button.enabled = True
self.layout.addWidget(self.combobox)
self.layout.addWidget(self.button)
self.layout.addStretch()
def on_button_clicked(self):
print("Getting the selected volume")
print(self.combobox.currentNode())
if self.combobox.currentNode() is None:
slicer.util.errorDisplay("No volume selected")
else:
try:
volume = sitkUtils.PullVolumeFromSlicer(self.combobox.currentNode())
slicer.util.infoDisplay("Volume name: ", volume.GetName())
except Exception as e:
print(f"An error occurred: {e}")
slicer.util.errorDisplay("Name error in slicer sitkUtils - check the python console")
Here the slicer build version I used: 5.7.0-2024-06-17 r32902 / 6101390
Best
Thanks @Rufio2790 , yes, there must have been some malware posts that have since been deleted.
If adding import os to the sitk utils is what's required maybe you could do a PR to add it?
Ok, I'll check how to do a MR in the next days.