From 1f72805ecc0cc068925e913f97bf446312c2456e Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Mon, 19 Feb 2024 09:21:15 +0000 Subject: [PATCH 1/7] Initial pass at implementing test fixtures --- src/HealthGPS.Tests/CMakeLists.txt | 1 + .../StaticLinearModel.Test.cpp | 132 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/HealthGPS.Tests/StaticLinearModel.Test.cpp diff --git a/src/HealthGPS.Tests/CMakeLists.txt b/src/HealthGPS.Tests/CMakeLists.txt index 84640332c..de06f00cc 100644 --- a/src/HealthGPS.Tests/CMakeLists.txt +++ b/src/HealthGPS.Tests/CMakeLists.txt @@ -39,6 +39,7 @@ target_sources( "RelativeRiskLookup.Test.cpp" "RuntimeMetric.Test.cpp" "Scenario.Test.cpp" + "StaticLinearModel.Test.cpp" "TestMain.cpp" "WeightModel.Test.cpp" "CountryModule.h" diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp new file mode 100644 index 000000000..ee61d717f --- /dev/null +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -0,0 +1,132 @@ +#include "pch.h" + +#include "HealthGPS/static_linear_model.h" + +auto expected = hgps::RiskFactorSexAgeTable{}; + +std::vector factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; +std::vector factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; + +std::unordered_map> factorsMale = { + {hgps::core::Identifier{"0"}, factorsMale}, + {hgps::core::Identifier{"1"}, factorsMale} +}; + +std::unordered_map> factorsFemale = { + {hgps::core::Identifier{"0"}, factorsFemale}, + {hgps::core::Identifier{"1"}, factorsFemale} +}; + +expected.emplace_row(hgps::core::Gender::male, factorsMale); +expected.emplace_row(hgps::core::Gender::female, factorsFemale); +std::vector names = {"FoodCarbohydrate", "FoodFat"}; + +// Define models +// We will define 2 RiskFactorModels: carb_model and fat_model +std::vector models; +hgps::LinearModelParams carb_model; +carb_model.intercept = -0.241505734056409; +std::unordered_map coefficients = { + {"Gender", 0.00191412437431455}, + {"Age", -0.000545257990453302}, + {"Age2", 3.06434596341924e-6}, + {"Age3", 5.24468215546687e-9}, + {"Sector", 0.0967308121487847}, + {"Income", 0.0810023788842083} +}; +carb_model.coefficients = coefficients; + +hgps::LinearModelParams fat_model; +fat_model.intercept = -0.693521187677; +coefficients = { + {"Gender", 0.00385126434589752}, + {"Age", -0.00433711587715954}, + {"Age2", 3.11799231385975e-5}, + {"Age3", 3.4291809335544e-8}, + {"Sector", -0.0629531974852436}, + {"Income", 0.373256996730791} +}; +fat_model.coefficients = coefficients; + +models.emplace_back(std::move(carb_model)); +models.emplace_back(std::move(fat_model)); + +std::vector lambda = {0.262626262626263, 0.141414141414141}; +std::vector stddev = {0.337810256621877, 0.436763527499362}; + +Eigen::MatrixXd correlation{2, 2}; +hgps::core::DataTable correlation_table; +// I really don't know how to build this correlation table. +// The data I want to use looks like this in a csv file: +// "FoodCarbohydrate","FoodFat" +// 1.0,0.322065425 +// 0.322065425,1.0 + +// Define policy models +std::vector policy_models; +std::vector policy_ranges; + +// We need 2 policy models for the 2 risk factor models +hgps::LinearModelParams carb_policy_model; +carb_policy_model.intercept = -1.29385215316281; +coefficients = { + {"Gender", 0.00845121355575458}, + {"Age", 0.000386003599279214}, + {"Sector", 0.0621763530502617}, + {"Income", 0.0560558815729017} +}; +carb_policy_model.coefficients = coefficients; +std::unordered_map log_coefficients = { + {"FoodCarbohydrate", 0.577638667323293}, + {"FoodFat", -0.0801300827856402}, + {"FoodProtein", -0.480946893299471}, + {"FoodSodium", -0.0396319735508116} +}; +carb_policy_model.log_coefficients = log_coefficients; +policy_ranges.emplace_back(hgps::core::DoubleInterval{-2.31063, 0.37526}); + +hgps::LinearModelParams fat_policy_model; +fat_policy_model.intercept = -0.734121205356728; +coefficients = { + +} + +policy_models.emplace_back(std::move(policy_model)); +// Check intervention policy covariance matrix column name matches risk factor name. +auto policy_column_name = policy_covariance_table.column(i).name(); +if (!hgps::core::case_insensitive::equals(key, policy_column_name)) { + throw hgps::core::HgpsException{ + fmt::format("Risk factor {} name ({}) does not match intervention " + "policy covariance matrix column {} name ({})", + i, key, i, policy_column_name)}; +} + +// todo: implement the rest of the parameters +// std::vector policy_ranges; +// Eigen::MatrixXd policy_cholesky; +// double info_speed; +// std::unordered_map> rural_prevalence; +// std::unordered_map income_models; +// double physical_activity_stddev; + +// Create test fixture for a StaticLinearModel class instance +class StaticLinearModelTestFixture : public::testing::Test { + public: + StaticLinearModel testModel( + expected, + names, + models, + lambda, + stddev, + cholesky, + policy_models, + policy_ranges, + policy_cholesky, + info_speed, + rural_prevalence, + income_models, + physical_activity_stddev + ); +}; From 5b5896c8abd8474706659dd56e96e3f74b96e6c8 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 21 Feb 2024 14:04:24 +0000 Subject: [PATCH 2/7] Finishing creating parameter fixtures --- .../StaticLinearModel.Test.cpp | 268 +++++++++++------- 1 file changed, 161 insertions(+), 107 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index ee61d717f..c069d6c70 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -2,114 +2,164 @@ #include "HealthGPS/static_linear_model.h" -auto expected = hgps::RiskFactorSexAgeTable{}; - -std::vector factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; -std::vector factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; - -std::unordered_map> factorsMale = { - {hgps::core::Identifier{"0"}, factorsMale}, - {hgps::core::Identifier{"1"}, factorsMale} -}; - -std::unordered_map> factorsFemale = { - {hgps::core::Identifier{"0"}, factorsFemale}, - {hgps::core::Identifier{"1"}, factorsFemale} -}; - -expected.emplace_row(hgps::core::Gender::male, factorsMale); -expected.emplace_row(hgps::core::Gender::female, factorsFemale); -std::vector names = {"FoodCarbohydrate", "FoodFat"}; - -// Define models -// We will define 2 RiskFactorModels: carb_model and fat_model -std::vector models; -hgps::LinearModelParams carb_model; -carb_model.intercept = -0.241505734056409; -std::unordered_map coefficients = { - {"Gender", 0.00191412437431455}, - {"Age", -0.000545257990453302}, - {"Age2", 3.06434596341924e-6}, - {"Age3", 5.24468215546687e-9}, - {"Sector", 0.0967308121487847}, - {"Income", 0.0810023788842083} -}; -carb_model.coefficients = coefficients; - -hgps::LinearModelParams fat_model; -fat_model.intercept = -0.693521187677; -coefficients = { - {"Gender", 0.00385126434589752}, - {"Age", -0.00433711587715954}, - {"Age2", 3.11799231385975e-5}, - {"Age3", 3.4291809335544e-8}, - {"Sector", -0.0629531974852436}, - {"Income", 0.373256996730791} -}; -fat_model.coefficients = coefficients; - -models.emplace_back(std::move(carb_model)); -models.emplace_back(std::move(fat_model)); - -std::vector lambda = {0.262626262626263, 0.141414141414141}; -std::vector stddev = {0.337810256621877, 0.436763527499362}; - -Eigen::MatrixXd correlation{2, 2}; -hgps::core::DataTable correlation_table; -// I really don't know how to build this correlation table. -// The data I want to use looks like this in a csv file: -// "FoodCarbohydrate","FoodFat" -// 1.0,0.322065425 -// 0.322065425,1.0 - -// Define policy models -std::vector policy_models; -std::vector policy_ranges; - -// We need 2 policy models for the 2 risk factor models -hgps::LinearModelParams carb_policy_model; -carb_policy_model.intercept = -1.29385215316281; -coefficients = { - {"Gender", 0.00845121355575458}, - {"Age", 0.000386003599279214}, - {"Sector", 0.0621763530502617}, - {"Income", 0.0560558815729017} -}; -carb_policy_model.coefficients = coefficients; -std::unordered_map log_coefficients = { - {"FoodCarbohydrate", 0.577638667323293}, - {"FoodFat", -0.0801300827856402}, - {"FoodProtein", -0.480946893299471}, - {"FoodSodium", -0.0396319735508116} +// Create a test fixture for the StaticLinearModel parameters +class ModelParameters : public ::testing::Test { + public: + hgps::RiskFactorSexAgeTable expected; + std::vector names; + std::vector models; + std::vector lambda; + std::vector stddev; + Eigen::Matrix2d correlation; + Eigen::MatrixXd cholesky; + std::vector policy_models; + std::vector policy_ranges; + Eigen::MatrixXd policy_cholesky; + + void SetUp() override { + auto expected = hgps::RiskFactorSexAgeTable{}; + std::vector factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; + std::vector factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; + + std::unordered_map> factorsMale = { + {hgps::core::Identifier{"0"}, factorsMale}, + {hgps::core::Identifier{"1"}, factorsMale} + }; + + std::unordered_map> factorsFemale = { + {hgps::core::Identifier{"0"}, factorsFemale}, + {hgps::core::Identifier{"1"}, factorsFemale} + }; + + expected.emplace_row(hgps::core::Gender::male, factorsMale); + expected.emplace_row(hgps::core::Gender::female, factorsFemale); + + std::vector names = {"FoodCarbohydrate", "FoodFat"}; + + // Define models + // We will define 2 RiskFactorModels: carb_model and fat_model + std::vector models; + hgps::LinearModelParams carb_model; + carb_model.intercept = -0.241505734056409; + std::unordered_map coefficients = { + {"Gender", 0.00191412437431455}, + {"Age", -0.000545257990453302}, + {"Age2", 3.06434596341924e-6}, + {"Age3", 5.24468215546687e-9}, + {"Sector", 0.0967308121487847}, + {"Income", 0.0810023788842083} + }; + carb_model.coefficients = coefficients; + + hgps::LinearModelParams fat_model; + fat_model.intercept = -0.693521187677; + coefficients = { + {"Gender", 0.00385126434589752}, + {"Age", -0.00433711587715954}, + {"Age2", 3.11799231385975e-5}, + {"Age3", 3.4291809335544e-8}, + {"Sector", -0.0629531974852436}, + {"Income", 0.373256996730791} + }; + fat_model.coefficients = coefficients; + + models.emplace_back(std::move(carb_model)); + models.emplace_back(std::move(fat_model)); + + std::vector lambda = {0.262626262626263, 0.141414141414141}; + std::vector stddev = {0.337810256621877, 0.436763527499362}; + + Eigen::Matrix2d correlation; + correlation << 1.0, 0.322065425, 0.322065425,1.0; + auto cholesky = Eigen::MatrixXd{Eigen::LLT{correlation}.matrixL()}; + + Eigen::MatrixXd policy_covariance; + policy_covariance << 0.1,0.1,0.1,0.41; + auto policy_cholesky = Eigen::MatrixXd{Eigen::LLT{policy_covariance}.matrixL()}; + + // Define policy models + std::vector policy_models; + std::vector policy_ranges; + + // We need 2 policy models for the 2 risk factor models + hgps::LinearModelParams carb_policy_model; + carb_policy_model.intercept = -1.29385215316281; + coefficients = { + {"Gender", 0.00845121355575458}, + {"Age", 0.000386003599279214}, + {"Sector", 0.0621763530502617}, + {"Income", 0.0560558815729017} + }; + carb_policy_model.coefficients = coefficients; + std::unordered_map log_coefficients = { + {"FoodCarbohydrate", 0.577638667323293}, + {"FoodFat", -0.0801300827856402}, + {"FoodProtein", -0.480946893299471}, + {"FoodSodium", -0.0396319735508116} + }; + carb_policy_model.log_coefficients = log_coefficients; + policy_ranges.emplace_back(hgps::core::DoubleInterval{-2.31063, 0.37526}); + + hgps::LinearModelParams fat_policy_model; + fat_policy_model.intercept = -0.734121205356728; + coefficients = { + {"Gender", 0.023422206650121}, + {"Age", 0.000557687923688474}, + {"Sector", 0.00166651997223223}, + {"Income", -0.498755023022772} + } + fat_policy_model.coefficients = coefficients; + log_coefficients = { + {"FoodCarbohydrate", 0.652615267035516}, + {"FoodFat", 0.743457905132894}, + {"FoodProtein", -1.2648185364167}, + {"FoodSodium", -0.17181077457271} + }; + fat_policy_model.log_coefficients = log_coefficients; + policy_ranges.emplace_back(hgps::core::DoubleInterval{-4.87205, -0.10346}); + + policy_models.emplace_back(std::move(carb_policy_model)); + policy_models.emplace_back(std::move(fat_policy_model)); + + const double info_speed = 0.1; + + std::unordered_map> rural_prevalence; + rural_prevalence["Under18"] = {{hgps::core::Gender::female, 0.65},{hgps::core::Gender::male, 0.65}}; + rural_prevalence["Over18"] = {{hgps::core::Gender::female, 0.6},{hgps::core::Gender::male, 0.6}}; + + std::unordered_map income_models; + + hgps::LinearModelParams income_model_low; + income_model_low.intercept = 0.0; + coefficients = {} + income_model_low.coefficients = coefficients; + + hgps::LinearModelParams income_model_middle; + income_model_middle.intercept = -0.0023671151435328; + coefficients = { + {"Gender", 0.0244784583947992}, + {"Over18", 0.276669591410528}, + {"Sector", -0.491794449118805} + }; + income_model_middle.coefficients = coefficients; + + hgps::LinearModelParams income_model_high; + income_model_high.intercept = 0.0187952471153142; + coefficients = { + {"Gender", 0.02018185354194}, + {"Over18", 0.607099252829797}, + {"Sector", -1.5870837069653} + }; + income_model_high.coefficients = coefficients; + + income_models.emplace(hgps::core::Income::low, std::move(income_model_low)); + income_models.emplace(hgps::core::Income::middle, std::move(income_model_middle)); + income_models.emplace(hgps::core::Income::high, std::move(income_model_high)); + + const double physical_activity_stddev = 0.06; + + }; }; -carb_policy_model.log_coefficients = log_coefficients; -policy_ranges.emplace_back(hgps::core::DoubleInterval{-2.31063, 0.37526}); - -hgps::LinearModelParams fat_policy_model; -fat_policy_model.intercept = -0.734121205356728; -coefficients = { - -} - -policy_models.emplace_back(std::move(policy_model)); -// Check intervention policy covariance matrix column name matches risk factor name. -auto policy_column_name = policy_covariance_table.column(i).name(); -if (!hgps::core::case_insensitive::equals(key, policy_column_name)) { - throw hgps::core::HgpsException{ - fmt::format("Risk factor {} name ({}) does not match intervention " - "policy covariance matrix column {} name ({})", - i, key, i, policy_column_name)}; -} - -// todo: implement the rest of the parameters -// std::vector policy_ranges; -// Eigen::MatrixXd policy_cholesky; -// double info_speed; -// std::unordered_map> rural_prevalence; -// std::unordered_map income_models; -// double physical_activity_stddev; // Create test fixture for a StaticLinearModel class instance class StaticLinearModelTestFixture : public::testing::Test { @@ -130,3 +180,7 @@ class StaticLinearModelTestFixture : public::testing::Test { physical_activity_stddev ); }; + +TEST_F(StaticLinearModelTestFixture, InitialiseFactors) { + // Test logic goes here +} \ No newline at end of file From 33516bd91f93b151448a1bc726d199231411f039 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 21 Feb 2024 16:37:50 +0000 Subject: [PATCH 3/7] Create StaticLinearModel instance from ModelParameters fixture --- .../StaticLinearModel.Test.cpp | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index c069d6c70..1734696fe 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -3,7 +3,7 @@ #include "HealthGPS/static_linear_model.h" // Create a test fixture for the StaticLinearModel parameters -class ModelParameters : public ::testing::Test { +class ModelParameters : public::testing::Test { public: hgps::RiskFactorSexAgeTable expected; std::vector names; @@ -164,23 +164,27 @@ class ModelParameters : public ::testing::Test { // Create test fixture for a StaticLinearModel class instance class StaticLinearModelTestFixture : public::testing::Test { public: - StaticLinearModel testModel( - expected, - names, - models, - lambda, - stddev, - cholesky, - policy_models, - policy_ranges, - policy_cholesky, - info_speed, - rural_prevalence, - income_models, - physical_activity_stddev - ); + StaticLinearModelTestFixture(const ModelParameters& params) : params_(params) { + // Create a StaticLinearModel instance + hgps::StaticLinearModel testModel{ + params_.expected, + params_.names, + params_.models, + params_.lambda, + params_.stddev, + params_.cholesky, + params_.policy_models, + params_.policy_ranges, + params_.policy_cholesky, + params_.info_speed, + params_.rural_prevalence, + params_.income_models, + params_.physical_activity_stddev + }; + } }; TEST_F(StaticLinearModelTestFixture, InitialiseFactors) { // Test logic goes here + } \ No newline at end of file From 306ef2fc7e21d7f4c8df467f8beb0daa52f8981f Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Thu, 22 Feb 2024 15:23:46 +0000 Subject: [PATCH 4/7] PR suggestions --- .../StaticLinearModel.Test.cpp | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index 1734696fe..c8288a770 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -18,21 +18,21 @@ class ModelParameters : public::testing::Test { void SetUp() override { auto expected = hgps::RiskFactorSexAgeTable{}; - std::vector factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; - std::vector factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.}; + std::vector factor_means_male = {200.,40.,30.,2000.,1.,1000.,3.,50.}; + std::vector factor_means_female = {200.,40.,30.,2000.,1.,1000.,3.,50.}; - std::unordered_map> factorsMale = { - {hgps::core::Identifier{"0"}, factorsMale}, - {hgps::core::Identifier{"1"}, factorsMale} + std::unordered_map> factors_male = { + {hgps::core::Identifier{"FoodCarbohydrate"}, factor_means_male}, + {hgps::core::Identifier{"FoodFat"}, factor_means_male} }; - std::unordered_map> factorsFemale = { - {hgps::core::Identifier{"0"}, factorsFemale}, - {hgps::core::Identifier{"1"}, factorsFemale} + std::unordered_map> factors_female = { + {hgps::core::Identifier{"FoodCarbohydrate"}, factor_means_female}, + {hgps::core::Identifier{"FoodFat"}, factor_means_female} }; - expected.emplace_row(hgps::core::Gender::male, factorsMale); - expected.emplace_row(hgps::core::Gender::female, factorsFemale); + expected.emplace_row(hgps::core::Gender::male, factors_male); + expected.emplace_row(hgps::core::Gender::female, factors_female); std::vector names = {"FoodCarbohydrate", "FoodFat"}; @@ -40,8 +40,8 @@ class ModelParameters : public::testing::Test { // We will define 2 RiskFactorModels: carb_model and fat_model std::vector models; hgps::LinearModelParams carb_model; - carb_model.intercept = -0.241505734056409; - std::unordered_map coefficients = { + carb_model.intercept = -0.241505734056409; + carb_model.coefficients = { {"Gender", 0.00191412437431455}, {"Age", -0.000545257990453302}, {"Age2", 3.06434596341924e-6}, @@ -49,11 +49,10 @@ class ModelParameters : public::testing::Test { {"Sector", 0.0967308121487847}, {"Income", 0.0810023788842083} }; - carb_model.coefficients = coefficients; hgps::LinearModelParams fat_model; fat_model.intercept = -0.693521187677; - coefficients = { + fat_model.coefficients = { {"Gender", 0.00385126434589752}, {"Age", -0.00433711587715954}, {"Age2", 3.11799231385975e-5}, @@ -61,7 +60,6 @@ class ModelParameters : public::testing::Test { {"Sector", -0.0629531974852436}, {"Income", 0.373256996730791} }; - fat_model.coefficients = coefficients; models.emplace_back(std::move(carb_model)); models.emplace_back(std::move(fat_model)); @@ -84,38 +82,34 @@ class ModelParameters : public::testing::Test { // We need 2 policy models for the 2 risk factor models hgps::LinearModelParams carb_policy_model; carb_policy_model.intercept = -1.29385215316281; - coefficients = { + carb_policy_model.coefficients = { {"Gender", 0.00845121355575458}, {"Age", 0.000386003599279214}, {"Sector", 0.0621763530502617}, {"Income", 0.0560558815729017} }; - carb_policy_model.coefficients = coefficients; - std::unordered_map log_coefficients = { + carb_policy_model.log_coefficients = { {"FoodCarbohydrate", 0.577638667323293}, {"FoodFat", -0.0801300827856402}, {"FoodProtein", -0.480946893299471}, {"FoodSodium", -0.0396319735508116} }; - carb_policy_model.log_coefficients = log_coefficients; policy_ranges.emplace_back(hgps::core::DoubleInterval{-2.31063, 0.37526}); hgps::LinearModelParams fat_policy_model; fat_policy_model.intercept = -0.734121205356728; - coefficients = { + fat_policy_model.coefficients = { {"Gender", 0.023422206650121}, {"Age", 0.000557687923688474}, {"Sector", 0.00166651997223223}, {"Income", -0.498755023022772} - } - fat_policy_model.coefficients = coefficients; - log_coefficients = { + }; + fat_policy_model.log_coefficients = { {"FoodCarbohydrate", 0.652615267035516}, {"FoodFat", 0.743457905132894}, {"FoodProtein", -1.2648185364167}, {"FoodSodium", -0.17181077457271} }; - fat_policy_model.log_coefficients = log_coefficients; policy_ranges.emplace_back(hgps::core::DoubleInterval{-4.87205, -0.10346}); policy_models.emplace_back(std::move(carb_policy_model)); @@ -131,26 +125,23 @@ class ModelParameters : public::testing::Test { hgps::LinearModelParams income_model_low; income_model_low.intercept = 0.0; - coefficients = {} - income_model_low.coefficients = coefficients; + income_model_low.coefficients = {}; hgps::LinearModelParams income_model_middle; income_model_middle.intercept = -0.0023671151435328; - coefficients = { + income_model_middle.coefficients = { {"Gender", 0.0244784583947992}, {"Over18", 0.276669591410528}, {"Sector", -0.491794449118805} }; - income_model_middle.coefficients = coefficients; hgps::LinearModelParams income_model_high; income_model_high.intercept = 0.0187952471153142; - coefficients = { + income_model_high.coefficients = { {"Gender", 0.02018185354194}, {"Over18", 0.607099252829797}, {"Sector", -1.5870837069653} }; - income_model_high.coefficients = coefficients; income_models.emplace(hgps::core::Income::low, std::move(income_model_low)); income_models.emplace(hgps::core::Income::middle, std::move(income_model_middle)); From 486d70214ee559082e2071e23b1f6f66d03d546f Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Thu, 22 Feb 2024 15:51:31 +0000 Subject: [PATCH 5/7] Move parameter declarations into fixture SetUp method --- .../StaticLinearModel.Test.cpp | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index c8288a770..72df93284 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -2,9 +2,10 @@ #include "HealthGPS/static_linear_model.h" -// Create a test fixture for the StaticLinearModel parameters -class ModelParameters : public::testing::Test { +// Create test fixture for a StaticLinearModel class instance +class StaticLinearModelTestFixture : public::testing::Test { public: + hgps::RiskFactorSexAgeTable expected; std::vector names; std::vector models; @@ -16,6 +17,8 @@ class ModelParameters : public::testing::Test { std::vector policy_ranges; Eigen::MatrixXd policy_cholesky; + StaticLinearModel* testModel; + void SetUp() override { auto expected = hgps::RiskFactorSexAgeTable{}; std::vector factor_means_male = {200.,40.,30.,2000.,1.,1000.,3.,50.}; @@ -149,30 +152,8 @@ class ModelParameters : public::testing::Test { const double physical_activity_stddev = 0.06; - }; -}; + }; -// Create test fixture for a StaticLinearModel class instance -class StaticLinearModelTestFixture : public::testing::Test { - public: - StaticLinearModelTestFixture(const ModelParameters& params) : params_(params) { - // Create a StaticLinearModel instance - hgps::StaticLinearModel testModel{ - params_.expected, - params_.names, - params_.models, - params_.lambda, - params_.stddev, - params_.cholesky, - params_.policy_models, - params_.policy_ranges, - params_.policy_cholesky, - params_.info_speed, - params_.rural_prevalence, - params_.income_models, - params_.physical_activity_stddev - }; - } }; TEST_F(StaticLinearModelTestFixture, InitialiseFactors) { From c411aa3a43c24734a78b0bf9c854af4198ea5b4b Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Fri, 23 Feb 2024 09:41:43 +0000 Subject: [PATCH 6/7] Initialise objects as member variables --- src/HealthGPS.Tests/StaticLinearModel.Test.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index 72df93284..dda436e5f 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -17,7 +17,9 @@ class StaticLinearModelTestFixture : public::testing::Test { std::vector policy_ranges; Eigen::MatrixXd policy_cholesky; - StaticLinearModel* testModel; + hgps::StaticLinearModel* testModel; + hgps::Person* testPerson; + hgps::Random* testRandom; void SetUp() override { auto expected = hgps::RiskFactorSexAgeTable{}; @@ -152,11 +154,16 @@ class StaticLinearModelTestFixture : public::testing::Test { const double physical_activity_stddev = 0.06; + testPerson = new hgps::Person(); + testRandom = new hgps::Random(); + + testModel = new hgps::StaticLinearModel(names, models, lambda, stddev, cholesky, policy_models, policy_ranges, policy_cholesky, info_speed, rural_prevalence, income_models, physical_activity_stddev); + }; }; TEST_F(StaticLinearModelTestFixture, InitialiseFactors) { - // Test logic goes here - + // Just check that initialise_factors runs successfully + testModel->initialise_factors(*testPerson, *testRandom); } \ No newline at end of file From e07ca7a1079e8b6d0ac0701c169c1b18712b174d Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Fri, 23 Feb 2024 12:37:01 +0000 Subject: [PATCH 7/7] Tidy object initialisation --- .../StaticLinearModel.Test.cpp | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp index dda436e5f..45530da99 100644 --- a/src/HealthGPS.Tests/StaticLinearModel.Test.cpp +++ b/src/HealthGPS.Tests/StaticLinearModel.Test.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "HealthGPS/static_linear_model.h" +#include "HealthGPS/mtrandom.h" // Create test fixture for a StaticLinearModel class instance class StaticLinearModelTestFixture : public::testing::Test { @@ -17,9 +18,9 @@ class StaticLinearModelTestFixture : public::testing::Test { std::vector policy_ranges; Eigen::MatrixXd policy_cholesky; - hgps::StaticLinearModel* testModel; - hgps::Person* testPerson; - hgps::Random* testRandom; + hgps::StaticLinearModel* test_model; + hgps::Person* test_person; + hgps::Random* test_random; void SetUp() override { auto expected = hgps::RiskFactorSexAgeTable{}; @@ -154,10 +155,25 @@ class StaticLinearModelTestFixture : public::testing::Test { const double physical_activity_stddev = 0.06; - testPerson = new hgps::Person(); - testRandom = new hgps::Random(); - - testModel = new hgps::StaticLinearModel(names, models, lambda, stddev, cholesky, policy_models, policy_ranges, policy_cholesky, info_speed, rural_prevalence, income_models, physical_activity_stddev); + test_person = new hgps::Person(); + auto engine = hgps::MTRandom32{123456789}; + test_random = new hgps::Random(engine); + + test_model = new hgps::StaticLinearModel( + expected, + names, + models, + lambda, + stddev, + cholesky, + policy_models, + policy_ranges, + policy_cholesky, + info_speed, + rural_prevalence, + income_models, + physical_activity_stddev + ); }; @@ -165,5 +181,5 @@ class StaticLinearModelTestFixture : public::testing::Test { TEST_F(StaticLinearModelTestFixture, InitialiseFactors) { // Just check that initialise_factors runs successfully - testModel->initialise_factors(*testPerson, *testRandom); + test_model->initialise_factors(*test_person, *test_random); } \ No newline at end of file