How can I read the cell positions of a network I generated with `bsb compile`?

21 Views Asked by At

I wrote a network configuration file, used bsb compile <that-file>.json and it generated an HDF5 output file, but it's hard to extract the cell positions from it. How can I read the cell positions from such a file?

1

There are 1 best solutions below

0
On

The output HDF5 file is a network storage file, in a library-specific format. It's not recommended to try to write your own code to read it, because the format may change arbitrarily. The library instead offers the from_storage function to load networks from such storage files. You can then read the cell positions using the PlacementSet.load_positions method, which will return an (Nx3) (x, y, z) numpy array with the cell positions for that cell type:

from bsb.core import from_storage

my_network = from_storage("my_file.hdf5")
ps = my_network.get_placement_set("unipolar_brush_cell")
positions = ps.load_positions()

If you don't know which cell type you want, you can iterate over all the placement sets like this:

for ps in my_network.get_placement_sets():
  # Prints something like "32401 unipolar_brush_cell cells", ...
  print(len(ps), ps.tag, "cells")
  positions = ps.load_positions()

Note: Calling load_positions reads all the data from file into memory which is an expensive operation. Don't call it multiple times if you don't have to, and make sure you have enough memory. You can use set_chunk_filter to load only parts of the data if memory is a problem.