Skip to content

Commit

Permalink
Merge mut/readonly somata.
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherioszisis committed Jan 17, 2022
1 parent 3ba82b5 commit a5b7df2
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 253 deletions.
1 change: 1 addition & 0 deletions binds/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pybind11_add_module(_morphio SYSTEM
bind_immutable.cpp
bindings_utils.cpp
bind_misc.cpp
bind_soma.cpp
bind_mutable.cpp
bind_vasculature.cpp
)
Expand Down
27 changes: 0 additions & 27 deletions binds/python/bind_immutable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,33 +209,6 @@ void bind_immutable_module(py::module& m) {
&morphio::EndoplasmicReticulum::filamentCounts,
"Returns the number of filaments for each neuronal section");


py::class_<morphio::Soma>(m, "Soma")
.def(py::init<const morphio::Soma&>())
.def_property_readonly(
"points",
[](morphio::Soma* soma) { return span_array_to_ndarray(soma->points()); },
"Returns the coordinates (x,y,z) of all soma point")
.def_property_readonly(
"diameters",
[](morphio::Soma* soma) { return span_to_ndarray(soma->diameters()); },
"Returns the diameters of all soma points")

.def_property_readonly(
"center",
[](morphio::Soma* soma) { return py::array(3, soma->center().data()); },
"Returns the center of gravity of the soma points")
.def_property_readonly("max_distance",
&morphio::Soma::maxDistance,
"Return the maximum distance between the center of gravity "
"and any of the soma points")
.def_property_readonly("type", &morphio::Soma::type, "Returns the soma type")

.def_property_readonly("surface",
&morphio::Soma::surface,
"Returns the soma surface\n\n"
"Note: the soma surface computation depends on the soma type");

