Skip to main content

SONATA files reader

Project description

banner

license coverage documentation status

libsonata

C++ / Python reader for SONATA circuit files: SONATA guide

Installation

Installing from PyPI

pip install libsonata

Installing as a Python package, directly from GitHub

pip install git+https://github.com/BlueBrain/libsonata

Building the C++ library

git clone git@github.com:BlueBrain/libsonata.git --recursive
cd libsonata
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DEXTLIB_FROM_SUBMODULES=ON ..
make -j

Since libsonata uses backports for std::optional and std::variant which turn into their actual STL implementation once available, it’s recommended to compile libsonata with the same C++ standard as the project linking to libsonata. This is done by passing -DCMAKE_CXX_STANDARD={14,17} to the cmake command above.

Usage (Python)

Nodes

NodeStorage
>>> import libsonata

>>> nodes = libsonata.NodeStorage('path/to/H5/file')

# list populations
>>> nodes.population_names

# open population
>>> population = nodes.open_population(<name>)
NodePopulation
# total number of nodes in the population
>>> population.size

# attribute names
>>> population.attribute_names

# get attribute value for single node, say 42
>>> population.get_attribute('mtype', 42)

# ...or Selection of nodes (see below) => returns NumPy array with corresponding values
>>> selection = libsonata.Selection(values=[1, 5, 9, 42])  # nodes 1, 5, 9, 42
>>> mtypes = population.get_attribute('mtype', selection)
>>> list(zip(selection.flatten(), mtypes))
[(1, u'mtype_of_1'), (5, u'mtype_of_5'), (9, u'mtype_of_9'), (42, u'mtype_of_42')]
Selection

List of element IDs (either node_id, or edge_id) where adjacent IDs are grouped for the sake of efficient HDF5 file access. For instance, {1, 2, 3, 5} sequence becomes {[1, 4), [5, 6)}.

Selection can be instantiated from:
  • a sequence of scalar values (works for NumPy arrays as well)

  • a sequence of pairs (interpreted as ranges above, works for N x 2 NumPy arrays as well)

EdgePopulation connectivity queries (see below) return Selections as well.

>>> selection = libsonata.Selection([1, 2, 3, 5])
>>> selection.ranges
[(1, 4), (5, 6)]
>>> selection = libsonata.Selection([(1, 4), (5, 6)])
>>> selection.flatten()
[1, 2, 3, 5]
>>> selection.flat_size
4
>>> bool(selection)
True
Node Sets

libsonata can work with the Node Set concept, as described here: SONATA guide: Node Sets File This allows the definition of names for groups of cells, and a way to query them. libsonata also allows for extended expressions, such as Regular expressions,and floating point tests, as described here: SONATA extension: Node Sets

# load a node set JSON file
>>> node_sets = libsonata.NodeSets.from_file('node_sets.json')

# list node sets
>>> node_sets.names
{'L6_UPC', 'Layer1', 'Layer2', 'Layer3', ....}

# get the selection of nodes that match in population
>>> selection = node_sets.materialize('Layer1', population)

# node sets can also be loaded from a JSON string
>>> node_sets_manual = libsonata.NodeSets(json.dumps({"SLM_PPA_and_SP_PC": {"mtype": ["SLM_PPA", "SP_PC"]}}))
>>> node_sets_manual.names
{'SLM_PPA_and_SP_PC'}

Edges

EdgeStorage

Population handling for EdgeStorage is analogous to NodeStorage:

>>> edges = libsonata.EdgeStorage('path/to/H5/file')

# list populations
>>> edges.population_names

# open population
>>> population = edges.open_population(<name>)
EdgePopulation
# total number of edges in the population
>>> population.size

# attribute names
>>> population.attribute_names

# get attribute value for single edge, say 123
>>> population.get_attribute('delay', 123)

