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
Stop using a tempfile for data reorientation #3727
Stop using a tempfile for data reorientation #3727
Conversation
Without doing this, on Windows machines, the following error is thrown: FileExistsError: [WinError 183] Cannot create a file when that file already exists I don't think the intermediate file is used for anything, so I think this is OK to do.
Codecov Report
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your question, "Why can't we just reorient in-place?"
Looking at Image.change_orientation()
, it returns a new Image object with an empty path field (as documented):
spinalcordtoolbox/spinalcordtoolbox/image.py
Lines 1061 to 1063 in 84060e2
if im_dst is None: | |
im_dst = im_src.copy() | |
im_dst._path = None |
So I think you could get rid of the rename, but you do have to specify the filename when saving the re-oriented copy.
Alternatively, the docstring for Image.change_orientation
explicitly suggests setting im_dst
to the input image for in-place operation. From skimming the code, I don't see any obvious reason why this would fail, so that might be the best option.
(As to "why is the code written this way", this business about renaming the file dates back to the initial commit from 5 years ago, so I don't know.)
It looks like there's two spinalcordtoolbox/spinalcordtoolbox/image.py Lines 425 to 437 in 84060e2
And, it looks like the method supplies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks so clean! Thanks 😄
Checklist
GitHub
PR contents
Description
This PR deletes an unused temporary file in order to avoid overwriting the same file repeatedly. Without doing this, on Windows machines, the following error is thrown:
I don't think the intermediate file is used for anything, so I think this is OK to do.
I wonder if using a temporary file is even necessary, though? Why can't we just reorient in-place? 🤔
Linked issues
Fixes #3714.