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/format.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 // MAHIMA: Optional — JACARDI packs set education/employment true; others omit / leave false.
162 if (d.contains("education") && !d["education"].is_null()) {
163 req.demographics.education = d["education"].get<bool>();
164 }
165 if (d.contains("employment") && !d["employment"].is_null()) {
166 req.demographics.employment = d["employment"].get<bool>();
167 }
168 if (d.contains("max_age_for_linear_models") && !d["max_age_for_linear_models"].is_null()) {
169 req.demographics.max_age_for_linear_models = d["max_age_for_linear_models"].get<int>();
170 }
171 if (d.contains("gender2") && !d["gender2"].is_null()) {
172 req.demographics.gender2 = d["gender2"].get<std::string>();
173 }
174 // MAHIMA: Income/PA/trend — when enabled is false only that flag is required; detail
175 // fields optional with struct defaults. When enabled is true, require type (/categories).
176 const auto &inc = pr["income"];
177 req.income.enabled = inc["enabled"].get<bool>();
178 if (req.income.enabled) {
179 if (!inc.contains("type") || !inc.contains("categories")) {
180 throw ConfigurationError{
181 "project_requirements.income.enabled is true but type and/or categories missing"};
182 }
183 req.income.type = inc["type"].get<std::string>();
184 req.income.categories = inc["categories"].get<std::string>();
185 } else {
186 if (inc.contains("type") && !inc["type"].is_null()) {
187 req.income.type = inc["type"].get<std::string>();
188 }
189 if (inc.contains("categories") && !inc["categories"].is_null()) {
190 req.income.categories = inc["categories"].get<std::string>();
191 }
193 }
194 if (inc.contains("adjust_to_factors_mean") && !inc["adjust_to_factors_mean"].is_null()) {
195 req.income.adjust_to_factors_mean = inc["adjust_to_factors_mean"].get<bool>();
196 }
197 if (inc.contains("trended") && !inc["trended"].is_null()) {
198 req.income.trended = inc["trended"].get<bool>();
199 }
200 if (inc.contains("income_based_csv_output") && !inc["income_based_csv_output"].is_null()) {
201 req.income.income_based_csv_output = inc["income_based_csv_output"].get<bool>();
202 }
203
204 const auto &pa = pr["physical_activity"];
205 req.physical_activity.enabled = pa["enabled"].get<bool>();
206 if (req.physical_activity.enabled) {
207 if (!pa.contains("type")) {
208 throw ConfigurationError{
209 "project_requirements.physical_activity.enabled is true but type is missing"};
210 }
211 req.physical_activity.type = pa["type"].get<std::string>();
212 } else if (pa.contains("type") && !pa["type"].is_null()) {
213 req.physical_activity.type = pa["type"].get<std::string>();
214 }
215 if (pa.contains("adjust_to_factors_mean") && !pa["adjust_to_factors_mean"].is_null()) {
216 req.physical_activity.adjust_to_factors_mean = pa["adjust_to_factors_mean"].get<bool>();
217 }
218 if (pa.contains("trended") && !pa["trended"].is_null()) {
219 req.physical_activity.trended = pa["trended"].get<bool>();
220 }
221
222 const auto &rf = pr["risk_factors"];
223 req.risk_factors.adjust_to_factors_mean = rf["adjust_to_factors_mean"].get<bool>();
224 req.risk_factors.trended = rf["trended"].get<bool>();
225
226 const auto &tr = pr["trend"];
227 req.trend.enabled = tr["enabled"].get<bool>();
228 if (req.trend.enabled) {
229 if (!tr.contains("type")) {
230 throw ConfigurationError{
231 "project_requirements.trend.enabled is true but type is missing"};
232 }
233 req.trend.type = tr["type"].get<std::string>();
234 } else if (tr.contains("type") && !tr["type"].is_null()) {
235 req.trend.type = tr["type"].get<std::string>();
236 } else {
237 req.trend.type = "null";
238 }
239
240 const auto &ts = pr["two_stage"];
241 req.two_stage.use_logistic = ts["use_logistic"].get<bool>();
242 if (ts.contains("logistic_file") && !ts["logistic_file"].is_null()) {
243 req.two_stage.logistic_file = ts["logistic_file"].get<std::string>();
244 }
245}
246} // 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:20
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:231
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:241
std::optional< int > max_age_for_linear_models
Definition poco.h:239
bool trended
Definition poco.h:249
std::string categories
Definition poco.h:247
bool income_based_csv_output
Definition poco.h:252
std::string type
Definition poco.h:246
bool enabled
Definition poco.h:245
bool adjust_to_factors_mean
Definition poco.h:248
bool adjust_to_factors_mean
Definition poco.h:263
std::string type
Definition poco.h:269
bool enabled
Definition poco.h:268
std::string logistic_file
Definition poco.h:274
bool use_logistic
Definition poco.h:273
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