13
13
from __future__ import division , print_function
14
14
import warnings
15
15
from io import BytesIO
16
+ from six import string_types
16
17
17
18
import numpy as np
18
19
import numpy .linalg as npl
232
233
(2003 , 'rgb vector' , (), "NIFTI_INTENT_RGB_VECTOR" ),
233
234
(2004 , 'rgba vector' , (), "NIFTI_INTENT_RGBA_VECTOR" ),
234
235
(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' ),
235
247
), fields = ('code' , 'label' , 'parameters' , 'niistring' ))
236
248
237
249
@@ -1308,18 +1320,15 @@ def get_intent(self, code_repr='label'):
1308
1320
if known_intent :
1309
1321
label = recoder .label [code ]
1310
1322
else :
1311
- label = ''
1323
+ label = 'unknown code ' + str ( code )
1312
1324
else :
1313
1325
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
1318
1327
params = (float (hdr ['intent_p%d' % (i + 1 )]) for i in range (n_params ))
1319
1328
name = asstr (np .asscalar (hdr ['intent_name' ]))
1320
1329
return label , tuple (params ), name
1321
1330
1322
- def set_intent (self , code , params = (), name = '' ):
1331
+ def set_intent (self , code , params = (), name = '' , allow_unknown = False ):
1323
1332
''' Set the intent code, parameters and name
1324
1333
1325
1334
If parameters are not specified, assumed to be all zero. Each
@@ -1338,6 +1347,10 @@ def set_intent(self, code, params=(), name=''):
1338
1347
defaults to (). Unspecified parameters are set to 0.0
1339
1348
name : string
1340
1349
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.
1341
1354
1342
1355
Returns
1343
1356
-------
@@ -1346,7 +1359,7 @@ def set_intent(self, code, params=(), name=''):
1346
1359
Examples
1347
1360
--------
1348
1361
>>> hdr = Nifti1Header()
1349
- >>> hdr.set_intent(0) # unknown code
1362
+ >>> hdr.set_intent(0) # no intent
1350
1363
>>> hdr.set_intent('z score')
1351
1364
>>> hdr.get_intent()
1352
1365
('z score', (), '')
@@ -1361,15 +1374,21 @@ def set_intent(self, code, params=(), name=''):
1361
1374
>>> hdr.set_intent('f test')
1362
1375
>>> hdr.get_intent()
1363
1376
('f test', (0.0, 0.0), '')
1377
+ >>> hdr.set_intent(9999, allow_unknown=True) # unknown code
1364
1378
'''
1365
1379
hdr = self ._structarr
1366
1380
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 ))
1367
1386
if known_intent :
1368
1387
icode = intent_codes .code [code ]
1369
1388
p_descr = intent_codes .parameters [code ]
1370
1389
else :
1371
1390
icode = code
1372
- p_descr = 3
1391
+ p_descr = ( 'p1' , 'p2' , 'p3' )
1373
1392
if len (params ) and len (params ) != len (p_descr ):
1374
1393
raise HeaderDataError ('Need params of form %s, or empty'
1375
1394
% (p_descr ,))
0 commit comments