Skip to content

Commit

Permalink
fix: siibra-python api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xgui3783 committed Oct 2, 2024
1 parent eee077e commit 74b380a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
15 changes: 8 additions & 7 deletions new_api/data_handlers/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def cache_region_statistic_map(parcellation_id: str, region_id: str, space_id: s

if len(maps) > 1:
warning_texts = f"Multiple ({len(maps)}) maps found"

volume_data = maps[0].fetch(region=region_id)
map = maps[0]
vol = map.extract_regional_map(region=region_id)
volume_data = vol.get_data()

error_text = f"{error_text}, with region_id '{region_id}'"
assert isinstance(volume_data, nib.Nifti1Image), f"{error_text}, volume provided is not of type Nifti1Image"
Expand Down Expand Up @@ -77,14 +78,14 @@ def cache_parcellation_labelled_map(parcellation_id: str, space_id: str, region_
import nibabel as nib
error_text = f"Map with parc id '{parcellation_id}', space id '{space_id}'"

labelled_map = siibra.get_map(parcellation_id, space_id, "labelled")
assert labelled_map is not None, f"{error_text} returns None"
volume_data = None
if region_id is not None:
region = siibra.get_region(parcellation_id, region_id)
volume_data = region.fetch_regional_map(space_id, "labelled")
volprov = labelled_map.extract_regional_map(region_id)
else:
labelled_map = siibra.get_map(parcellation_id, space_id, "labelled")
assert labelled_map is not None, f"{error_text} returns None"
volume_data = labelled_map.fetch()
volprov = labelled_map.extract_full_map()
volume_data = volprov.get_data()

assert isinstance(volume_data, nib.Nifti1Image), f"{error_text}, volume provided is not of type Nifti1Image"

Expand Down
40 changes: 23 additions & 17 deletions new_api/v3/serialization/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from siibra.atlases.parcellationmap import Map
from siibra.atlases.sparsemap import SparseMap
from siibra.attributes.descriptions import Name, EbrainsRef
from siibra.attributes.dataitems.base import Archive
from siibra.attributes.dataitems.volume.base import Volume, MESH_FORMATS, IMAGE_FORMATS
from siibra.attributes.dataproviders.base import Archive
from siibra.attributes.dataproviders.volume.base import VolumeProvider
from siibra.operations.volume_fetcher.base import VolumeFormats
from siibra.factory.livequery.ebrains import EbrainsQuery, DatasetVersion

def parse_archive_options(archive: Union[Archive, None]):
Expand Down Expand Up @@ -140,10 +141,14 @@ def dsv_id_to_model(id: str):
species = mp.species

# TODO fix datasets
all_volumes = mp._find(Volume)
all_volumes = mp._find(VolumeProvider)
volumes: List[VolumeModel] = []

for vol in all_volumes:
indices = defaultdict(list)
volume_name_to_idx = {}

for idx, vol in enumerate(all_volumes):
volume_name_to_idx[vol.name] = idx
vol_ds: List[EbrainsDatasetModel] = []
if vol.id:
vol_ds = [dsv_id_to_model(dsv)
Expand All @@ -154,8 +159,8 @@ def dsv_id_to_model(id: str):
volumes.append(
VolumeModel(name="",
formats=[vol.format],
provides_mesh=vol.format in MESH_FORMATS,
provides_image=vol.format in IMAGE_FORMATS,
provides_mesh=vol.format in VolumeFormats.MESH_FORMATS,
provides_image=vol.format in VolumeFormats.IMAGE_FORMATS,
fragments={},
variant=None,
provided_volumes={
Expand All @@ -166,17 +171,19 @@ def dsv_id_to_model(id: str):
},
datasets=vol_ds))

indices = defaultdict(list)
for idx, vol in enumerate(all_volumes):
for regionname, value in vol.mapping.items():

for regionname, mappings in mp.region_mapping.items():
for mapping in mappings:
target = mapping["target"]
assert target in volume_name_to_idx, f"target {target} not found in volume name {volume_name_to_idx}"
new_index = {
"volume": idx
"volume": volume_name_to_idx[target]
}
if value.get("label"):
new_index["label"] = value.get("label")
if mapping.get("label"):
new_index["label"] = mapping.get("label")
indices[regionname].append(new_index)
indices[clear_name(regionname)].append(new_index)

if mp.space_id == FSA_ID:
assert len(all_volumes) == 2, f"Expected fsaverage to have 2 volumes, but got {len(all_volumes)}"

Expand All @@ -191,19 +198,18 @@ def dsv_id_to_model(id: str):
formats = list({lh_vol.format, rh_vol.format})
assert len(formats) == 1, f"Expected only one type of format, but got {formats}"
format = formats[0]
assert lh_vol.archive_options is None and rh_vol.archive_options is None, f"Expected neither volume has archive options"
# assert lh_vol.archive_options is None and rh_vol.archive_options is None, f"Expected neither volume has archive options"

all_vol_ids = [vol.id for vol in all_volumes if vol.id]
all_vol_ds = [dsv_id_to_model(dsv)
for ref in mp._find(EbrainsRef)
for dsv in ref._dataset_verion_ids
if ref.annotates in all_vol_ids]

volumes = [
VolumeModel(name="",
formats=[format],
provides_mesh=vol.format in MESH_FORMATS,
provides_image=vol.format in IMAGE_FORMATS,
provides_mesh=vol.format in VolumeFormats.MESH_FORMATS,
provides_image=vol.format in VolumeFormats.IMAGE_FORMATS,
fragments={},
variant=None,
provided_volumes={
Expand Down

0 comments on commit 74b380a

Please sign in to comment.