Skip to content

Commit d5fa00f

Browse files
authored
Merge pull request #510 from FZJ-INM1-BDA/increase_intensity_profile_compounding_speed
Increase intensity profile compounding speed
2 parents 0f6f914 + 4d6f42f commit d5fa00f

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

siibra/features/anchor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ..vocabularies import REGION_ALIASES
2828

29-
from typing import Union, List, Dict
29+
from typing import Union, List, Dict, Iterable
3030

3131

3232
class AnatomicalAnchor:
@@ -46,7 +46,7 @@ def __init__(
4646

4747
if isinstance(species, (str, Species)):
4848
self.species = {Species.decode(species)}
49-
elif isinstance(species, list):
49+
elif isinstance(species, Iterable):
5050
assert all(isinstance(_, Species) for _ in species)
5151
self.species = set(species)
5252
else:

siibra/features/feature.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,10 @@ def _element_index(self) -> Any:
672672
assert hash(index), "`_element_index` of a compoundable must be hashable."
673673
return index
674674

675+
@classmethod
676+
def _merge_anchors(cls, anchors: List[_anchor.AnatomicalAnchor]):
677+
return sum(anchors)
678+
675679

676680
class CompoundFeature(Feature):
677681
"""
@@ -716,7 +720,7 @@ def __init__(
716720
self,
717721
modality=modality,
718722
description="\n".join({f.description for f in elements}),
719-
anchor=sum([f.anchor for f in elements]),
723+
anchor=self._feature_type._merge_anchors([f.anchor for f in elements]),
720724
datasets=list(dict.fromkeys([ds for f in elements for ds in f.datasets]))
721725
)
722726
self._queryconcept = queryconcept

siibra/features/tabular/bigbrain_intensity_profile.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
from . import cortical_profile
1717

18-
from ...locations import point
18+
from typing import List, TYPE_CHECKING
19+
if TYPE_CHECKING:
20+
from ...features.anchor import AnatomicalAnchor
1921

2022

2123
class BigBrainIntensityProfile(
@@ -38,18 +40,11 @@ class BigBrainIntensityProfile(
3840

3941
def __init__(
4042
self,
41-
regionname: str,
43+
anchor: "AnatomicalAnchor",
4244
depths: list,
4345
values: list,
44-
boundaries: list,
45-
location: point.Point
46+
boundaries: list
4647
):
47-
from ..anchor import AnatomicalAnchor
48-
anchor = AnatomicalAnchor(
49-
location=location,
50-
region=regionname,
51-
species='Homo sapiens'
52-
)
5348
cortical_profile.CorticalProfile.__init__(
5449
self,
5550
description=self.DESCRIPTION,
@@ -67,3 +62,19 @@ def __init__(
6762
@property
6863
def location(self):
6964
return self.anchor.location
65+
66+
@classmethod
67+
def _merge_anchors(cls, anchors: List['AnatomicalAnchor']):
68+
from ...locations.pointset import PointSet
69+
from ...features.anchor import AnatomicalAnchor
70+
71+
location = PointSet(
72+
[anchor.location for anchor in anchors],
73+
space="BigBrain"
74+
)
75+
regions = {anchor._regionspec for anchor in anchors}
76+
return AnatomicalAnchor(
77+
location=location,
78+
region=", ".join(regions),
79+
species='Homo sapiens'
80+
)

siibra/features/tabular/layerwise_bigbrain_intensities.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import pandas as pd
2020
import numpy as np
2121

22+
from typing import TYPE_CHECKING
23+
if TYPE_CHECKING:
24+
from ...features.anchor import AnatomicalAnchor
25+
2226

2327
class LayerwiseBigBrainIntensities(
2428
tabular.Tabular,
@@ -39,16 +43,10 @@ class LayerwiseBigBrainIntensities(
3943

4044
def __init__(
4145
self,
42-
regionname: str,
46+
anchor: "AnatomicalAnchor",
4347
means: list,
4448
stds: list,
4549
):
46-
47-
from ..anchor import AnatomicalAnchor
48-
anchor = AnatomicalAnchor(
49-
region=regionname,
50-
species='Homo sapiens',
51-
)
5250
data = pd.DataFrame(
5351
np.array([means, stds]).T,
5452
columns=['mean', 'std'],

siibra/livequeries/bigbrain.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,16 @@ def query(self, concept: structure.BrainStructure, **kwargs) -> List[bigbrain_in
9494
assert isinstance(matched, (point.Point, pointset.PointSet))
9595
assert matched.labels is not None
9696
for i in matched.labels:
97+
anchor = _anchor.AnatomicalAnchor(
98+
location=point.Point(loader._vertices[i], space='bigbrain'),
99+
region=str(concept),
100+
species='Homo sapiens'
101+
)
97102
prof = bigbrain_intensity_profile.BigBrainIntensityProfile(
98-
regionname=str(concept),
103+
anchor=anchor,
99104
depths=loader.profile_labels,
100105
values=loader._profiles[i],
101-
boundaries=loader._boundary_depths[i],
102-
location=point.Point(loader._vertices[i], space='bigbrain')
106+
boundaries=loader._boundary_depths[i]
103107
)
104108
prof.anchor._assignments[concept] = _anchor.AnatomicalAssignment(
105109
query_structure=concept,
@@ -137,12 +141,16 @@ def query(self, concept: structure.BrainStructure, **kwargs) -> List[layerwise_b
137141
for b in boundary_depths
138142
]).reshape((-1, 200))
139143

144+
anchor = _anchor.AnatomicalAnchor(
145+
location=pointset.PointSet(loader._vertices[indices, :], space='bigbrain'),
146+
region=str(concept),
147+
species='Homo sapiens'
148+
)
140149
result = layerwise_bigbrain_intensities.LayerwiseBigBrainIntensities(
141-
regionname=str(concept),
150+
anchor=anchor,
142151
means=[matched_profiles[layer_labels == layer].mean() for layer in range(1, 7)],
143152
stds=[matched_profiles[layer_labels == layer].std() for layer in range(1, 7)],
144153
)
145-
result.anchor._location_cached = pointset.PointSet(loader._vertices[indices, :], space='bigbrain')
146154
result.anchor._assignments[concept] = _anchor.AnatomicalAssignment(
147155
query_structure=concept,
148156
assigned_structure=concept,

0 commit comments

Comments
 (0)