From 23dc72ea9700ad3c01f5497f15da1129ba498ccb Mon Sep 17 00:00:00 2001 From: witNie Date: Wed, 18 Nov 2026 22:24:35 +0100 Subject: [PATCH 1/4] Add validation functions for vectorized arguments and object arrays --- src/wrapper/utils.h | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/wrapper/utils.h b/src/wrapper/utils.h index 289e565..702bff3 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,64 @@ 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) { + 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)) { + 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 From 313c5a5a72820a0412e0711b5cdd24b1d82b62bc Mon Sep 17 00:00:00 2001 From: witNie Date: Wed, 18 Nov 2026 22:35:08 +0100 Subject: [PATCH 2/4] Enhance validate_vectorized_argument to support optional all-values check --- src/wrapper/utils.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wrapper/utils.h b/src/wrapper/utils.h index 702bff3..9635ddc 100644 --- a/src/wrapper/utils.h +++ b/src/wrapper/utils.h @@ -64,7 +64,11 @@ inline bool is_object_array(const nb::object& array) { template -inline bool validate_vectorized_argument(const nb::object& argument, const ScalarValidatorFunc& validate_scalar) { +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); @@ -91,7 +95,7 @@ inline bool validate_vectorized_argument(const nb::object& argument, const Scala // 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)) { + if (is_object_array(argument) || check_all_values == true) { return validate_vectorized_argument(values, validate_scalar); } else { return validate_scalar(array_values[0]); From 6842471735eb650c7c5536c1c0fc5d7c852e3d31 Mon Sep 17 00:00:00 2001 From: witNie Date: Wed, 18 Nov 2026 22:52:46 +0100 Subject: [PATCH 3/4] Refactor validation functions for particle and material arguments to improve clarity and error handling --- src/materials/materials.h | 27 ++++++++++----------------- src/particles/particle_arguments.h | 17 +---------------- src/stopping/stopping_power.cpp | 4 +++- 3 files changed, 14 insertions(+), 34 deletions(-) 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..bef4fe3 100644 --- a/src/particles/particle_arguments.h +++ b/src/particles/particle_arguments.h @@ -22,20 +22,7 @@ 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 void validate_particle_scalar(const nb::object& argument) { if (nb::isinstance(argument)) { particle_to_internal_no(nb::cast(argument)); return; @@ -46,8 +33,6 @@ inline void validate_particle_argument(const nb::object& argument) { "particle must be an Ion (stopping power requires Z and A); " "elementary particles are not supported"); } - - throw nb::type_error("particle must be an Ion, list, or NumPy array of Ions"); } /** diff --git a/src/stopping/stopping_power.cpp b/src/stopping/stopping_power.cpp index 90b2721..87ea8b8 100644 --- a/src/stopping/stopping_power.cpp +++ b/src/stopping/stopping_power.cpp @@ -193,7 +193,9 @@ 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); + if (!validate_vectorized_argument(particle, validate_particle_scalar)) { + throw nb::type_error("particle must be an Ion, list, or NumPy array of Ions"); + } validate_material_argument(material); const long requested_source = parse_stopping_power_source(source); From fd3861d546a6d6c90777c46f13762470801a0f97 Mon Sep 17 00:00:00 2001 From: witNie Date: Wed, 18 Nov 2026 23:02:16 +0100 Subject: [PATCH 4/4] Refactor validation for particle and material arguments to support vectorized input --- src/particles/particle_arguments.h | 6 ++++-- src/proton_models/dose_bortfeld.cpp | 5 ++++- src/proton_models/dose_bortfeld.h | 2 +- src/proton_models/let_wilkens.cpp | 5 ++++- src/proton_models/let_wilkens.h | 2 +- src/stopping/stopping_power.cpp | 6 ++++-- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/particles/particle_arguments.h b/src/particles/particle_arguments.h index bef4fe3..74ccb66 100644 --- a/src/particles/particle_arguments.h +++ b/src/particles/particle_arguments.h @@ -22,10 +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_scalar(const nb::object& argument) { +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)) { @@ -33,6 +33,8 @@ inline void validate_particle_scalar(const nb::object& argument) { "particle must be an Ion (stopping power requires Z and A); " "elementary particles are not supported"); } + + throw nb::type_error("particle must be an Ion, list, or NumPy array of Ions"); } /** 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 87ea8b8..e6beb82 100644 --- a/src/stopping/stopping_power.cpp +++ b/src/stopping/stopping_power.cpp @@ -193,10 +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) { - if (!validate_vectorized_argument(particle, validate_particle_scalar)) { + if (!validate_vectorized_argument(particle, validate_particle_scalar, true)) { throw nb::type_error("particle must be an Ion, list, or NumPy array of Ions"); } - 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"); + } const long requested_source = parse_stopping_power_source(source); std::vector resolved_sources;