Skip to content

Commit

Permalink
Fix _get_morphology_loaders to preload only once required morphologie…
Browse files Browse the repository at this point in the history
…s and metadata.

Add function to update morphology metadata.
  • Loading branch information
drodarie committed Jun 29, 2023
1 parent bc6692c commit ff291d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions bsb_hdf5/morphology_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def get_all_meta(self, handle=HANDLED):
return {}
return json.loads(handle["morphology_meta"][()], object_hook=meta_object_hook)

@handles_handles("a")
def update_all_meta(self, meta, handle=HANDLED):
all_meta = self.get_all_meta(handle=handle)
all_meta.update(meta)
self.set_all_meta(all_meta, handle=handle)

@handles_handles("a")
def set_all_meta(self, all_meta, handle=HANDLED):
if "morphology_meta" in handle:
Expand Down
43 changes: 32 additions & 11 deletions bsb_hdf5/placement_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,14 @@ def load_rotations(self, handle=HANDLED):
@handles_handles("r")
def load_morphologies(self, handle=HANDLED, allow_empty=False):
"""
Load the cell morphologies.
Preload the cell morphologies.
:param handle: hdf5 file handler
:type handle: hdf5.File
:param allow_empty: If False (default), will raise an error in absence of morphologies,
:type allow_empty: bool
:returns: MorphologySet object containing the loader of all morphologies
:rtype: bsb.morphologies.MorphologySet
:raises: DatasetNotFoundError when the morphology data is not found.
"""
reader = self._morphology_chunks.get_chunk_reader(handle, True)
Expand Down Expand Up @@ -197,18 +203,17 @@ def load_additional(self, key=None, handle=HANDLED):
@handles_handles("r")
def _get_morphology_loaders(self, handle=HANDLED):
stor_mor = {}
meta = self._engine.morphologies.get_all_meta()
for chunk in self.get_loaded_chunks():
path = self.get_chunk_path(chunk)
try:
_map = handle[path].attrs["morphology_loaders"]
except KeyError:
continue
stor_mor.update(
(m.name, m)
for m in self._engine.morphologies.select(
_MapSelector(ps=self, names=_map)
)
)
for label in _map:
if label not in stor_mor and label in meta:
stor_mor[label] = self._engine.morphologies.preload(name=label,
meta=meta[label])
return stor_mor

@handles_handles("a")
Expand Down Expand Up @@ -244,15 +249,20 @@ def append_data(
Append data to the placement set.
:param chunk: The chunk to store data in.
:type chunk: ~bsb.storage.Chunk
:param positions: Cell positions
:type positions: :class:`numpy.ndarray`
:param rotations: Cell rotations
:type rotations: ~bsb.morphologies.RotationSet
:param morphologies: Cell morphologies
:type morphologies: ~bsb.morphologies.MorphologySet
:param additional: Additional data to attach to chunck
:type additional: dict
:param count: Amount of entities to place. Excludes the use of any positional,
rotational or morphological data.
:type count: int
:param handle: h5py file handler
:type handle: :class:`h5py.Group`
"""
if not isinstance(chunk, Chunk):
chunk = Chunk(chunk, None)
Expand All @@ -278,7 +288,7 @@ def append_data(
if additional is not None:
for key, ds in additional.items():
self.append_additional(key, chunk, ds)
self._track_add(handle, chunk, len(positions))
self._track_add(handle, chunk, len(positions) if positions is not None else count)

def _append_morphologies(self, chunk, new_set):
with self.chunk_context([chunk]):
Expand All @@ -288,6 +298,17 @@ def _append_morphologies(self, chunk, new_set):
self._morphology_chunks.append(chunk, morphology_set.get_indices())

def append_entities(self, chunk, count, additional=None):
"""
Append entities to the placement set.
:param chunk: The chunk to store data in.
:type chunk: ~bsb.storage.Chunk
:param count: Amount of entities to place. Excludes the use of any positional,
rotational or morphological data.
:type count: int
:param additional: Additional data to attach to chunck
:type additional: dict
"""
self.append_data(chunk, count=count, additional=additional)

def append_additional(self, name, chunk, data):
Expand Down Expand Up @@ -402,12 +423,12 @@ def _track_add(self, handle, chunk, count):
stats = global_stats.setdefault(
str(chunk.id), {"placed": 0, "connections": {"inc": 0, "out": 0}}
)
stats["placed"] += count
stats["placed"] += int(count)
handle.attrs["chunks"] = json.dumps(global_stats)
# Track addition in placement set
handle[self._path].attrs["len"] = handle[self._path].attrs.get("len", 0) + count
chunk_stats = json.loads(handle[self._path].attrs.get("chunks", "{}"))
chunk_stats[str(chunk.id)] = chunk_stats.get(str(chunk.id), 0) + count
chunk_stats[str(chunk.id)] = chunk_stats.get(str(chunk.id), 0) + int(count)
handle[self._path].attrs["chunks"] = json.dumps(chunk_stats)

@handles_handles("r")
Expand Down

0 comments on commit ff291d4

Please sign in to comment.