diff --git a/src/materials/materials.h b/src/materials/materials.h index 0c80319..b4b3743 100644 --- a/src/materials/materials.h +++ b/src/materials/materials.h @@ -163,34 +163,27 @@ class Material { * @throws std::invalid_argument (ValueError) if a material ID is not in the material table. * @throws nb::type_error if the argument is not an int, Material, list, or NumPy array. */ -inline void validate_material_argument(const nb::object& argument) { - if (nb::isinstance(argument)) { - nb::list values = nb::cast(argument); - for (size_t i = 0; i < values.size(); ++i) { - validate_material_argument(values[i]); - } - return; - } - - if (nb::isinstance>(argument)) { - validate_material_argument(argument.attr("tolist")()); - return; - } - +inline bool validate_material_scalar(const nb::object& argument) { long id; + if (nb::isinstance(argument)) { id = nb::cast(argument).id; } else if (PyBool_Check(argument.ptr())) { - throw nb::type_error("material must be an int (not bool), a Material, or a list / NumPy array of either"); + throw nb::type_error( + "material must be an int (not bool), a Material, or a list / NumPy array of either"); } else if (nb::isinstance(argument)) { id = nb::cast(argument); } else { - throw nb::type_error("material must be an int, a Material, or a list / NumPy array of either"); + throw nb::type_error( + "material must be an int, a Material, or a list / NumPy array of either"); } if (id < 1 || AT_material_index_from_material_number(id) < 0) { - throw std::invalid_argument("invalid material ID: " + std::to_string(id)); + throw std::invalid_argument( + "invalid material ID: " + std::to_string(id)); } + + return true; } inline nb::object parse_material_argument(const nb::object& argument) { diff --git a/src/particles/particle_arguments.h b/src/particles/particle_arguments.h index 1e4791b..74ccb66 100644 --- a/src/particles/particle_arguments.h +++ b/src/particles/particle_arguments.h @@ -22,23 +22,10 @@ namespace nb = nanobind; * @throws std::invalid_argument (ValueError) if an Ion is missing Z/A. * @throws nb::type_error if the argument is not an Ion, list, or NumPy array. */ -inline void validate_particle_argument(const nb::object& argument) { - if (nb::isinstance(argument)) { - nb::list values = nb::cast(argument); - for (size_t i = 0; i < values.size(); ++i) { - validate_particle_argument(values[i]); - } - return; - } - - if (nb::isinstance>(argument)) { - validate_particle_argument(argument.attr("tolist")()); - return; - } - +inline bool validate_particle_scalar(const nb::object& argument) { if (nb::isinstance(argument)) { particle_to_internal_no(nb::cast(argument)); - return; + return true; } if (nb::isinstance(argument)) { diff --git a/src/proton_models/dose_bortfeld.cpp b/src/proton_models/dose_bortfeld.cpp index bb58df9..eb337c1 100644 --- a/src/proton_models/dose_bortfeld.cpp +++ b/src/proton_models/dose_bortfeld.cpp @@ -7,6 +7,7 @@ #include "../wrapper/cartesian_product.h" #include "../wrapper/multi_argument.h" +#include "../wrapper/utils.h" extern "C" { #include "AT_ProtonAnalyticalModels.h" @@ -15,7 +16,9 @@ extern "C" { nb::object dose_bortfeld(const nb::object& depth_cm, const nb::object& fluence_cm2, const nb::object& energy_MeV, const nb::object& energy_spread_fraction, const nb::object& material, const nb::object& eps, bool cartesian_product) { - validate_material_argument(material); + if (!validate_vectorized_argument(material, validate_material_scalar, true)) { + throw nb::type_error("material must be an int, a Material, or a list / NumPy array of either"); + } std::vector arguments_vector; arguments_vector.push_back(depth_cm); diff --git a/src/proton_models/dose_bortfeld.h b/src/proton_models/dose_bortfeld.h index 9646029..a1af9a9 100644 --- a/src/proton_models/dose_bortfeld.h +++ b/src/proton_models/dose_bortfeld.h @@ -3,7 +3,7 @@ #include -#include "../materials/materials.h" // for validate_material_argument, parse_material_argument +#include "../materials/materials.h" // for validate_material_scalar, parse_material_argument namespace nb = nanobind; diff --git a/src/proton_models/let_wilkens.cpp b/src/proton_models/let_wilkens.cpp index 35c4fc5..e4e877a 100644 --- a/src/proton_models/let_wilkens.cpp +++ b/src/proton_models/let_wilkens.cpp @@ -8,6 +8,7 @@ #include "../wrapper/cartesian_product.h" #include "../wrapper/multi_argument.h" +#include "../wrapper/utils.h" extern "C" { #include "AT_ProtonAnalyticalModels.h" @@ -33,7 +34,9 @@ nb::object let_wilkens(const nb::object& depth_cm, const nb::object& energy_MeV, throw nb::type_error("averaging must be an Averaging enum value or a string"); } - validate_material_argument(material); + if (!validate_vectorized_argument(material, validate_material_scalar, true)) { + throw nb::type_error("material must be an int, a Material, or a list / NumPy array of either"); + } std::vector arguments_vector; arguments_vector.push_back(depth_cm); diff --git a/src/proton_models/let_wilkens.h b/src/proton_models/let_wilkens.h index a173a0b..c8075c6 100644 --- a/src/proton_models/let_wilkens.h +++ b/src/proton_models/let_wilkens.h @@ -5,7 +5,7 @@ #include -#include "../materials/materials.h" // for validate_material_argument, parse_material_argument +#include "../materials/materials.h" // for validate_material_scalar, parse_material_argument namespace nb = nanobind; diff --git a/src/stopping/stopping_power.cpp b/src/stopping/stopping_power.cpp index 90b2721..e6beb82 100644 --- a/src/stopping/stopping_power.cpp +++ b/src/stopping/stopping_power.cpp @@ -193,8 +193,12 @@ nb::object evaluate_stopping_power(const nb::object& energy_MeV, const nb::objec const nb::object& material, const nb::object& source, bool cartesian_product, bool allow_bethe_fallback, bool full_output, StoppingPowerFunction stopping_power_function) { - validate_particle_argument(particle); - validate_material_argument(material); + if (!validate_vectorized_argument(particle, validate_particle_scalar, true)) { + throw nb::type_error("particle must be an Ion, list, or NumPy array of Ions"); + } + if (!validate_vectorized_argument(material, validate_material_scalar, true)) { + throw nb::type_error("material must be an int, a Material, or a list / NumPy array of either"); + } const long requested_source = parse_stopping_power_source(source); std::vector resolved_sources; diff --git a/src/wrapper/utils.h b/src/wrapper/utils.h index 289e565..9635ddc 100644 --- a/src/wrapper/utils.h +++ b/src/wrapper/utils.h @@ -1,9 +1,11 @@ #ifndef WRAPPER_UTILS_H #define WRAPPER_UTILS_H +#include #include #include +#include #include namespace nb = nanobind; @@ -55,4 +57,68 @@ T variant_cast(const std::variant& v) { return std::visit([](auto&& arg) -> T { return static_cast(arg); }, v); } +inline bool is_object_array(const nb::object& array) { + return nb::cast( + array.attr("dtype").attr("kind")) == "O"; +} + + +template +inline bool validate_vectorized_argument( + const nb::object& argument, + const ScalarValidatorFunc& validate_scalar, + bool check_all_values=false) { + + if (nb::isinstance(argument)) { + auto list_values = nb::cast(argument); + + if (list_values.size() == 0) return false; + + for(size_t i=0; i>(argument)) { + auto array = nb::cast>(argument); + auto values = argument.attr("tolist")(); + + if (array.ndim() == 0) return validate_scalar(values); + + if (array.ndim() != 1) return false; + + auto array_values = nb::cast(values); + + if (array_values.size() == 0) return false; + + // Numpy array can only have elements of the same type. + // The exception is ndarray of type object. Such ndarray can have elements of different types + // Type upscaling validation should be managed in scalar function + if (is_object_array(argument) || check_all_values == true) { + return validate_vectorized_argument(values, validate_scalar); + } else { + return validate_scalar(array_values[0]); + } + } + + return validate_scalar(argument); +} + +inline bool validate_numeric_scalar(const nb::object& argument) { + // bool is a subtype for int in python but it should not be treated as corrent value in this case + if (PyBool_Check(argument.ptr())) { + return false; + } + + if (nb::isinstance(argument) || + nb::isinstance(argument)) { + return true; + } + + auto numpy = nb::module_::import_("numpy"); + return nb::isinstance(argument, numpy.attr("integer")) || + nb::isinstance(argument, numpy.attr("floating")); +} + #endif