@@ -504,8 +504,8 @@ def from_fileobj(klass, fileobj, size, byteswap):
504
504
505
505
506
506
class Nifti1Header (SpmAnalyzeHeader ):
507
- ''' Class for NIFTI1 header
508
-
507
+ ''' Class for NIFTI1 header
508
+
509
509
The NIFTI1 header has many more coded fields than the simpler Analyze
510
510
variants. Nifti1 headers also have extensions.
511
511
@@ -517,7 +517,7 @@ class Nifti1Header(SpmAnalyzeHeader):
517
517
This class handles the header-preceding-data case.
518
518
'''
519
519
# Copies of module level definitions
520
- _dtype = header_dtype
520
+ template_dtype = header_dtype
521
521
_data_type_codes = data_type_codes
522
522
523
523
# fields with recoders for their values
@@ -563,7 +563,7 @@ def copy(self):
563
563
564
564
@classmethod
565
565
def from_fileobj (klass , fileobj , endianness = None , check = True ):
566
- raw_str = fileobj .read (klass ._dtype .itemsize )
566
+ raw_str = fileobj .read (klass .template_dtype .itemsize )
567
567
hdr = klass (raw_str , endianness , check )
568
568
# Read next 4 bytes to see if we have extensions. The nifti standard
569
569
# has this as a 4 byte string; if the first value is not zero, then we
@@ -575,15 +575,15 @@ def from_fileobj(klass, fileobj, endianness=None, check=True):
575
575
if not klass .is_single :
576
576
extsize = - 1
577
577
else : # otherwise read until the beginning of the data
578
- extsize = hdr ._header_data ['vox_offset' ] - fileobj .tell ()
578
+ extsize = hdr ._structarr ['vox_offset' ] - fileobj .tell ()
579
579
byteswap = endian_codes ['native' ] != hdr .endianness
580
580
hdr .extensions = klass .exts_klass .from_fileobj (fileobj , extsize , byteswap )
581
581
return hdr
582
582
583
583
def write_to (self , fileobj ):
584
584
# First check that vox offset is large enough
585
585
if self .is_single :
586
- vox_offset = self ._header_data ['vox_offset' ]
586
+ vox_offset = self ._structarr ['vox_offset' ]
587
587
min_vox_offset = 352 + self .extensions .get_sizeondisk ()
588
588
if vox_offset < min_vox_offset :
589
589
raise HeaderDataError ('vox offset of %d, but need at least %d'
@@ -601,18 +601,18 @@ def write_to(self, fileobj):
601
601
602
602
def get_best_affine (self ):
603
603
''' Select best of available transforms '''
604
- hdr = self ._header_data
604
+ hdr = self ._structarr
605
605
if hdr ['sform_code' ] != 0 :
606
606
return self .get_sform ()
607
607
if hdr ['qform_code' ] != 0 :
608
608
return self .get_qform ()
609
609
return self .get_base_affine ()
610
610
611
- def _empty_headerdata (self , endianness = None ):
611
+ @classmethod
612
+ def default_structarr (klass , endianness = None ):
612
613
''' Create empty header binary block with given endianness '''
613
- hdr_data = analyze .AnalyzeHeader ._empty_headerdata (self , endianness )
614
- hdr_data ['scl_slope' ] = 1
615
- if self .is_single :
614
+ hdr_data = super (Nifti1Header , klass ).default_structarr (endianness )
615
+ if klass .is_single :
616
616
hdr_data ['magic' ] = 'n+1'
617
617
hdr_data ['vox_offset' ] = 352
618
618
else :
@@ -625,14 +625,14 @@ def get_qform_quaternion(self):
625
625
626
626
Fills a value by assuming this is a unit quaternion
627
627
'''
628
- hdr = self ._header_data
628
+ hdr = self ._structarr
629
629
bcd = [hdr ['quatern_b' ], hdr ['quatern_c' ], hdr ['quatern_d' ]]
630
630
# Adjust threshold to fact that source data was float32
631
631
return fillpositive (bcd , FLOAT32_EPS_3 )
632
632
633
633
def get_qform (self ):
634
634
''' Return 4x4 affine matrix from qform parameters in header '''
635
- hdr = self ._header_data
635
+ hdr = self ._structarr
636
636
quat = self .get_qform_quaternion ()
637
637
R = quat2mat (quat )
638
638
vox = hdr ['pixdim' ][1 :4 ].copy ()
@@ -695,7 +695,7 @@ def set_qform(self, affine, code=None):
695
695
>>> int(hdr['qform_code'])
696
696
1
697
697
'''
698
- hdr = self ._header_data
698
+ hdr = self ._structarr
699
699
if code is None :
700
700
code = hdr ['qform_code' ]
701
701
if code == 0 : # default is 'aligned'
@@ -732,7 +732,7 @@ def set_qform(self, affine, code=None):
732
732
733
733
def get_sform (self ):
734
734
''' Return sform 4x4 affine matrix from header '''
735
- hdr = self ._header_data
735
+ hdr = self ._structarr
736
736
out = np .eye (4 )
737
737
out [0 , :] = hdr ['srow_x' ][:]
738
738
out [1 , :] = hdr ['srow_y' ][:]
@@ -776,7 +776,7 @@ def set_sform(self, affine, code=None):
776
776
>>> int(hdr['sform_code'])
777
777
1
778
778
'''
779
- hdr = self ._header_data
779
+ hdr = self ._structarr
780
780
if code is None :
781
781
code = hdr ['sform_code' ]
782
782
if code == 0 :
@@ -853,8 +853,8 @@ def set_slope_inter(self, slope, inter=0.0):
853
853
slope = 0.0
854
854
if inter is None :
855
855
inter = 0.0
856
- self ._header_data ['scl_slope' ] = slope
857
- self ._header_data ['scl_inter' ] = inter
856
+ self ._structarr ['scl_slope' ] = slope
857
+ self ._structarr ['scl_inter' ] = inter
858
858
859
859
def get_dim_info (self ):
860
860
''' Gets nifti MRI slice etc dimension information
@@ -882,7 +882,7 @@ def get_dim_info(self):
882
882
See set_dim_info function
883
883
884
884
'''
885
- hdr = self ._header_data
885
+ hdr = self ._structarr
886
886
info = int (hdr ['dim_info' ])
887
887
freq = info & 3
888
888
phase = (info >> 2 ) & 3
@@ -936,7 +936,7 @@ def set_dim_info(self, freq=None, phase=None, slice=None):
936
936
info = info | (((phase + 1 ) & 3 ) << 2 )
937
937
if not slice is None :
938
938
info = info | (((slice + 1 ) & 3 ) << 4 )
939
- self ._header_data ['dim_info' ] = info
939
+ self ._structarr ['dim_info' ] = info
940
940
941
941
def get_intent (self , code_repr = 'label' ):
942
942
''' Get intent code, parameters and name
@@ -965,7 +965,7 @@ def get_intent(self, code_repr='label'):
965
965
>>> hdr.get_intent('code')
966
966
(3, (10.0,), 'some score')
967
967
'''
968
- hdr = self ._header_data
968
+ hdr = self ._structarr
969
969
recoder = self ._field_recoders ['intent_code' ]
970
970
code = int (hdr ['intent_code' ])
971
971
if code_repr == 'code' :
@@ -1022,7 +1022,7 @@ def set_intent(self, code, params=(), name=''):
1022
1022
>>> hdr.get_intent()
1023
1023
('f test', (0.0, 0.0), '')
1024
1024
'''
1025
- hdr = self ._header_data
1025
+ hdr = self ._structarr
1026
1026
icode = intent_codes .code [code ]
1027
1027
p_descr = intent_codes .parameters [code ]
1028
1028
if len (params ) and len (params ) != len (p_descr ):
@@ -1060,7 +1060,7 @@ def get_slice_duration(self):
1060
1060
if slice_dim is None :
1061
1061
raise HeaderDataError ('Slice dimension must be set '
1062
1062
'for duration to be valid' )
1063
- return float (self ._header_data ['slice_duration' ])
1063
+ return float (self ._structarr ['slice_duration' ])
1064
1064
1065
1065
def set_slice_duration (self , duration ):
1066
1066
''' Set slice duration
@@ -1078,12 +1078,12 @@ def set_slice_duration(self, duration):
1078
1078
if slice_dim is None :
1079
1079
raise HeaderDataError ('Slice dimension must be set '
1080
1080
'for duration to be valid' )
1081
- self ._header_data ['slice_duration' ] = duration
1081
+ self ._structarr ['slice_duration' ] = duration
1082
1082
1083
1083
def get_n_slices (self ):
1084
1084
''' Return the number of slices
1085
1085
'''
1086
- hdr = self ._header_data
1086
+ hdr = self ._structarr
1087
1087
_ , _ , slice_dim = self .get_dim_info ()
1088
1088
if slice_dim is None :
1089
1089
raise HeaderDataError ('Slice dimension not set in header '
@@ -1121,7 +1121,7 @@ def get_slice_times(self):
1121
1121
>>> np.allclose(slice_times, [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
1122
1122
True
1123
1123
'''
1124
- hdr = self ._header_data
1124
+ hdr = self ._structarr
1125
1125
slice_len = self .get_n_slices ()
1126
1126
duration = self .get_slice_duration ()
1127
1127
slabel = self .get_value_label ('slice_code' )
@@ -1168,7 +1168,7 @@ def set_slice_times(self, slice_times):
1168
1168
5
1169
1169
'''
1170
1170
# Check if number of slices matches header
1171
- hdr = self ._header_data
1171
+ hdr = self ._structarr
1172
1172
slice_len = self .get_n_slices ()
1173
1173
if slice_len != len (slice_times ):
1174
1174
raise HeaderDataError ('Number of slice times does not '
@@ -1258,11 +1258,11 @@ def set_xyzt_units(self, xyz=None, t=None):
1258
1258
def _set_format_specifics (self ):
1259
1259
''' Utility routine to set format specific header stuff '''
1260
1260
if self .is_single :
1261
- self ._header_data ['magic' ] = 'n+1'
1262
- if self ._header_data ['vox_offset' ] < 352 :
1263
- self ._header_data ['vox_offset' ] = 352
1261
+ self ._structarr ['magic' ] = 'n+1'
1262
+ if self ._structarr ['vox_offset' ] < 352 :
1263
+ self ._structarr ['vox_offset' ] = 352
1264
1264
else :
1265
- self ._header_data ['magic' ] = 'ni1'
1265
+ self ._structarr ['magic' ] = 'ni1'
1266
1266
1267
1267
''' Checks only below here '''
1268
1268
0 commit comments