Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PythonSlicer problem with vtk and ReadData #6484

Closed
fbordignon opened this issue Jul 28, 2022 · 8 comments
Closed

PythonSlicer problem with vtk and ReadData #6484

fbordignon opened this issue Jul 28, 2022 · 8 comments
Labels
Type: Bug Something isn't working correctly

Comments

@fbordignon
Copy link
Contributor

The code block below reports an error when run with PythonSlicer.exe on Windows 10

import slicer,mrml
A = slicer.vtkMRMLNRRDStorageNode()
A.SetFileName("foo")
n = mrml.vtkMRMLScalarVolumeNode()
A.ReadData(n)

TypeError: ReadData argument 1: method requires a VTK object

This occurred on Slicer 5 nightly build 31096

I suspect this issue is also affecting SlicerCAT built custom apps CLIs, since my custom app clis are not being able to ReadData() as well.

The code block above used to work on Slicer 4.

Another issue:

import mrml
mrml.vtkMRMLScalarVolumeNode().GetClassName()

AttributeError: 'MRMLCore.vtkMRMLScalarVolumeNode' object has no attribute 'GetClassName'

@fbordignon fbordignon added the Type: Bug Something isn't working correctly label Jul 28, 2022
@jamesobutler
Copy link
Contributor

jamesobutler commented Jul 28, 2022

Using Slicer 5.1.0-2022-07-26 (Revision 30196) and also latest Slicer stable 5.0.3 (Revision 30893) I'm not able to replicate these issues when using the python interactor from inside Slicer. I downloaded both of these versions from https://download.slicer.org/.

Notably this was on Windows 11, but I don't see why Windows 10 would cause this issue for you, but likely due to the environment of PythonSlicer.exe.

image

@jamesobutler
Copy link
Contributor

https://discourse.slicer.org/t/how-to-import-slicer-in-python-cli/8083/3?u=jamesobutler

Generally, CLIs are not intended to have direct access to the Slicer scene / infrastructure. They are meant for non-interactive data processing with non-slicer libraries. If you want to work within the Slicer environment, you should keep the code within a python scripted module.

@fbordignon
Copy link
Contributor Author

fbordignon commented Jul 28, 2022

I am sorry if I was not very clear the first time. The problem occurs when running this commands using PythonSlicer.exe console ran independently from slicer.

image

@pieper
Copy link
Member

pieper commented Jul 28, 2022

Importing the python-wrapped VTK C++ code can be tricky - if they aren't imported in the correct order then the methods may not be available and sometimes you need to import the libraries directly. This probably hasn't been looked at before for the PythonSlicer environment with the new VTK. I'm guessing it is solvable with a script to manage the import process or some change in the way they are packaged.

@fbordignon
Copy link
Contributor Author

Thanks, @pieper your comment lead me to try different import orders and it seems that it works if I import vtk first and everything in the same line:

import vtk, slicer, slicer.util, mrml
A = slicer.vtkMRMLNRRDStorageNode()
A.SetFileName("foo")
n = mrml.vtkMRMLScalarVolumeNode()
A.ReadData(n)

The error now is relative to the incorrect filename "foo" but if I select a valid file, everything seems to work and ReadData returns 1. GetClassName also works

@lassoan
Copy link
Contributor

lassoan commented Jul 28, 2022

My understanding is that the very first time you instantiate a Python-wrapped VTK class, the wrapper traverses up the class hierarchy to determine the base classes of the object. This is done only once, at the first instantiation. If at that point you haven't imported all the base classes then you'll never have full hierarchy information for that instantiated class.

Therefore, you need to import vtk before you instantiate VTK-based classes.

I've checked and calling import vtk before your code snippet fixes the TypeError: ReadData argument 1: method requires a VTK object error.

@fbordignon
Copy link
Contributor Author

Thanks @lassoan, it is indeed the import order like I've described above. Since it was working on Slicer 4 without importing vtk, I was thinking it could be a bug. Anyways, this could serve as a reference for people that bumps into the same issue.
Regards, Fernando.

@lassoan
Copy link
Contributor

lassoan commented Jul 28, 2022

Since many parts of Slicer (all module logics, PythonQt, etc.) are loaded by the Slicer application, currently it is quite limited what can be done in a Python environment that is not created by the Slicer application. That's why we don't put too much effort into documenting this.

But in the long term we want to make more and more parts of Slicer usable without the Slicer application (starting with MRML, DICOM, module logic classes). So, it is good that you explore this and we would be open to making improvements that helps moving in this direction.

jhlegarreta added a commit to jhlegarreta/SlicerDMRI that referenced this issue Nov 4, 2023
Fix DWI shell extraction module import sorting: follow the class
hierarchy and library dependencies.

Fixes:
```
```

raised for example in.

Related discussions:
Slicer/Slicer#6484
https://discourse.slicer.org/t/extension-tests-fail-on-cdash-why/26571/13
jhlegarreta added a commit to jhlegarreta/SlicerDMRI that referenced this issue Nov 4, 2023
Fix DWI shell extraction module import sorting: sort the imported
packages following the class hierarchy and library dependencies.

Fixes:
```
loading:  /SlicerDMRI/SlicerDMRI/Testdata/3x3x3_13_b1000_b3000.nrrd
Traceback (most recent call last):
  File "/SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py",
  line 207, in <module>
    main()
  File "/SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py",
  line 127, in main
    sn.ReadData(node_in)
TypeError: ReadData argument 1: method requires a VTK object
```

raised for example at:
https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/6722459285/job/18270424711?pr=191#step:8:2631

Related discussion:
Slicer/Slicer#6484
jhlegarreta added a commit to jhlegarreta/SlicerDMRI that referenced this issue Nov 4, 2023
Fix DWI shell extraction module import sorting: sort the imported
packages following the class hierarchy and library dependencies.

Fixes:
```
loading:  SlicerDMRI/SlicerDMRI/Testdata/3x3x3_13_b1000_b3000.nrrd
Traceback (most recent call last):
  File "SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py",
  line 207, in <module>
    main()
  File "SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py",
  line 127, in main
    sn.ReadData(node_in)
TypeError: ReadData argument 1: method requires a VTK object
```

raised for example at:
https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/6722459285/job/18270424711?pr=191#step:8:2631

Related discussion:
Slicer/Slicer#6484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working correctly
Development

No branches or pull requests

4 participants