# ...or Selection of edges => returns NumPy array with corresponding values
>>> selection = libsonata.Selection([1, 5, 9])
>>> population.get_attribute('delay', selection) # returns delays for edges 1, 5, 9

…with additional methods for querying connectivity, where the results are selections that can be applied like above

# get source / target node ID for the 42nd edge:
>>> population.source_node(42)
>>> population.target_node(42)

# query connectivity (result is Selection object)
>>> selection_to_1 = population.afferent_edges(1)  # all edges with target node_id 1
>>> population.target_nodes(selection_to_1)  # since selection only contains edges
                                             # targeting node_id 1 the result will be a
                                             # numpy array of all 1's
>>> selection_from_2 = population.efferent_edges(2)  # all edges sourced from node_id 2
>>> selection = population.connecting_edges(2, 1)  # this selection is all edges from
                                                   # node_id 2 to node_id 1

# ...or their vectorized analogues
>>> selection = population.afferent_edges([1, 2, 3])
>>> selection = population.efferent_edges([1, 2, 3])
>>> selection = population.connecting_edges([1, 2, 3], [4, 5, 6])

Reports

SpikeReader
>>> import libsonata

>>> spikes = libsonata.SpikeReader('path/to/H5/file')

# list populations
>>> spikes.get_population_names()

# open population
>>> population = spikes['<name>']
SpikePopulation
# get all spikes [(node_id, timestep)]
>>> population.get()
[(5, 0.1), (2, 0.2), (3, 0.3), (2, 0.7), (3, 1.3)]

# get all spikes betwen tstart and tstop
>>> population.get(tstart=0.2, tstop=1.0)
[(2, 0.2), (3, 0.3), (2, 0.7)]

# get spikes attribute sorting (by_time, by_id, none)
>>> population.sorting
'by_time'

Pandas can be used to create a dataframe and get a better representation of the data
>>> import pandas

data = population.get()
df = pandas.DataFrame(data=data, columns=['ids', 'times']).set_index('times')
print(df)
       ids
times
0.1      5
0.2      2
0.3      3
0.7      2
1.3      3
SomaReportReader
>>> somas = libsonata.SomaReportReader('path/to/H5/file')

# list populations
>>> somas.get_population_names()

# open population
>>> population_somas = somas['<name>']
SomaReportPopulation
# get times (tstart, tstop, dt)
>>> population_somas.times
(0.0, 1.0, 0.1)

# get unit attributes
>>> population_somas.time_units
'ms'
>>> population_somas.data_units
'mV'

# node_ids sorted?
>>> population_somas.sorted
True

# get a list of all node ids in the selected population
>>> population_somas.get_node_ids()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# get the DataFrame of the node_id values for the timesteps between tstart and tstop
>>> data_frame = population_somas.get(node_ids=[13, 14], tstart=0.8, tstop=1.0)

# get the data values
>>> data_frame.data
[[13.8, 14.8], [13.9, 14.9]]

# get the list of timesteps
>>> data_frame.times
[0.8, 0.9]

# get the list of node ids
>>> data_frame.ids
[13, 14]

Once again, pandas can be used to create a dataframe using the data, ids and times lists

>>> import pandas

df = pandas.DataFrame(data_frame.data, columns=data_frame.ids, index=data_frame.times)
print(df)
       13    14
0.8  13.8  14.8
0.9  13.9  14.9
ElementReportReader
>>> elements = libsonata.ElementReportReader('path/to/H5/file')

# list populations
>>> elements.get_population_names()

# open population
>>> population_elements = elements['<name>']
ElementReportPopulation
# get times (tstart, tstop, dt)
>>> population_elements.times
(0.0, 4.0, 0.2)

>>> population_elements.get_node_ids()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# get the DataFrame of the node_id values for the timesteps between tstart and tstop
>>> data_frame = population_elements.get(node_ids=[13, 14], tstart=0.8, tstop=1.0)

