Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial pass at implementing test fixtures #316

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/HealthGPS.Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
186 changes: 186 additions & 0 deletions src/HealthGPS.Tests/StaticLinearModel.Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include "pch.h"

#include "HealthGPS/static_linear_model.h"

// Create a test fixture for the StaticLinearModel parameters
class ModelParameters : public ::testing::Test {
public:
hgps::RiskFactorSexAgeTable expected;
std::vector<hgps::core::Identifier> names;
std::vector<hgps::LinearModelParams> models;
std::vector<double> lambda;
std::vector<double> stddev;
Eigen::Matrix2d correlation;
Eigen::MatrixXd cholesky;
std::vector<hgps::LinearModelParams> policy_models;
std::vector<hgps::core::DoubleInterval> policy_ranges;
Eigen::MatrixXd policy_cholesky;

void SetUp() override {
auto expected = hgps::RiskFactorSexAgeTable{};
std::vector<double> factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.};
std::vector<double> factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.};

std::unordered_map<hgps::core::Identifier, std::vector<double>> factorsMale = {

Check failure on line 24 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

conflicting declaration ‘std::unordered_map<hgps::core::Identifier, std::vector<double> > factorsMale’

Check failure on line 24 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

conflicting declaration ‘std::unordered_map<hgps::core::Identifier, std::vector<double> > factorsMale’
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redefinition of 'factorsMale' with a different type: 'std::unordered_map<hgps::core::Identifier, std::vector>' vs 'std::vector' [clang-diagnostic-error]

            std::unordered_map<hgps::core::Identifier, std::vector<double>> factorsMale = {
                                                                            ^
Additional context

src/HealthGPS.Tests/StaticLinearModel.Test.cpp:20: previous definition is here

            std::vector<double> factorsMale = {200.,40.,30.,2000.,1.,1000.,3.,50.};
                                ^

TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
{hgps::core::Identifier{"0"}, factorsMale},
{hgps::core::Identifier{"1"}, factorsMale}
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
};

std::unordered_map<hgps::core::Identifier, std::vector<double>> factorsFemale = {

Check failure on line 29 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

conflicting declaration ‘std::unordered_map<hgps::core::Identifier, std::vector<double> > factorsFemale’

Check failure on line 29 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

conflicting declaration ‘std::unordered_map<hgps::core::Identifier, std::vector<double> > factorsFemale’
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redefinition of 'factorsFemale' with a different type: 'std::unordered_map<hgps::core::Identifier, std::vector>' vs 'std::vector' [clang-diagnostic-error]

            std::unordered_map<hgps::core::Identifier, std::vector<double>> factorsFemale = {
                                                                            ^
Additional context

src/HealthGPS.Tests/StaticLinearModel.Test.cpp:21: previous definition is here

            std::vector<double> factorsFemale = {200.,40.,30.,2000.,1.,1000.,3.,50.};
                                ^

{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<hgps::core::Identifier> names = {"FoodCarbohydrate", "FoodFat"};

// Define models
// We will define 2 RiskFactorModels: carb_model and fat_model
std::vector<hgps::LinearModelParams> models;
hgps::LinearModelParams carb_model;
carb_model.intercept = -0.241505734056409;
std::unordered_map<hgps::core::Identifier, double> coefficients = {
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
{"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 = {
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
{"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<double> lambda = {0.262626262626263, 0.141414141414141};
std::vector<double> stddev = {0.337810256621877, 0.436763527499362};

Eigen::Matrix2d correlation;
correlation << 1.0, 0.322065425, 0.322065425,1.0;
auto cholesky = Eigen::MatrixXd{Eigen::LLT<Eigen::Matrix2d>{correlation}.matrixL()};

Eigen::MatrixXd policy_covariance;
policy_covariance << 0.1,0.1,0.1,0.41;
auto policy_cholesky = Eigen::MatrixXd{Eigen::LLT<Eigen::MatrixXd>{policy_covariance}.matrixL()};

// Define policy models
std::vector<hgps::LinearModelParams> policy_models;
std::vector<hgps::core::DoubleInterval> 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<hgps::core::Identifier, double> 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});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unnecessary temporary object created while calling emplace_back [modernize-use-emplace]

Suggested change
policy_ranges.emplace_back(hgps::core::DoubleInterval{-2.31063, 0.37526});
policy_ranges.emplace_back(-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}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected ';' after expression [clang-diagnostic-error]

Suggested change
}
};

fat_policy_model.coefficients = coefficients;

Check failure on line 111 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

expected ‘;’ before ‘fat_policy_model’

Check failure on line 111 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

expected ‘;’ before ‘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});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unnecessary temporary object created while calling emplace_back [modernize-use-emplace]

Suggested change
policy_ranges.emplace_back(hgps::core::DoubleInterval{-4.87205, -0.10346});
policy_ranges.emplace_back(-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;

Check failure on line 124 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

unused variable ‘info_speed’ [-Werror=unused-variable]

Check failure on line 124 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

unused variable ‘info_speed’ [-Werror=unused-variable]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unused variable 'info_speed' [clang-diagnostic-unused-variable]

            const double info_speed = 0.1;
                         ^


std::unordered_map<hgps::core::Identifier, std::unordered_map<hgps::core::Gender, double>> 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<hgps::core::Income, hgps::LinearModelParams> income_models;

hgps::LinearModelParams income_model_low;
income_model_low.intercept = 0.0;
coefficients = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected ';' after expression [clang-diagnostic-error]

Suggested change
coefficients = {}
coefficients = {};

income_model_low.coefficients = coefficients;

Check failure on line 135 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

expected ‘;’ before ‘income_model_low’

Check failure on line 135 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

expected ‘;’ before ‘income_model_low’

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;

Check failure on line 159 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

unused variable ‘physical_activity_stddev’ [-Werror=unused-variable]

Check failure on line 159 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

unused variable ‘physical_activity_stddev’ [-Werror=unused-variable]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unused variable 'physical_activity_stddev' [clang-diagnostic-unused-variable]

            const double physical_activity_stddev = 0.06;
                         ^


};
};

