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

Stop using a tempfile for data reorientation #3727

Merged

Conversation

joshuacwnewton
Copy link
Member

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:

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.

I wonder if using a temporary file is even necessary, though? Why can't we just reorient in-place? 🤔

Linked issues

Fixes #3714.

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.
@joshuacwnewton joshuacwnewton added bug category: fixes an error in the code sct_analyze_texture context: labels Mar 7, 2022
@joshuacwnewton joshuacwnewton added this to the 5.6 milestone Mar 7, 2022
@joshuacwnewton joshuacwnewton self-assigned this Mar 7, 2022
@codecov
Copy link

codecov bot commented Mar 7, 2022

Codecov Report

Merging #3727 (6dfd3ba) into master (f26377c) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Flag Coverage Δ
api-tests 22.29% <0.00%> (+<0.01%) ⬆️
cli-tests 58.73% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
spinalcordtoolbox/scripts/sct_analyze_texture.py 82.38% <100.00%> (-0.10%) ⬇️

Copy link
Member

@mguaypaq mguaypaq left a 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):

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.)

@joshuacwnewton
Copy link
Member Author

joshuacwnewton commented Mar 22, 2022

Looking at Image.change_orientation(), it returns a new Image object with an empty path field (as documented):

It looks like there's two change_orientation functions: The image.py function, as well as the Image class method, which calls the aforementioned function:

def change_orientation(self, orientation, inverse=False):
"""
Change orientation on image (in-place).
:param orientation: orientation string (SCT "from" convention)
:param inverse: if you think backwards, use this to specify that you actually\
want to transform *from* the specified orientation, not *to*\
it.
"""
change_orientation(self, orientation, self, inverse=inverse)
return self

And, it looks like the method supplies self for im_dst. So, I think 73c7880 should be sufficient?

@joshuacwnewton joshuacwnewton changed the title Remove tmp file in between reorientations to avoid overwriting the same file Stop using a tempfile for data reorientation Mar 22, 2022
Copy link
Member

@mguaypaq mguaypaq left a 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 😄

@joshuacwnewton joshuacwnewton merged commit 03eadb0 into master Mar 22, 2022
@joshuacwnewton joshuacwnewton deleted the jn/3714-stop-overwriting-tempfile-during-reorientation branch March 22, 2022 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: fixes an error in the code OS: Windows (native) sct_analyze_texture context:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reorient_data() repeatedly overwrites the same temporary file, causing FileExistsError
2 participants