py::class_<morphio::Section>(m, "Section")
.def("__str__",
[](const morphio::Section& section) {
Expand Down
40 changes: 2 additions & 38 deletions binds/python/bind_mutable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void bind_mutable_module(py::module& m) {
py::return_value_policy::reference)
.def_property_readonly(
"soma",
static_cast<std::shared_ptr<morphio::mut::Soma>& (morphio::mut::Morphology::*) ()>(
&morphio::mut::Morphology::soma),
static_cast<std::shared_ptr<morphio::Soma> (morphio::mut::Morphology::*) ()>(
&morphio::mut::Morphology::soma),
"Returns a reference to the soma object\n\n"
"Note: multiple morphologies can share the same Soma "
"instance")
Expand Down Expand Up @@ -452,42 +452,6 @@ void bind_mutable_module(py::module& m) {
"point_level_properties"_a,
"section_type"_a = morphio::SectionType::SECTION_UNDEFINED);

py::class_<morphio::mut::Soma, std::shared_ptr<morphio::mut::Soma>>(m, "Soma")
.def(py::init<const morphio::Property::PointLevel&>())
.def_property(
"points",
[](morphio::mut::Soma* soma) {
return py::array(static_cast<py::ssize_t>(soma->points().size()),
soma->points().data());
},
[](morphio::mut::Soma* soma, py::array_t<morphio::floatType> _points) {
soma->points() = array_to_points(_points);
},
"Returns the coordinates (x,y,z) of all soma point")
.def_property(
"diameters",
[](morphio::mut::Soma* soma) {
return py::array(static_cast<py::ssize_t>(soma->diameters().size()),
soma->diameters().data());
},
[](morphio::mut::Soma* soma, py::array_t<morphio::floatType> _diameters) {
soma->diameters() = _diameters.cast<std::vector<morphio::floatType>>();
},
"Returns the diameters of all soma points")
.def_property_readonly("type", &morphio::mut::Soma::type, "Returns the soma type")
.def_property_readonly("surface",
&morphio::mut::Soma::surface,
"Returns the soma surface\n\n"
"Note: the soma surface computation depends on the soma type")
.def_property_readonly("max_distance",
&morphio::mut::Soma::maxDistance,
"Return the maximum distance between the center of gravity "
"and any of the soma points")
.def_property_readonly(
"center",
[](morphio::mut::Soma* soma) { return py::array(3, soma->center().data()); },
"Returns the center of gravity of the soma points");

py::class_<morphio::mut::EndoplasmicReticulum>(m, "EndoplasmicReticulum")
.def(py::init<>())
.def(py::init<const std::vector<uint32_t>&,
Expand Down
64 changes: 64 additions & 0 deletions binds/python/bind_soma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>

#include <morphio/soma.h>
#include <morphio/types.h>

#include "bind_soma.h"
#include "bindings_utils.h"

namespace py = pybind11;

void bind_soma_module(py::module& m) {

py::class_<morphio::Soma, std::shared_ptr<morphio::Soma>>(m, "Soma")
.def(py::init<const morphio::Soma&>())

.def_property(
"points",
[](morphio::Soma* soma) {
return py::array(static_cast<py::ssize_t>(soma->points().size()),
soma->points().data());
},
[](morphio::Soma* soma, py::array_t<morphio::floatType> _points) {
soma->points() = array_to_points(_points);
},
"Returns the coordinates (x,y,z) of all soma point"
)

.def_property(
"diameters",
[](morphio::Soma* soma) {
return py::array(static_cast<py::ssize_t>(soma->diameters().size()),
soma->diameters().data());
},
[](morphio::Soma* soma, py::array_t<morphio::floatType> _diameters) {
soma->diameters() = _diameters.cast<std::vector<morphio::floatType>>();
},
"Returns the diameters of all soma points"
)

.def_property_readonly(
"center",
[](morphio::Soma* soma) { return py::array(3, soma->center().data()); },
"Returns the center of gravity of the soma points"
)

.def_property_readonly(
"max_distance",
&morphio::Soma::maxDistance,
"Return the maximum distance between the center of gravity "
"and any of the soma points"
)

.def_property_readonly("type", &morphio::Soma::type, "Returns the soma type")

.def_property_readonly(
"surface",
&morphio::Soma::surface,
"Returns the soma surface\n\n"
"Note: the soma surface computation depends on the soma type"
);

}
4 changes: 4 additions & 0 deletions binds/python/bind_soma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <pybind11/pybind11.h>

void bind_soma_module(pybind11::module& m);
17 changes: 17 additions & 0 deletions binds/python/bindings_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ py::array_t<morphio::floatType> span_to_ndarray(const morphio::range<const T>& s
}


template <typename T>
py::array_t<morphio::floatType> vector_to_ndarray(const std::vector<T>& span) {
const void* ptr = static_cast<const void*>(span.data());
const auto buffer_info = py::buffer_info(
// Cast from (const void*) to (void*) for function signature matching
const_cast<void*>(ptr), /* Pointer to buffer */
sizeof(T), /* Size of one scalar */
py::format_descriptor<T>::format(), /* Python struct-style format descriptor */
1, /* Number of dimensions */

// Forced cast to prevent error:
// template argument deduction/substitution failed */
{static_cast<int>(span.size())}, /* buffer dimentions */
{sizeof(T)}); /* Strides (in bytes) for each index */
return py::array(buffer_info);
}

/**
* @brief "Casts" a Cpp sequence to a python array (no memory copies)
* Python capsule handles void pointers to objects and makes sure
Expand Down
2 changes: 2 additions & 0 deletions binds/python/morphio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include "bind_immutable.h"
#include "bind_misc.h"
#include "bind_soma.h"
#include "bind_mutable.h"
#include "bind_vasculature.h"

namespace py = pybind11;

PYBIND11_MODULE(_morphio, m) {
bind_misc(m);
bind_soma_module(m);
bind_immutable_module(m);

py::module mut_module = m.def_submodule("mut");
Expand Down
1 change: 1 addition & 0 deletions include/morphio/morphology.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Morphology
const MorphologyVersion& version() const;

protected:

friend class mut::Morphology;
Morphology(const Property::Properties& properties, unsigned int options);

Expand Down
13 changes: 3 additions & 10 deletions include/morphio/mut/morphology.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <morphio/exceptions.h>
#include <morphio/mut/endoplasmic_reticulum.h>
#include <morphio/mut/mitochondria.h>
#include <morphio/mut/soma.h>
#include <morphio/soma.h>
#include <morphio/properties.h>
#include <morphio/section.h>
#include <morphio/types.h>
Expand Down Expand Up @@ -68,14 +68,14 @@ class Morphology
Note: multiple morphologies can share the same Soma instance
**/
inline std::shared_ptr<Soma>& soma() noexcept;
std::shared_ptr<Soma> soma() noexcept { return _soma; }

/**
Returns a shared pointer on the Soma
Note: multiple morphologies can share the same Soma instance
**/
inline const std::shared_ptr<Soma>& soma() const noexcept;
const std::shared_ptr<Soma> soma() const noexcept { return _soma; }

/**
* Return the mitochondria container class
Expand Down Expand Up @@ -252,13 +252,6 @@ inline const std::map<uint32_t, std::shared_ptr<Section>>& Morphology::sections(
return _sections;
}

inline std::shared_ptr<Soma>& Morphology::soma() noexcept {
return _soma;
}

inline const std::shared_ptr<Soma>& Morphology::soma() const noexcept {
return _soma;
}

inline Mitochondria& Morphology::mitochondria() noexcept {
return _mitochondria;
Expand Down
94 changes: 0 additions & 94 deletions include/morphio/mut/soma.h

This file was deleted.

Loading

0 comments on commit a5b7df2

Please sign in to comment.