Health-GPS 3.0.0.0
Global Health Policy Simulation model (Health-GPS)
Loading...
Searching...
No Matches
configuration_parsing_helpers.h
Go to the documentation of this file.
1
9#pragma once
10#include "configuration.h"
11#include "poco.h"
12
14
15#include <fmt/color.h>
16#include <fmt/core.h>
17#include <nlohmann/json.hpp>
18
19#include <filesystem>
20#include <string>
21
22namespace hgps::input {
28nlohmann::json get(const nlohmann::json &j, const std::string &key);
29
36template <class T> bool get_to(const nlohmann::json &j, const std::string &key, T &out) noexcept {
37 try {
38 out = j.at(key).get<T>();
39 return true;
40 } catch (const nlohmann::json::out_of_range &) {
41 fmt::print(fg(fmt::color::red), "Missing key \"{}\"\n", key);
42 return false;
43 } catch (const nlohmann::json::type_error &) {
44 fmt::print(fg(fmt::color::red), "Key \"{}\" is of wrong type\n", key);
45 return false;
46 }
47}
48
56template <class T>
57bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success) noexcept {
58 const bool ret = get_to(j, key, out);
59 if (!ret) {
60 success = false;
61 }
62 return ret;
63}
64
69void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path &base_dir);
70
77bool rebase_valid_path_to(const nlohmann::json &j, const std::string &key,
78 std::filesystem::path &out,
79 const std::filesystem::path &base_dir) noexcept;
80
87void rebase_valid_path_to(const nlohmann::json &j, const std::string &key,
88 std::filesystem::path &out, const std::filesystem::path &base_dir,
89 bool &success) noexcept;
90
96FileInfo get_file_info(const nlohmann::json &node, const std::filesystem::path &base_dir);
97
102SettingsInfo get_settings(const nlohmann::json &j);
103
109BaselineInfo get_baseline_info(const nlohmann::json &j, const std::filesystem::path &base_dir);
110
115void load_interventions(const nlohmann::json &running, Configuration &config);
116
118inline void reject_deprecated_root_config_fields(const nlohmann::json &opt) {
119 if (!opt.contains("project_requirements")) {
120 return;
121 }
122 if (opt.contains("trend_type") || opt.contains("income_categories")) {
123 throw ConfigurationError{
124 "Deprecated root-level trend_type and/or income_categories are not allowed when "
125 "project_requirements is present. Use project_requirements.trend and "
126 "project_requirements.income.categories instead."};
127 }
128}
129
130inline void validate_legacy_trend_type_string(const std::string &trend_type) {
131 if (trend_type != "null" && trend_type != "trend" && trend_type != "upf_trend" &&
132 trend_type != "UPFTrend" && trend_type != "income_trend") {
133 throw ConfigurationError{fmt::format("Invalid trend_type: {}. Must be one of: null, trend, "
134 "upf_trend, UPFTrend, income_trend",
135 trend_type)};
136 }
137}
138
140inline void apply_legacy_root_config_fields(const nlohmann::json &opt, ProjectRequirements &req) {
141 if (opt.contains("trend_type")) {
142 const auto trend_type = opt["trend_type"].get<std::string>();
144 req.trend.type = trend_type;
145 req.trend.enabled = trend_type != "null";
146 }
147 if (opt.contains("income_categories")) {
148 const auto categories = opt["income_categories"].get<std::string>();
150 req.income.categories = categories;
151 }
152}
153
155inline void parse_project_requirements_block(const nlohmann::json &pr, ProjectRequirements &req) {
156 const auto &d = pr["demographics"];
157 req.demographics.age = d["age"].get<bool>();
158 req.demographics.gender = d["gender"].get<bool>();
159 req.demographics.region = d["region"].get<bool>();
160 req.demographics.ethnicity = d["ethnicity"].get<bool>();
161 if (d.contains("max_age_for_linear_models") && !d["max_age_for_linear_models"].is_null()) {
162 req.demographics.max_age_for_linear_models = d["max_age_for_linear_models"].get<int>();
163 }
164 if (d.contains("gender2") && !d["gender2"].is_null()) {
165 req.demographics.gender2 = d["gender2"].get<std::string>();
166 }
167 const auto &inc = pr["income"];
168 req.income.enabled = inc["enabled"].get<bool>();
169 req.income.type = inc["type"].get<std::string>();
170 req.income.categories = inc["categories"].get<std::string>();
171 req.income.adjust_to_factors_mean = inc["adjust_to_factors_mean"].get<bool>();
172 req.income.trended = inc["trended"].get<bool>();
173 if (inc.contains("income_based_csv_output")) {
174 req.income.income_based_csv_output = inc["income_based_csv_output"].get<bool>();
175 }
176 const auto &pa = pr["physical_activity"];
177 req.physical_activity.enabled = pa["enabled"].get<bool>();
178 req.physical_activity.type = pa["type"].get<std::string>();
179 req.physical_activity.adjust_to_factors_mean = pa["adjust_to_factors_mean"].get<bool>();
180 req.physical_activity.trended = pa["trended"].get<bool>();
181 const auto &rf = pr["risk_factors"];
182 req.risk_factors.adjust_to_factors_mean = rf["adjust_to_factors_mean"].get<bool>();
183 req.risk_factors.trended = rf["trended"].get<bool>();
184 const auto &tr = pr["trend"];
185 req.trend.enabled = tr["enabled"].get<bool>();
186 req.trend.type = tr["type"].get<std::string>();
187 const auto &ts = pr["two_stage"];
188 req.two_stage.use_logistic = ts["use_logistic"].get<bool>();
189 if (ts.contains("logistic_file") && !ts["logistic_file"].is_null()) {
190 req.two_stage.logistic_file = ts["logistic_file"].get<std::string>();
191 }
192}
193} // namespace hgps::input
Represents an error that occurred with the format of a config file.
Definition configuration.h:94
Main header file for functionality related to loading config files.
IncomeCategoryLayout income_category_layout_from_config(std::string_view categories)
Parse project_requirements.income.categories ("3", "4", or "5").
Definition income_category_layout.cpp:19
Data structures containing model parameters and configuration options.
Definition configuration.cpp:75
void validate_legacy_trend_type_string(const std::string &trend_type)
Definition configuration_parsing_helpers.h:130
nlohmann::json get(const json &j, const std::string &key)
Load value from JSON, printing an error message if it fails.
Definition configuration_parsing.cpp:124
bool get_to(const nlohmann::json &j, const std::string &key, T &out) noexcept
Get value from JSON object and store in out.
Definition configuration_parsing_helpers.h:36
void load_interventions(const json &running, Configuration &config)
Load interventions from running section.
Definition configuration_parsing.cpp:226
bool rebase_valid_path_to(const json &j, const std::string &key, std::filesystem::path &out, const std::filesystem::path &base_dir) noexcept
Get a valid path from a JSON object.
Definition configuration_parsing.cpp:146
void reject_deprecated_root_config_fields(const nlohmann::json &opt)
Reject deprecated root-level fields when project_requirements is the source of truth.
Definition configuration_parsing_helpers.h:118
BaselineInfo get_baseline_info(const json &j, const std::filesystem::path &base_dir)
Load BaselineInfo from JSON.
Definition configuration_parsing.cpp:193
void parse_project_requirements_block(const nlohmann::json &pr, ProjectRequirements &req)
Parse project_requirements block from config.json.
Definition configuration_parsing_helpers.h:155
void apply_legacy_root_config_fields(const nlohmann::json &opt, ProjectRequirements &req)
Map legacy root-level trend_type / income_categories into project_requirements.
Definition configuration_parsing_helpers.h:140
FileInfo get_file_info(const json &node, const std::filesystem::path &base_dir)
Load FileInfo from JSON.
Definition configuration_parsing.cpp:170
void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path &base_dir)
Rebase path on base_dir.
Definition configuration_parsing.cpp:133
SettingsInfo get_settings(const json &j)
Load settings section of JSON.
Definition configuration_parsing.cpp:184
std::string gender2
Which sex gets value 1 for the gender2 CSV regression row ("male" or "female").
Definition poco.h:232
std::optional< int > max_age_for_linear_models
Definition poco.h:230
bool trended
Definition poco.h:240
std::string categories
Definition poco.h:238
bool income_based_csv_output
Definition poco.h:243
std::string type
Definition poco.h:237
bool enabled
Definition poco.h:236
bool adjust_to_factors_mean
Definition poco.h:239
bool adjust_to_factors_mean
Definition poco.h:254
std::string type
Definition poco.h:260
bool enabled
Definition poco.h:259
std::string logistic_file
Definition poco.h:265
bool use_logistic
Definition poco.h:264
struct hgps::input::ProjectRequirements::TwoStage two_stage
struct hgps::input::ProjectRequirements::Income income
struct hgps::input::ProjectRequirements::PhysicalActivity physical_activity
struct hgps::input::ProjectRequirements::RiskFactors risk_factors
struct hgps::input::ProjectRequirements::Demographics demographics
struct hgps::input::ProjectRequirements::Trend trend