Select Git revision
cable_cell_io.cpp
cable_cell_io.cpp 3.94 KiB
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <fstream>
#include <iomanip>
#include <arbor/cable_cell.hpp>
#include <arborio/cableio.hpp>
#include "error.hpp"
#include "strprintf.hpp"
namespace pyarb {
arborio::cable_cell_component load_component(const std::string& fname) {
std::ifstream fid{fname};
if (!fid.good()) {
throw pyarb_error("Can't open file '{}'" + fname);
}
auto component = arborio::parse_component(fid);
if (!component) {
throw pyarb_error("Error while trying to load component from \"" + fname + "\": " + component.error().what());
}
return component.value();
};
template<typename T>
void write_component(const T& component, const std::string& fname) {
std::ofstream fid(fname);
arborio::write_component(fid, component, arborio::meta_data{});
}
void write_component(const arborio::cable_cell_component& component, const std::string& fname) {
std::ofstream fid(fname);
arborio::write_component(fid, component);
}
void register_cable_loader(pybind11::module& m) {
m.def("load_component",
&load_component,
pybind11::arg_v("filename", "the name of the file."),
"Load arbor-component (decor, morphology, label_dict, cable_cell) from file.");
m.def("write_component",
[](const arborio::cable_cell_component& d, const std::string& fname) {
return write_component(d, fname);
},
pybind11::arg_v("object", "the cable_component object."),
pybind11::arg_v("filename", "the name of the file."),
"Write cable_component to file.");
m.def("write_component",
[](const arb::decor& d, const std::string& fname) {
return write_component<arb::decor>(d, fname);
},
pybind11::arg_v("object", "the decor object."),
pybind11::arg_v("filename", "the name of the file."),
"Write decor to file.");
m.def("write_component",
[](const arb::label_dict& d, const std::string& fname) {
return write_component<arb::label_dict>(d, fname);
},
pybind11::arg_v("object", "the label_dict object."),
pybind11::arg_v("filename", "the name of the file."),
"Write label_dict to file.");
m.def("write_component",
[](const arb::morphology& d, const std::string& fname) {