Skip to content

Commit c3e8d4f

Browse files
committed
ENH: Expose to/from_stream methods
1 parent 293d8ca commit c3e8d4f

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

nibabel/filebasedimages.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,30 @@ def _filemap_from_iobase(klass, io_obj: io.IOBase):
549549
return klass.make_file_map({klass.files_types[0][0]: io_obj})
550550

551551
@classmethod
552-
def _from_iobase(klass, io_obj: io.IOBase):
552+
def from_stream(klass, io_obj: io.IOBase):
553553
"""Load image from readable IO stream
554554
555555
Convert to BytesIO to enable seeking, if input stream is not seekable
556+
557+
Parameters
558+
----------
559+
io_obj : IOBase object
560+
Readable stream
556561
"""
557562
if not io_obj.seekable():
558563
io_obj = io.BytesIO(io_obj.read())
559564
return klass.from_file_map(klass._filemap_from_iobase(io_obj))
560565

561-
def _to_iobase(self, io_obj: io.IOBase, **kwargs):
562-
"""Save image from writable IO stream"""
566+
def to_stream(self, io_obj: io.IOBase, **kwargs):
567+
"""Save image to writable IO stream
568+
569+
Parameters
570+
----------
571+
io_obj : IOBase object
572+
Writable stream
573+
\*\*kwargs : keyword arguments
574+
Keyword arguments that may be passed to ``img.to_file_map()``
575+
"""
563576
self.to_file_map(self._filemap_from_iobase(io_obj), **kwargs)
564577

565578
@classmethod
@@ -573,7 +586,7 @@ def from_bytes(klass, bytestring: bytes):
573586
bstring : bytes
574587
Byte string containing the on-disk representation of an image
575588
"""
576-
return klass._from_iobase(io.BytesIO(bytestring))
589+
return klass.from_stream(io.BytesIO(bytestring))
577590

578591
def to_bytes(self, **kwargs) -> bytes:
579592
r""" Return a ``bytes`` object with the contents of the file that would
@@ -590,7 +603,7 @@ def to_bytes(self, **kwargs) -> bytes:
590603
Serialized image
591604
"""
592605
bio = io.BytesIO()
593-
self._to_iobase(bio, **kwargs)
606+
self.to_stream(bio, **kwargs)
594607
return bio.getvalue()
595608

596609
@classmethod
@@ -603,6 +616,8 @@ def from_url(klass, url, timeout=5):
603616
----------
604617
url : str or urllib.request.Request object
605618
URL of file to retrieve
619+
timeout : float, optional
620+
Time (in seconds) to wait for a response
606621
"""
607622
response = request.urlopen(url, timeout=timeout)
608-
return klass._from_iobase(response)
623+
return klass.from_stream(response)

0 commit comments

Comments
 (0)