-
Notifications
You must be signed in to change notification settings - Fork 263
ENH: Add SerializableImage class with to/from_bytes methods #644
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #644 +/- ##
=========================================
- Coverage 89.97% 88.4% -1.57%
=========================================
Files 94 188 +94
Lines 12004 24038 +12034
Branches 2127 4254 +2127
=========================================
+ Hits 10800 21251 +10451
- Misses 862 2100 +1238
- Partials 342 687 +345
Continue to review full report at Codecov.
|
Current state: CIFTI issues. Not clear why it's not consistent with the file output, but may be due to Based on TimVanMourik/Armadillo#11, it seems that deserializing would be similarly useful. Incidentally, I'm not committed to "(de)serialize" as function names. To be consistent with the rest of the API, we could use |
to_bytes and from_bytes sounds good to me. Easier for a novice to interpret
as well.
…On Tue, Jul 31, 2018 at 12:21 PM, Chris Markiewicz ***@***.*** > wrote:
Current state: CIFTI issues. Not clear why it's not consistent with the
file output, but may be due to to_file_map performing updates. Perhaps
they're not idempotent.
Based on TimVanMourik/Armadillo#11
<TimVanMourik/Armadillo#11>, it seems that
deserializing would be similarly useful.
Incidentally, I'm not committed to "(de)serialize" as function names. To
be consistent with the rest of the API, we could use to_bytes and
from_bytes. Let me know what y'all think.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#644 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AFC1mG82We8kHI6gSc4gTJf9nM9IyOdAks5uMIPvgaJpZM4U_0Of>
.
|
I think this is ready for a review, if anybody has some time. Pretty compact, in the end. I may not have used the testing machinery to its full potential, but hopefully it isn't too grotesque. |
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'm a bit worried by the heuristic of simply dumping the header and the image into the same file, if the format does not specify how that should be done.
Maybe we could allow file formats to specify how that should be done, but barf if they do not.
We could start by barfing with any file format that has more than one image, and then build up?
nibabel/filebasedimages.py
Outdated
|
||
* from_bytes(bytestring) - make instance by deserializing a byte string | ||
|
||
The following properties should hold: |
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.
How do you want this to look in the rendered doc? What happens with these indented lists?
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 actually haven't built the docs here. I'll fiddle with this.
nibabel/filebasedimages.py
Outdated
|
||
The following properties should hold: | ||
|
||
* ``klass.from_bytes(bstr).to_bytes() == bstr`` |
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.
Is that true in general for round-tripping? For example, what happens with nifti images with image scaling? I think the scale factors may get recalculated.
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.
Fair point. I guess what we want is load-level idempotence, to the same level of precision as save/load cycles.:
img_a = klass.from_bytes(bstr)
img_b = klass.from_bytes(img_a.to_bytes())
np.allclose(img_a.get_fdata(), img_b.get_fdata())
Would the following be too strong, as well?
img_a.to_bytes() == img_b.to_bytes()
67dc4fa
to
2407015
Compare
@matthew-brett I've addressed (or responded to) your comments, if you want to have another look. |
I've updated this PR to do the following:
@matthew-brett I believe this has addressed all of your comments. Please let me know if you have any further concerns. I would like to get this in before the final Python 2 feature release (2.5.0) if possible. If anybody else has some spare cycles, I'd appreciate a review on this one. |
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.
Thanks - yes - you've covered the stuff I was worried about.
fobj.write(img.to_bytes()) | ||
|
||
Images that consist of separate header and data files (e.g., Analyze | ||
images) currently do not support this interface. |
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.
Maybe say something about the fact that image types can override this class method to implement it, for multi-file images.
Images that consist of separate header and data files (e.g., Analyze | ||
images) currently do not support this interface. | ||
''' | ||
@classmethod |
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.
PEP8 doesn't need a newline here?
nibabel/tests/test_volumeutils.py
Outdated
@@ -700,7 +700,7 @@ def test_a2f_non_numeric(): | |||
# Some versions of numpy can cast structured types to float, others not | |||
try: | |||
arr.astype(float) | |||
except ValueError: | |||
except (TypeError, ValueError): |
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.
Why do you need this change in this PR?
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.
Ah, nope. Rebased onto the wrong branch.
Thanks for the review, Matthew. |
Closes #639.
Implements to/from_bytes for single-image variants of NIfTI-1, NIfTI-2, GIFTI and CIFTI2 images, as well as MGH images.
Created an abstract class deriving from
FileBasedImage
that can be used in other images, if it becomes desirable, but didn't want to spend a ton of time nailing it down for image types I have an, at best, passing familiarity with.