Skip to content

Commit

Permalink
[REF] optimize get_studies_by_mask (#891)
Browse files Browse the repository at this point in the history
make the function more performant
  • Loading branch information
jdkent authored Jul 25, 2024
1 parent 792bed9 commit 0c48c2f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions nimare/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,19 +643,20 @@ def get_studies_by_mask(self, mask):
found_ids : :obj:`list`
A list of IDs from the Dataset with at least one focus in the mask.
"""
from scipy.spatial.distance import cdist

mask = load_niimg(mask)

dset_mask = self.masker.mask_img

if not np.array_equal(dset_mask.affine, mask.affine):
LGR.warning("Mask affine does not match Dataset affine. Assuming same space.")

dset_ijk = mm2vox(self.coordinates[["x", "y", "z"]].values, mask.affine)
mask_ijk = np.vstack(np.where(mask.get_fdata())).T
distances = cdist(mask_ijk, dset_ijk)
distances = np.any(distances == 0, axis=0)
found_ids = list(self.coordinates.loc[distances, "id"].unique())
mask_data = mask.get_fdata()
mask_coords = np.vstack(np.where(mask_data)).T

# Check for presence of coordinates in mask
in_mask = np.any(np.all(dset_ijk[:, None] == mask_coords[None, :], axis=-1), axis=-1)
found_ids = list(self.coordinates.loc[in_mask, "id"].unique())

return found_ids

def get_studies_by_coordinate(self, xyz, r=20):
Expand Down

0 comments on commit 0c48c2f

Please sign in to comment.