Skip to content

Commit 6b40777

Browse files
author
Ben Cipollini
committed
make from_image an abstract method
1 parent a34220a commit 6b40777

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

nibabel/filebasedimages.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,22 @@ def instance_to_filename(klass, img, filename):
416416
img = klass.from_image(img)
417417
img.to_filename(filename)
418418

419+
@classmethod
420+
def from_image(klass, img):
421+
''' Class method to create new instance of own class from `img`
422+
423+
Parameters
424+
----------
425+
img : ``spatialimage`` instance
426+
In fact, an object with the API of ``FileBasedImage``.
427+
428+
Returns
429+
-------
430+
cimg : ``spatialimage`` instance
431+
Image, of our own class
432+
'''
433+
raise NotImplementedError()
434+
419435
@classmethod
420436
def is_valid_extension(klass, ext):
421437
valid = tuple(ft[1] for ft in klass.files_types) + klass.alternate_exts

nibabel/spatialimages.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,26 @@ def get_affine(self):
636636
"""
637637
return self.affine
638638

639+
@classmethod
640+
def from_image(klass, img):
641+
''' Class method to create new instance of own class from `img`
642+
643+
Parameters
644+
----------
645+
img : ``spatialimage`` instance
646+
In fact, an object with the API of ``spatialimage`` -
647+
specifically ``dataobj``, ``affine``, ``header`` and ``extra``.
648+
649+
Returns
650+
-------
651+
cimg : ``spatialimage`` instance
652+
Image, of our own class
653+
'''
654+
return klass(img.dataobj,
655+
img.affine,
656+
klass.header_class.from_header(img.header),
657+
extra=img.extra.copy())
658+
639659
def __getitem__(self):
640660
''' No slicing or dictionary interface for images
641661
'''

0 commit comments

Comments
 (0)