def test_function_ptrs(fname): skip_if_no_external('nibabel') ds = load_example_fmri_dataset() # add a mapper with a function ptr inside ds = ds.get_mapped(mean_sample()) h5save(fname, ds) ds_loaded = h5load(fname) fresh = load_example_fmri_dataset().O # check that the reconstruction function pointer in the FxMapper points # to the right one assert_array_equal(ds_loaded.a.mapper.forward(fresh), ds.samples)
def test_function_ptrs(): if not externals.exists('nibabel'): raise SkipTest ds = load_example_fmri_dataset() # add a mapper with a function ptr inside ds = ds.get_mapped(mean_sample()) f = tempfile.NamedTemporaryFile() h5save(f.name, ds) ds_loaded = h5load(f.name) fresh = load_example_fmri_dataset().O # check that the reconstruction function pointer in the FxMapper points # to the right one assert_array_equal(ds_loaded.a.mapper.forward(fresh), ds.samples)
def generate_testing_fmri_dataset(filename=None): """Helper to generate a dataset for regression testing of mvpa2/nibabel Parameters ---------- filename : str Filename of a dataset file to store. If not provided, it is composed using :func:`get_testing_fmri_dataset_filename` Returns ------- Dataset, string Generated dataset, filename to the HDF5 where it was stored """ import mvpa2 from mvpa2.base.hdf5 import h5save from mvpa2.datasets.sources import load_example_fmri_dataset # Load our sample dataset ds_full = load_example_fmri_dataset(name='1slice', literal=False) # Subselect a small "ROI" ds = ds_full[20:23, 10:14] # collect all versions/dependencies for possible need to troubleshoot later ds.a['wtf'] = mvpa2.wtf() ds.a['versions'] = mvpa2.externals.versions # save to a file identified by version of PyMVPA and nibabel and hash of # all other versions out_filename = filename or get_testing_fmri_dataset_filename() h5save(out_filename, ds, compression=9) # ATM it produces >700kB .hdf5 which is this large because of # the ds.a.mapper with both Flatten and StaticFeatureSelection occupying # more than 190kB each, with ds.a.mapper as a whole generating 570kB file # Among those .ca seems to occupy notable size, e.g. 130KB for the FlattenMapper # even though no heavy storage is really needed for any available value -- # primarily all is meta-information embedded into hdf5 to describe our things return ds, out_filename
def test_get_contrasts(): # preamble borrowed from the previous test skip_if_no_external('nibabel') skip_if_no_external('nipy') # ATM relies on NiPy's GLM implementation # taking subset of the dataset to speed testing up ds = load_example_fmri_dataset('25mm', literal=True)[{ 'chunks': [0, 1] }, :3] # TODO: simulate short dataset with known properties and use it # for testing events = find_events(targets=ds.sa.targets, chunks=ds.sa.chunks) tr = ds.a.imghdr['pixdim'][4] for ev in events: for a in ('onset', 'duration'): ev[a] = ev[a] * tr evds = fit_event_hrf_model( ds, events=events, time_attr='time_coords', condition_attr='targets', design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), return_model=True, ) # Simple one -- stat per each condition cons = get_contrasts(evds) # and let's get p-values cons_p = get_contrasts(evds, fxname='p_value') # Without contrasts explicitly prescribed -- there will be one per each # condition assert_array_equal(cons.UT, evds.UT) # and per each feature assert_equal(cons.shape, (len(evds.UT), evds.nfeatures)) assert_array_less(cons_p, 1) assert_array_less(0, cons_p) cons_fh = get_contrasts( evds, contrasts={ 'face-house': { 'face': 1, 'house': -1 }, 'betterface': { 'face': 1, 'house': -0.5, 'scrambledpix': -0.5 } }, ) # print(cons_fh.samples) assert_array_equal(cons_fh.UT, ['betterface', 'face-house']) # and nipy does one tailed test so all p-values should correspond to z-s skip_if_no_external('scipy') import scipy.stats.distributions as ssd assert_array_almost_equal(ssd.norm().isf(cons_p), cons)
def test_hrf_modeling(): skip_if_no_external('nibabel') skip_if_no_external('nipy') # ATM relies on NiPy's GLM implementation # taking subset of the dataset to speed testing up ds = load_example_fmri_dataset('25mm', literal=True)[{ 'chunks': [0, 1] }, :3] # TODO: simulate short dataset with known properties and use it # for testing events = find_events(targets=ds.sa.targets, chunks=ds.sa.chunks) tr = ds.a.imghdr['pixdim'][4] for ev in events: for a in ('onset', 'duration'): ev[a] = ev[a] * tr evds = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # same voxels assert_equal(ds.nfeatures, evds.nfeatures) assert_array_equal(ds.fa.voxel_indices, evds.fa.voxel_indices) # one sample for each condition, plus constant assert_equal(sorted(ds.sa['targets'].unique), sorted(evds.sa.targets)) assert_equal(evds.a.add_regs.sa.regressor_names[0], 'constant') # with centered data zscore(ds) evds_demean = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # after demeaning the constant should consume a lot less assert (evds.a.add_regs[0].samples.mean() > evds_demean.a.add_regs[0].samples.mean()) # from eyeballing the sensitivity example -- would be better to test this on # the tutorial data assert(evds_demean[evds.sa.targets == 'shoe'].samples.max() \ > evds_demean[evds.sa.targets == 'bottle'].samples.max()) # HRF models assert ('regressors' in evds.sa) assert ('regressors' in evds.a.add_regs.sa) assert_equal(evds.sa.regressors.shape[1], len(ds)) # custom regressors evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # one more output sample in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 1, len(evds.a.add_regs)) # comes last before constant assert_equal('time_indices', evds_regrs.a.add_regs.sa.regressor_names[-2]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom multiple regressors ds.sa['time_indices_squared'] = ds.sa.time_indices**2 # custom regressors evds_regrs = eventrelated_dataset( ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices', 'time_indices_squared'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # two more output samples in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 2, len(evds.a.add_regs)) # come last before constant assert_array_equal(['time_indices', 'time_indices_squared'], evds_regrs.a.add_regs.sa.regressor_names[-3:-1]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom multiple regressors with multidimensional one ds.sa['time_indices_powers'] = np.vstack( [ds.sa.time_indices**2, ds.sa.time_indices**3]).T # custom regressors evds_regrs = eventrelated_dataset( ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices', 'time_indices_powers'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # three more output samples in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 3, len(evds.a.add_regs)) # come last before constant assert_array_equal( ['time_indices', 'time_indices_powers.0', 'time_indices_powers.1'], evds_regrs.a.add_regs.sa.regressor_names[-4:-1]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom regressors from external sources evds_regrs = eventrelated_dataset( ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_coords'], design_kwargs=dict(drift_model='blank', add_regs=np.linspace(1, -1, len(ds))[None].T, add_reg_names=['negative_trend']), glmfit_kwargs=dict(model='ols'), model='hrf') assert_equal(len(evds_regrs), len(evds)) # But we got one more in additional regressors assert_equal(len(evds_regrs.a.add_regs) - 2, len(evds.a.add_regs)) # comes last before constant assert_array_equal(['negative_trend', 'time_coords', 'constant'], evds_regrs.a.add_regs.sa.regressor_names) # order is otherwise unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # HRF models with estimating per each chunk assert_equal(ds.sa.time_coords[0], 0) evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr=['targets', 'chunks'], regr_attrs=['time_indices'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') assert_true('add_regs' in evds_regrs.a) assert_true('time_indices' in evds_regrs.a.add_regs.sa.regressor_names) assert_equal(len(ds.UC) * len(ds.UT), len(evds_regrs)) assert_equal(len(evds_regrs.UC) * len(evds_regrs.UT), len(evds_regrs)) from mvpa2.mappers.fx import mean_group_sample evds_regrs_meaned = mean_group_sample(['targets'])(evds_regrs) assert_array_equal(evds_regrs_meaned.T, evds.T) # targets should be the same
def test_hrf_modeling(): skip_if_no_external('nibabel') skip_if_no_external('nipy') # ATM relies on NiPy's GLM implementation # taking subset of the dataset to speed testing up ds = load_example_fmri_dataset('25mm', literal=True)[{'chunks': [0, 1]}, :3] # TODO: simulate short dataset with known properties and use it # for testing events = find_events(targets=ds.sa.targets, chunks=ds.sa.chunks) tr = ds.a.imghdr['pixdim'][4] for ev in events: for a in ('onset', 'duration'): ev[a] = ev[a] * tr evds = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # same voxels assert_equal(ds.nfeatures, evds.nfeatures) assert_array_equal(ds.fa.voxel_indices, evds.fa.voxel_indices) # one sample for each condition, plus constant assert_equal(sorted(ds.sa['targets'].unique), sorted(evds.sa.targets)) assert_equal(evds.a.add_regs.sa.regressor_names[0], 'constant') # with centered data zscore(ds) evds_demean = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # after demeaning the constant should consume a lot less assert(evds.a.add_regs[0].samples.mean() > evds_demean.a.add_regs[0].samples.mean()) # from eyeballing the sensitivity example -- would be better to test this on # the tutorial data assert(evds_demean[evds.sa.targets == 'shoe'].samples.max() \ > evds_demean[evds.sa.targets == 'bottle'].samples.max()) # HRF models assert('regressors' in evds.sa) assert('regressors' in evds.a.add_regs.sa) assert_equal(evds.sa.regressors.shape[1], len(ds)) # custom regressors evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # one more output sample in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 1, len(evds.a.add_regs)) # comes last before constant assert_equal('time_indices', evds_regrs.a.add_regs.sa.regressor_names[-2]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom multiple regressors ds.sa['time_indices_squared'] = ds.sa.time_indices ** 2 # custom regressors evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices', 'time_indices_squared'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # two more output samples in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 2, len(evds.a.add_regs)) # come last before constant assert_array_equal( ['time_indices', 'time_indices_squared'], evds_regrs.a.add_regs.sa.regressor_names[-3:-1]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom multiple regressors with multidimensional one ds.sa['time_indices_powers'] = np.vstack([ds.sa.time_indices ** 2, ds.sa.time_indices ** 3]).T # custom regressors evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_indices', 'time_indices_powers'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') # verify that nothing screwed up time_coords assert_equal(ds.sa.time_coords[0], 0) assert_equal(len(evds_regrs), len(evds)) # three more output samples in .a.add_regs assert_equal(len(evds_regrs.a.add_regs) - 3, len(evds.a.add_regs)) # come last before constant assert_array_equal( ['time_indices', 'time_indices_powers.0', 'time_indices_powers.1'], evds_regrs.a.add_regs.sa.regressor_names[-4:-1]) # order of main regressors is unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # custom regressors from external sources evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr='targets', regr_attrs=['time_coords'], design_kwargs=dict(drift_model='blank', add_regs=np.linspace(1, -1, len(ds))[None].T, add_reg_names=['negative_trend']), glmfit_kwargs=dict(model='ols'), model='hrf') assert_equal(len(evds_regrs), len(evds)) # But we got one more in additional regressors assert_equal(len(evds_regrs.a.add_regs) - 2, len(evds.a.add_regs)) # comes last before constant assert_array_equal(['negative_trend', 'time_coords', 'constant'], evds_regrs.a.add_regs.sa.regressor_names) # order is otherwise unchanged assert_array_equal(evds.sa.targets, evds_regrs.sa.targets) # HRF models with estimating per each chunk assert_equal(ds.sa.time_coords[0], 0) evds_regrs = eventrelated_dataset(ds, events, time_attr='time_coords', condition_attr=['targets', 'chunks'], regr_attrs=['time_indices'], design_kwargs=dict(drift_model='blank'), glmfit_kwargs=dict(model='ols'), model='hrf') assert_true('add_regs' in evds_regrs.a) assert_true('time_indices' in evds_regrs.a.add_regs.sa.regressor_names) assert_equal(len(ds.UC) * len(ds.UT), len(evds_regrs)) assert_equal(len(evds_regrs.UC) * len(evds_regrs.UT), len(evds_regrs)) from mvpa2.mappers.fx import mean_group_sample evds_regrs_meaned = mean_group_sample(['targets'])(evds_regrs) assert_array_equal(evds_regrs_meaned.T, evds.T) # targets should be the same