def load_example_fmri_dataset(name='1slice', literal=False): """Load minimal fMRI dataset that is shipped with PyMVPA.""" from mvpa2.datasets.eventrelated import events2sample_attr from mvpa2.datasets.sources.openfmri import OpenFMRIDataset from mvpa2.datasets.mri import fmri_dataset from mvpa2.misc.io import SampleAttributes basedir = os.path.join(pymvpa_dataroot, 'openfmri') mask = {'1slice': os.path.join(pymvpa_dataroot, 'mask.nii.gz'), '25mm': os.path.join(basedir, 'sub001', 'masks', '25mm', 'brain.nii.gz')}[name] if literal: model = 1 subj = 1 openfmri = OpenFMRIDataset(basedir) ds = openfmri.get_model_bold_dataset(model, subj, flavor=name, mask=mask, noinfolabel='rest') # re-imagine the global time_coords of a concatenated time series # this is only for the purpose of keeping the example data in the # exact same shape as it has always been. in absolute terms this makes no # sense as there is no continuous time in this dataset ds.sa['run_time_coords'] = ds.sa.time_coords ds.sa['time_coords'] = np.arange(len(ds)) * 2.5 else: if name == '25mm': raise ValueError("The 25mm dataset is no longer available with " "numerical labels") attr = SampleAttributes(os.path.join(pymvpa_dataroot, 'attributes.txt')) ds = fmri_dataset(samples=os.path.join(pymvpa_dataroot, 'bold.nii.gz'), targets=attr.targets, chunks=attr.chunks, mask=mask) return ds
def load_example_fmri_dataset(name="1slice", literal=False): """Load minimal fMRI dataset that is shipped with PyMVPA.""" from mvpa2.datasets.sources.openfmri import OpenFMRIDataset from mvpa2.datasets.mri import fmri_dataset from mvpa2.misc.io import SampleAttributes basedir = op.join(pymvpa_dataroot, "haxby2001") mask = { "1slice": op.join(pymvpa_dataroot, "mask.nii.gz"), "25mm": op.join(basedir, "sub001", "masks", "25mm", "brain.nii.gz"), }[name] if literal: model = 1 subj = 1 openfmri = OpenFMRIDataset(basedir) ds = openfmri.get_model_bold_dataset(model, subj, flavor=name, mask=mask, noinfolabel="rest") # re-imagine the global time_coords of a concatenated time series # this is only for the purpose of keeping the example data in the # exact same shape as it has always been. in absolute terms this makes no # sense as there is no continuous time in this dataset ds.sa["run_time_coords"] = ds.sa.time_coords ds.sa["time_coords"] = np.arange(len(ds)) * 2.5 else: if name == "25mm": raise ValueError("The 25mm dataset is no longer available with " "numerical labels") attr = SampleAttributes(op.join(pymvpa_dataroot, "attributes.txt")) ds = fmri_dataset( samples=op.join(pymvpa_dataroot, "bold.nii.gz"), targets=attr.targets, chunks=attr.chunks, mask=mask ) return ds
def load_example_fmri_dataset(name='1slice', literal=False): """Load minimal fMRI dataset that is shipped with PyMVPA.""" from mvpa2.datasets.sources.openfmri import OpenFMRIDataset from mvpa2.datasets.mri import fmri_dataset from mvpa2.misc.io import SampleAttributes basedir = pathjoin(pymvpa_dataroot, 'haxby2001') mask = {'1slice': pathjoin(pymvpa_dataroot, 'mask.nii.gz'), '25mm': pathjoin(basedir, 'sub001', 'masks', '25mm', 'brain.nii.gz')}[name] if literal: model = 1 subj = 1 openfmri = OpenFMRIDataset(basedir) ds = openfmri.get_model_bold_dataset(model, subj, flavor=name, mask=mask, noinfolabel='rest') # re-imagine the global time_coords of a concatenated time series # this is only for the purpose of keeping the example data in the # exact same shape as it has always been. in absolute terms this makes no # sense as there is no continuous time in this dataset ds.sa['run_time_coords'] = ds.sa.time_coords ds.sa['time_coords'] = np.arange(len(ds)) * 2.5 else: if name == '25mm': raise ValueError("The 25mm dataset is no longer available with " "numerical labels") attr = SampleAttributes(pathjoin(pymvpa_dataroot, 'attributes.txt')) ds = fmri_dataset(samples=pathjoin(pymvpa_dataroot, 'bold.nii.gz'), targets=attr.targets, chunks=attr.chunks, mask=mask) return ds
def load_datadb_tutorial_data(path=os.path.join( pymvpa_datadbroot, 'tutorial_data', 'tutorial_data', 'data'), roi='brain', add_fa=None): """Loads the block-design demo dataset from PyMVPA dataset DB. Parameters ---------- path : str Path of the directory containing the dataset files. roi : str or int or tuple or None Region Of Interest to be used for masking the dataset. If a string is given a corresponding mask image from the demo dataset will be used (mask_<str>.nii.gz). If an int value is given, the corresponding ROI is determined from the atlas image (mask_hoc.nii.gz). If a tuple is provided it may contain int values that a processed as explained before, but the union of a ROIs is taken to produce the final mask. If None, no masking is performed. add_fa : dict Passed on to the dataset creator function (see fmri_dataset() for more information). """ import nibabel as nb from mvpa2.datasets.sources.openfmri import OpenFMRIDataset task = model = subj = 1 dhandle = OpenFMRIDataset(path) maskpath = os.path.join(path, 'sub001', 'masks', 'orig') if roi is None: mask = None elif isinstance(roi, str): mask = os.path.join(maskpath, roi + '.nii.gz') elif isinstance(roi, int): nimg = nb.load(os.path.join(maskpath, 'hoc.nii.gz')) tmpmask = nimg.get_data() == roi mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, tuple) or isinstance(roi, list): nimg = nb.load(os.path.join(maskpath, 'hoc.nii.gz')) if externals.versions['nibabel'] >= '1.2': img_shape = nimg.shape else: img_shape = nimg.get_shape() tmpmask = np.zeros(img_shape, dtype='bool') for r in roi: tmpmask = np.logical_or(tmpmask, nimg.get_data() == r) mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, nb.Nifti1Image): mask=roi else: raise ValueError("Got something as mask that I cannot handle.") ds = dhandle.get_model_bold_dataset(model, subj, mask=mask, add_fa=add_fa, noinfolabel='rest') # fixup time_coords to make the impression of a continuous time series # this is only necessary until we have changed the tutorial to # show/encourage run-wise processing ds.sa['time_coords'] = np.linspace(0, (len(ds) * 2.5), len(ds) + 1)[:-1] return ds
def create_betas_per_run_with_pymvpa(study_path, subj, conf, mask_name, flavor): of = OpenFMRIDataset(study_path) mask_fname = _opj(study_path, "sub{:0>3d}".format(subj), "masks", conf.mvpa_tasks[0], "{}.nii.gz".format(mask_name)) ds = of.get_model_bold_dataset( model_id=1, subj_id=subj, flavor=flavor, mask=mask_fname, # preproc_img=smooth, preproc_ds=detrend, modelfx=fit_event_hrf_model, time_attr="time_coords", condition_attr="condition", ) return ds
def make_ds(sub, datapath, flavor): of = OpenFMRIDataset(datapath) ds = of.get_model_bold_dataset( model_id=1, subj_id=sub, #ds = of.get_bold_run_dataset(1,1,1, flavor=flavor, mask=_opj( datapath, 'sub%.3i' % sub, 'masks', 'task001_run001', 'grey.nii.gz'), #preproc_img=smooth, #preproc_ds = detrend, #modelfx=fit_event_hrf_model, time_attr='time_coords', condition_attr='condition') for i in np.unique(ds.chunks)[5:]: detrend(ds[ds.chunks == i])
def make_ds(sub, datapath, flavor): of = OpenFMRIDataset(datapath) ds = of.get_model_bold_dataset( model_id=1, subj_id=sub, flavor=flavor, mask=_opj(datapath, 'sub%.3i' % sub, 'masks', 'task001_run001', 'grey.nii.gz'), #preproc_img=smooth, preproc_ds=detrend, modelfx=fit_event_hrf_model, time_attr='time_coords', condition_attr='condition') ds14 = ds[np.array([c in ['G1', 'G4'] for c in ds.sa['condition']])] ds23 = ds[np.array([c in ['G2', 'G3'] for c in ds.sa['condition']])] result_dir = _opj(datapath, 'mvpa', 'ds', flavor) if not os.path.isdir(result_dir): os.makedirs(result_dir) print "{:0>3d}-ds14 {},{}".format(sub, ds14.shape, ds14.sa.condition) print "{:0>3d}-ds23 {},{}".format(sub, ds23.shape, ds23.sa.condition) h5save(_opj(result_dir, 'sub%.3i_14_hrf.hdf5' % sub), ds14) h5save(_opj(result_dir, 'sub%.3i_23_hrf.hdf5' % sub), ds23)
def make_ds(sub, datapath, flavor): of = OpenFMRIDataset(datapath) ds = of.get_model_bold_dataset( model_id=1, subj_id=sub, flavor=flavor, mask=_opj(datapath, "sub%.3i" % sub, "masks", "task001_run001", "grey.nii.gz"), # preproc_img=smooth, preproc_ds=detrend, modelfx=fit_event_hrf_model, time_attr="time_coords", condition_attr="condition", ) ds14 = ds[np.array([c in ["G1", "G4"] for c in ds.sa["condition"]])] ds23 = ds[np.array([c in ["G2", "G3"] for c in ds.sa["condition"]])] result_dir = _opj(datapath, "mvpa", "ds", flavor) if not os.path.isdir(result_dir): os.makedirs(result_dir) print "{:0>3d}-ds14 {},{}".format(sub, ds14.shape, ds14.sa.condition) print "{:0>3d}-ds23 {},{}".format(sub, ds23.shape, ds23.sa.condition) h5save(_opj(result_dir, "sub%.3i_14_hrf.hdf5" % sub), ds14) h5save(_opj(result_dir, "sub%.3i_23_hrf.hdf5" % sub), ds23)
def load_tutorial_data(path=None, roi='brain', add_fa=None, flavor=None): """Loads the block-design demo dataset from PyMVPA dataset DB. Parameters ---------- path : str, optional Path to the directory with the extracted content of the tutorial data package. This is only necessary for accessing the full resolution data. The ``1slice``, and ``25mm`` flavors are shipped with PyMVPA itself, and the path argument is ignored for them. This function also honors the MVPA_LOCATION_TUTORIAL_DATA environment variable, and the respective configuration setting. roi : str or int or tuple or None, optional Region Of Interest to be used for masking the dataset. If a string is given a corresponding mask image from the demo dataset will be used (mask_<str>.nii.gz). If an int value is given, the corresponding ROI is determined from the atlas image (mask_hoc.nii.gz). If a tuple is provided it may contain int values that a processed as explained before, but the union of a ROIs is taken to produce the final mask. If None, no masking is performed. add_fa : dict, optional Passed on to the dataset creator function (see fmri_dataset() for more information). flavor: str, optional Resolution flavor of the data to load. By default, the data is loaded in its original resolution. The PyMVPA source distribution contains a '25mm' flavor that has been downsampled to a very coarse resolution and can be used for quick test execution. Likewise a ``1slice`` flavor is available that contents a full-resultion single-slice subset of the dataset. """ if path is None: if flavor in ('1slice', '25mm'): # we know that this part is there path = op.join(pymvpa_dataroot) else: # check config for info, pretend it is in the working dir otherwise path = mvpa2.cfg.get('location', 'tutorial data', default=op.curdir) # we need the haxby2001 portion of the tutorial data path = op.join(path, 'haxby2001') import nibabel as nb from mvpa2.datasets.sources.openfmri import OpenFMRIDataset model = subj = 1 dhandle = OpenFMRIDataset(path) if flavor is None: maskpath = op.join(path, 'sub001', 'masks', 'orig') else: maskpath = op.join(path, 'sub001', 'masks', flavor) if roi is None: mask = None elif isinstance(roi, str): mask = op.join(maskpath, roi + '.nii.gz') elif isinstance(roi, int): nimg = nb.load(op.join(maskpath, 'hoc.nii.gz')) tmpmask = nimg.get_data() == roi mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, tuple) or isinstance(roi, list): nimg = nb.load(op.join(maskpath, 'hoc.nii.gz')) if externals.versions['nibabel'] >= '1.2': img_shape = nimg.shape else: img_shape = nimg.get_shape() tmpmask = np.zeros(img_shape, dtype='bool') for r in roi: tmpmask = np.logical_or(tmpmask, nimg.get_data() == r) mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, nb.Nifti1Image): mask = roi else: raise ValueError("Got something as mask that I cannot handle.") ds = dhandle.get_model_bold_dataset(model, subj, flavor=flavor, mask=mask, add_fa=add_fa, noinfolabel='rest') # fixup time_coords to make the impression of a continuous time series # this is only necessary until we have changed the tutorial to # show/encourage run-wise processing ds.sa['time_coords'] = np.linspace(0, (len(ds) * 2.5), len(ds) + 1)[:-1] return ds
datapath = 'BASEDIR' of = OpenFMRIDataset(datapath) sub = int(sys.argv[1]) + 1 def smooth(img): # we need to preserve the original header because the smoothing function # f***s the TR up nimg = smooth_img(img, fwhm=2.0) return nb.Nifti1Image(nimg.get_data(), img.get_affine(), header=img.get_header()) ds = of.get_model_bold_dataset( model_id=1, subj_id=sub, flavor='dico_bold7Tp1_to_subjbold7Tp1', # full brain mask=_opj(datapath, 'sub%.3i' % sub, 'templates', 'bold7Tp1', 'qa', 'jointfgbrainmask_bold7Tp1_to_subjbold7Tp1.nii.gz'), preproc_img=smooth, # HP filtering is done by NiPy's GLM modelfx=fit_event_hrf_model, time_attr='time_coords', condition_attr='condition') h5save(_opj('data', 'sub%.3i_2.0mm_hrf.hdf5' % sub), ds)
def run(args): from mvpa2.base.hdf5 import h5save ds = None vol_attr = dict() if args.add_vol_attr is not None: # XXX add a way to use the mapper of an existing dataset to # add a volume attribute without having to load the entire # mri data again vol_attr = dict(args.add_vol_attr) if not len(args.add_vol_attr) == len(vol_attr): warning("--vol-attr option with duplicate attribute name: " "check arguments!") verbose(2, "Prepare to add volumetric feature attributes: %s" % vol_attr) if args.txt_data is not None: verbose(1, "Load data from TXT file '%s'" % args.txt_data) samples = _load_from_txt(args.txt_data) ds = Dataset(samples) elif args.npy_data is not None: verbose(1, "Load data from NPY file '%s'" % args.npy_data) samples = _load_from_npy(args.npy_data) ds = Dataset(samples) elif args.mri_data is not None: verbose(1, "Load data from MRI image(s) %s" % args.mri_data) from mvpa2.datasets.mri import fmri_dataset ds = fmri_dataset(args.mri_data, mask=args.mask, add_fa=vol_attr) elif args.openfmri_modelbold is not None: verbose(1, "Load data from OpenFMRI model specification %s" % args.openfmri_modelbold) if not len(args.openfmri_modelbold[3]): args.openfmri_modelbold[3] = None # load openfmri dataset from mvpa2.datasets.sources.openfmri import OpenFMRIDataset of = OpenFMRIDataset(args.openfmri_modelbold[0]) ds = of.get_model_bold_dataset( int(args.openfmri_modelbold[1]), int(args.openfmri_modelbold[2]), flavor=args.openfmri_modelbold[3], mask=args.mask, add_fa=vol_attr, add_sa=args.add_fsl_mcpar, ) if ds is None: if args.data is None: raise RuntimeError("no data source specific") else: ds = hdf2ds(args.data)[0] else: if args.data is not None: verbose(1, "ignoring dataset input in favor of other data source -- remove either one to disambiguate") # act on all attribute options ds = process_common_dsattr_opts(ds, args) if args.openfmri_modelbold is None and args.add_fsl_mcpar is not None: from mvpa2.misc.fsl.base import McFlirtParams mc_par = McFlirtParams(args.add_fsl_mcpar) for param in mc_par: verbose(2, "Add motion regressor as sample attribute '%s'" % ("mc_" + param)) ds.sa["mc_" + param] = mc_par[param] verbose(3, "Dataset summary %s" % (ds.summary())) # and store outfilename = args.output if not outfilename.endswith(".hdf5"): outfilename += ".hdf5" verbose(1, "Save dataset to '%s'" % outfilename) h5save(outfilename, ds, mkdir=True, compression=args.hdf5_compression)
def load_tutorial_data(path=None, roi="brain", add_fa=None, flavor=None): """Loads the block-design demo dataset from PyMVPA dataset DB. Parameters ---------- path : str, optional Path to the directory with the extracted content of the tutorial data package. This is only necessary for accessing the full resolution data. The ``1slice``, and ``25mm`` flavors are shipped with PyMVPA itself, and the path argument is ignored for them. This function also honors the MVPA_LOCATION_TUTORIAL_DATA environment variable, and the respective configuration setting. roi : str or int or tuple or None, optional Region Of Interest to be used for masking the dataset. If a string is given a corresponding mask image from the demo dataset will be used (mask_<str>.nii.gz). If an int value is given, the corresponding ROI is determined from the atlas image (mask_hoc.nii.gz). If a tuple is provided it may contain int values that a processed as explained before, but the union of a ROIs is taken to produce the final mask. If None, no masking is performed. add_fa : dict, optional Passed on to the dataset creator function (see fmri_dataset() for more information). flavor: str, optional Resolution flavor of the data to load. By default, the data is loaded in its original resolution. The PyMVPA source distribution contains a '25mm' flavor that has been downsampled to a very coarse resolution and can be used for quick test execution. Likewise a ``1slice`` flavor is available that contents a full-resultion single-slice subset of the dataset. """ if path is None: if flavor in ("1slice", "25mm"): # we know that this part is there path = op.join(pymvpa_dataroot) else: # check config for info, pretend it is in the working dir otherwise path = mvpa2.cfg.get("location", "tutorial data", default=op.curdir) # we need the haxby2001 portion of the tutorial data path = op.join(path, "haxby2001") import nibabel as nb from mvpa2.datasets.sources.openfmri import OpenFMRIDataset model = subj = 1 dhandle = OpenFMRIDataset(path) if flavor is None: maskpath = op.join(path, "sub001", "masks", "orig") else: maskpath = op.join(path, "sub001", "masks", flavor) if roi is None: mask = None elif isinstance(roi, str): mask = op.join(maskpath, roi + ".nii.gz") elif isinstance(roi, int): nimg = nb.load(op.join(maskpath, "hoc.nii.gz")) tmpmask = nimg.get_data() == roi mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, tuple) or isinstance(roi, list): nimg = nb.load(op.join(maskpath, "hoc.nii.gz")) if externals.versions["nibabel"] >= "1.2": img_shape = nimg.shape else: img_shape = nimg.get_shape() tmpmask = np.zeros(img_shape, dtype="bool") for r in roi: tmpmask = np.logical_or(tmpmask, nimg.get_data() == r) mask = nb.Nifti1Image(tmpmask.astype(int), nimg.get_affine(), nimg.get_header()) elif isinstance(roi, nb.Nifti1Image): mask = roi else: raise ValueError("Got something as mask that I cannot handle.") ds = dhandle.get_model_bold_dataset(model, subj, flavor=flavor, mask=mask, add_fa=add_fa, noinfolabel="rest") # fixup time_coords to make the impression of a continuous time series # this is only necessary until we have changed the tutorial to # show/encourage run-wise processing ds.sa["time_coords"] = np.linspace(0, (len(ds) * 2.5), len(ds) + 1)[:-1] return ds
def run(args): from mvpa2.base.hdf5 import h5save ds = None vol_attr = dict() if args.add_vol_attr is not None: # XXX add a way to use the mapper of an existing dataset to # add a volume attribute without having to load the entire # mri data again vol_attr = dict(args.add_vol_attr) if not len(args.add_vol_attr) == len(vol_attr): warning("--vol-attr option with duplicate attribute name: " "check arguments!") verbose(2, "Prepare to add volumetric feature attributes: %s" % vol_attr) if args.txt_data is not None: verbose(1, "Load data from TXT file '%s'" % args.txt_data) samples = _load_from_txt(args.txt_data) ds = Dataset(samples) elif args.npy_data is not None: verbose(1, "Load data from NPY file '%s'" % args.npy_data) samples = _load_from_npy(args.npy_data) ds = Dataset(samples) elif args.mri_data is not None: verbose(1, "Load data from MRI image(s) %s" % args.mri_data) from mvpa2.datasets.mri import fmri_dataset ds = fmri_dataset(args.mri_data, mask=args.mask, add_fa=vol_attr) elif args.openfmri_modelbold is not None: verbose( 1, "Load data from OpenFMRI model specification %s" % args.openfmri_modelbold) if not len(args.openfmri_modelbold[3]): args.openfmri_modelbold[3] = None # load openfmri dataset from mvpa2.datasets.sources.openfmri import OpenFMRIDataset of = OpenFMRIDataset(args.openfmri_modelbold[0]) ds = of.get_model_bold_dataset(int(args.openfmri_modelbold[1]), int(args.openfmri_modelbold[2]), flavor=args.openfmri_modelbold[3], mask=args.mask, add_fa=vol_attr, add_sa=args.add_fsl_mcpar) if ds is None: if args.data is None: raise RuntimeError('no data source specific') else: ds = hdf2ds(args.data)[0] else: if args.data is not None: verbose( 1, 'ignoring dataset input in favor of other data source -- remove either one to disambiguate' ) # act on all attribute options ds = process_common_dsattr_opts(ds, args) if args.openfmri_modelbold is None and args.add_fsl_mcpar is not None: from mvpa2.misc.fsl.base import McFlirtParams mc_par = McFlirtParams(args.add_fsl_mcpar) for param in mc_par: verbose( 2, "Add motion regressor as sample attribute '%s'" % ('mc_' + param)) ds.sa['mc_' + param] = mc_par[param] verbose(3, "Dataset summary %s" % (ds.summary())) # and store outfilename = args.output if not outfilename.endswith('.hdf5'): outfilename += '.hdf5' verbose(1, "Save dataset to '%s'" % outfilename) h5save(outfilename, ds, mkdir=True, compression=args.hdf5_compression)
import nibabel as nb datapath = 'BASEDIR' of = OpenFMRIDataset(datapath) sub = int(sys.argv[1]) + 1 def smooth(img): # we need to preserve the original header because the smoothing function # f***s the TR up nimg = smooth_img(img, fwhm=2.0) return nb.Nifti1Image(nimg.get_data(), img.get_affine(), header=img.get_header()) ds = of.get_model_bold_dataset( model_id=1, subj_id=sub, flavor='dico_bold7Tp1_to_subjbold7Tp1', # full brain mask=_opj( datapath, 'sub%.3i' % sub, 'templates', 'bold7Tp1', 'qa', 'jointfgbrainmask_bold7Tp1_to_subjbold7Tp1.nii.gz'), preproc_img=smooth, # HP filtering is done by NiPy's GLM modelfx=fit_event_hrf_model, time_attr='time_coords', condition_attr='condition') h5save(_opj('data', 'sub%.3i_2.0mm_hrf.hdf5' % sub), ds)