Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fetch specific label from gii labels #574

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions e2e/volumes/test_parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from siibra.volumes.volume import Subvolume

from itertools import product
import numpy as np

maps_to_compress = [
siibra.get_map("2.9", "mni152"), # contains fragments
Expand Down Expand Up @@ -123,3 +124,19 @@ def test_fetching_merged_volume():
mp = siibra.get_map("julich 2.9", "bigbrain")
assert len(mp) > 1
_ = mp.fetch()


@pytest.mark.parametrize("siibramap", [
siibra.get_map('julich 3', 'fsaverage'),
siibra.get_map('julich 3', 'mni152'),
])
def test_fetching_mask(siibramap: Map):
for fmt in siibramap.formats:
for region in siibramap.regions:
mesh_or_img = siibramap.fetch(region, format=fmt)
arr = mesh_or_img['labels'] if isinstance(mesh_or_img, dict) else mesh_or_img.dataobj
unq_vals = np.unique(arr)
assert np.array_equal(unq_vals, [0, 1]), (
f"{siibramap}, {fmt}. Mask for {region} should only contain 0 "
f"and 1 but found: {unq_vals}"
)
7 changes: 5 additions & 2 deletions siibra/volumes/providers/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ def __init__(self, url: Union[str, dict]):
else:
raise NotImplementedError(f"Urls for {self.__class__.__name__} are expected to be of type str or dict.")

def fetch(self, fragment: str = None, **kwargs):
def fetch(self, fragment: str = None, label: int = None, **kwargs):
"""Returns a 1D numpy array of label indices."""
labels = []
for fragment_name, loader in self._loaders.items():
if fragment is not None and fragment.lower() not in fragment_name.lower():
continue
assert len(loader.data.darrays) == 1
labels.append(loader.data.darrays[0].data)
if label is not None:
labels.append((loader.data.darrays[0].data == label).astype('uint8'))
else:
labels.append(loader.data.darrays[0].data)

return {"labels": np.hstack(labels)}

Expand Down
Loading