# get the data values (list of list of floats with data[time_index][element_index])
>>> data_frame.data
[[46.0, 46.1, 46.2, 46.3, 46.4, 46.5, 46.6, 46.7, 46.8, 46.9], [56.0, 56.1, 56.2, 56.3, 56.4, 56.5, 56.6, 56.7, 56.8, 56.9]]

# get the list of timesteps
>>> data_frame.times
[0.8, 1.0]

# get the list of (node id, element_id)
>>> data_frame.ids
[(13, 30), (13, 30), (13, 31), (13, 31), (13, 32), (14, 32), (14, 33), (14, 33), (14, 34), (14, 34)]

The same way than with spikes and soma reports, pandas can be used to get a better representation of the data

>>> import pandas

df = pandas.DataFrame(data_frame.data, columns=pandas.MultiIndex.from_tuples(data_frame.ids), index=data_frame.times)
print(df)
       13                            14
       30    30    31    31    32    32    33    33    34    34
0.8  46.0  46.1  46.2  46.3  46.4  46.5  46.6  46.7  46.8  46.9
1.0  56.0  56.1  56.2  56.3  56.4  56.5  56.6  56.7  56.8  56.9

For big datasets, using numpy arrays could greatly improve the performance

>>> import numpy

np_data = numpy.asarray(data_frame.data)
np_ids = numpy.asarray(data_frame.ids).T
np_times = numpy.asarray(data_frame.times)

df = pandas.DataFrame(np_data, columns=pandas.MultiIndex.from_arrays(np_ids), index=np_times)

Acknowledgements

The development of this software was supported by funding to the Blue Brain Project, a research center of the École polytechnique fédérale de Lausanne (EPFL), from the Swiss government’s ETH Board of the Swiss Federal Institutes of Technology.

This research was supported by the EBRAINS research infrastructure, funded from the European Union’s Horizon 2020 Framework Programme for Research and Innovation under the Specific Grant Agreement No. 945539 (Human Brain Project SGA3). This project/research has received funding from the European Union’s Horizon 2020 Framework Programme for Research and Innovation under the Specific Grant Agreement No. 785907 (Human Brain Project SGA2).

License

libsonata is distributed under the terms of the GNU Lesser General Public License version 3, unless noted otherwise, for example, for external dependencies. Refer to COPYING.LESSER and COPYING files for details.

Copyright (c) 2018-2022 Blue Brain Project/EPFL

libsonata is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 as published by the Free Software Foundation.

libsonata is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with libsonata. If not, see <https://www.gnu.org/licenses/>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libsonata-0.1.21.tar.gz (1.0 MB view details)

Uploaded Source

Built Distributions

libsonata-0.1.21-cp311-cp311-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

libsonata-0.1.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

libsonata-0.1.21-cp311-cp311-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11 macOS 10.13+ x86-64

libsonata-0.1.21-cp310-cp310-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

libsonata-0.1.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

libsonata-0.1.21-cp310-cp310-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10 macOS 10.13+ x86-64

libsonata-0.1.21-cp39-cp39-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

libsonata-0.1.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

libsonata-0.1.21-cp39-cp39-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9 macOS 10.13+ x86-64

libsonata-0.1.21-cp38-cp38-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

libsonata-0.1.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

libsonata-0.1.21-cp38-cp38-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8 macOS 10.13+ x86-64

File details

Details for the file libsonata-0.1.21.tar.gz.

File metadata

  • Download URL: libsonata-0.1.21.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for libsonata-0.1.21.tar.gz
