[pymvpa] Masking a dataset after loading it

Johan Carlin jdc55 at cam.ac.uk
Thu Aug 20 17:33:27 UTC 2009

Hi all,

I'm trying to apply a mask to a dataset after loading it - this is
useful for an ROI analysis where I don't want in-script ROI definition
to fail because the centre voxel has ended up outside the mask... So
far, this is the best I've been able to do:

def mask_data(ds,mask):
    '''Applies a mask to an already loaded dataset. Useful for
    sub-masking a spherical ROI (masking before ROI definition
    goes wrong when the ROI centre is masked out - this is not
    a problem here). Returns a new dataset.'''
# Load mask, flip labels_map dict around
    M = NiftiImage(mask)
    lmap = dict(zip(ds.labels_map.values(),ds.labels_map.keys()))
# Create new dataset
    m_ds = MaskedDataset(samples=ds.samples_original, labels=ds.L,
chunks=ds.C, mask=M.data, labels_map=lmap)
    return removeInvariantFeatures(m_ds)

There are 2 problems here: 1) I lose the ability to map2Nifti, should
I ever want to, 2) the removeInvariantFeatures step seems to be
necessary, since the number of samples expands to the full size of the
volume in the MaskedDataset. This is not a big deal in itself -
however, this function throws an error when your ROI/mask combination
contains no non-invariant voxels. I would like to catch this
particular problem later in my processing stream, if possible.

Is there a better way to do this? I imagine there must be a NiftiImage
instance hidden somewhere in the original dataset, so that I could
create a NiftiDataset instead of a MaskedDataset.


Johan Carlin
Graduate Student
MRC Cognition & Brain Sciences Unit
15 Chaucer Road
Cambridge CB2 7EF

johan.carlin at mrc-cbu.cam.ac.uk
+44 (0)1223 355294 ext 593

More information about the Pkg-ExpPsy-PyMVPA mailing list