// Create test fixture for a StaticLinearModel class instance
class StaticLinearModelTestFixture : public::testing::Test {
public:
StaticLinearModel testModel(

Check failure on line 167 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

‘StaticLinearModel’ does not name a type

Check failure on line 167 in src/HealthGPS.Tests/StaticLinearModel.Test.cpp

View workflow job for this annotation

GitHub Actions / Build and test (linux, ubuntu-22.04, gcc-latest, false)

‘StaticLinearModel’ does not name a type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'StaticLinearModel'; did you mean 'hgps::StaticLinearModel'? [clang-diagnostic-error]

Suggested change
StaticLinearModel testModel(
hgps::StaticLinearModel testModel(
Additional context

src/HealthGPS/static_linear_model.h:22: 'hgps::StaticLinearModel' declared here

class StaticLinearModel final : public RiskFactorAdjustableModel {
      ^

expected,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'expected' [clang-diagnostic-error]

            expected,
            ^

names,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'names' [clang-diagnostic-error]

            names,
            ^

models,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'models'; did you mean 'mode_t'? [clang-diagnostic-error]

Suggested change
models,
mode_t,
Additional context

/usr/include/x86_64-linux-gnu/sys/types.h:68: 'mode_t' declared here

typedef __mode_t mode_t;
                 ^

lambda,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'lambda' [clang-diagnostic-error]

            lambda,
            ^

stddev,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'stddev' [clang-diagnostic-error]

            stddev,
            ^

cholesky,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'cholesky' [clang-diagnostic-error]

            cholesky,
            ^

policy_models,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'policy_models' [clang-diagnostic-error]

            policy_models,
            ^

policy_ranges,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'policy_ranges' [clang-diagnostic-error]

            policy_ranges,
            ^

policy_cholesky,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'policy_cholesky' [clang-diagnostic-error]

            policy_cholesky,
            ^

info_speed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'info_speed' [clang-diagnostic-error]

            info_speed,
            ^

rural_prevalence,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'rural_prevalence' [clang-diagnostic-error]

            rural_prevalence,
            ^

income_models,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'income_models' [clang-diagnostic-error]

            income_models,
            ^

physical_activity_stddev
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'physical_activity_stddev' [clang-diagnostic-error]

            physical_activity_stddev
            ^

);
};

TEST_F(StaticLinearModelTestFixture, InitialiseFactors) {
// Test logic goes here
}
Loading