diff --git a/src/HealthGPS.Console/configuration.cpp b/src/HealthGPS.Console/configuration.cpp index ce90ded79..85f30a841 100644 --- a/src/HealthGPS.Console/configuration.cpp +++ b/src/HealthGPS.Console/configuration.cpp @@ -338,7 +338,7 @@ ModelInput create_model_input(core::DataTable &input_table, core::Country countr if (item.range.empty()) { mapping.emplace_back(item.name, item.level); } else { - auto boundary = FactorRange{item.range[0], item.range[1]}; + auto boundary = hgps::OptionalRange{{item.range[0], item.range[1]}}; mapping.emplace_back(item.name, item.level, boundary); } } diff --git a/src/HealthGPS/mapping.cpp b/src/HealthGPS/mapping.cpp index faf004a8f..171404782 100644 --- a/src/HealthGPS/mapping.cpp +++ b/src/HealthGPS/mapping.cpp @@ -8,8 +8,8 @@ namespace hgps { -MappingEntry::MappingEntry(std::string name, int level, FactorRange range) - : name_{std::move(name)}, name_key_{name_}, level_{level}, range_{range} {} +MappingEntry::MappingEntry(std::string name, int level, OptionalRange range) + : name_{std::move(name)}, name_key_{name_}, level_{level}, range_{std::move(range)} {} const std::string &MappingEntry::name() const noexcept { return name_; } @@ -17,14 +17,13 @@ int MappingEntry::level() const noexcept { return level_; } const core::Identifier &MappingEntry::key() const noexcept { return name_key_; } -const FactorRange &MappingEntry::range() const noexcept { return range_; } +const OptionalRange &MappingEntry::range() const noexcept { return range_; } double MappingEntry::get_bounded_value(const double &value) const noexcept { - if (range_.empty) { - return value; + if (range_.has_value()) { + return std::min(std::max(value, range_->first), range_->second); } - - return std::min(std::max(value, range_.minimum), range_.maximum); + return value; } inline bool operator>(const MappingEntry &lhs, const MappingEntry &rhs) { @@ -74,4 +73,4 @@ std::vector HierarchicalMapping::at_level(int level) const noexcep return result; } -} // namespace hgps \ No newline at end of file +} // namespace hgps diff --git a/src/HealthGPS/mapping.h b/src/HealthGPS/mapping.h index b8877536b..8ee51ff76 100644 --- a/src/HealthGPS/mapping.h +++ b/src/HealthGPS/mapping.h @@ -11,34 +11,8 @@ namespace hgps { /// @brief The constant in the regression model presentation identifier inline const core::Identifier InterceptKey = core::Identifier{"intercept"}; -/// @brief Defines the risk factor allowed range data type -/// -/// @details The factors range is defined from the fitted dataset and enforced -/// by the simulation algorithm. The default constructor, creates an empty range. -struct FactorRange { - /// @brief Initialises a new instance of the FactorRange structure - FactorRange() = default; - - /// @brief Initialises a new instance of the FactorRange structure - /// @param min_value Minimum factor value - /// @param max_value Maximum factor value - /// @throws std::invalid_argument for minimum greater than the maximum value - FactorRange(double min_value, double max_value) - : empty{false}, minimum{min_value}, maximum{max_value} { - if (min_value > max_value) { - throw std::invalid_argument("Factor range minimum must not be greater than maximum."); - } - } - - /// @brief Gets a value indicating whether the range is empty, no limits. - bool empty{true}; - - /// @brief The range minimum value - double minimum{}; - - /// @brief The range maximum value - double maximum{}; -}; +/// @brief Optional Range of doubles data type +using OptionalRange = std::optional>; /// @brief Defines risk factor mapping entry data type /// @@ -52,7 +26,7 @@ class MappingEntry { /// @param name Risk factor name /// @param level The hierarchical level /// @param range The factor range - MappingEntry(std::string name, int level, FactorRange range = {}); + MappingEntry(std::string name, int level, OptionalRange range = {}); /// @brief Gets the factor name /// @return Factor name @@ -68,7 +42,7 @@ class MappingEntry { /// @brief Gets the factor allowed values range /// @return Factor values range - const FactorRange &range() const noexcept; + const OptionalRange &range() const noexcept; /// @brief Adjusts a value to the factor range, if provided /// @param value The value to adjust @@ -79,7 +53,7 @@ class MappingEntry { std::string name_; core::Identifier name_key_; int level_{}; - FactorRange range_; + OptionalRange range_; }; /// @brief Defines the hierarchical model mapping data type @@ -147,4 +121,4 @@ class HierarchicalMapping { std::vector mapping_; }; -} // namespace hgps \ No newline at end of file +} // namespace hgps