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: Fix targetting class to be neuron compatible #898

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
62 changes: 57 additions & 5 deletions bsb/simulation/targetting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..config import refs, types

if typing.TYPE_CHECKING:
from ..cell_types import CellType
from .cell import CellModel


Expand Down Expand Up @@ -58,6 +59,21 @@ def get_targets(self, adapter, simulation, simdata):
}


class CellTypeFilter:
cell_types: list["CellType"] = config.reflist(refs.cell_type_ref, required=False)
only_local: bool = config.attr(type=bool, default=True)

def get_targets(self, adapter, simulation, simdata):
chunks = simdata.chunks
if not self.only_local:
chunks = None
danilobenozzo marked this conversation as resolved.
Show resolved Hide resolved
return {
cell_name: cell_type.get_placement_set(chunks=chunks)
for cell_name, cell_type in simulation.scaffold.cell_types.items()
if not self.cell_types or cell_type in self.cell_types
}


class FractionFilter:
count = config.attr(
type=int, required=types.mut_excl("fraction", "count", required=False)
Expand Down Expand Up @@ -141,12 +157,18 @@ class ByIdTargetting(FractionFilter, CellTargetting, classmap_entry="by_id"):

@FractionFilter.filter
def get_targets(self, adapter, simulation, simdata):
by_name = {model.name: model for model in simdata.populations.keys()}
return {
model: simdata.populations[model][ids]
for model_name, ids in self.ids.items()
if (model := by_name.get(model_name)) is not None
by_name = {
model.name: model
for model, pop in simdata.populations.items()
if len(pop) > 0
}
dict_target = {}
for model_name, ids in self.ids.items():
if (model := by_name.get(model_name)) is not None:
ps_ids = list(simdata.placement[model].load_ids())
ids_here = [ps_ids.index(ids_i) for ids_i in ids if ids_i in ps_ids]
dict_target[model] = simdata.populations[model][ids_here]
Comment on lines +165 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this piece of code supposed to do? It's written very inefficiently, if I understand the input/output relation perhaps we can optimize it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, my understanding of ByIdTargetting is that it should select those cell models with (global) id as in ids.
Originally ids was used as index in the subpopulation of each simdata: simdata.populations[model][ids] (kind of local id).
Here ps_ids has the cellmodel ids of that specific simdata, then ids_here finds whether and where any of the ids that we are looking for is in ps_ids (like np.where).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give a numeric example with let's say 3 or 4 elements per array so I can visualize the transformation?

return dict_target


@config.node
Expand Down Expand Up @@ -209,6 +231,36 @@ def get_targets(self, adapter, simulation, simdata):
}


@config.node
class SphericalTargettingCellTypes(
CellTypeFilter, FractionFilter, Targetting, classmap_entry="sphere_cell_types"
):
"""
Targets all cell types in a sphere.
"""

origin: list[float] = config.attr(type=types.list(type=float, size=3), required=True)
radius: float = config.attr(type=float, required=True)

@FractionFilter.filter
def get_targets(self, adapter, simulation, simdata):
"""
Target all or certain cells within a sphere of specified radius.
"""
return {
model: ps.load_ids()[
(
np.sum(
(ps.load_positions() - self.origin) ** 2,
axis=1,
)
< self.radius**2
)
]
for model, ps in super().get_targets(adapter, simulation, simdata).items()
}


@config.node
class SphericalTargetting(
CellModelFilter, FractionFilter, CellTargetting, classmap_entry="sphere"
Expand Down
Loading