Repr should not load the image
See original GitHub issue🚀 Feature
Changing the way __repr__
gives an representation of the image.
Motivation
Debuggers call __repr__
to give a preview of the python object.
Pitch
When having torchio Images that have not been loaded and the debugger calls __repr__
to give a preview of the python object, due to the attributes embedded in the returned string, __repr__
will always load the image.
That can, however, alter the program flow in ways that make it difficult to debug as the usual python program would not load the image at that point and it wouldn’t be necessary to do so, if it weren’t for the debugger.
The proposed change therefore is to have a __repr__
like this:
def __repr__(self) -> str:
if self._loaded:
... # current repr logic since we can safely access all attributes of the image if it was loaded.
else:
return f"{self.__class__.__name__}(path: {self.path})"
I think in such cases it is better to have the image path instead of just loading the image just for this preview
Alternatives We could keep it as is and just state this somewhere in the docs. I am afraid, however, that this won’t be helpful for most people since it’s quite involved with debugger internals (I had to dig a bit through debugger sources on my own).
Additional context
cc @fepegar
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Or rather “this functionality is not supported for DICOM files”. I think it should work for things like NRRD too. Why in the world would someone use DICOM in a research setting anyway? 😄
Yes but you cannot do that for DICOM data. So it is kind of inconsistent 😃
That is indeed very strange. I really have no idea on why it behaves like this.