def test_grid_creation(self): mask = self.dataset.masker.volume # Test with mask grid = imageutils.create_grid(image=mask, scale=4) self.assertEquals(grid.shape, (91, 109, 91)) self.assertEquals(len(np.unique(grid.get_data())), 4359) # Test without mask grid = imageutils.create_grid(image=mask, scale=4, apply_mask=False) self.assertGreater(len(np.unique(grid.get_data())), 4359)
def apply_grid(dataset, masker=None, scale=5, threshold=None): """ Imposes a 3D grid on the brain volume and averages across all voxels that fall within each cell. Args: dataset: Data to apply grid to. Either a Dataset instance, or a numpy array with voxels in rows and features in columns. masker: Optional Masker instance used to map between the created grid and the dataset. This is only needed if dataset is a numpy array; if dataset is a Dataset instance, the Masker in the dataset will be used. scale: int; scaling factor (in mm) to pass onto create_grid(). threshold: Optional float to pass to reduce.average_within_regions(). Returns: A tuple of length 2, where the first element is a numpy array of dimensions n_cubes x n_studies, and the second element is a numpy array, with the same dimensions as the Masker instance in the current Dataset, that maps voxel identities onto cell IDs in the grid. """ if masker is None: if isinstance(dataset, Dataset): masker = dataset.masker else: raise ValueError( "If dataset is a numpy array, a masker must be provided.") grid = imageutils.create_grid(masker.volume, scale) cm = masker.mask(grid, in_global_mask=True) data = average_within_regions(dataset, cm, threshold) return (data, grid)