@@ -79,8 +79,8 @@ class FileBasedImage(object):
79
79
80
80
methods:
81
81
82
- * . get_header() (deprecated, use header property instead)
83
- * . to_filename(fname) - writes data to filename(s) derived from
82
+ * get_header() (deprecated, use header property instead)
83
+ * to_filename(fname) - writes data to filename(s) derived from
84
84
``fname``, where the derivation may differ between formats.
85
85
* to_file_map() - save image to files with which the image is already
86
86
associated.
@@ -524,37 +524,43 @@ class SerializableImage(FileBasedImage):
524
524
525
525
methods:
526
526
527
- * . to_bytes() - serialize image to byte string
527
+ * to_bytes() - serialize image to byte string
528
528
529
529
classmethods:
530
530
531
531
* from_bytes(bytestring) - make instance by deserializing a byte string
532
532
533
- The following properties should hold :
533
+ Loading from byte strings should provide round-trip equivalence :
534
534
535
- * ``klass.from_bytes(bstr).to_bytes() == bstr``
536
- * if ``img = orig.__class__.from_bytes(orig.to_bytes())``, then
537
- ``img.header == orig.header`` and ``img.get_data() == orig.get_data()``
535
+ .. code:: python
536
+
537
+ img_a = klass.from_bytes(bstr)
538
+ img_b = klass.from_bytes(img_a.to_bytes())
539
+
540
+ np.allclose(img_a.get_fdata(), img_b.get_fdata())
541
+ np.allclose(img_a.affine, img_b.affine)
538
542
539
543
Further, for images that are single files on disk, the following methods of loading
540
544
the image must be equivalent:
541
545
546
+ .. code:: python
547
+
542
548
img = klass.from_filename(fname)
543
549
544
550
with open(fname, 'rb') as fobj:
545
551
img = klass.from_bytes(fobj.read())
546
552
547
553
And the following methods of saving a file must be equivalent:
548
554
555
+ .. code:: python
556
+
549
557
img.to_filename(fname)
550
558
551
559
with open(fname, 'wb') as fobj:
552
560
fobj.write(img.to_bytes())
553
561
554
- Images that consist of separate header and data files will generally
555
- place the header with the data, but if the header is not of fixed
556
- size and does not define its own size, a new format may need to be
557
- defined.
562
+ Images that consist of separate header and data files (e.g., Analyze
563
+ images) currently do not support this interface.
558
564
'''
559
565
@classmethod
560
566
def from_bytes (klass , bytestring ):
0 commit comments