Paths under Slicer python

Why does the first segmentation load fail, even though the file is clearly found? Do files always have to be referred with their full path?

>>> os.getcwd()
'/Users/amaga'
>>> os.path.isfile("MD/"+segmentation)
True
>>> slicer.util.loadSegmentation("MD/"+segmentation)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Applications/Slicer.app/Contents/bin/Python/slicer/util.py", line 903, in loadSegmentation
    return loadNodeFromFile(filename, "SegmentationFile", properties, returnNode)
  File "/Applications/Slicer.app/Contents/bin/Python/slicer/util.py", line 734, in loadNodeFromFile
    raise RuntimeError(errorMessage)
RuntimeError: Failed to load node from file: MD/IMPC_sample_data.seg.nrrd
Error: Loading MD/IMPC_sample_data.seg.nrrd -  load failed.

[VTK] vtkMRMLSegmentationStorageNode::ReadDataInternal: Segmentation file '/Users/amaga/Documents/MD/IMPC_sample_data.seg.nrrd' is not found while trying to read node (vtkMRMLSegmentationStorageNode5).
[VTK] vtkMRMLStorageNode::ReadData: Failed to read node IMPC_sample_data_11 (vtkMRMLSegmentationNode5) from filename='MD/IMPC_sample_data.seg.nrrd'
[VTK] LoadSegmentationFromFile: Error reading MD/IMPC_sample_data.seg.nrrd
>>> slicer.util.loadSegmentation("/Users/amaga/MD/IMPC_sample_data.seg.nrrd")
<MRMLCorePython.vtkMRMLSegmentationNode(0x7fcd19ef9f80) at 0x170fcebe0>

I will rephrase my question, now I see why the first load is failing. Because the path is incorrect.

So, why is the command loadSegmentation("MD/"+segmentation) expanding the path to

/Users/amaga/Documents/MD/IMPC_sample_data.seg.nrrd'

As shown in the first line, working directory is /Users/amaga. Where is the Documents coming from?

Probably that’s where your scene is saved. There’s logic to try to find relative paths with respect to the scene file if they can’t be found. (This code is 16-17 years old, so maybe it’s time to revisit that logic - it’s used in all scene loading so we’d need to be careful. Maybe changing the slicer.util behavior to create absolute paths would be cleaner).

This example was with a blank Slicer session, no scene was saved. But for my Slicer. yes default scene location is in indeed /Users/amaga/Documents as shown in the Application Preferences.

So these loadSegmentation loadVolume functions always prefix the scene path, if a relative path provided? I am trying to understand how to work with relative paths with respect to a cloned repo.

I think the best bet in your code would be to construct the absolute path before calling loadSegmentation.

It is not so much about how to make it work then why this is happening. It is a valid file path, and error is unexpected (and I don’t think this is documented anywhere). I am writing a tutorial so it is important to explain why this is happening, which otherwise looks like weird behavior.

Is it because the working directory inside the Slicer mrml is different than working directory in the python, or the cwd function inside the python console has no bearing on the path of the scene?

To further the confusion, loadVolume is perfectly happy with relative path,

>>> slicer.util.loadVolume("MD/"+segmentation)
<MRMLCorePython.vtkMRMLScalarVolumeNode(0x7fe6a3408d80) at 0x1754e6d60>

while loadSegmentation fails:

>>> slicer.util.loadSegmentation("MD/"+segmentation)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Applications/Slicer.app/Contents/bin/Python/slicer/util.py", line 903, in loadSegmentation
    return loadNodeFromFile(filename, "SegmentationFile", properties, returnNode)
  File "/Applications/Slicer.app/Contents/bin/Python/slicer/util.py", line 734, in loadNodeFromFile
    raise RuntimeError(errorMessage)
RuntimeError: Failed to load node from file: MD/IMPC_sample_data.seg.nrrd
Error: Loading MD/IMPC_sample_data.seg.nrrd -  load failed.

[VTK] vtkMRMLSegmentationStorageNode::ReadDataInternal: Segmentation file '/Users/amaga/Documents/MD/IMPC_sample_data.seg.nrrd' is not found while trying to read node (vtkMRMLSegmentationStorageNode1).
[VTK] vtkMRMLStorageNode::ReadData: Failed to read node IMPC_sample_data_3 (vtkMRMLSegmentationNode1) from filename='MD/IMPC_sample_data.seg.nrrd'
[VTK] LoadSegmentationFromFile: Error reading MD/IMPC_sample_data.seg.nrrd

It’s kind of deep in the logic of the scene loading so someone would need to spend some time tracking it down. I still think generating absolute paths is best practice anyway. I can’t think of any time when I have used relative paths or relied on the working directory to get the right data. Maybe the easy thing is to add a warning if a non-absolute path is passed. The docs say ‘full path’ but that is ambiguous and maybe it should say ‘absolute’.

https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.loadNodeFromFile