-
Notifications
You must be signed in to change notification settings - Fork 259
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
Feature Request: Detect when writing to open mmap #492
Comments
Hi, sorry about the delay. My guess is that the image is an You could try |
Better late than never. I don't have the technical background to fully understand your solution, but I'm sure it works. However, shouldn't nibabel be able to prevent this from hapenning? |
Perhaps. Certainly worth a discussion. I do believe (though I haven't tested) that #466 or something similar would resolve the problem by unlinking the input file before writing to that name. It should also be possible to verify that the target filename is the same as the source As an aside, I would say that, for reproducibility and documentation reasons, I'd be very cautious about overwriting files. In this case, performing your action twice is the same as performing it once, so at least there's no danger to forgetting you've already done it. But in general, I'd strongly recommend setting up your workflows such that there is a clear flow of |
Sure, one has to be cautious when overwriting. But if everything I want is, say, change the |
Fair enough. Well, in the short term, I'd say use |
Duly noted, thanks! |
Thank you @effigies . .get_data().copy() helps me a lot! |
I should also say, that in general you should prefer |
See nipy/nibabel#492 for probable explanation
I confirm this problem is still happening: fepegar/torchio#77 |
Right. The reason it looked like Loading with Fiddling with this, though, I think we can reliably predict the getattr(new.dataobj, '_mmap') and \
os.path.realpath(new.dataobj.filename) == os.path.realpath(imPath) The simplest and safest thing to do is to detect this case and copy the data. It would have to go in This would in any case be no greater a penalty than requiring This is a long way to say: I think we can (sometimes) detect when writing to an open mmap. I'm not sure that doing so is a good idea. |
I have written a small function in IPython to delete the origin information from an image:
In [1]: def removeOrigin(imPath):
...: nii=nib.load(imPath)
...: affine = nii.affine
...: affine[:3, 3] = 0
...: data = nii.get_data()
...: new = nib.Nifti1Pair(data, affine)
...: nib.save(new, imPath)
My images are in Nifti1Pair format and very small (500 kB - 1 MB).
When I call my function, IPython crashes, the Terminal says
Bus error
and something very bad happens: my image data is gone!! The .hdr/.imgs file contain now 0 bytes, so I just lost my data.I've tried outside the function and it happens the same when I try to overwrite the original image, i.e.:
nib.save(new, imPath)
-> Bus errornib.save(new, imPath.replace('.hdr', '_2.hdr'))
-> OkThe text was updated successfully, but these errors were encountered: