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

Expose diverse probes in Python API. #1225

Merged
merged 6 commits into from
Nov 18, 2020

Conversation

halfflat
Copy link
Contributor

@halfflat halfflat commented Nov 9, 2020

Major changes in Python library and documentation:

  • Add global state (immutable after initialization) for the Python module that
    manages the mapping between probe metadata types and the method by which the
    corresponding sample data can be collected and presented as Python objects.
    This is necessary for decoupling the implementation of the simulation wrapper
    from that of the various cable cell probe types.

  • Wrap each of the C++ cable cell probe types with a Python function that
    returns a probe_info object.

  • Add code for converting and accumulating C++ probe metadata and sample
    data, registered in the module global state against the metadata type info.

  • Make the arbor.simulator object responsible for recording all spike and
    sample data, with corresponding new methods.

  • Use NumPy arrays and structured datatypes for returning spike and
    sample data.

  • Place Python schedule proxies under an abstract interface so that consumers
    of the proxies can be made generic over schedule types.

  • Add unit tests for the arbor.simulator class and its new functionality,
    including distributed spike collection.

  • Rework all Python API documentation concerning probes, sampling, and spike
    recording; add information on newly exposed cable cell probe addresses.

  • Add new python example single_cell_recipe.py which is a generic recipe
    version of single_cell_model.py.

  • Adjust other code in the wrapper and examples to accomodate these changes.

Minor changes in Python library:

  • Add equality tests for arbor.location and arbor.cable Python classes.

  • Use the py:: namespace shorthand more often in the Python wrapping sources.

  • Add an implicit conversion for a pair of numbers to arbor.cell_member, so
    that e.g. probe ids can be specified simply as (gid, index).

  • Add an implicit conversion from a tuple to arbor.mpoint so that a
    morphological point can be specified simply as (x, y, z, radius).

  • Modify the interface to arbor.regular_schedule so that the overloads
    correspond more closely to the C++ API. In particular,
    arbor.regular_schedule(dt) now describes a schedule with times 0, dt, 2*dt,
    etc.

Minor changes in C++ library:

  • Change test in FVM lowered cell implementation and exception message for
    bad_source_description: assert number of detectors is exactly the number
    of sources promised by the recipe.

  • Add comment describing probe_metadata object in sampling.hpp which
    asserts that the type and value of probe-specific metadata completely
    determines the correct interpretation of sampled data provided to a callback.

Implements #1160.

@thorstenhater
Copy link
Contributor

Hi,

a short comment on the motivation would be helpful for me.

T

Copy link
Contributor

@noraabiakar noraabiakar left a comment

Choose a reason for hiding this comment

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

This is great!
I have a few questions and suggestions. My main problem is with the probe/sampling docs. I don't think they're very clear. They probably should be extended, if not in this PR, then soon after.

doc/python/cable_cell.rst Outdated Show resolved Hide resolved
doc/python/cable_cell.rst Outdated Show resolved Hide resolved
doc/python/cable_cell.rst Show resolved Hide resolved
doc/python/cable_cell.rst Show resolved Hide resolved
doc/python/cable_cell.rst Outdated Show resolved Hide resolved
doc/python/cable_cell.rst Show resolved Hide resolved
doc/python/simulation.rst Outdated Show resolved Hide resolved
python/cable_probes.cpp Outdated Show resolved Hide resolved
python/identifiers.cpp Show resolved Hide resolved
python/test/unit/test_simulator.py Outdated Show resolved Hide resolved
@halfflat
Copy link
Contributor Author

a short comment on the motivation would be helpful for me.

I totally forgot to link to the corresponding issue (#1160). I've added that to the description above.

doc/python/simulation.rst Outdated Show resolved Hide resolved
python/simulation.cpp Outdated Show resolved Hide resolved
arbor/CMakeLists.txt Outdated Show resolved Hide resolved
@bcumming
Copy link
Member

bors try

bors bot added a commit that referenced this pull request Nov 11, 2020
@bors
Copy link

bors bot commented Nov 11, 2020

try

Build succeeded:

Copy link
Member

@bcumming bcumming left a comment

Choose a reason for hiding this comment

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

Nice work. There are a few requested changes.

@@ -377,6 +377,8 @@ Overriding properties locally
the morphology.


.. _cable-cell-probes:
Copy link
Member

Choose a reason for hiding this comment

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

Generic probe-related documentation is missing from the concepts docs. Can we move the generic definitions/concepts there, then have the cpp and python docs refer to those and give only language-specific implementation and usage details?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would this be the content that's been put under simulation? I'm not averse to this, but maybe we could make the concept docs a different task, as it's not exactly in scope for this PR, and would delay merging.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know what you are referring to by "under simulation".

The issue here is that documentation has been created under Python API, but nothing added to concepts. The cpp and python docs might be simplified if a lot of the common part of that documentation is moved to concepts. So I think that it is actually in the scope of this PR.

If it is in a separate PR, then it should be done ASAP, otherwise it will be forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean, I added some text in the documentation of the simulation class that explained the probes and sampling. Is this suitable material for moving into Concepts?

python/example/network_ring.py Show resolved Hide resolved
python/example/single_cell_recipe.py Outdated Show resolved Hide resolved
" index: The cell-local index of the item.\n")
.def(py::init([](py::tuple t) {
if (py::len(t)!=2) throw std::runtime_error("tuple length != 4");
return arb::cell_member_type{t[0].cast<arb::cell_gid_type>(), t[1].cast<arb::cell_lid_type>()};
Copy link
Member

Choose a reason for hiding this comment

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

What happens with these casts if the user passes a tuple with non-convertible types, e.g.:

m = arbor.cell_member(("hello", 23.2))

You can force pybind11 to do the casting and checking for you by making the arguments to be std::tuple<arb::cell_gid_type, arb::cell_lid_type> instead of pybind11::typle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we do that with the current version of the PR, we get:

RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details)

Is that sufficient?

Copy link
Member

Choose a reason for hiding this comment

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

Sufficient. I was hoping that it would just work! I always have to test.

"Enumeration used to indicate which hardware backend to execute a cell group on.")
.value("gpu", arb::backend_kind::gpu,
"Use GPU backend.")
.value("multicore", arb::backend_kind::multicore,
"Use multicore backend.");

pybind11::enum_<arb::binning_kind>(m, "binning",
py::enum_<arb::binning_kind>(m, "binning",
Copy link
Member

Choose a reason for hiding this comment

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

I deliberately chose pybind11 as the namespace. By doing this we are now inconsistent (you are probably going to show me one spot where I used py instead of pybind11 now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, there were multiple cases of using py:: instead of pybind11::, so I went with the shorter for consistency and brevity.

@@ -0,0 +1,84 @@
#pragma once
Copy link
Member

Choose a reason for hiding this comment

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

How about putting this content in python/cable_probes.hpp, because this is really specific to probes/samplers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's probes and samplers, but not cable probes specifically.

Part of the motivation for this design is to keep the simulator code uncoupled from the cable probe definitions and wrappers. This way, the cable probe wrapper code is responsible for registering its metadata handlers, and the simulator code is responsible for using those handlers to process samples.

python/simulation.cpp Outdated Show resolved Hide resolved
python/simulation.cpp Outdated Show resolved Hide resolved
python/test/unit_distributed/test_simulator.py Outdated Show resolved Hide resolved
python/pyarb.cpp Show resolved Hide resolved
Major changes in Python library and documentation:

* Add global state (immutable after initialization) for the Python module that
  manages the mapping between probe metadata types and the method by which the
  corresponding [sample data can be collected and presented as Python objects.
  This is necessary for decoupling the implementation of the simulation wrapper
  from that of the various cable cell probe types.

* Wrap each of the C++ cable cell probe types with a Python function that
  returns a `probe_info` object.

* Add code for converting and accumulating C++ probe metadata and sample
  data, registered in the module global state against the metadata type info.

* Make the `arbor.simulator` object responsible for recording all spike and
  sample data, with corresponding new methods.

* Use NumPy arrays and structured datatypes for returning spike and
  sample data.

* Place Python schedule proxies under an abstract interface so that consumers
  of the proxies can be made generic over schedule types.

* Add unit tests for the `arbor.simulator` class and its new functionality,
  including distributed spike collection.

* Rework all Python API documentation concerning probes, sampling, and spike
  recording; add information on newly exposed cable cell probe addresses.

* Add new python example `single_cell_recipe.py` which is a generic recipe
  version of `single_cell_model.py`.

* Adjust other code in the wrapper and examples to accomodate these changes.

Minor changes in Python library:

* Add equality tests for `arbor.location` and `arbor.cable` Python classes.

* Use the `py::` namespace shorthand more often in the Python wrapping sources.

* Add an implicit conversion for a pair of numbers to `arbor.cell_member`, so
  that e.g. probe ids can be specified simply as `(gid, index)`.

* Add an implicit conversion from a tuple to `arbor.mpoint` so that a
  morphological point can be specified simply as `(x, y, z, radius)`.

* Modify the interface to `arbor.regular_schedule` so that the overloads
  correspond more closely to the C++ API. In particular,
  `arbor.regular_schedule(dt)` now describes a schedule with times 0, dt, 2*dt,
  etc.

Minor changes in C++ library:

* Change test in FVM lowered cell implementation and exception message for
  `bad_source_description`: assert number of detectors is exactly the number
  of sources promised by the recipe.

* Add comment describing `probe_metadata` object in `sampling.hpp` which
  asserts that the type and value of probe-specific metadata completely
  determines the correct interpretation of sampled data provided to a callback.
* Add named arguments to cable probe address functions.
* Add tests for successful construction, correct metadata for each cable probe address function.
* Build arbor library with -fPIC to avoid relocation errors with Python lib.
* Remove redundant line of old debug code in Python simulaiton test.
* Add pointer from Python cable cell probes documentation to simulation class documentation for details regarding probes etc.
* Use `where` instead of `locations` in documentation of Python cable cell probe address wrappers, so that it matches the keyword name in the wrapper.
* Fix typos and misformats identified in PR review.
* Clarify in Python cable cell probe docs that location expressions in probe addresses are interpreted in the context of the cell on which the probe is defined.
* Add missing assertion in multiple probe per probe id metadata check in test_simulator.py.
* Add `single_cell_recipe.py` to set of Python examples run in Travis test script.
* Add comment re: PYBIND11_NUMPY_DTYPE.
* Remove debug prints from unit_distributed/test_simulator.py.
* Add `ci=None` to seaborn plot in single_cell_recipe.py.
* Rename: `py_simulation` -> `simulation_shim`; `py_spike_recording` → `spike_recording`.
@halfflat halfflat force-pushed the feature/python-probe-diversity branch from a630474 to 14e1d9e Compare November 11, 2020 17:58
@halfflat
Copy link
Contributor Author

Addressed some of the PR comments; rebased off of current master.

bcumming pushed a commit that referenced this pull request Nov 13, 2020
Fix Python linking errors on some platforms.

This appears to be related to the GCC visibility of methods overridden in header files, and is fixed by moving the implementation to the corresponding cpp files. 

Addresses the Travis CI failure of #1225
* Use pybind11/operators.h instead of explicit __eq__ method.
@halfflat halfflat merged commit 1bfe96b into arbor-sim:master Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants