Skip to content

Commit

Permalink
Merge pull request #225 from imperialCHEPI/rf_model_rename
Browse files Browse the repository at this point in the history
Class and file renaming
  • Loading branch information
jamesturner246 authored Oct 18, 2023
2 parents 735e0f3 + 8ad7d8e commit 057b8f9
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 185 deletions.
20 changes: 10 additions & 10 deletions src/HealthGPS.Console/model_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ load_static_risk_model_definition(const std::string &model_name, const poco::jso
fmt::format("Static model name '{}' not recognised", model_name)};
}

std::unique_ptr<hgps::HierarchicalLinearModelDefinition>
std::unique_ptr<hgps::StaticHierarchicalLinearModelDefinition>
load_hlm_risk_model_definition(const poco::json &opt) {
MEASURE_FUNCTION();
std::map<int, hgps::HierarchicalLevel> levels;
Expand Down Expand Up @@ -121,8 +121,8 @@ load_hlm_risk_model_definition(const poco::json &opt) {
.variances = at.variances});
}

return std::make_unique<hgps::HierarchicalLinearModelDefinition>(std::move(models),
std::move(levels));
return std::make_unique<hgps::StaticHierarchicalLinearModelDefinition>(std::move(models),
std::move(levels));
}

std::unique_ptr<hgps::StaticLinearModelDefinition>
Expand Down Expand Up @@ -173,7 +173,7 @@ load_dynamic_risk_model_definition(const std::string &model_name, const poco::js
}

