Skip to content

Commit

Permalink
Merge pull request #510 from FZJ-INM1-BDA/increase_intensity_profile_…
Browse files Browse the repository at this point in the history
…compounding_speed

Increase intensity profile compounding speed
  • Loading branch information
AhmetNSimsek authored Jan 23, 2024
2 parents 0f6f914 + 4d6f42f commit d5fa00f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions siibra/features/anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from ..vocabularies import REGION_ALIASES

from typing import Union, List, Dict
from typing import Union, List, Dict, Iterable


class AnatomicalAnchor:
Expand All @@ -46,7 +46,7 @@ def __init__(

if isinstance(species, (str, Species)):
self.species = {Species.decode(species)}
elif isinstance(species, list):
elif isinstance(species, Iterable):
assert all(isinstance(_, Species) for _ in species)
self.species = set(species)
else:
Expand Down
6 changes: 5 additions & 1 deletion siibra/features/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ def _element_index(self) -> Any:
assert hash(index), "`_element_index` of a compoundable must be hashable."
return index

@classmethod
def _merge_anchors(cls, anchors: List[_anchor.AnatomicalAnchor]):
return sum(anchors)


class CompoundFeature(Feature):
"""
Expand Down Expand Up @@ -716,7 +720,7 @@ def __init__(
self,
modality=modality,
description="\n".join({f.description for f in elements}),
anchor=sum([f.anchor for f in elements]),
anchor=self._feature_type._merge_anchors([f.anchor for f in elements]),
datasets=list(dict.fromkeys([ds for f in elements for ds in f.datasets]))
)
self._queryconcept = queryconcept
Expand Down
31 changes: 21 additions & 10 deletions siibra/features/tabular/bigbrain_intensity_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

from . import cortical_profile

from ...locations import point
from typing import List, TYPE_CHECKING
if TYPE_CHECKING:
from ...features.anchor import AnatomicalAnchor


class BigBrainIntensityProfile(
Expand All @@ -38,18 +40,11 @@ class BigBrainIntensityProfile(

def __init__(
self,
regionname: str,
anchor: "AnatomicalAnchor",
depths: list,
values: list,
boundaries: list,
location: point.Point
boundaries: list
):
from ..anchor import AnatomicalAnchor
anchor = AnatomicalAnchor(
location=location,
region=regionname,
species='Homo sapiens'
)
cortical_profile.CorticalProfile.__init__(
self,
description=self.DESCRIPTION,
Expand All @@ -67,3 +62,19 @@ def __init__(
@property
def location(self):
return self.anchor.location

@classmethod
def _merge_anchors(cls, anchors: List['AnatomicalAnchor']):
from ...locations.pointset import PointSet
from ...features.anchor import AnatomicalAnchor

location = PointSet(
[anchor.location for anchor in anchors],
space="BigBrain"
)
regions = {anchor._regionspec for anchor in anchors}
return AnatomicalAnchor(
location=location,
region=", ".join(regions),
species='Homo sapiens'
)
12 changes: 5 additions & 7 deletions siibra/features/tabular/layerwise_bigbrain_intensities.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import pandas as pd
import numpy as np

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ...features.anchor import AnatomicalAnchor


class LayerwiseBigBrainIntensities(
tabular.Tabular,
Expand All @@ -39,16 +43,10 @@ class LayerwiseBigBrainIntensities(

def __init__(
self,
regionname: str,
anchor: "AnatomicalAnchor",
means: list,
stds: list,
):

from ..anchor import AnatomicalAnchor
anchor = AnatomicalAnchor(
region=regionname,
species='Homo sapiens',
)
data = pd.DataFrame(
np.array([means, stds]).T,
columns=['mean', 'std'],
Expand Down
18 changes: 13 additions & 5 deletions siibra/livequeries/bigbrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ def query(self, concept: structure.BrainStructure, **kwargs) -> List[bigbrain_in
assert isinstance(matched, (point.Point, pointset.PointSet))
assert matched.labels is not None
for i in matched.labels:
anchor = _anchor.AnatomicalAnchor(
location=point.Point(loader._vertices[i], space='bigbrain'),
region=str(concept),
species='Homo sapiens'
)
prof = bigbrain_intensity_profile.BigBrainIntensityProfile(
regionname=str(concept),
anchor=anchor,
depths=loader.profile_labels,
values=loader._profiles[i],
boundaries=loader._boundary_depths[i],
location=point.Point(loader._vertices[i], space='bigbrain')
boundaries=loader._boundary_depths[i]
)
prof.anchor._assignments[concept] = _anchor.AnatomicalAssignment(
query_structure=concept,
Expand Down Expand Up @@ -137,12 +141,16 @@ def query(self, concept: structure.BrainStructure, **kwargs) -> List[layerwise_b
for b in boundary_depths
]).reshape((-1, 200))

anchor = _anchor.AnatomicalAnchor(
location=pointset.PointSet(loader._vertices[indices, :], space='bigbrain'),
region=str(concept),
species='Homo sapiens'
)
result = layerwise_bigbrain_intensities.LayerwiseBigBrainIntensities(
regionname=str(concept),
anchor=anchor,
means=[matched_profiles[layer_labels == layer].mean() for layer in range(1, 7)],
stds=[matched_profiles[layer_labels == layer].std() for layer in range(1, 7)],
)
result.anchor._location_cached = pointset.PointSet(loader._vertices[indices, :], space='bigbrain')
result.anchor._assignments[concept] = _anchor.AnatomicalAssignment(
query_structure=concept,
assigned_structure=concept,
Expand Down

0 comments on commit d5fa00f

Please sign in to comment.