-
Notifications
You must be signed in to change notification settings - Fork 258
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
Ensure affine attribute in sync with header after init. #688
base: master
Are you sure you want to change the base?
Conversation
Any thoughts on that? |
Thanks for this. Looking at it, I have a couple things I want to consider about the order of operations. I'll try to sort out my thoughts in the next day or two. In the meantime, would you mind merging master? Also, want to have a look at the tests and see what might be causing the failure? |
Thanks for pointing out the failing tests. The tests that fail are:
Since my changes alter the affine to float32 precision on init at NIfti1 images it seems logical for some tests comparing affines to fail. I see two options here.
|
Okay. So now I'm less sure that this is a good approach, as this will be a pretty major violation of the historical expectation for images in memory to not fully synchronize until written to disk. We could consider an alternative method for class FileBasedImage:
def as_synchronized(self):
"""Generate a copy of the image as if it had been saved to disk and reloaded"""
... This would allow the programmer to explicitly choose when the constraints of the format should be enforced. |
That is an interesting proposition. I believe it a has a bigger implementation scope though (correct me if wrong). I was kind of aware of this expectation (read about it in other issues mostly) and also learned about it the hard way. I think it needs to be better communicated in the documentation. Except it is and I missed that of course. |
At least for import io
def synchronize_image(img):
bio = io.BytesIO()
file_map = img.make_file_map({'image': bio, 'header': bio})
img.to_file_map(file_map)
return img.__class__.from_file_map(file_map) So if you just need this in your own code for now, you can use that, and we can work toward a good nibabel-level solution on a longer time frame. As to the documentation, do you have any suggestions for making these features more obvious? |
Hi I didn't have time to look mote into that yet. I will come back to this asap |
Hi,
I made the following changes that ensure that the affine attribute is in sync with the header after initializing a Nifti Image.
Those two may differ because the affine provided may contain values that need float64 to store, while they have to be casted to float32 for the nifti1 header and thus some values are slightly changed.
I actually encountered this while using
as_closest_canonical
(which actually creates a new instance) on images and then expecting the affine I had in memory to be the same as the one that will be written and later read from file.I found some related issues such as http://nipy.org/nibabel/devel/modified_images.html and
#669 but they deal with the more general issue of memory/file mismatch that may happen while modifying things on the instance.
This is a more fundamental step that ensures the affine attribute just after init is the same as the one that will be written excluding later modifications