From a699a743d7656f2aa70dbebd6f1c79d0e8711d54 Mon Sep 17 00:00:00 2001
From: Nora Abi Akar <nora.abiakar@gmail.com>
Date: Mon, 1 Mar 2021 16:18:39 +0100
Subject: [PATCH]  More flexible ion species parameter specification [python]
 (#1388)

Update `set_ion` method of `cable_global_properties` to allow the addition of new ions
---
 python/cells.cpp | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/python/cells.cpp b/python/cells.cpp
index 2d9589b3..5788bb86 100644
--- a/python/cells.cpp
+++ b/python/cells.cpp
@@ -343,23 +343,35 @@ void register_cells(pybind11::module& m) {
         // add/modify ion species
         .def("set_ion",
             [](arb::cable_cell_global_properties& props, const char* ion,
-               optional<double> int_con, optional<double> ext_con,
-               optional<double> rev_pot, pybind11::object method)
+               optional<double> valence, optional<double> int_con,
+               optional<double> ext_con, optional<double> rev_pot,
+               pybind11::object method)
             {
+                if (!props.ion_species.count(ion) && !valence) {
+                    throw std::runtime_error(util::pprintf("New ion species: '{}', missing valence", ion));
+                }
+                if (valence) props.ion_species[ion] = *valence;
+
                 auto& data = props.default_parameters.ion_data[ion];
                 if (int_con) data.init_int_concentration = *int_con;
                 if (ext_con) data.init_ext_concentration = *ext_con;
                 if (rev_pot) data.init_reversal_potential = *rev_pot;
+
                 if (auto m = maybe_method(method)) {
                     props.default_parameters.reversal_potential_method[ion] = *m;
                 }
             },
             pybind11::arg_v("ion", "name of the ion species."),
+            pybind11::arg_v("valence", pybind11::none(), "valence of the ion species."),
             pybind11::arg_v("int_con", pybind11::none(), "initial internal concentration [mM]."),
             pybind11::arg_v("ext_con", pybind11::none(), "initial external concentration [mM]."),
             pybind11::arg_v("rev_pot", pybind11::none(), "reversal potential [mV]."),
             pybind11::arg_v("method",  pybind11::none(), "method for calculating reversal potential."),
-            "Set the global default propoerties of ion species named 'ion'.\n"
+            "Set the global default properties of ion species named 'ion'.\n"
+            "There are 3 ion species predefined in arbor: 'ca', 'na' and 'k'.\n"
+            "If 'ion' in not one of these ions it will be added to the list, making it\n"
+            "available to mechanisms. The user has to provide the valence of a previously\n"
+            "undefined ion the first time this function is called with it as an argument.\n"
             "Species concentrations and reversal potential can be overridden on\n"
             "specific regions using the paint interface, while the method for calculating\n"
             "reversal potential is global for all compartments in the cell, and can't be\n"
-- 
GitLab