Skip to content

Commit

Permalink
Merge pull request #183 from rmarkello/surfmm
Browse files Browse the repository at this point in the history
[REF] Allow negative tolerance for surfaces
  • Loading branch information
rmarkello authored Mar 25, 2021
2 parents 4ff817f + 719fdc6 commit bb23d7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
15 changes: 10 additions & 5 deletions abagen/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def label_samples(self, annotation, tolerance=2):
samples['structure'] = 'cortex'

if self.volumetric:
labels = self._match_volume(samples, tolerance)
labels = self._match_volume(samples, abs(tolerance))
else:
cortex = samples['structure'] == 'cortex'
labels = np.zeros(len(samples))
Expand Down Expand Up @@ -247,10 +247,15 @@ def _match_surface(self, samples, tolerance):
if self.atlas_info is not None:
labels = _check_label(labels, samples, self.atlas_info)

if len(labels) > 1:
with np.errstate(invalid='ignore'):
mask = dist > dist.mean() + (dist.std(ddof=1) * tolerance)
labels[mask] = 0
if tolerance < 0:
mask = dist > -tolerance
else:
if len(labels) > 1:
with np.errstate(invalid='ignore'):
mask = dist > dist.mean() + (dist.std(ddof=1) * tolerance)
else:
mask = np.zeros(len(labels), dtype=bool)
labels[mask] = 0

return labels

Expand Down
8 changes: 8 additions & 0 deletions abagen/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def gen_report(self):
parcel.
""".format(space='MNI' if self.group_atlas else 'native voxel',
tolerance=self.tolerance)
elif self.tolerance < 0 and not self.atlas.volumetric:
report += """
Samples were assigned to brain regions by minimizing the Euclidean
distance between the {space} coordinates of each sample and the
nearest surface vertex. Samples where the Euclidean distance to the
nearest vertex was more than {tolerance}mm were excluded.
""".format(space='MNI' if self.group_atlas else 'native voxel',
tolerance=abs(self.tolerance))

if self.atlas_info is not None:
report += """
Expand Down
6 changes: 6 additions & 0 deletions abagen/tests/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def test_AtlasTree(atlas, surface, testfiles):
labels = tree.label_samples([tree.centroids[1], tree.centroids[2]])
assert np.all(labels['label'] == [1, 2])

# check negative surface tolerance
labels = tree.label_samples([-72, -25, -13], tolerance=-4)
assert np.all(labels['label'] == 14)
labels = tree.label_samples([-72, -25, -13], tolerance=-3)
assert np.all(labels['label'] == 0)

# no coordinates
with pytest.raises(ValueError):
matching.AtlasTree(np.random.choice(10, size=(100,)))
Expand Down

0 comments on commit bb23d7c

Please sign in to comment.