21
21
import re
22
22
import collections
23
23
24
- import numpy as np
25
-
26
24
from .. import xmlutils as xml
27
- from ..filebasedimages import FileBasedHeader , FileBasedImage
28
- from ..nifti2 import Nifti2Image
25
+ from ..filebasedimages import FileBasedHeader
26
+ from ..dataobj_images import DataobjImage
27
+ from ..nifti2 import Nifti2Image , Nifti2Header
28
+ from ..arrayproxy import reshape_dataobj
29
29
30
30
31
31
def _float_01 (val ):
@@ -939,62 +939,63 @@ def _to_xml_element(self):
939
939
cifti .append (mat_xml )
940
940
return cifti
941
941
942
- @classmethod
943
- def from_header (klass , header = None ):
944
- if header is None :
945
- return klass ()
946
- if type (header ) == klass :
947
- return header .copy ()
948
- raise ValueError ('header is not a Cifti2Header' )
949
-
950
942
@classmethod
951
943
def may_contain_header (klass , binaryblock ):
952
944
from .parse_cifti2 import _Cifti2AsNiftiHeader
953
945
return _Cifti2AsNiftiHeader .may_contain_header (binaryblock )
954
946
955
947
956
- class Cifti2Image (FileBasedImage ):
957
- """ Class for single file CIfTI2 format image
948
+ class Cifti2Image (DataobjImage ):
949
+ """ Class for single file CIFTI2 format image
958
950
"""
959
951
header_class = Cifti2Header
960
952
valid_exts = Nifti2Image .valid_exts
961
953
files_types = Nifti2Image .files_types
962
954
makeable = False
963
955
rw = True
964
956
965
- def __init__ (self , data = None , header = None , nifti_header = None ):
957
+ def __init__ (self ,
958
+ dataobj = None ,
959
+ header = None ,
960
+ nifti_header = None ,
961
+ extra = None ,
962
+ file_map = None ):
966
963
''' Initialize image
967
964
968
- The image is a combination of (array, affine matrix, header, nifti_header),
969
- with optional metadata in `extra`, and filename / file-like objects
970
- contained in the `file_map` mapping.
965
+ The image is a combination of (dataobj, header), with optional metadata
966
+ in `nifti_header` (a NIfTI2 header). There may be more metadata in the
967
+ mapping `extra`. Filename / file-like objects can also go in the
968
+ `file_map` mapping.
971
969
972
970
Parameters
973
971
----------
974
972
dataobj : object
975
- Object containg image data. It should be some object that retuns an
976
- array from ``np.asanyarray``. It should have a ``shape`` attribute
977
- or property
978
- header : Cifti2Header object
979
- nifti_header : None or mapping or nifti2 header instance, optional
980
- metadata for this image format
973
+ Object containing image data. It should be some object that returns
974
+ an array from ``np.asanyarray``. It should have a ``shape``
975
+ attribute or property.
976
+ header : Cifti2Header instance
977
+ Header with data for / from XML part of CIFTI2 format.
978
+ nifti_header : None or mapping or NIfTI2 header instance, optional
979
+ Metadata for NIfTI2 component of this format.
980
+ extra : None or mapping
981
+ Extra metadata not captured by `header` or `nifti_header`.
982
+ file_map : mapping, optional
983
+ Mapping giving file information for this image format.
981
984
'''
982
- self ._header = header if header is not None else Cifti2Header ()
983
- self .data = data
984
- self .extra = nifti_header
985
-
986
- def get_data (self ):
987
- return self .data
985
+ super (Cifti2Image , self ).__init__ (dataobj , header = header ,
986
+ extra = extra , file_map = file_map )
987
+ self ._nifti_header = Nifti2Header .from_header (nifti_header )
988
988
989
989
@property
990
- def shape (self ):
991
- return self .data . shape
990
+ def nifti_header (self ):
991
+ return self ._nifti_header
992
992
993
993
@classmethod
994
994
def from_file_map (klass , file_map ):
995
995
""" Load a Cifti2 image from a file_map
996
996
997
997
Parameters
998
+ ----------
998
999
file_map : file_map
999
1000
1000
1001
Returns
@@ -1011,51 +1012,54 @@ def from_file_map(klass, file_map):
1011
1012
cifti_header = item .get_content ()
1012
1013
break
1013
1014
else :
1014
- raise ValueError ('Nifti2 header does not contain a CIFTI2 '
1015
+ raise ValueError ('NIfTI2 header does not contain a CIFTI2 '
1015
1016
'extension' )
1016
1017
1017
- # Construct cifti image
1018
- cifti_img = Cifti2Image (data = nifti_img .dataobj [0 , 0 , 0 , 0 ],
1019
- header = cifti_header ,
1020
- nifti_header = nifti_img .header )
1021
- cifti_img .file_map = nifti_img .file_map
1022
- return cifti_img
1018
+ # Construct cifti image.
1019
+ # User array proxy object where possible
1020
+ dataobj = nifti_img .dataobj
1021
+ return Cifti2Image (reshape_dataobj (dataobj , dataobj .shape [4 :]),
1022
+ header = cifti_header ,
1023
+ nifti_header = nifti_img .header ,
1024
+ file_map = file_map )
1023
1025
1024
1026
@classmethod
1025
1027
def from_image (klass , img ):
1026
1028
''' Class method to create new instance of own class from `img`
1027
1029
1028
1030
Parameters
1029
1031
----------
1030
- img : ``spatialimage`` instance
1031
- In fact, an object with the API of ``FileBasedImage` `.
1032
+ img : instance
1033
+ In fact, an object with the API of :class:`DataobjImage `.
1032
1034
1033
1035
Returns
1034
1036
-------
1035
- cimg : ``spatialimage`` instance
1036
- Image, of our own class
1037
+ cimg : instance
1038
+ Image, of our own class
1037
1039
'''
1038
1040
if isinstance (img , klass ):
1039
1041
return img
1040
- else :
1041
- raise NotImplementedError
1042
+ raise NotImplementedError
1042
1043
1043
1044
def to_file_map (self , file_map = None ):
1044
- """ Save the current image to the specified file_map
1045
+ """ Write image to `file_map` or contained ``self. file_map``
1045
1046
1046
1047
Parameters
1047
1048
----------
1048
- file_map : string
1049
+ file_map : None or mapping, optional
1050
+ files mapping. If None (default) use object's ``file_map``
1051
+ attribute instead.
1049
1052
1050
1053
Returns
1051
1054
-------
1052
1055
None
1053
1056
"""
1054
1057
from .parse_cifti2 import Cifti2Extension
1055
- header = self .extra
1058
+ header = self ._nifti_header
1056
1059
extension = Cifti2Extension (content = self .header .to_xml ())
1057
1060
header .extensions .append (extension )
1058
- data = np .reshape (self .data , (1 , 1 , 1 , 1 ) + self .data .shape )
1061
+ data = reshape_dataobj (self .dataobj ,
1062
+ (1 , 1 , 1 , 1 ) + self .dataobj .shape )
1059
1063
# If qform not set, reset pixdim values so Nifti2 does not complain
1060
1064
if header ['qform_code' ] == 0 :
1061
1065
header ['pixdim' ][:4 ] = 1
0 commit comments