Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit a5b7df2

Browse files
Merge mut/readonly somata.
1 parent 3ba82b5 commit a5b7df2

19 files changed

Lines changed: 175 additions & 253 deletions

binds/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pybind11_add_module(_morphio SYSTEM
55
bind_immutable.cpp
66
bindings_utils.cpp
77
bind_misc.cpp
8+
bind_soma.cpp
89
bind_mutable.cpp
910
bind_vasculature.cpp
1011
)

binds/python/bind_immutable.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -209,33 +209,6 @@ void bind_immutable_module(py::module& m) {
209209
&morphio::EndoplasmicReticulum::filamentCounts,
210210
"Returns the number of filaments for each neuronal section");
211211

212-
213-
py::class_<morphio::Soma>(m, "Soma")
214-
.def(py::init<const morphio::Soma&>())
215-
.def_property_readonly(
216-
"points",
217-
[](morphio::Soma* soma) { return span_array_to_ndarray(soma->points()); },
218-
"Returns the coordinates (x,y,z) of all soma point")
219-
.def_property_readonly(
220-
"diameters",
221-
[](morphio::Soma* soma) { return span_to_ndarray(soma->diameters()); },
222-
"Returns the diameters of all soma points")
223-
224-
.def_property_readonly(
225-
"center",
226-
[](morphio::Soma* soma) { return py::array(3, soma->center().data()); },
227-
"Returns the center of gravity of the soma points")
228-
.def_property_readonly("max_distance",
229-
&morphio::Soma::maxDistance,
230-
"Return the maximum distance between the center of gravity "
231-
"and any of the soma points")
232-
.def_property_readonly("type", &morphio::Soma::type, "Returns the soma type")
233-
234-
.def_property_readonly("surface",
235-
&morphio::Soma::surface,
236-
"Returns the soma surface\n\n"
237-
"Note: the soma surface computation depends on the soma type");
238-
239212
py::class_<morphio::Section>(m, "Section")
240213
.def("__str__",
241214
[](const morphio::Section& section) {

binds/python/bind_mutable.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void bind_mutable_module(py::module& m) {
5454
py::return_value_policy::reference)
5555
.def_property_readonly(
5656
"soma",
57-
static_cast<std::shared_ptr<morphio::mut::Soma>& (morphio::mut::Morphology::*) ()>(
58-
&morphio::mut::Morphology::soma),
57+
static_cast<std::shared_ptr<morphio::Soma> (morphio::mut::Morphology::*) ()>(
58+
&morphio::mut::Morphology::soma),
5959
"Returns a reference to the soma object\n\n"
6060
"Note: multiple morphologies can share the same Soma "
6161
"instance")
@@ -452,42 +452,6 @@ void bind_mutable_module(py::module& m) {
452452
"point_level_properties"_a,
453453
"section_type"_a = morphio::SectionType::SECTION_UNDEFINED);
454454

455-
py::class_<morphio::mut::Soma, std::shared_ptr<morphio::mut::Soma>>(m, "Soma")
456-
.def(py::init<const morphio::Property::PointLevel&>())
457-
.def_property(
458-
"points",
459-
[](morphio::mut::Soma* soma) {
460-
return py::array(static_cast<py::ssize_t>(soma->points().size()),
461-
soma->points().data());
462-
},
463-
[](morphio::mut::Soma* soma, py::array_t<morphio::floatType> _points) {
464-
soma->points() = array_to_points(_points);
465-
},
466-
"Returns the coordinates (x,y,z) of all soma point")
467-
.def_property(
468-
"diameters",
469-
[](morphio::mut::Soma* soma) {
470-
return py::array(static_cast<py::ssize_t>(soma->diameters().size()),
471-
soma->diameters().data());
472-
},
473-
[](morphio::mut::Soma* soma, py::array_t<morphio::floatType> _diameters) {
474-
soma->diameters() = _diameters.cast<std::vector<morphio::floatType>>();
475-
},
476-
"Returns the diameters of all soma points")
477-
.def_property_readonly("type", &morphio::mut::Soma::type, "Returns the soma type")
478-
.def_property_readonly("surface",
479-
&morphio::mut::Soma::surface,
480-
"Returns the soma surface\n\n"
481-
"Note: the soma surface computation depends on the soma type")
482-
.def_property_readonly("max_distance",
483-
&morphio::mut::Soma::maxDistance,
484-
"Return the maximum distance between the center of gravity "
485-
"and any of the soma points")
486-
.def_property_readonly(
487-
"center",
488-
[](morphio::mut::Soma* soma) { return py::array(3, soma->center().data()); },
489-
"Returns the center of gravity of the soma points");
490-
491455
py::class_<morphio::mut::EndoplasmicReticulum>(m, "EndoplasmicReticulum")
492456
.def(py::init<>())
493457
.def(py::init<const std::vector<uint32_t>&,

binds/python/bind_soma.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#include <pybind11/numpy.h>
3+
#include <pybind11/pybind11.h>
4+
5+
#include <morphio/soma.h>
6+
#include <morphio/types.h>
7+
8+
#include "bind_soma.h"
9+
#include "bindings_utils.h"
10+
11+
namespace py = pybind11;
12+
13+
void bind_soma_module(py::module& m) {
14+
15+
py::class_<morphio::Soma, std::shared_ptr<morphio::Soma>>(m, "Soma")
16+
.def(py::init<const morphio::Soma&>())
17+
18+
.def_property(
19+
"points",
20+
[](morphio::Soma* soma) {
21+
return py::array(static_cast<py::ssize_t>(soma->points().size()),
22+
soma->points().data());
23+
},
24+
[](morphio::Soma* soma, py::array_t<morphio::floatType> _points) {
25+
soma->points() = array_to_points(_points);
26+
},
27+
"Returns the coordinates (x,y,z) of all soma point"
28+
)
29+
30+
.def_property(
31+
"diameters",
32+
[](morphio::Soma* soma) {
33+
return py::array(static_cast<py::ssize_t>(soma->diameters().size()),
34+
soma->diameters().data());
35+
},
36+
[](morphio::Soma* soma, py::array_t<morphio::floatType> _diameters) {
37+
soma->diameters() = _diameters.cast<std::vector<morphio::floatType>>();
38+
},
39+
"Returns the diameters of all soma points"
40+
)
41+
42+
.def_property_readonly(
43+
"center",
44+
[](morphio::Soma* soma) { return py::array(3, soma->center().data()); },
45+
"Returns the center of gravity of the soma points"
46+
)
47+
48+
.def_property_readonly(
49+
"max_distance",
50+
&morphio::Soma::maxDistance,
51+
"Return the maximum distance between the center of gravity "
52+
"and any of the soma points"
53+
)
54+
55+
.def_property_readonly("type", &morphio::Soma::type, "Returns the soma type")
56+
57+
.def_property_readonly(
58+
"surface",
59+
&morphio::Soma::surface,
60+
"Returns the soma surface\n\n"
61+
"Note: the soma surface computation depends on the soma type"
62+
);
63+
64+
}

binds/python/bind_soma.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include <pybind11/pybind11.h>
3+
4+
void bind_soma_module(pybind11::module& m);

binds/python/bindings_utils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ py::array_t<morphio::floatType> span_to_ndarray(const morphio::range<const T>& s
2929
}
3030

3131

32+
template <typename T>
33+
py::array_t<morphio::floatType> vector_to_ndarray(const std::vector<T>& span) {
34+
const void* ptr = static_cast<const void*>(span.data());
35+
const auto buffer_info = py::buffer_info(
36+
// Cast from (const void*) to (void*) for function signature matching
37+
const_cast<void*>(ptr), /* Pointer to buffer */
38+
sizeof(T), /* Size of one scalar */
39+
py::format_descriptor<T>::format(), /* Python struct-style format descriptor */
40+
1, /* Number of dimensions */
41+
42+
// Forced cast to prevent error:
43+
// template argument deduction/substitution failed */
44+
{static_cast<int>(span.size())}, /* buffer dimentions */
45+
{sizeof(T)}); /* Strides (in bytes) for each index */
46+
return py::array(buffer_info);
47+
}
48+
3249
/**
3350
* @brief "Casts" a Cpp sequence to a python array (no memory copies)
3451
* Python capsule handles void pointers to objects and makes sure

binds/python/morphio.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
#include "bind_immutable.h"
44
#include "bind_misc.h"
5+
#include "bind_soma.h"
56
#include "bind_mutable.h"
67
#include "bind_vasculature.h"
78

89
namespace py = pybind11;
910

1011
PYBIND11_MODULE(_morphio, m) {
1112
bind_misc(m);
13+
bind_soma_module(m);
1214
bind_immutable_module(m);
1315

1416
py::module mut_module = m.def_submodule("mut");

include/morphio/morphology.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class Morphology
141141
const MorphologyVersion& version() const;
142142

143143
protected:
144+
144145
friend class mut::Morphology;
145146
Morphology(const Property::Properties& properties, unsigned int options);
146147

include/morphio/mut/morphology.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <morphio/exceptions.h>
1111
#include <morphio/mut/endoplasmic_reticulum.h>
1212
#include <morphio/mut/mitochondria.h>
13-
#include <morphio/mut/soma.h>
13+
#include <morphio/soma.h>
1414
#include <morphio/properties.h>
1515
#include <morphio/section.h>
1616
#include <morphio/types.h>
@@ -68,14 +68,14 @@ class Morphology
6868
6969
Note: multiple morphologies can share the same Soma instance
7070
**/
71-
inline std::shared_ptr<Soma>& soma() noexcept;
71+
std::shared_ptr<Soma> soma() noexcept { return _soma; }
7272

7373
/**
7474
Returns a shared pointer on the Soma
7575
7676
Note: multiple morphologies can share the same Soma instance
7777
**/
78-
inline const std::shared_ptr<Soma>& soma() const noexcept;
78+
const std::shared_ptr<Soma> soma() const noexcept { return _soma; }
7979

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

255-
inline std::shared_ptr<Soma>& Morphology::soma() noexcept {
256-
return _soma;
257-
}
258-
259-
inline const std::shared_ptr<Soma>& Morphology::soma() const noexcept {
260-
return _soma;
261-
}
262255

263256
inline Mitochondria& Morphology::mitochondria() noexcept {
264257
return _mitochondria;

include/morphio/mut/soma.h

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)