Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/materials/materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<nb::list>(argument)) {
nb::list values = nb::cast<nb::list>(argument);
for (size_t i = 0; i < values.size(); ++i) {
validate_material_argument(values[i]);
}
return;
}

if (nb::isinstance<nb::ndarray<>>(argument)) {
validate_material_argument(argument.attr("tolist")());
return;
}

inline bool validate_material_scalar(const nb::object& argument) {
long id;

if (nb::isinstance<Material>(argument)) {
id = nb::cast<Material>(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<nb::int_>(argument)) {
id = nb::cast<long>(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) {
Expand Down
17 changes: 2 additions & 15 deletions src/particles/particle_arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<nb::list>(argument)) {
nb::list values = nb::cast<nb::list>(argument);
for (size_t i = 0; i < values.size(); ++i) {
validate_particle_argument(values[i]);
}
return;
}

if (nb::isinstance<nb::ndarray<>>(argument)) {
validate_particle_argument(argument.attr("tolist")());
return;
}

inline bool validate_particle_scalar(const nb::object& argument) {
if (nb::isinstance<Ion>(argument)) {
particle_to_internal_no(nb::cast<Ion>(argument));
return;
return true;
}

if (nb::isinstance<Particle>(argument)) {
Expand Down
5 changes: 4 additions & 1 deletion src/proton_models/dose_bortfeld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "../wrapper/cartesian_product.h"
#include "../wrapper/multi_argument.h"
#include "../wrapper/utils.h"

extern "C" {
#include "AT_ProtonAnalyticalModels.h"
Expand All @@ -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<nb::object> arguments_vector;
arguments_vector.push_back(depth_cm);
Expand Down
2 changes: 1 addition & 1 deletion src/proton_models/dose_bortfeld.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <nanobind/nanobind.h>

#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;

Expand Down
5 changes: 4 additions & 1 deletion src/proton_models/let_wilkens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "../wrapper/cartesian_product.h"
#include "../wrapper/multi_argument.h"
#include "../wrapper/utils.h"

extern "C" {
#include "AT_ProtonAnalyticalModels.h"
Expand All @@ -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<nb::object> arguments_vector;
arguments_vector.push_back(depth_cm);
Expand Down
2 changes: 1 addition & 1 deletion src/proton_models/let_wilkens.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <string>

#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;

Expand Down
8 changes: 6 additions & 2 deletions src/stopping/stopping_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<long> resolved_sources;
Expand Down
66 changes: 66 additions & 0 deletions src/wrapper/utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef WRAPPER_UTILS_H
#define WRAPPER_UTILS_H

#include <nanobind/stl/string.h>
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>

#include <string>
#include <variant>

namespace nb = nanobind;
Expand Down Expand Up @@ -55,4 +57,68 @@ T variant_cast(const std::variant<double, int>& v) {
return std::visit([](auto&& arg) -> T { return static_cast<T>(arg); }, v);
}

inline bool is_object_array(const nb::object& array) {
return nb::cast<std::string>(
array.attr("dtype").attr("kind")) == "O";
}


template <typename ScalarValidatorFunc>
inline bool validate_vectorized_argument(
const nb::object& argument,
const ScalarValidatorFunc& validate_scalar,
bool check_all_values=false) {

if (nb::isinstance<nb::list>(argument)) {
auto list_values = nb::cast<nb::list>(argument);

if (list_values.size() == 0) return false;

for(size_t i=0; i<list_values.size(); i++) {
if (!validate_scalar(list_values[i])) return false;
}
return true;
}

if (nb::isinstance<nb::ndarray<>>(argument)) {
auto array = nb::cast<nb::ndarray<>>(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<nb::list>(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<nb::int_>(argument) ||
nb::isinstance<nb::float_>(argument)) {
return true;
}

auto numpy = nb::module_::import_("numpy");
return nb::isinstance(argument, numpy.attr("integer")) ||
nb::isinstance(argument, numpy.attr("floating"));
}

#endif
Loading