2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
3
"""Example analyzing the FIAC dataset with NIPY.
4
4
5
+ * Single run models with per-voxel AR(1).
6
+ * Cross-run, within-subject models with optimal effect estimates.
7
+ * Cross-subject models using fixed / random effects variance ratios.
8
+ * Permutation testing for inference on cross-subject result.
9
+
10
+ See ``parallel_run.py`` for a rig to run these analysis in parallel using the
11
+ IPython parallel machinery.
12
+
5
13
This script needs the pre-processed FIAC data. See ``README.txt`` and
6
14
``fiac_util.py`` for details.
7
15
8
16
See ``examples/labs/need_data/first_level_fiac.py`` for an alternative approach
9
- to this analysis .
17
+ to some of these analyses .
10
18
"""
11
19
#-----------------------------------------------------------------------------
12
20
# Imports
@@ -281,17 +289,17 @@ def fixed_effects(subj, design):
281
289
fixdir = pjoin (rootdir , "fixed" )
282
290
# Fetch results images from run estimations
283
291
results = futil .results_table (path_dict )
284
- # Get our hands on the relevant coordmap to
285
- # save our results
292
+ # Get our hands on the relevant coordmap to save our results
286
293
coordmap = futil .load_image_fiac ("fiac_%02d" % subj ,
287
294
"wanatomical.nii" ).coordmap
288
295
# Compute the "fixed" effects for each type of contrast
289
296
for con in results :
290
297
fixed_effect = 0
291
298
fixed_var = 0
292
299
for effect , sd in results [con ]:
293
- effect = load_image (effect ); sd = load_image (sd )
294
- var = np .array (sd )** 2
300
+ effect = load_image (effect ).get_data ()
301
+ sd = load_image (sd ).get_data ()
302
+ var = sd ** 2
295
303
296
304
# The optimal, in terms of minimum variance, combination of the
297
305
# effects has weights 1 / var
@@ -332,9 +340,19 @@ def group_analysis(design, contrast):
332
340
333
341
# Which subjects have this (contrast, design) pair?
334
342
subj_con_dirs = futil .subj_des_con_dirs (design , contrast )
335
-
336
- sd = array ([array (load_image (pjoin (s , "sd.nii" ))) for s in subj_con_dirs ])
337
- Y = array ([array (load_image (pjoin (s , "effect.nii" ))) for s in subj_con_dirs ])
343
+ if len (subj_con_dirs ) == 0 :
344
+ raise ValueError ('No subjects for %s, %s' % (design , contrast ))
345
+
346
+ # Assemble effects and sds into 4D arrays
347
+ sds = []
348
+ Ys = []
349
+ for s in subj_con_dirs :
350
+ sd_img = load_image (pjoin (s , "sd.nii" ))
351
+ effect_img = load_image (pjoin (s , "effect.nii" ))
352
+ sds .append (sd_img .get_data ())
353
+ Ys .append (effect_img .get_data ())
354
+ sd = array (sds )
355
+ Y = array (Ys )
338
356
339
357
# This function estimates the ratio of the fixed effects variance
340
358
# (sum(1/sd**2, 0)) to the estimated random effects variance
@@ -378,7 +396,9 @@ def group_analysis_signs(design, contrast, mask, signs=None):
378
396
----------
379
397
design: one of 'block', 'event'
380
398
contrast: str
381
- mask: array-like
399
+ name of contrast to estimate
400
+ mask : ``Image`` instance or array-like
401
+ image containing mask, or array-like
382
402
signs: ndarray, optional
383
403
Defaults to np.ones. Should have shape (*,nsubj)
384
404
where nsubj is the number of effects combined in the group analysis.
@@ -390,15 +410,25 @@ def group_analysis_signs(design, contrast, mask, signs=None):
390
410
maxT: np.ndarray, maxima of T statistic within mask, one for each
391
411
vector of signs
392
412
"""
393
- maska = np .asarray (mask ).astype (np .bool )
413
+ if api .is_image (mask ):
414
+ maska = mask .get_data ()
415
+ else :
416
+ maska = np .asarray (mask )
417
+ maska = maska .astype (np .bool )
394
418
395
419
# Which subjects have this (contrast, design) pair?
396
420
subj_con_dirs = futil .subj_des_con_dirs (design , contrast )
397
421
398
- sd = np .array ([np .array (load_image (pjoin (s , "sd.nii" )))[:,maska ]
399
- for s in subj_con_dirs ])
400
- Y = np .array ([np .array (load_image (pjoin (s , "effect.nii" )))[:,maska ]
401
- for s in subj_con_dirs ])
422
+ # Assemble effects and sds into 4D arrays
423
+ sds = []
424
+ Ys = []
425
+ for s in subj_con_dirs :
426
+ sd_img = load_image (pjoin (s , "sd.nii" ))
427
+ effect_img = load_image (pjoin (s , "effect.nii" ))
428
+ sds .append (sd_img .get_data ()[maska ])
429
+ Ys .append (effect_img .get_data ()[maska ])
430
+ sd = np .array (sds )
431
+ Y = np .array (Ys )
402
432
403
433
if signs is None :
404
434
signs = np .ones ((1 , Y .shape [0 ]))
@@ -428,22 +458,26 @@ def permutation_test(design, contrast, mask=GROUP_MASK, nsample=1000):
428
458
429
459
Parameters
430
460
----------
431
- design: one of ['block', 'event']
432
- contrast: str
433
- nsample: int
461
+ design: str
462
+ one of ['block', 'event']
463
+ contrast : str
464
+ name of contrast to estimate
465
+ mask : ``Image`` instance or array-like, optional
466
+ image containing mask, or array-like
467
+ nsample: int, optional
468
+ number of permutations
434
469
435
470
Returns
436
471
-------
437
472
min_vals: np.ndarray
438
473
max_vals: np.ndarray
439
474
"""
440
- maska = np .asarray (mask ).astype (np .bool )
441
475
subj_con_dirs = futil .subj_des_con_dirs (design , contrast )
442
- Y = np . array ([ np . array ( load_image ( pjoin ( s , "effect.nii" )))[:, maska ]
443
- for s in subj_con_dirs ])
444
- nsubj = Y . shape [ 0 ]
476
+ nsubj = len ( subj_con_dirs )
477
+ if nsubj == 0 :
478
+ raise ValueError ( 'No subjects have %s, %s' % ( design , contrast ))
445
479
signs = 2 * np .greater (np .random .sample (size = (nsample , nsubj )), 0.5 ) - 1
446
- min_vals , max_vals = group_analysis_signs (design , contrast , maska , signs )
480
+ min_vals , max_vals = group_analysis_signs (design , contrast , mask , signs )
447
481
return min_vals , max_vals
448
482
449
483
0 commit comments