1313from __future__ import division , print_function
1414import warnings
1515from io import BytesIO
16+ from six import string_types
1617
1718import numpy as np
1819import numpy .linalg as npl
232233 (2003 , 'rgb vector' , (), "NIFTI_INTENT_RGB_VECTOR" ),
233234 (2004 , 'rgba vector' , (), "NIFTI_INTENT_RGBA_VECTOR" ),
234235 (2005 , 'shape' , (), "NIFTI_INTENT_SHAPE" ),
236+ # FSL-specific intent codes - FNIRT
237+ (2006 , 'fnirt disp field' , (), 'FSL_FNIRT_DISPLACEMENT_FIELD' ),
238+ (2007 , 'fnirt cubic spline coef' , (), 'FSL_CUBIC_SPLINE_COEFFICIENTS' ),
239+ (2008 , 'fnirt dct coef' , (), 'FSL_DCT_COEFFICIENTS' ),
240+ (2009 , 'fnirt quad spline coef' , (), 'FSL_QUADRATIC_SPLINE_COEFFICIENTS' ),
241+ # FSL-specific intent codes - TOPUP
242+ (2016 , 'topup cubic spline coef ' , (),
243+ 'FSL_TOPUP_CUBIC_SPLINE_COEFFICIENTS' ),
244+ (2017 , 'topup quad spline coef' , (),
245+ 'FSL_TOPUP_QUADRATIC_SPLINE_COEFFICIENTS' ),
246+ (2018 , 'topup field' , (), 'FSL_TOPUP_FIELD' ),
235247), fields = ('code' , 'label' , 'parameters' , 'niistring' ))
236248
237249
@@ -1308,18 +1320,15 @@ def get_intent(self, code_repr='label'):
13081320 if known_intent :
13091321 label = recoder .label [code ]
13101322 else :
1311- label = ''
1323+ label = 'unknown code ' + str ( code )
13121324 else :
13131325 raise TypeError ('repr can be "label" or "code"' )
1314- if known_intent :
1315- n_params = len (recoder .parameters [code ])
1316- else :
1317- n_params = 0
1326+ n_params = len (recoder .parameters [code ]) if known_intent else 0
13181327 params = (float (hdr ['intent_p%d' % (i + 1 )]) for i in range (n_params ))
13191328 name = asstr (np .asscalar (hdr ['intent_name' ]))
13201329 return label , tuple (params ), name
13211330
1322- def set_intent (self , code , params = (), name = '' ):
1331+ def set_intent (self , code , params = (), name = '' , allow_unknown = False ):
13231332 ''' Set the intent code, parameters and name
13241333
13251334 If parameters are not specified, assumed to be all zero. Each
@@ -1338,6 +1347,10 @@ def set_intent(self, code, params=(), name=''):
13381347 defaults to (). Unspecified parameters are set to 0.0
13391348 name : string
13401349 intent name (description). Defaults to ''
1350+ allow_unknown : bool
1351+ Allow unknown integer intent codes. If False (the default),
1352+ a KeyError is raised on attempts to set the intent
1353+ to an unknown code.
13411354
13421355 Returns
13431356 -------
@@ -1346,7 +1359,7 @@ def set_intent(self, code, params=(), name=''):
13461359 Examples
13471360 --------
13481361 >>> hdr = Nifti1Header()
1349- >>> hdr.set_intent(0) # unknown code
1362+ >>> hdr.set_intent(0) # no intent
13501363 >>> hdr.set_intent('z score')
13511364 >>> hdr.get_intent()
13521365 ('z score', (), '')
@@ -1361,15 +1374,21 @@ def set_intent(self, code, params=(), name=''):
13611374 >>> hdr.set_intent('f test')
13621375 >>> hdr.get_intent()
13631376 ('f test', (0.0, 0.0), '')
1377+ >>> hdr.set_intent(9999, allow_unknown=True) # unknown code
13641378 '''
13651379 hdr = self ._structarr
13661380 known_intent = code in intent_codes
1381+ if not known_intent :
1382+ # We can set intent via an unknown integer code, but can't via an
1383+ # unknown string label
1384+ if not allow_unknown or isinstance (code , string_types ):
1385+ raise KeyError ('Unknown intent code: ' + str (code ))
13671386 if known_intent :
13681387 icode = intent_codes .code [code ]
13691388 p_descr = intent_codes .parameters [code ]
13701389 else :
13711390 icode = code
1372- p_descr = 3
1391+ p_descr = ( 'p1' , 'p2' , 'p3' )
13731392 if len (params ) and len (params ) != len (p_descr ):
13741393 raise HeaderDataError ('Need params of form %s, or empty'
13751394 % (p_descr ,))
0 commit comments