def test_sanity(): from nipy.modalities.fmri import design, hrf import nipy.modalities.fmri.fmristat.hrf as fshrf from nipy.modalities.fmri.fmristat.tests import FIACdesigns from nipy.modalities.fmri.fmristat.tests.test_FIAC import matchcol from nipy.algorithms.statistics import formula from nose.tools import assert_true """ Single subject fitting of FIAC model """ # Based on file # subj3_evt_fonc1.txt # subj3_bloc_fonc3.txt for subj, run, design_type in [(3, 1, 'event'), (3, 3, 'block')]: nvol = 191 TR = 2.5 Tstart = 1.25 volume_times = np.arange(nvol) * TR + Tstart volume_times_rec = formula.make_recarray(volume_times, 't') path_dict = {'subj': subj, 'run': run} if exists( pjoin(DATADIR, "fiac_%(subj)02d", "block", "initial_%(run)02d.csv") % path_dict): path_dict['design'] = 'block' else: path_dict['design'] = 'event' experiment = csv2rec( pjoin(DATADIR, "fiac_%(subj)02d", "%(design)s", "experiment_%(run)02d.csv") % path_dict) initial = csv2rec( pjoin(DATADIR, "fiac_%(subj)02d", "%(design)s", "initial_%(run)02d.csv") % path_dict) X_exper, cons_exper = design.event_design(experiment, volume_times_rec, hrfs=fshrf.spectral) X_initial, _ = design.event_design(initial, volume_times_rec, hrfs=[hrf.glover]) X, cons = design.stack_designs((X_exper, cons_exper), (X_initial, {})) # Get original fmristat design Xf = FIACdesigns.fmristat[design_type] # Check our new design can be closely matched to the original for i in range(X.shape[1]): # Columns can be very well correlated negatively or positively assert_true(abs(matchcol(X[:, i], Xf)[1]) > 0.999)
def test_block_amplitudes(): # Test event design helper function # An event design with one event type onsets = np.array([0, 20, 40, 60]) durations = np.array([2, 3, 4, 5]) offsets = onsets + durations amplitudes = [3, 2, 1, 4] t = np.arange(0, 100, 2.5) def mk_blk_tc(amplitudes=None, hrf=glover): func_amp = blocks(zip(onsets, offsets), amplitudes) # Make real time course for block onset / offsets / amplitudes term = convolve_functions(func_amp, hrf(T), (-5, 70), # step func support (0, 30.), # conv kernel support 0.02) # dt return lambdify_t(term)(t) no_amps = make_recarray(zip(onsets, offsets), ('start', 'end')) amps = make_recarray(zip(onsets, offsets, amplitudes), ('start', 'end', 'amplitude')) X, contrasts = block_amplitudes('ev0', no_amps, t) assert_almost_equal(X, mk_blk_tc()) assert_dict_almost_equal(contrasts, {'ev0_0': 1}) # Same thing as 2D array X, contrasts = block_amplitudes('ev0', np.c_[onsets, offsets], t) assert_almost_equal(X, mk_blk_tc()) assert_dict_almost_equal(contrasts, {'ev0_0': 1}) # Now as list X, contrasts = block_amplitudes('ev0', list(zip(onsets, offsets)), t) assert_almost_equal(X, mk_blk_tc()) assert_dict_almost_equal(contrasts, {'ev0_0': 1}) # Add amplitudes X_a, contrasts_a = block_amplitudes('ev1', amps, t) assert_almost_equal(X_a, mk_blk_tc(amplitudes=amplitudes)) assert_dict_almost_equal(contrasts_a, {'ev1_0': 1}) # Same thing as 2D array X_a, contrasts_a = block_amplitudes('ev1', np.c_[onsets, offsets, amplitudes], t) assert_almost_equal(X_a, mk_blk_tc(amplitudes=amplitudes)) assert_dict_almost_equal(contrasts_a, {'ev1_0': 1}) # Add another HRF X_2, contrasts_2 = block_amplitudes('ev0', no_amps, t, (glover, dglover)) assert_almost_equal(X_2, np.c_[mk_blk_tc(), mk_blk_tc(hrf=dglover)]) assert_dict_almost_equal(contrasts_2, {'ev0_0': [1, 0], 'ev0_1': [0, 1]}) # Errors on bad input no_start = make_recarray(zip(onsets, offsets), ('begin', 'end')) assert_raises(ValueError, block_amplitudes, 'ev0', no_start, t) no_end = make_recarray(zip(onsets, offsets), ('start', 'finish')) assert_raises(ValueError, block_amplitudes, 'ev0', no_end, t) funny_amp = make_recarray(zip(onsets, offsets, amplitudes), ('start', 'end', 'intensity')) assert_raises(ValueError, block_amplitudes, 'ev0', funny_amp, t) funny_extra = make_recarray(zip(onsets, offsets, amplitudes, onsets), ('start', 'end', 'amplitude', 'extra_field')) assert_raises(ValueError, block_amplitudes, 'ev0', funny_extra, t)
def test_sanity(): from nipy.modalities.fmri import design, hrf import nipy.modalities.fmri.fmristat.hrf as fshrf from nipy.modalities.fmri.fmristat.tests import FIACdesigns from nipy.modalities.fmri.fmristat.tests.test_FIAC import matchcol from nipy.algorithms.statistics import formula from nose.tools import assert_true """ Single subject fitting of FIAC model """ # Based on file # subj3_evt_fonc1.txt # subj3_bloc_fonc3.txt for subj, run, design_type in [(3, 1, 'event'), (3, 3, 'block')]: nvol = 191 TR = 2.5 Tstart = 1.25 volume_times = np.arange(nvol)*TR + Tstart volume_times_rec = formula.make_recarray(volume_times, 't') path_dict = {'subj':subj, 'run':run} if exists(pjoin(DATADIR, "fiac_%(subj)02d", "block", "initial_%(run)02d.csv") % path_dict): path_dict['design'] = 'block' else: path_dict['design'] = 'event' experiment = csv2rec(pjoin(DATADIR, "fiac_%(subj)02d", "%(design)s", "experiment_%(run)02d.csv") % path_dict) initial = csv2rec(pjoin(DATADIR, "fiac_%(subj)02d", "%(design)s", "initial_%(run)02d.csv") % path_dict) X_exper, cons_exper = design.event_design(experiment, volume_times_rec, hrfs=fshrf.spectral) X_initial, _ = design.event_design(initial, volume_times_rec, hrfs=[hrf.glover]) X, cons = design.stack_designs((X_exper, cons_exper), (X_initial, {})) # Get original fmristat design Xf = FIACdesigns.fmristat[design_type] # Check our new design can be closely matched to the original for i in range(X.shape[1]): # Columns can be very well correlated negatively or positively assert_true(abs(matchcol(X[:,i], Xf)[1]) > 0.999)
def model_generator(formula, data, volume_start_times, iterable=None, slicetimes=None, model_type=OLSModel, model_params = lambda x: ()): """ Generator for the models for a pass of fmristat analysis. """ volume_start_times = make_recarray(volume_start_times.astype(float), 't') # Generator for slices of the data with time as first axis axis0_gen = fmri_generator(data, iterable=iterable) # Iterate over 2D slices of the data for indexer, indexed_data in matrix_generator(axis0_gen): model_args = model_params(indexer) # model may depend on i # Get the design for these volume start times design = formula.design(volume_start_times, return_float=True) # Make the model from the design rmodel = model_type(design, *model_args) yield indexer, indexed_data, rmodel
def model_generator(formula, data, volume_start_times, iterable=None, slicetimes=None, model_type=OLSModel, model_params = lambda x: ()): """ Generator for the models for a pass of fmristat analysis. """ volume_start_times = make_recarray(volume_start_times.astype(float), 't') # Generator for slices of the data with time as first axis axis0_gen = axis0_generator(data, slicers=iterable) # Iterate over 2D slices of the data for indexer, indexed_data in matrix_generator(axis0_gen): model_args = model_params(indexer) # model may depend on i # Get the design for these volume start times design = formula.design(volume_start_times, return_float=True) # Make the model from the design rmodel = model_type(design, *model_args) yield indexer, indexed_data, rmodel
def mk_blk_spec(factors, names): names = ('start', 'end') + tuple(names) return make_recarray(zip(onsets, offsets, *factors), names)
def mk_ev_spec(factors, names): names = ('time',) + tuple(names) if len(factors) == 0: return make_recarray(onsets, names) return make_recarray(zip(onsets, *factors), names)
def test_event_design(): # Test event design helper function # An event design with one event type onsets = np.array([0, 20, 40, 60]) durations = np.array([2, 3, 4, 5]) offsets = onsets + durations c_fac = np.array([1, 1, 1, 1]) # constant factor (one level) fac_1 = np.array([0, 1, 0, 1]) # factor 1, two levels fac_2 = np.array([0, 0, 1, 1]) # factor 2, two levels t = np.arange(0, 100, 2) def mk_ev_spec(factors, names): names = ('time',) + tuple(names) if len(factors) == 0: return make_recarray(onsets, names) return make_recarray(zip(onsets, *factors), names) def mk_blk_spec(factors, names): names = ('start', 'end') + tuple(names) return make_recarray(zip(onsets, offsets, *factors), names) def mk_ev_tc(ev_inds): # Make real time course for given event onsets return lambdify_t(events(onsets[ev_inds], f=glover))(t) def mk_blk_tc(ev_inds): # Make real time course for block onset / offsets B = blocks(zip(onsets[ev_inds], offsets[ev_inds])) term = convolve_functions(B, glover(T), (-5, 70), # step func support (0, 30.), # conv kernel support 0.02) # dt return lambdify_t(term)(t) for d_maker, spec_maker, tc_maker, null_name in ( (event_design, mk_ev_spec, mk_ev_tc, '_event_'), (block_design, mk_blk_spec, mk_blk_tc, '_block_')): # Event spec with no event factor -> single column design, no contrasts spec_0 = spec_maker((), ()) X_0, contrasts_0 = d_maker(spec_0, t) exp_x_0 = tc_maker(onsets==onsets) assert_almost_equal(X_0, exp_x_0) assert_dict_almost_equal(contrasts_0, dict(constant_0=1)) X_0, contrasts_0 = d_maker(spec_0, t, level_contrasts=True) assert_almost_equal(X_0, exp_x_0) assert_dict_almost_equal(contrasts_0, {'constant_0': 1, null_name + '_1_0': 1}) # Event spec with single factor, but only one level spec_1c = spec_maker((c_fac,), ('smt',)) X_1c, contrasts_1c = d_maker(spec_1c, t) assert_almost_equal(X_1c, exp_x_0) assert_dict_almost_equal(contrasts_1c, dict(constant_0=1)) X_1c, contrasts_1c = d_maker(spec_1c, t, level_contrasts=True) assert_dict_almost_equal(contrasts_1c, dict(constant_0=1, smt_1_0=1)) # Event spec with single factor, two levels spec_1d = spec_maker((fac_1,), ('smt',)) exp_x_0 = tc_maker(fac_1 == 0) exp_x_1 = tc_maker(fac_1 == 1) X_1d, contrasts_1d = d_maker(spec_1d, t) assert_almost_equal(X_1d, np.c_[exp_x_0, exp_x_1]) assert_dict_almost_equal(contrasts_1d, dict(constant_0=[1, 1], smt_0=[1, -1])) X_1d, contrasts_1d = d_maker(spec_1d, t, level_contrasts=True) assert_dict_almost_equal(contrasts_1d, dict(constant_0=1, smt_0=[1, -1], # main effect smt_0_0=[1, 0], # level 0, hrf 0 smt_1_0=[0, 1])) # level 1, hrf 0 # Event spec with two factors, one with two levels, another with one spec_2dc = spec_maker((fac_1, c_fac), ('smt', 'smte')) X_2dc, contrasts_2dc = d_maker(spec_2dc, t) assert_almost_equal(X_2dc, np.c_[exp_x_0, exp_x_1]) assert_dict_almost_equal(contrasts_2dc, {'constant_0': [1, 1], 'smt_0': [1, -1], # main effect 'smt:smte_0': [1, -1], # interaction }) X_2dc, contrasts_2dc = d_maker(spec_2dc, t, level_contrasts=True) assert_dict_almost_equal(contrasts_2dc, {'constant_0': [1, 1], 'smt_0': [1, -1], # main effect 'smt:smte_0': [1, -1], # interaction 'smt_0*smte_1_0': [1, 0], # smt 0, smte 0, hrf 0 'smt_1*smte_1_0': [0, 1], # smt 1, smte 0, hrf 0 }) # Event spec with two factors, both with two levels spec_2dd = spec_maker((fac_1, fac_2), ('smt', 'smte')) exp_x_0 = tc_maker((fac_1 == 0) & (fac_2 == 0)) exp_x_1 = tc_maker((fac_1 == 0) & (fac_2 == 1)) exp_x_2 = tc_maker((fac_1 == 1) & (fac_2 == 0)) exp_x_3 = tc_maker((fac_1 == 1) & (fac_2 == 1)) X_2dd, contrasts_2dd = d_maker(spec_2dd, t) assert_almost_equal(X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]) exp_cons = {'constant_0': [1, 1, 1, 1], 'smt_0': [1, 1, -1, -1], # main effect fac_1 'smte_0': [1, -1, 1, -1], # main effect fac_2 'smt:smte_0': [1, -1, -1, 1], # interaction } assert_dict_almost_equal(contrasts_2dd, exp_cons) X_2dd, contrasts_2dd = d_maker(spec_2dd, t, level_contrasts=True) level_cons = exp_cons.copy() level_cons.update({ 'smt_0*smte_0_0': [1, 0, 0, 0], # smt 0, smte 0, hrf 0 'smt_0*smte_1_0': [0, 1, 0, 0], # smt 0, smte 1, hrf 0 'smt_1*smte_0_0': [0, 0, 1, 0], # smt 1, smte 0, hrf 0 'smt_1*smte_1_0': [0, 0, 0, 1], # smt 1, smte 1, hrf 0 }) assert_dict_almost_equal(contrasts_2dd, level_cons) # Test max order >> 2, no error X_2dd, contrasts_2dd = d_maker(spec_2dd, t, order=100) assert_almost_equal(X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]) assert_dict_almost_equal(contrasts_2dd, exp_cons) # Test max order = 1 X_2dd, contrasts_2dd = d_maker(spec_2dd, t, order=1) assert_almost_equal(X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]) # No interaction assert_dict_almost_equal(contrasts_2dd, {'constant_0': [1, 1, 1, 1], 'smt_0': [1, 1, -1, -1], # main effect fac_1 'smte_0': [1, -1, 1, -1], # main effect fac_2 }) # events : test field called "time" is necessary spec_1d = make_recarray(zip(onsets, fac_1), ('brighteyes', 'smt')) assert_raises(ValueError, event_design, spec_1d, t) # blocks : test fields called "start" and "end" are necessary spec_1d = make_recarray(zip(onsets, offsets, fac_1), ('mister', 'brighteyes', 'smt')) assert_raises(ValueError, block_design, spec_1d, t) spec_1d = make_recarray(zip(onsets, offsets, fac_1), ('start', 'brighteyes', 'smt')) assert_raises(ValueError, block_design, spec_1d, t) spec_1d = make_recarray(zip(onsets, offsets, fac_1), ('mister', 'end', 'smt')) assert_raises(ValueError, block_design, spec_1d, t)