Skip to content

Commit

Permalink
Merge pull request #486 from FZJ-INM1-BDA/fix_map_compute_centroids
Browse files Browse the repository at this point in the history
Fix: map.compute_centroids fail for maps with no fragments
  • Loading branch information
AhmetNSimsek authored Oct 11, 2023
2 parents 088b461 + b5d64cd commit 5aeeabd
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions siibra/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,22 +579,18 @@ def compute_centroids(self) -> Dict[str, point.Point]:
Region names as keys and computed centroids as items.
"""
centroids = {}
# list of regions sorted by mapindex
regions = sorted(self._indices.items(), key=lambda v: min(_.volume for _ in v[1]))
current_vol_index = MapIndex(volume=0)
maparr = None
for regionname, indexlist in siibra_tqdm(regions, unit="regions", desc="Computing centroids"):
for regionname, indexlist in siibra_tqdm(
self._indices.items(), unit="regions", desc="Computing centroids"
):
assert len(indexlist) == 1
index = indexlist[0]
if index.label == 0:
continue
vol_index = MapIndex(volume=index.volume, fragment=index.fragment)
if vol_index != current_vol_index:
current_vol_index = vol_index
with QUIET:
mapimg = self.fetch(index=vol_index)
maparr = np.asanyarray(mapimg.dataobj)
centroid_vox = np.array(np.where(maparr == index.label)).mean(1)
with QUIET:
mapimg = self.fetch(index=index) # returns a mask of the region
maparr = np.asanyarray(mapimg.dataobj)
centroid_vox = np.mean(np.where(maparr == 1), axis=1)
assert regionname not in centroids
centroids[regionname] = point.Point(
np.dot(mapimg.affine, np.r_[centroid_vox, 1])[:3], space=self.space
Expand Down

0 comments on commit 5aeeabd

Please sign in to comment.