// NOLINTBEGIN(readability-function-cognitive-complexity)
std::unique_ptr<hgps::LiteHierarchicalModelDefinition>
std::unique_ptr<hgps::DynamicHierarchicalLinearModelDefinition>
load_ebhlm_risk_model_definition(const poco::json &opt) {
MEASURE_FUNCTION();
auto percentage = 0.05;
Expand Down Expand Up @@ -245,12 +245,12 @@ load_ebhlm_risk_model_definition(const poco::json &opt) {
equations.emplace(age_key, std::move(age_equations));
}

return std::make_unique<hgps::LiteHierarchicalModelDefinition>(
return std::make_unique<hgps::DynamicHierarchicalLinearModelDefinition>(
std::move(equations), std::move(variables), percentage);
}
// NOLINTEND(readability-function-cognitive-complexity)

std::unique_ptr<hgps::EnergyBalanceModelDefinition>
std::unique_ptr<hgps::KevinHallModelDefinition>
load_kevinhall_risk_model_definition(const poco::json &opt, const host::Configuration &config) {
MEASURE_FUNCTION();
std::unordered_map<hgps::core::Identifier, double> energy_equation;
Expand Down Expand Up @@ -304,24 +304,24 @@ load_kevinhall_risk_model_definition(const poco::json &opt, const host::Configur
age_mean_height.emplace(hgps::core::Gender::male, std::move(male_height));
age_mean_height.emplace(hgps::core::Gender::female, std::move(female_height));

return std::make_unique<hgps::EnergyBalanceModelDefinition>(
return std::make_unique<hgps::KevinHallModelDefinition>(
std::move(energy_equation), std::move(nutrient_ranges), std::move(nutrient_equations),
std::move(food_prices), std::move(age_mean_height));
}

std::pair<hgps::HierarchicalModelType, std::unique_ptr<hgps::RiskFactorModelDefinition>>
std::pair<hgps::RiskFactorModelType, std::unique_ptr<hgps::RiskFactorModelDefinition>>
load_risk_model_definition(const std::string &model_type, const poco::json &opt,
const host::Configuration &config) {
// Get model name from JSON
const auto model_name = hgps::core::to_lower(opt["ModelName"].get<std::string>());

// Load appropriate model
if (hgps::core::case_insensitive::equals(model_type, "static")) {
return std::make_pair(hgps::HierarchicalModelType::Static,
return std::make_pair(hgps::RiskFactorModelType::Static,
load_static_risk_model_definition(model_name, opt, config));
}
if (hgps::core::case_insensitive::equals(model_type, "dynamic")) {
return std::make_pair(hgps::HierarchicalModelType::Dynamic,
return std::make_pair(hgps::RiskFactorModelType::Dynamic,
load_dynamic_risk_model_definition(model_name, opt, config));
}

Expand Down
22 changes: 13 additions & 9 deletions src/HealthGPS.Console/model_parser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once
#include "HealthGPS/repository.h"

#include "HealthGPS/dynamic_hierarchical_linear_model.h"
#include "HealthGPS/kevin_hall_model.h"
#include "HealthGPS/riskfactor_adjustment_types.h"
#include "HealthGPS/static_hierarchical_linear_model.h"
#include "HealthGPS/static_linear_model.h"

#include "configuration.h"
#include "jsonparser.h"
Expand All @@ -26,9 +30,9 @@ load_static_risk_model_definition(const std::string &model_name, const poco::jso

/// @brief Loads the full hierarchical linear regression model definition from a JSON file
/// @param opt The parsed model definition JSON file
/// @return An instance of the hgps::HierarchicalLinearModelDefinition type
/// @return An instance of the hgps::StaticHierarchicalLinearModelDefinition type
/// @throw std::invalid_argument if static model is unrecognised
std::unique_ptr<hgps::HierarchicalLinearModelDefinition>
std::unique_ptr<hgps::StaticHierarchicalLinearModelDefinition>
load_hlm_risk_model_definition(const poco::json &opt);

/// @brief Loads the static linear regression model definition from a JSON file
Expand All @@ -50,23 +54,23 @@ load_dynamic_risk_model_definition(const std::string &model_name, const poco::js

/// @brief Loads the old energy balance model definition from a JSON file
/// @param opt The parsed model definition JSON file
/// @return An instance of the hgps::LiteHierarchicalModelDefinition type
std::unique_ptr<hgps::LiteHierarchicalModelDefinition>
/// @return An instance of the hgps::DynamicHierarchicalLinearModelDefinition type
std::unique_ptr<hgps::DynamicHierarchicalLinearModelDefinition>
load_ebhlm_risk_model_definition(const poco::json &opt);

/// @brief Loads the new energy balance model definition from a JSON file
/// @brief Loads the Kevin Hall energy balance model definition from a JSON file
/// @param opt The parsed model definition JSON file
/// @param config The model configuration
/// @return An instance of the hgps::EnergyBalanceModelDefinition type
std::unique_ptr<hgps::EnergyBalanceModelDefinition>
/// @return An instance of the hgps::KevinHallModelDefinition type
std::unique_ptr<hgps::KevinHallModelDefinition>
load_kevinhall_risk_model_definition(const poco::json &opt, const host::Configuration &config);

/// @brief Loads a risk model definition from a JSON file
/// @param model_type The type of model ("dynamic"/"static") to load
/// @param opt The parsed model definition JSON file
/// @param config The model configuration
/// @return A std::pair containing the model type and model definition
std::pair<hgps::HierarchicalModelType, std::unique_ptr<hgps::RiskFactorModelDefinition>>
std::pair<hgps::RiskFactorModelType, std::unique_ptr<hgps::RiskFactorModelDefinition>>
load_risk_model_definition(const std::string &model_type, const poco::json &opt,
const host::Configuration &config);

Expand Down
24 changes: 12 additions & 12 deletions src/HealthGPS.Tests/HealthGPS.Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ TEST(TestHealthGPS, SimulationInitialise)
auto repository = CachedRepository(manager);
auto baseline_data = hgps::BaselineAdjustment{};
repository.register_linear_model_definition(HierarchicalModelType::Static,
repository.register_linear_model_definition(RiskFactorModelType::Static,
get_static_test_model(baseline_data));
repository.register_linear_model_definition(HierarchicalModelType::Dynamic,
repository.register_linear_model_definition(RiskFactorModelType::Dynamic,
get_dynamic_test_model(baseline_data));
auto factory = get_default_simulation_module_factory(repository);
Expand Down Expand Up @@ -382,18 +382,18 @@ TEST(TestHealthGPS, CreateRiskFactorModule)
// Test data code generation via JSON model definition.
//auto static_code = generate_test_code(
// HierarchicalModelType::Static, "C:/HealthGPS/Test/HLM.Json");
// RiskFactorModelType::Static, "C:/HealthGPS/Test/HLM.Json");
//auto dynamic_code = generate_test_code(
// HierarchicalModelType::Dynamic, "C:/HealthGPS/Test/DHLM.Json");
// RiskFactorModelType::Dynamic, "C:/HealthGPS/Test/DHLM.Json");
auto baseline_data = hgps::BaselineAdjustment{};
auto static_definition = get_static_test_model(baseline_data);
auto dynamic_definion = get_dynamic_test_model(baseline_data);
auto risk_models = std::unordered_map<HierarchicalModelType,
std::unique_ptr<HierarchicalLinearModel>>(); risk_models.emplace(HierarchicalModelType::Static,
auto risk_models = std::unordered_map<RiskFactorModelType,
std::unique_ptr<RiskFactorModel>>(); risk_models.emplace(RiskFactorModelType::Static,
std::make_unique<StaticHierarchicalLinearModel>(static_definition));
risk_models.emplace(HierarchicalModelType::Dynamic,
risk_models.emplace(RiskFactorModelType::Dynamic,
std::make_unique<DynamicHierarchicalLinearModel>(dynamic_definion));
auto risk_module = RiskFactorModule{ std::move(risk_models) };
Expand All @@ -406,7 +406,7 @@ std::make_unique<DynamicHierarchicalLinearModel>(dynamic_definion));

TEST(TestHealthGPS, CreateRiskFactorModuleFailWithEmpty) {
using namespace hgps;
auto risk_models = std::map<HierarchicalModelType, std::unique_ptr<HierarchicalLinearModel>>();
auto risk_models = std::map<RiskFactorModelType, std::unique_ptr<RiskFactorModel>>();
auto adjustments = BaselineAdjustment{};
ASSERT_THROW(
auto x = RiskFactorModule(std::move(risk_models), RiskfactorAdjustmentModel{adjustments}),
Expand All @@ -419,8 +419,8 @@ TEST(TestHealthGPS, CreateRiskFactorModuleFailWithoutStatic)
auto baseline_data = hgps::BaselineAdjustment{};
auto dynamic_definion = get_dynamic_test_model(baseline_data);
auto risk_models = std::unordered_map<HierarchicalModelType,
std::unique_ptr<HierarchicalLinearModel>>(); risk_models.emplace(HierarchicalModelType::Dynamic,
auto risk_models = std::unordered_map<RiskFactorModelType,
std::unique_ptr<RiskFactorModel>>(); risk_models.emplace(RiskFactorModelType::Dynamic,
std::make_unique<DynamicHierarchicalLinearModel>(dynamic_definion));
ASSERT_THROW(auto x = RiskFactorModule(std::move(risk_models)), std::invalid_argument);
Expand All @@ -432,8 +432,8 @@ TEST(TestHealthGPS, CreateRiskFactorModuleFailWithoutDynamic)
auto baseline_data = hgps::BaselineAdjustment{};
auto static_definition = get_static_test_model(baseline_data());
auto risk_models = std::unordered_map<HierarchicalModelType,
std::unique_ptr<HierarchicalLinearModel>>(); risk_models.emplace(HierarchicalModelType::Static,
auto risk_models = std::unordered_map<RiskFactorModelType,
std::unique_ptr<RiskFactorModel>>(); risk_models.emplace(RiskFactorModelType::Static,
std::make_unique<StaticHierarchicalLinearModel>(static_definition));
ASSERT_THROW(auto x = RiskFactorModule(std::move(risk_models)), std::invalid_argument);
Expand Down
2 changes: 1 addition & 1 deletion src/HealthGPS.Tests/RiskFactorData.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::string join_string_map(const std::vector<std::string> &v, std::string_view
return s.str();
}

// std::string generate_test_code(hgps::HierarchicalModelType model_type, std::string filename) {
// std::string generate_test_code(hgps::RiskFactorModelType model_type, std::string filename) {
// std::stringstream ss;
// HierarchicalModelInfo hmodel;
// std::ifstream ifs(filename, std::ifstream::in);
Expand Down
13 changes: 6 additions & 7 deletions src/HealthGPS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ target_sources(
"disease.h"
"disease_table.cpp"
"disease_table.h"
"energy_balance_hierarchical_model.cpp"
"energy_balance_hierarchical_model.h"
"energy_balance_model.cpp"
"energy_balance_model.h"
"dynamic_hierarchical_linear_model.cpp"
"dynamic_hierarchical_linear_model.h"
"kevin_hall_model.cpp"
"kevin_hall_model.h"
"error_message.cpp"
"error_message.h"
"event_bus.cpp"
Expand All @@ -43,8 +43,8 @@ target_sources(
"healthgps.h"
"static_linear_model.cpp"
"static_linear_model.h"
"hierarchical_model_static.cpp"
"hierarchical_model_static.h"
"static_hierarchical_linear_model.cpp"
"static_hierarchical_linear_model.h"
"info_message.cpp"
"info_message.h"
"life_table.cpp"
Expand Down Expand Up @@ -120,7 +120,6 @@ target_sources(
"finally.h"
"gender_table.h"
"gender_value.h"
"hierarchical_model.h"
"interfaces.h"
"intervention_scenario.h"
"map2d.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "energy_balance_hierarchical_model.h"
#include "dynamic_hierarchical_linear_model.h"
#include "runtime_context.h"

#include "HealthGPS.Core/exception.h"

namespace hgps {

EnergyBalanceHierarchicalModel::EnergyBalanceHierarchicalModel(
DynamicHierarchicalLinearModel::DynamicHierarchicalLinearModel(
const std::map<core::IntegerInterval, AgeGroupGenderEquation> &equations,
const std::map<core::Identifier, core::Identifier> &variables, double boundary_percentage)
: equations_{equations}, variables_{variables}, boundary_percentage_{boundary_percentage} {
Expand All @@ -19,19 +19,19 @@ EnergyBalanceHierarchicalModel::EnergyBalanceHierarchicalModel(
}
}

HierarchicalModelType EnergyBalanceHierarchicalModel::type() const noexcept {
return HierarchicalModelType::Dynamic;
RiskFactorModelType DynamicHierarchicalLinearModel::type() const noexcept {
return RiskFactorModelType::Dynamic;
}

std::string EnergyBalanceHierarchicalModel::name() const noexcept { return "Dynamic"; }
std::string DynamicHierarchicalLinearModel::name() const noexcept { return "Dynamic"; }

void EnergyBalanceHierarchicalModel::generate_risk_factors(
void DynamicHierarchicalLinearModel::generate_risk_factors(
[[maybe_unused]] RuntimeContext &context) {
throw core::HgpsException(
"EnergyBalanceHierarchicalModel::generate_risk_factors not yet implemented.");
"DynamicHierarchicalLinearModel::generate_risk_factors not yet implemented.");
}

void EnergyBalanceHierarchicalModel::update_risk_factors(RuntimeContext &context) {
void DynamicHierarchicalLinearModel::update_risk_factors(RuntimeContext &context) {
auto age_key = core::Identifier{"age"};
for (auto &entity : context.population()) {
// Ignore if inactive, newborn risk factors must be generated, not updated!
Expand All @@ -56,7 +56,7 @@ void EnergyBalanceHierarchicalModel::update_risk_factors(RuntimeContext &context
}
}

const AgeGroupGenderEquation &EnergyBalanceHierarchicalModel::equations_at(int age) const {
const AgeGroupGenderEquation &DynamicHierarchicalLinearModel::equations_at(int age) const {
for (const auto &entry : equations_) {
if (entry.first.contains(age)) {
// If there is an equation for the age, return it.
Expand All @@ -71,7 +71,7 @@ const AgeGroupGenderEquation &EnergyBalanceHierarchicalModel::equations_at(int a
return equations_.rbegin()->second;
}

void EnergyBalanceHierarchicalModel::update_risk_factors_exposure(
void DynamicHierarchicalLinearModel::update_risk_factors_exposure(
RuntimeContext &context, Person &entity,
const std::map<core::Identifier, double> &current_risk_factors,
const std::map<core::Identifier, FactorDynamicEquation> &equations) {
Expand Down Expand Up @@ -107,7 +107,7 @@ void EnergyBalanceHierarchicalModel::update_risk_factors_exposure(
}

std::map<core::Identifier, double>
EnergyBalanceHierarchicalModel::get_current_risk_factors(const HierarchicalMapping &mapping,
DynamicHierarchicalLinearModel::get_current_risk_factors(const HierarchicalMapping &mapping,
Person &entity) {
auto entity_risk_factors = std::map<core::Identifier, double>();
entity_risk_factors.emplace(InterceptKey, entity.get_risk_factor_value(InterceptKey));
Expand All @@ -118,15 +118,15 @@ EnergyBalanceHierarchicalModel::get_current_risk_factors(const HierarchicalMappi
return entity_risk_factors;
}

double EnergyBalanceHierarchicalModel::sample_normal_with_boundary(Random &random, double mean,
double DynamicHierarchicalLinearModel::sample_normal_with_boundary(Random &random, double mean,
double standard_deviation,
double boundary) const {
auto candidate = random.next_normal(mean, standard_deviation);
auto cap = boundary_percentage_ * boundary;
return std::min(std::max(candidate, -cap), +cap);
}

LiteHierarchicalModelDefinition::LiteHierarchicalModelDefinition(
DynamicHierarchicalLinearModelDefinition::DynamicHierarchicalLinearModelDefinition(
std::map<core::IntegerInterval, AgeGroupGenderEquation> equations,
std::map<core::Identifier, core::Identifier> variables, const double boundary_percentage)
: equations_{std::move(equations)}, variables_{std::move(variables)},
Expand All @@ -141,8 +141,8 @@ LiteHierarchicalModelDefinition::LiteHierarchicalModelDefinition(
}
}

std::unique_ptr<HierarchicalLinearModel> LiteHierarchicalModelDefinition::create_model() const {
return std::make_unique<EnergyBalanceHierarchicalModel>(equations_, variables_,
std::unique_ptr<RiskFactorModel> DynamicHierarchicalLinearModelDefinition::create_model() const {
return std::make_unique<DynamicHierarchicalLinearModel>(equations_, variables_,
boundary_percentage_);
}

Expand Down
Loading

0 comments on commit 057b8f9

Please sign in to comment.