Algorithm Hash digest
SHA256 5b280d82cb21a745d9cdb736c00446e0209494c21d2d17a2c878fabf23a08b68
MD5 4f8a2c843ad8ee4f76ccdc9761b8ef90
BLAKE2b-256 28a6d1346f5d4ce626d78450834c392aa1392a476a74fd22324538b051b47c15

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0d2bc41dde3a0fecf5eb60d18b601029ec57c16347cfd22abf259fa86ec45b51
MD5 b3629017536baa658fe768cbaabad470
BLAKE2b-256 f9edc6569eee9c1558c2f371ad49f439ab3a5d3c2aa36f9ad8bceb912328a29b

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4a5ebe4e6602ee4989443ab76b8d5cf1dde8f9053912fdde8fe7d89bb38d814
MD5 067b56543e233089393f5c55014f51af
BLAKE2b-256 3705cdce88b5d1d9cbeefbd66564dbcf74bc6593e237bddd8edc28789ca1bbc7

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp311-cp311-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 3a8122cc93b381de529ca3e763ca13f415241b54c058132b8b03ab800b01ecad
MD5 0c86c8a6c3af35c198cfcc21ea430696
BLAKE2b-256 82a60bb4ff4d572507fb4595b20d38be40921fab1e206184fa4c6bcccf7f49c2

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4d22cc152cbd4af7bf7d266ae746499d545815d284c68bf13e97abcc02e99461
MD5 35af48b080683f3e0cc71934fbd6e4c2
BLAKE2b-256 c6bbbde948d5d117e1e158797f5fd67df4fb8525bb22dea2dbf8e6d941a85349

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69ae9bc0758a1dc89cb7ee5f3361e3434b500843855aec236cbe2f23942397ef
MD5 3cef9221062af6ff07ac6d3f9607287f
BLAKE2b-256 d4fa97cce75989744efaa89587fd17992b0d06defec3bb92c1fdb41ea142c3a7

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp310-cp310-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp310-cp310-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 46291c69eca9f155edef7b4482a0ca6c6c364674906c9e50a2a51aa4f64d68fa
MD5 c0115676a27b3418fb76bfb51c9dbcfa
BLAKE2b-256 4224e5804e52388525ccd0b242d2140bfffb16b9d2f39fff4c1d95dbe0e21b05

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9d515e89f74d99fda53372add7a5aca959c836a38867f06c9d7f288569f5d1c7
MD5 864779947c21bf65c6d3cc6c1151075c
BLAKE2b-256 a5241c92dc5b9c0efc0b749bf254fbb801d40c0284e59ea31148d42a0fc2632e

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e4b377773365c8b5af50e49fee0fe24613879f6d573d21d02437908b86546733
MD5 095b4d80fbd3dc2a31cff5348d1d069d
BLAKE2b-256 3a618375abb11fcac62ad0122df92ed3e19484ba1f3a7bec380f0513d06d1ccd

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp39-cp39-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp39-cp39-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 35df266be28492056e45b8f05ed1e19d132ff9bfc2850cda7f1a3bf4d7a60c8f
MD5 29098196b3c6aef28cf5528d63c815f7
BLAKE2b-256 309bd54a2ed7546ecaed8228dd42ed24c7b67614001ac45b369860b6b830de7a

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d21d0c1f29d7662139ca4894e99fc5daff6e320c8bd6d5d969ab8d822db774b9
MD5 8c9a4e8b0282a4c9c3551c2659bad1e1
BLAKE2b-256 ef8a71c345e96079fc22adb801c7759996204e1ea73fa8f0a7a5538d0a9161f1

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6aeb69ed857672b8a7e296ae018d07b4677ab0a5ce63d4fbe0af29cc8a0244db
MD5 0c97650467f76ccf80d0ee7c4deefeab
BLAKE2b-256 b55bb57adecfbd07287895f80a79488edbbae325e635d714045ef77cabbfdceb

See more details on using hashes here.

File details

Details for the file libsonata-0.1.21-cp38-cp38-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for libsonata-0.1.21-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d61956e2b92aa7fde148c88d7ede89e8c504d61c755de7a84cd5176f7d168001
MD5 913c78eca8df908eb8cecdf12128c342
BLAKE2b-256 2a19e37e779e8bfeb66e00ddaacc383e4c56d2de1a0c022331816d2cb01bf7c9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page