fix(python-bindings): brought bindings in line with new namespace
This commit is contained in:
@@ -15,7 +15,7 @@ std::string sv_to_string(std::string_view sv) {
|
|||||||
return std::string(sv);
|
return std::string(sv);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_ostream_str(const composition::Composition& comp) {
|
std::string get_ostream_str(const serif::composition::Composition& comp) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << comp;
|
oss << comp;
|
||||||
return oss.str();
|
return oss.str();
|
||||||
@@ -24,38 +24,38 @@ std::string get_ostream_str(const composition::Composition& comp) {
|
|||||||
|
|
||||||
void register_comp_bindings(pybind11::module &comp_submodule) {
|
void register_comp_bindings(pybind11::module &comp_submodule) {
|
||||||
// --- Bindings for composition and species module ---
|
// --- Bindings for composition and species module ---
|
||||||
py::class_<composition::GlobalComposition>(comp_submodule, "GlobalComposition")
|
py::class_<serif::composition::GlobalComposition>(comp_submodule, "GlobalComposition")
|
||||||
.def_readonly("specificNumberDensity", &composition::GlobalComposition::specificNumberDensity)
|
.def_readonly("specificNumberDensity", &serif::composition::GlobalComposition::specificNumberDensity)
|
||||||
.def_readonly("meanParticleMass", &composition::GlobalComposition::meanParticleMass)
|
.def_readonly("meanParticleMass", &serif::composition::GlobalComposition::meanParticleMass)
|
||||||
.def("__repr__", // Add a string representation for easy printing in Python
|
.def("__repr__", // Add a string representation for easy printing in Python
|
||||||
[](const composition::GlobalComposition &gc) {
|
[](const serif::composition::GlobalComposition &gc) {
|
||||||
return "<GlobalComposition(specNumDens=" + std::to_string(gc.specificNumberDensity) +
|
return "<GlobalComposition(specNumDens=" + std::to_string(gc.specificNumberDensity) +
|
||||||
", meanMass=" + std::to_string(gc.meanParticleMass) + ")>";
|
", meanMass=" + std::to_string(gc.meanParticleMass) + ")>";
|
||||||
});
|
});
|
||||||
|
|
||||||
py::class_<composition::CompositionEntry>(comp_submodule, "CompositionEntry")
|
py::class_<serif::composition::CompositionEntry>(comp_submodule, "CompositionEntry")
|
||||||
.def("symbol", &composition::CompositionEntry::symbol)
|
.def("symbol", &serif::composition::CompositionEntry::symbol)
|
||||||
.def("mass_fraction",
|
.def("mass_fraction",
|
||||||
py::overload_cast<>(&composition::CompositionEntry::mass_fraction, py::const_),
|
py::overload_cast<>(&serif::composition::CompositionEntry::mass_fraction, py::const_),
|
||||||
"Gets the mass fraction of the species.")
|
"Gets the mass fraction of the species.")
|
||||||
.def("mass_fraction",
|
.def("mass_fraction",
|
||||||
py::overload_cast<double>(&composition::CompositionEntry::mass_fraction, py::const_),
|
py::overload_cast<double>(&serif::composition::CompositionEntry::mass_fraction, py::const_),
|
||||||
py::arg("meanMolarMass"), // Name the argument in Python
|
py::arg("meanMolarMass"), // Name the argument in Python
|
||||||
"Gets the mass fraction of the species given the mean molar mass.")
|
"Gets the mass fraction of the species given the mean molar mass.")
|
||||||
.def("number_fraction",
|
.def("number_fraction",
|
||||||
py::overload_cast<>(&composition::CompositionEntry::number_fraction, py::const_),
|
py::overload_cast<>(&serif::composition::CompositionEntry::number_fraction, py::const_),
|
||||||
"Gets the number fraction of the species.")
|
"Gets the number fraction of the species.")
|
||||||
.def("number_fraction",
|
.def("number_fraction",
|
||||||
py::overload_cast<double>(&composition::CompositionEntry::number_fraction, py::const_),
|
py::overload_cast<double>(&serif::composition::CompositionEntry::number_fraction, py::const_),
|
||||||
py::arg("totalMoles"),
|
py::arg("totalMoles"),
|
||||||
"Gets the number fraction of the species given the total moles.")
|
"Gets the number fraction of the species given the total moles.")
|
||||||
|
|
||||||
.def("rel_abundance", &composition::CompositionEntry::rel_abundance)
|
.def("rel_abundance", &serif::composition::CompositionEntry::rel_abundance)
|
||||||
.def("isotope", &composition::CompositionEntry::isotope) // Assuming Species is bound or convertible
|
.def("isotope", &serif::composition::CompositionEntry::isotope) // Assuming Species is bound or convertible
|
||||||
.def("getMassFracMode", &composition::CompositionEntry::getMassFracMode)
|
.def("getMassFracMode", &serif::composition::CompositionEntry::getMassFracMode)
|
||||||
|
|
||||||
.def("__repr__", // Optional: nice string representation
|
.def("__repr__", // Optional: nice string representation
|
||||||
[](const composition::CompositionEntry &ce) {
|
[](const serif::composition::CompositionEntry &ce) {
|
||||||
// You might want to include more info here now
|
// You might want to include more info here now
|
||||||
return "<CompositionEntry(symbol='" + ce.symbol() + "', " +
|
return "<CompositionEntry(symbol='" + ce.symbol() + "', " +
|
||||||
"mass_frac=" + std::to_string(ce.mass_fraction()) + ", " +
|
"mass_frac=" + std::to_string(ce.mass_fraction()) + ", " +
|
||||||
@@ -63,7 +63,7 @@ void register_comp_bindings(pybind11::module &comp_submodule) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// --- Binding for the main Composition class ---
|
// --- Binding for the main Composition class ---
|
||||||
py::class_<composition::Composition>(comp_submodule, "Composition")
|
py::class_<serif::composition::Composition>(comp_submodule, "Composition")
|
||||||
// Constructors
|
// Constructors
|
||||||
.def(py::init<>(), "Default constructor")
|
.def(py::init<>(), "Default constructor")
|
||||||
.def(py::init<const std::vector<std::string>&>(),
|
.def(py::init<const std::vector<std::string>&>(),
|
||||||
@@ -75,61 +75,61 @@ void register_comp_bindings(pybind11::module &comp_submodule) {
|
|||||||
"Constructor taking symbols, fractions, and mode (True=Mass, False=Number)")
|
"Constructor taking symbols, fractions, and mode (True=Mass, False=Number)")
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
.def("finalize", &composition::Composition::finalize, py::arg("norm") = false,
|
.def("finalize", &serif::composition::Composition::finalize, py::arg("norm") = false,
|
||||||
"Finalize the composition, optionally normalizing fractions to sum to 1.")
|
"Finalize the composition, optionally normalizing fractions to sum to 1.")
|
||||||
|
|
||||||
.def("registerSymbol", py::overload_cast<const std::string&, bool>(&composition::Composition::registerSymbol),
|
.def("registerSymbol", py::overload_cast<const std::string&, bool>(&serif::composition::Composition::registerSymbol),
|
||||||
py::arg("symbol"), py::arg("massFracMode") = true, "Register a single symbol.")
|
py::arg("symbol"), py::arg("massFracMode") = true, "Register a single symbol.")
|
||||||
.def("registerSymbol", py::overload_cast<const std::vector<std::string>&, bool>(&composition::Composition::registerSymbol),
|
.def("registerSymbol", py::overload_cast<const std::vector<std::string>&, bool>(&serif::composition::Composition::registerSymbol),
|
||||||
py::arg("symbols"), py::arg("massFracMode") = true, "Register multiple symbols.")
|
py::arg("symbols"), py::arg("massFracMode") = true, "Register multiple symbols.")
|
||||||
|
|
||||||
.def("getRegisteredSymbols", &composition::Composition::getRegisteredSymbols,
|
.def("getRegisteredSymbols", &serif::composition::Composition::getRegisteredSymbols,
|
||||||
"Get the set of registered symbols.")
|
"Get the set of registered symbols.")
|
||||||
|
|
||||||
.def("setMassFraction", py::overload_cast<const std::string&, const double&>(&composition::Composition::setMassFraction),
|
.def("setMassFraction", py::overload_cast<const std::string&, const double&>(&serif::composition::Composition::setMassFraction),
|
||||||
py::arg("symbol"), py::arg("mass_fraction"), "Set mass fraction for a single symbol (requires massFracMode). Returns old value.")
|
py::arg("symbol"), py::arg("mass_fraction"), "Set mass fraction for a single symbol (requires massFracMode). Returns old value.")
|
||||||
.def("setMassFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&composition::Composition::setMassFraction),
|
.def("setMassFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&serif::composition::Composition::setMassFraction),
|
||||||
py::arg("symbols"), py::arg("mass_fractions"), "Set mass fractions for multiple symbols (requires massFracMode). Returns list of old values.")
|
py::arg("symbols"), py::arg("mass_fractions"), "Set mass fractions for multiple symbols (requires massFracMode). Returns list of old values.")
|
||||||
|
|
||||||
.def("setNumberFraction", py::overload_cast<const std::string&, const double&>(&composition::Composition::setNumberFraction),
|
.def("setNumberFraction", py::overload_cast<const std::string&, const double&>(&serif::composition::Composition::setNumberFraction),
|
||||||
py::arg("symbol"), py::arg("number_fraction"), "Set number fraction for a single symbol (requires !massFracMode). Returns old value.")
|
py::arg("symbol"), py::arg("number_fraction"), "Set number fraction for a single symbol (requires !massFracMode). Returns old value.")
|
||||||
.def("setNumberFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&composition::Composition::setNumberFraction),
|
.def("setNumberFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&serif::composition::Composition::setNumberFraction),
|
||||||
py::arg("symbols"), py::arg("number_fractions"), "Set number fractions for multiple symbols (requires !massFracMode). Returns list of old values.")
|
py::arg("symbols"), py::arg("number_fractions"), "Set number fractions for multiple symbols (requires !massFracMode). Returns list of old values.")
|
||||||
|
|
||||||
.def("mix", &composition::Composition::mix, py::arg("other"), py::arg("fraction"),
|
.def("mix", &serif::composition::Composition::mix, py::arg("other"), py::arg("fraction"),
|
||||||
"Mix with another composition. Returns new Composition.")
|
"Mix with another composition. Returns new Composition.")
|
||||||
|
|
||||||
.def("getMassFraction", py::overload_cast<const std::string&>(&composition::Composition::getMassFraction, py::const_),
|
.def("getMassFraction", py::overload_cast<const std::string&>(&serif::composition::Composition::getMassFraction, py::const_),
|
||||||
py::arg("symbol"), "Get mass fraction for a symbol (calculates if needed). Requires finalization.")
|
py::arg("symbol"), "Get mass fraction for a symbol (calculates if needed). Requires finalization.")
|
||||||
.def("getMassFraction", py::overload_cast<>(&composition::Composition::getMassFraction, py::const_),
|
.def("getMassFraction", py::overload_cast<>(&serif::composition::Composition::getMassFraction, py::const_),
|
||||||
"Get dictionary of all mass fractions. Requires finalization.")
|
"Get dictionary of all mass fractions. Requires finalization.")
|
||||||
|
|
||||||
.def("getNumberFraction", py::overload_cast<const std::string&>(&composition::Composition::getNumberFraction, py::const_),
|
.def("getNumberFraction", py::overload_cast<const std::string&>(&serif::composition::Composition::getNumberFraction, py::const_),
|
||||||
py::arg("symbol"), "Get number fraction for a symbol (calculates if needed). Requires finalization.")
|
py::arg("symbol"), "Get number fraction for a symbol (calculates if needed). Requires finalization.")
|
||||||
.def("getNumberFraction", py::overload_cast<>(&composition::Composition::getNumberFraction, py::const_),
|
.def("getNumberFraction", py::overload_cast<>(&serif::composition::Composition::getNumberFraction, py::const_),
|
||||||
"Get dictionary of all number fractions. Requires finalization.")
|
"Get dictionary of all number fractions. Requires finalization.")
|
||||||
|
|
||||||
// Note: pybind11 automatically converts std::pair to a Python tuple
|
// Note: pybind11 automatically converts std::pair to a Python tuple
|
||||||
.def("getComposition", py::overload_cast<const std::string&>(&composition::Composition::getComposition, py::const_),
|
.def("getComposition", py::overload_cast<const std::string&>(&serif::composition::Composition::getComposition, py::const_),
|
||||||
py::arg("symbol"), "Returns a tuple (CompositionEntry, GlobalComposition) for the symbol. Requires finalization.")
|
py::arg("symbol"), "Returns a tuple (CompositionEntry, GlobalComposition) for the symbol. Requires finalization.")
|
||||||
// Binding the version returning map<string, Entry> requires a bit more care or helper function
|
// Binding the version returning map<string, Entry> requires a bit more care or helper function
|
||||||
// to convert the map to a Python dict if needed directly. Let's bind the pair version for now.
|
// to convert the map to a Python dict if needed directly. Let's bind the pair version for now.
|
||||||
.def("getComposition", py::overload_cast<>(&composition::Composition::getComposition, py::const_),
|
.def("getComposition", py::overload_cast<>(&serif::composition::Composition::getComposition, py::const_),
|
||||||
"Returns a tuple (dict[str, CompositionEntry], GlobalComposition) for all symbols. Requires finalization.")
|
"Returns a tuple (dict[str, CompositionEntry], GlobalComposition) for all symbols. Requires finalization.")
|
||||||
|
|
||||||
|
|
||||||
.def("subset", &composition::Composition::subset, py::arg("symbols"), py::arg("method") = "norm",
|
.def("subset", &serif::composition::Composition::subset, py::arg("symbols"), py::arg("method") = "norm",
|
||||||
"Create a new Composition containing only the specified symbols.")
|
"Create a new Composition containing only the specified symbols.")
|
||||||
.def("hasSymbol", &composition::Composition::hasSymbol, py::arg("symbol"),
|
.def("hasSymbol", &serif::composition::Composition::hasSymbol, py::arg("symbol"),
|
||||||
"Check if a symbol is registered.")
|
"Check if a symbol is registered.")
|
||||||
.def("setCompositionMode", &composition::Composition::setCompositionMode, py::arg("massFracMode"),
|
.def("setCompositionMode", &serif::composition::Composition::setCompositionMode, py::arg("massFracMode"),
|
||||||
"Set the mode (True=Mass, False=Number). Requires finalization before switching.")
|
"Set the mode (True=Mass, False=Number). Requires finalization before switching.")
|
||||||
|
|
||||||
// Operator overload
|
// Operator overload
|
||||||
.def(py::self + py::self, "Mix equally with another composition.") // Binds operator+
|
.def(py::self + py::self, "Mix equally with another composition.") // Binds operator+
|
||||||
|
|
||||||
// Add __repr__ or __str__
|
// Add __repr__ or __str__
|
||||||
.def("__repr__", [](const composition::Composition &comp) {
|
.def("__repr__", [](const serif::composition::Composition &comp) {
|
||||||
return get_ostream_str(comp); // Use helper for C++ operator<<
|
return get_ostream_str(comp); // Use helper for C++ operator<<
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ template <typename T>
|
|||||||
void def_config_get(py::module &m) {
|
void def_config_get(py::module &m) {
|
||||||
m.def("get",
|
m.def("get",
|
||||||
[](const std::string &key, T defaultValue) {
|
[](const std::string &key, T defaultValue) {
|
||||||
return Config::getInstance().get<T>(key, defaultValue);
|
return serif::config::Config::getInstance().get<T>(key, defaultValue);
|
||||||
},
|
},
|
||||||
py::arg("key"), py::arg("defaultValue"),
|
py::arg("key"), py::arg("defaultValue"),
|
||||||
"Get configuration value (type inferred from default)");
|
"Get configuration value (type inferred from default)");
|
||||||
@@ -28,28 +28,28 @@ void register_config_bindings(pybind11::module &config_submodule) {
|
|||||||
|
|
||||||
config_submodule.def("loadConfig",
|
config_submodule.def("loadConfig",
|
||||||
[](const std::string& configFilePath) {
|
[](const std::string& configFilePath) {
|
||||||
return Config::getInstance().loadConfig(configFilePath);
|
return serif::config::Config::getInstance().loadConfig(configFilePath);
|
||||||
},
|
},
|
||||||
py::arg("configFilePath"),
|
py::arg("configFilePath"),
|
||||||
"Load configuration from a YAML file.");
|
"Load configuration from a YAML file.");
|
||||||
|
|
||||||
config_submodule.def("has",
|
config_submodule.def("has",
|
||||||
[](const std::string &key) {
|
[](const std::string &key) {
|
||||||
return Config::getInstance().has(key);
|
return serif::config::Config::getInstance().has(key);
|
||||||
},
|
},
|
||||||
py::arg("key"),
|
py::arg("key"),
|
||||||
"Check if a key exists in the configuration.");
|
"Check if a key exists in the configuration.");
|
||||||
|
|
||||||
config_submodule.def("keys",
|
config_submodule.def("keys",
|
||||||
[]() {
|
[]() {
|
||||||
return py::cast(Config::getInstance().keys());
|
return py::cast(serif::config::Config::getInstance().keys());
|
||||||
},
|
},
|
||||||
"Get a list of all configuration keys.");
|
"Get a list of all configuration keys.");
|
||||||
|
|
||||||
config_submodule.def("__repr__",
|
config_submodule.def("__repr__",
|
||||||
[]() {
|
[]() {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << Config::getInstance(); // Use the existing operator<<
|
oss << serif::config::Config::getInstance(); // Use the existing operator<<
|
||||||
return std::string("<fourdsse_bindings.config module accessing C++ Singleton>\n") + oss.str();
|
return std::string("<fourdsse_bindings.config module accessing C++ Singleton>\n") + oss.str();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,44 +10,44 @@ namespace py = pybind11;
|
|||||||
|
|
||||||
|
|
||||||
void register_const_bindings(pybind11::module &const_submodule) {
|
void register_const_bindings(pybind11::module &const_submodule) {
|
||||||
py::class_<Constant>(const_submodule, "Constant")
|
py::class_<serif::constant::Constant>(const_submodule, "Constant")
|
||||||
.def_readonly("name", &Constant::name)
|
.def_readonly("name", &serif::constant::Constant::name)
|
||||||
.def_readonly("value", &Constant::value)
|
.def_readonly("value", &serif::constant::Constant::value)
|
||||||
.def_readonly("uncertainty", &Constant::uncertainty)
|
.def_readonly("uncertainty", &serif::constant::Constant::uncertainty)
|
||||||
.def_readonly("unit", &Constant::unit)
|
.def_readonly("unit", &serif::constant::Constant::unit)
|
||||||
.def_readonly("reference", &Constant::reference)
|
.def_readonly("reference", &serif::constant::Constant::reference)
|
||||||
.def("__repr__", [](const Constant &c) {
|
.def("__repr__", [](const serif::constant::Constant &c) {
|
||||||
return "<Constant(name='" + c.name + "', value=" + std::to_string(c.value) +
|
return "<Constant(name='" + c.name + "', value=" + std::to_string(c.value) +
|
||||||
", uncertainty=" + std::to_string(c.uncertainty) +
|
", uncertainty=" + std::to_string(c.uncertainty) +
|
||||||
", unit='" + c.unit + "')>";
|
", unit='" + c.unit + "')>";
|
||||||
});
|
});
|
||||||
|
|
||||||
py::class_<Constants>(const_submodule, "Constants")
|
py::class_<serif::constant::Constants>(const_submodule, "Constants")
|
||||||
.def_property_readonly("loaded", &Constants::isLoaded)
|
.def_property_readonly("loaded", &serif::constant::Constants::isLoaded)
|
||||||
.def_static("get",
|
.def_static("get",
|
||||||
[](const std::string &name) {
|
[](const std::string &name) {
|
||||||
return py::cast(
|
return py::cast(
|
||||||
Constants::getInstance().get(name)
|
serif::constant::Constants::getInstance().get(name)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
"Get a constant by name. Returns None if not found."
|
"Get a constant by name. Returns None if not found."
|
||||||
)
|
)
|
||||||
.def_static("has",
|
.def_static("has",
|
||||||
[](const std::string &name) {
|
[](const std::string &name) {
|
||||||
return Constants::getInstance().has(name);
|
return serif::constant::Constants::getInstance().has(name);
|
||||||
},
|
},
|
||||||
"Check if a constant exists by name.")
|
"Check if a constant exists by name.")
|
||||||
.def_static("keys",
|
.def_static("keys",
|
||||||
[]() {
|
[]() {
|
||||||
return py::cast(
|
return py::cast(
|
||||||
Constants::getInstance().keys()
|
serif::constant::Constants::getInstance().keys()
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
"Get a list of all constant names.")
|
"Get a list of all constant names.")
|
||||||
.def_static("__class_getitem__",
|
.def_static("__class_getitem__",
|
||||||
[](const std::string &name) {
|
[](const std::string &name) {
|
||||||
return py::cast(
|
return py::cast(
|
||||||
Constants::getInstance().get(name)
|
serif::constant::Constants::getInstance().get(name)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user