From d00133eddd934fd6a6426d4015fa9adcc5141b08 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 3 Jul 2024 20:37:57 +0100 Subject: [PATCH 1/9] Implement calcluated stats --- src/HealthGPS/analysis_module.cpp | 161 ++++++++++++++++++++++++++---- src/HealthGPS/analysis_module.h | 17 ++++ 2 files changed, 156 insertions(+), 22 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index bc731b7a4..bc0ca8da4 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -319,36 +319,88 @@ DALYsIndicator AnalysisModule::calculate_dalys(Population &population, unsigned } void AnalysisModule::calculate_population_statistics(RuntimeContext &context) { - size_t num_factors_to_calculate = - context.mapping().entries().size() - factors_to_calculate_.size(); + + auto current_time = static_cast(context.time_now()); for (const auto &person : context.population()) { - // Get the bin index for each factor - std::vector bin_indices; - for (size_t i = 0; i < factors_to_calculate_.size(); i++) { - double factor_value = person.get_risk_factor_value(factors_to_calculate_[i]); - auto bin_index = - static_cast((factor_value - factor_min_values_[i]) / factor_bin_widths_[i]); - bin_indices.push_back(bin_index); - } + // First let's fetch the correct `calculated_stats_` bin index for this person + size_t index = calculate_index(person); - // Calculate the index in the calculated_stats_ vector - size_t index = 0; - for (size_t i = 0; i < bin_indices.size() - 1; i++) { - size_t accumulated_bins = - std::accumulate(std::next(factor_bins_.cbegin(), i + 1), factor_bins_.cend(), - size_t{1}, std::multiplies<>()); - index += bin_indices[i] * accumulated_bins * num_factors_to_calculate; + // Now we can add the calculated stats for this person to the correct index + if (!person.is_active()) { + if (!person.is_alive() && person.time_of_death() == current_time) { + calculated_stats_[index + get_channel_index("deaths")]++; + float expected_life = + definition_.life_expectancy().at(context.time_now(), person.gender); + double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; + calculated_stats_[index + get_channel_index("mean_yll")] += yll; + calculated_stats_[index + get_channel_index("mean_daly")] += yll; + } + + if (person.has_emigrated() && person.time_of_migration() == current_time) { + calculated_stats_[index + get_channel_index("emigrations")]++; + } + + continue; } - index += bin_indices.back() * num_factors_to_calculate; - // Now we can add the values of the factors that are not in factors_to_calculate_ + calculated_stats_[index + get_channel_index("count")]++; + for (const auto &factor : context.mapping().entries()) { - if (std::find(factors_to_calculate_.cbegin(), factors_to_calculate_.cend(), - factor.key()) == factors_to_calculate_.cend()) { - calculated_stats_[index++] += person.get_risk_factor_value(factor.key()); + double value = person.get_risk_factor_value(factor.key()); + calculated_stats_[index + get_channel_index("mean_" + factor.key().to_string())] += + value; + } + + for (const auto &[disease_name, disease_state] : person.diseases) { + if (disease_state.status == DiseaseStatus::active) { + calculated_stats_[index + + get_channel_index("prevalence_" + disease_name.to_string())]++; + if (disease_state.start_time == context.time_now()) { + calculated_stats_[index + + get_channel_index("incidence_" + disease_name.to_string())]++; + } } } + + double dw = calculate_disability_weight(person); + double yld = dw * DALY_UNITS; + calculated_stats_[index + get_channel_index("mean_yld")] += yld; + calculated_stats_[index + get_channel_index("mean_daly")] += yld; + + classify_weight(person); + } + + // For each bin in the calculated stats... + for (size_t i = 0; i < calculated_stats_.size(); i += channels_.size()) { + double count_F = calculated_stats_[i + get_channel_index("count")]; + double count_M = calculated_stats_[i + get_channel_index("count")]; + double deaths_F = calculated_stats_[i + get_channel_index("deaths")]; + double deaths_M = calculated_stats_[i + get_channel_index("deaths")]; + + // Calculate in-place factor averages. + for (const auto &factor : context.mapping().entries()) { + calculated_stats_[i + get_channel_index("mean_" + factor.key().to_string())] /= count_F; + calculated_stats_[i + get_channel_index("mean_" + factor.key().to_string())] /= count_M; + } + + // Calculate in-place disease prevalence and incidence rates. + for (const auto &disease : context.diseases()) { + calculated_stats_[i + get_channel_index("prevalence_" + disease.code.to_string())] /= + count_F; + calculated_stats_[i + get_channel_index("prevalence_" + disease.code.to_string())] /= + count_M; + calculated_stats_[i + get_channel_index("incidence_" + disease.code.to_string())] /= + count_F; + calculated_stats_[i + get_channel_index("incidence_" + disease.code.to_string())] /= + count_M; + } + + // Calculate in-place YLL/YLD/DALY averages. + for (const auto &column : {"mean_yll", "mean_yld", "mean_daly"}) { + calculated_stats_[i + get_channel_index(column)] /= (count_F + deaths_F); + calculated_stats_[i + get_channel_index(column)] /= (count_M + deaths_M); + } } } @@ -531,6 +583,26 @@ void AnalysisModule::classify_weight(DataSeries &series, const Person &entity) c } } +void AnalysisModule::classify_weight(const Person &person) { + auto weight_class = weight_classifier_.classify_weight(person); + switch (weight_class) { + case WeightCategory::normal: + calculated_stats_[get_channel_index("normal_weight")]++; + break; + case WeightCategory::overweight: + calculated_stats_[get_channel_index("over_weight")]++; + calculated_stats_[get_channel_index("above_weight")]++; + break; + case WeightCategory::obese: + calculated_stats_[get_channel_index("obese_weight")]++; + calculated_stats_[get_channel_index("above_weight")]++; + break; + default: + throw std::logic_error("Unknown weight classification category."); + break; + } +} + void AnalysisModule::initialise_output_channels(RuntimeContext &context) { if (!channels_.empty()) { return; @@ -560,8 +632,53 @@ void AnalysisModule::initialise_output_channels(RuntimeContext &context) { channels_.emplace_back("std_yld"); channels_.emplace_back("mean_daly"); channels_.emplace_back("std_daly"); + + // Since we will be performing frequent lookups, we will store the strings and indexes in a map + // for quick access. + for (size_t i = 0; i < channels_.size(); i++) { + channel_index_.emplace(channels_[i], i); + } +} + +size_t AnalysisModule::calculate_index(const Person &person) const { + // Get the bin index for each factor + std::vector bin_indices; + for (size_t i = 0; i < factors_to_calculate_.size(); i++) { + double factor_value = person.get_risk_factor_value(factors_to_calculate_[i]); + auto bin_index = + static_cast((factor_value - factor_min_values_[i]) / factor_bin_widths_[i]); + bin_indices.push_back(bin_index); + } + + // Calculate the index in the calculated_stats_ vector + size_t index = 0; + for (size_t i = 0; i < bin_indices.size() - 1; i++) { + size_t accumulated_bins = + std::accumulate(std::next(factor_bins_.cbegin(), i + 1), factor_bins_.cend(), size_t{1}, + std::multiplies<>()); + index += bin_indices[i] * accumulated_bins * channels_.size(); + } + index += bin_indices.back() * channels_.size(); + + return index; } +size_t AnalysisModule::get_channel_index(const std::string &channel) const { + auto it = channel_index_.find(channel); + if (it == channel_index_.end()) { + throw std::out_of_range("Unknown channel: " + channel); + } + + return it->second; +} + +void AnalysisModule::accumulate_squared_diffs(size_t bin_index, size_t channel_index) const { + for (const auto &factor : factors_to_calculate_) { + const double mean = calculated_stats_[bin_index + channel_index]; + const double diff = value - mean; + } +}; + std::unique_ptr build_analysis_module(Repository &repository, const ModelInput &config) { auto analysis_entity = repository.manager().get_disease_analysis(config.settings().country()); diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index 58ea43fe7..d6873fdf1 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -46,6 +46,7 @@ class AnalysisModule final : public UpdatableModule { WeightModel weight_classifier_; DoubleAgeGenderTable residual_disability_weight_; std::vector channels_; + std::unordered_map channel_index_; unsigned int comorbidities_; std::string name_{"Analysis"}; std::vector factors_to_calculate_ = {"Gender"_id, "Age"_id}; @@ -70,8 +71,24 @@ class AnalysisModule final : public UpdatableModule { void calculate_population_statistics(RuntimeContext &context, DataSeries &series) const; void classify_weight(hgps::DataSeries &series, const hgps::Person &entity) const; + void classify_weight(const Person &person); void initialise_output_channels(RuntimeContext &context); + /// @brief Calculates the bin index in `calculated_stats_` for a given person + /// @param person The person to calculate the index for + /// @return The index in `calculated_stats_` + size_t calculate_index(const Person &person) const; + + /// @brief Gets the index of the given channel name + /// @param channel The channel name + /// @return The channel index + size_t get_channel_index(const std::string &channel) const; + + /// @brief Accumulates the squared differences between the given value and the mean + /// @param bin_index The index of the bin in `calculated_stats_` + /// @param channel_index The index of the channel in `channels_` + void accumulate_squared_diffs(size_t bin_index, size_t channel_index) const; + /// @brief Calculates the standard deviation of factors given data series containing means /// @param context The runtime context /// @param series The data series containing factor means From ed573eda55ccfefb89e3fec9a32eef63e6016b31 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Thu, 4 Jul 2024 09:40:52 +0100 Subject: [PATCH 2/9] Remove unused methods for now --- src/HealthGPS/analysis_module.cpp | 7 ------- src/HealthGPS/analysis_module.h | 5 ----- 2 files changed, 12 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index bc0ca8da4..c7fcca533 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -672,13 +672,6 @@ size_t AnalysisModule::get_channel_index(const std::string &channel) const { return it->second; } -void AnalysisModule::accumulate_squared_diffs(size_t bin_index, size_t channel_index) const { - for (const auto &factor : factors_to_calculate_) { - const double mean = calculated_stats_[bin_index + channel_index]; - const double diff = value - mean; - } -}; - std::unique_ptr build_analysis_module(Repository &repository, const ModelInput &config) { auto analysis_entity = repository.manager().get_disease_analysis(config.settings().country()); diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index d6873fdf1..db8cb7606 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -84,11 +84,6 @@ class AnalysisModule final : public UpdatableModule { /// @return The channel index size_t get_channel_index(const std::string &channel) const; - /// @brief Accumulates the squared differences between the given value and the mean - /// @param bin_index The index of the bin in `calculated_stats_` - /// @param channel_index The index of the channel in `channels_` - void accumulate_squared_diffs(size_t bin_index, size_t channel_index) const; - /// @brief Calculates the standard deviation of factors given data series containing means /// @param context The runtime context /// @param series The data series containing factor means From 3b259be515730c057c5e12f938db1e18fdf012fc Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 17 Jul 2024 17:39:34 +0100 Subject: [PATCH 3/9] Use size_t as value type --- src/HealthGPS/analysis_module.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index db8cb7606..183bb509f 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -46,7 +46,7 @@ class AnalysisModule final : public UpdatableModule { WeightModel weight_classifier_; DoubleAgeGenderTable residual_disability_weight_; std::vector channels_; - std::unordered_map channel_index_; + std::unordered_map channel_index_; unsigned int comorbidities_; std::string name_{"Analysis"}; std::vector factors_to_calculate_ = {"Gender"_id, "Age"_id}; From 891b71d5f689f568e8182be8ad6c3f08a1fec348 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 17 Jul 2024 18:19:31 +0100 Subject: [PATCH 4/9] Simplify function by splitting into smaller functions --- src/HealthGPS/analysis_module.cpp | 74 +++++++++++++++++-------------- src/HealthGPS/analysis_module.h | 2 + 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index c7fcca533..0235a9265 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -318,51 +318,57 @@ DALYsIndicator AnalysisModule::calculate_dalys(Population &population, unsigned .disability_adjusted_life_years = yll + yld}; } -void AnalysisModule::calculate_population_statistics(RuntimeContext &context) { - +void AnalysisModule::update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context){ auto current_time = static_cast(context.time_now()); - for (const auto &person : context.population()) { - // First let's fetch the correct `calculated_stats_` bin index for this person - size_t index = calculate_index(person); + if (!person.is_alive() && person.time_of_death() == context.time_now()) { + calculated_stats_[index + get_channel_index("deaths")]++; + float expected_life = + definition_.life_expectancy().at(context.time_now(), person.gender); + double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; + calculated_stats_[index + get_channel_index("mean_yll")] += yll; + calculated_stats_[index + get_channel_index("mean_daly")] += yll; + } - // Now we can add the calculated stats for this person to the correct index - if (!person.is_active()) { - if (!person.is_alive() && person.time_of_death() == current_time) { - calculated_stats_[index + get_channel_index("deaths")]++; - float expected_life = - definition_.life_expectancy().at(context.time_now(), person.gender); - double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; - calculated_stats_[index + get_channel_index("mean_yll")] += yll; - calculated_stats_[index + get_channel_index("mean_daly")] += yll; - } + if (person.has_emigrated() && person.time_of_migration() == context.time_now()) { + calculated_stats_[index + get_channel_index("emigrations")]++; + } +} - if (person.has_emigrated() && person.time_of_migration() == current_time) { - calculated_stats_[index + get_channel_index("emigrations")]++; - } +void AnalysisModule::update_calculated_stats_for_person(RuntimeContext &context, const Person &person, size_t index){ + calculated_stats_[index + get_channel_index("count")]++; - continue; + for (const auto &factor : context.mapping().entries()) { + double value = person.get_risk_factor_value(factor.key()); + calculated_stats_[index + get_channel_index("mean_" + factor.key().to_string())] += + value; + } + + for (const auto &[disease_name, disease_state] : person.diseases) { + if (disease_state.status == DiseaseStatus::active) { + calculated_stats_[index + + get_channel_index("prevalence_" + disease_name.to_string())]++; + if (disease_state.start_time == context.time_now()) { + calculated_stats_[index + + get_channel_index("incidence_" + disease_name.to_string())]++; + } } + } +} - calculated_stats_[index + get_channel_index("count")]++; +void AnalysisModule::calculate_population_statistics(RuntimeContext &context) { - for (const auto &factor : context.mapping().entries()) { - double value = person.get_risk_factor_value(factor.key()); - calculated_stats_[index + get_channel_index("mean_" + factor.key().to_string())] += - value; - } + for (const auto &person : context.population()) { + // First let's fetch the correct `calculated_stats_` bin index for this person + size_t index = calculate_index(person); - for (const auto &[disease_name, disease_state] : person.diseases) { - if (disease_state.status == DiseaseStatus::active) { - calculated_stats_[index + - get_channel_index("prevalence_" + disease_name.to_string())]++; - if (disease_state.start_time == context.time_now()) { - calculated_stats_[index + - get_channel_index("incidence_" + disease_name.to_string())]++; - } - } + if (!person.is_active()) { + update_death_and_migration_stats(person, index, context); + continue; } + update_calculated_stats_for_person(context, person, index); + double dw = calculate_disability_weight(person); double yld = dw * DALY_UNITS; calculated_stats_[index + get_channel_index("mean_yld")] += yld; diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index 183bb509f..03597938b 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -66,6 +66,8 @@ class AnalysisModule final : public UpdatableModule { double calculate_disability_weight(const Person &entity) const; DALYsIndicator calculate_dalys(Population &population, unsigned int max_age, unsigned int death_year) const; + void update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context); + void update_calculated_stats_for_person(RuntimeContext &context, const Person &person, size_t index); void calculate_population_statistics(RuntimeContext &context); void calculate_population_statistics(RuntimeContext &context, DataSeries &series) const; From 155757cdd3ac00c920bb54d592a84d7df32c29ec Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 17 Jul 2024 18:24:06 +0100 Subject: [PATCH 5/9] Simplify finding channel index --- src/HealthGPS/analysis_module.cpp | 63 +++++++++++++------------------ src/HealthGPS/analysis_module.h | 5 --- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index 0235a9265..6082e6cfb 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -322,35 +322,35 @@ void AnalysisModule::update_death_and_migration_stats(const Person &person, size auto current_time = static_cast(context.time_now()); if (!person.is_alive() && person.time_of_death() == context.time_now()) { - calculated_stats_[index + get_channel_index("deaths")]++; + calculated_stats_[index + channel_index_.at("deaths")]++; float expected_life = definition_.life_expectancy().at(context.time_now(), person.gender); double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; - calculated_stats_[index + get_channel_index("mean_yll")] += yll; - calculated_stats_[index + get_channel_index("mean_daly")] += yll; + calculated_stats_[index + channel_index_.at("mean_yll")] += yll; + calculated_stats_[index + channel_index_.at("mean_daly")] += yll; } if (person.has_emigrated() && person.time_of_migration() == context.time_now()) { - calculated_stats_[index + get_channel_index("emigrations")]++; + calculated_stats_[index + channel_index_.at("emigrations")]++; } } void AnalysisModule::update_calculated_stats_for_person(RuntimeContext &context, const Person &person, size_t index){ - calculated_stats_[index + get_channel_index("count")]++; + calculated_stats_[index + channel_index_.at("count")]++; for (const auto &factor : context.mapping().entries()) { double value = person.get_risk_factor_value(factor.key()); - calculated_stats_[index + get_channel_index("mean_" + factor.key().to_string())] += + calculated_stats_[index + channel_index_.at("mean_" + factor.key().to_string())] += value; } for (const auto &[disease_name, disease_state] : person.diseases) { if (disease_state.status == DiseaseStatus::active) { calculated_stats_[index + - get_channel_index("prevalence_" + disease_name.to_string())]++; + channel_index_.at("prevalence_" + disease_name.to_string())]++; if (disease_state.start_time == context.time_now()) { calculated_stats_[index + - get_channel_index("incidence_" + disease_name.to_string())]++; + channel_index_.at("incidence_" + disease_name.to_string())]++; } } } @@ -371,41 +371,41 @@ void AnalysisModule::calculate_population_statistics(RuntimeContext &context) { double dw = calculate_disability_weight(person); double yld = dw * DALY_UNITS; - calculated_stats_[index + get_channel_index("mean_yld")] += yld; - calculated_stats_[index + get_channel_index("mean_daly")] += yld; + calculated_stats_[index + channel_index_.at("mean_yld")] += yld; + calculated_stats_[index + channel_index_.at("mean_daly")] += yld; classify_weight(person); } // For each bin in the calculated stats... for (size_t i = 0; i < calculated_stats_.size(); i += channels_.size()) { - double count_F = calculated_stats_[i + get_channel_index("count")]; - double count_M = calculated_stats_[i + get_channel_index("count")]; - double deaths_F = calculated_stats_[i + get_channel_index("deaths")]; - double deaths_M = calculated_stats_[i + get_channel_index("deaths")]; + double count_F = calculated_stats_[i + channel_index_.at("count")]; + double count_M = calculated_stats_[i + channel_index_.at("count")]; + double deaths_F = calculated_stats_[i + channel_index_.at("deaths")]; + double deaths_M = calculated_stats_[i + channel_index_.at("deaths")]; // Calculate in-place factor averages. for (const auto &factor : context.mapping().entries()) { - calculated_stats_[i + get_channel_index("mean_" + factor.key().to_string())] /= count_F; - calculated_stats_[i + get_channel_index("mean_" + factor.key().to_string())] /= count_M; + calculated_stats_[i + channel_index_.at("mean_" + factor.key().to_string())] /= count_F; + calculated_stats_[i + channel_index_.at("mean_" + factor.key().to_string())] /= count_M; } // Calculate in-place disease prevalence and incidence rates. for (const auto &disease : context.diseases()) { - calculated_stats_[i + get_channel_index("prevalence_" + disease.code.to_string())] /= + calculated_stats_[i + channel_index_.at("prevalence_" + disease.code.to_string())] /= count_F; - calculated_stats_[i + get_channel_index("prevalence_" + disease.code.to_string())] /= + calculated_stats_[i + channel_index_.at("prevalence_" + disease.code.to_string())] /= count_M; - calculated_stats_[i + get_channel_index("incidence_" + disease.code.to_string())] /= + calculated_stats_[i + channel_index_.at("incidence_" + disease.code.to_string())] /= count_F; - calculated_stats_[i + get_channel_index("incidence_" + disease.code.to_string())] /= + calculated_stats_[i + channel_index_.at("incidence_" + disease.code.to_string())] /= count_M; } // Calculate in-place YLL/YLD/DALY averages. for (const auto &column : {"mean_yll", "mean_yld", "mean_daly"}) { - calculated_stats_[i + get_channel_index(column)] /= (count_F + deaths_F); - calculated_stats_[i + get_channel_index(column)] /= (count_M + deaths_M); + calculated_stats_[i + channel_index_.at(column)] /= (count_F + deaths_F); + calculated_stats_[i + channel_index_.at(column)] /= (count_M + deaths_M); } } } @@ -593,15 +593,15 @@ void AnalysisModule::classify_weight(const Person &person) { auto weight_class = weight_classifier_.classify_weight(person); switch (weight_class) { case WeightCategory::normal: - calculated_stats_[get_channel_index("normal_weight")]++; + calculated_stats_[channel_index_.at("normal_weight")]++; break; case WeightCategory::overweight: - calculated_stats_[get_channel_index("over_weight")]++; - calculated_stats_[get_channel_index("above_weight")]++; + calculated_stats_[channel_index_.at("over_weight")]++; + calculated_stats_[channel_index_.at("above_weight")]++; break; case WeightCategory::obese: - calculated_stats_[get_channel_index("obese_weight")]++; - calculated_stats_[get_channel_index("above_weight")]++; + calculated_stats_[channel_index_.at("obese_weight")]++; + calculated_stats_[channel_index_.at("above_weight")]++; break; default: throw std::logic_error("Unknown weight classification category."); @@ -669,15 +669,6 @@ size_t AnalysisModule::calculate_index(const Person &person) const { return index; } -size_t AnalysisModule::get_channel_index(const std::string &channel) const { - auto it = channel_index_.find(channel); - if (it == channel_index_.end()) { - throw std::out_of_range("Unknown channel: " + channel); - } - - return it->second; -} - std::unique_ptr build_analysis_module(Repository &repository, const ModelInput &config) { auto analysis_entity = repository.manager().get_disease_analysis(config.settings().country()); diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index 03597938b..12a1d55e3 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -81,11 +81,6 @@ class AnalysisModule final : public UpdatableModule { /// @return The index in `calculated_stats_` size_t calculate_index(const Person &person) const; - /// @brief Gets the index of the given channel name - /// @param channel The channel name - /// @return The channel index - size_t get_channel_index(const std::string &channel) const; - /// @brief Calculates the standard deviation of factors given data series containing means /// @param context The runtime context /// @param series The data series containing factor means From f915dcc1b3781c2f0574c7dfdf5cf0d5d479fd10 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Wed, 17 Jul 2024 23:55:55 +0100 Subject: [PATCH 6/9] Remove unreachable throw --- src/HealthGPS/analysis_module.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index 6082e6cfb..ae1c9b4d7 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -605,7 +605,6 @@ void AnalysisModule::classify_weight(const Person &person) { break; default: throw std::logic_error("Unknown weight classification category."); - break; } } From 88ca8cef63c7375dacfc0be17fe324733440820f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:56:26 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/HealthGPS/analysis_module.cpp | 18 +++++++++--------- src/HealthGPS/analysis_module.h | 6 ++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index ae1c9b4d7..9fd55de1a 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -318,13 +318,13 @@ DALYsIndicator AnalysisModule::calculate_dalys(Population &population, unsigned .disability_adjusted_life_years = yll + yld}; } -void AnalysisModule::update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context){ +void AnalysisModule::update_death_and_migration_stats(const Person &person, size_t index, + RuntimeContext &context) { auto current_time = static_cast(context.time_now()); if (!person.is_alive() && person.time_of_death() == context.time_now()) { calculated_stats_[index + channel_index_.at("deaths")]++; - float expected_life = - definition_.life_expectancy().at(context.time_now(), person.gender); + float expected_life = definition_.life_expectancy().at(context.time_now(), person.gender); double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; calculated_stats_[index + channel_index_.at("mean_yll")] += yll; calculated_stats_[index + channel_index_.at("mean_daly")] += yll; @@ -335,25 +335,25 @@ void AnalysisModule::update_death_and_migration_stats(const Person &person, size } } -void AnalysisModule::update_calculated_stats_for_person(RuntimeContext &context, const Person &person, size_t index){ +void AnalysisModule::update_calculated_stats_for_person(RuntimeContext &context, + const Person &person, size_t index) { calculated_stats_[index + channel_index_.at("count")]++; for (const auto &factor : context.mapping().entries()) { double value = person.get_risk_factor_value(factor.key()); - calculated_stats_[index + channel_index_.at("mean_" + factor.key().to_string())] += - value; + calculated_stats_[index + channel_index_.at("mean_" + factor.key().to_string())] += value; } for (const auto &[disease_name, disease_state] : person.diseases) { if (disease_state.status == DiseaseStatus::active) { calculated_stats_[index + - channel_index_.at("prevalence_" + disease_name.to_string())]++; + channel_index_.at("prevalence_" + disease_name.to_string())]++; if (disease_state.start_time == context.time_now()) { calculated_stats_[index + - channel_index_.at("incidence_" + disease_name.to_string())]++; + channel_index_.at("incidence_" + disease_name.to_string())]++; } } - } + } } void AnalysisModule::calculate_population_statistics(RuntimeContext &context) { diff --git a/src/HealthGPS/analysis_module.h b/src/HealthGPS/analysis_module.h index 12a1d55e3..c850a82be 100644 --- a/src/HealthGPS/analysis_module.h +++ b/src/HealthGPS/analysis_module.h @@ -66,8 +66,10 @@ class AnalysisModule final : public UpdatableModule { double calculate_disability_weight(const Person &entity) const; DALYsIndicator calculate_dalys(Population &population, unsigned int max_age, unsigned int death_year) const; - void update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context); - void update_calculated_stats_for_person(RuntimeContext &context, const Person &person, size_t index); + void update_death_and_migration_stats(const Person &person, size_t index, + RuntimeContext &context); + void update_calculated_stats_for_person(RuntimeContext &context, const Person &person, + size_t index); void calculate_population_statistics(RuntimeContext &context); void calculate_population_statistics(RuntimeContext &context, DataSeries &series) const; From acf8067da60d0a15c4b0e0a36fe373f227b06aa7 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Thu, 18 Jul 2024 00:09:45 +0100 Subject: [PATCH 8/9] Remove unused variable --- src/HealthGPS/analysis_module.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index 9fd55de1a..3dc693fe0 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -320,7 +320,6 @@ DALYsIndicator AnalysisModule::calculate_dalys(Population &population, unsigned void AnalysisModule::update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context) { - auto current_time = static_cast(context.time_now()); if (!person.is_alive() && person.time_of_death() == context.time_now()) { calculated_stats_[index + channel_index_.at("deaths")]++; From 52e23d94584702450cf60618332ebd9f192cfdb7 Mon Sep 17 00:00:00 2001 From: TinyMarsh Date: Thu, 18 Jul 2024 00:12:41 +0100 Subject: [PATCH 9/9] static_cast to avoid comparing integers of different signs --- src/HealthGPS/analysis_module.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/HealthGPS/analysis_module.cpp b/src/HealthGPS/analysis_module.cpp index 3dc693fe0..91962d796 100644 --- a/src/HealthGPS/analysis_module.cpp +++ b/src/HealthGPS/analysis_module.cpp @@ -321,7 +321,9 @@ DALYsIndicator AnalysisModule::calculate_dalys(Population &population, unsigned void AnalysisModule::update_death_and_migration_stats(const Person &person, size_t index, RuntimeContext &context) { - if (!person.is_alive() && person.time_of_death() == context.time_now()) { + auto current_time = static_cast(context.time_now()); + + if (!person.is_alive() && person.time_of_death() == current_time) { calculated_stats_[index + channel_index_.at("deaths")]++; float expected_life = definition_.life_expectancy().at(context.time_now(), person.gender); double yll = std::max(expected_life - person.age, 0.0f) * DALY_UNITS; @@ -329,7 +331,7 @@ void AnalysisModule::update_death_and_migration_stats(const Person &person, size calculated_stats_[index + channel_index_.at("mean_daly")] += yll; } - if (person.has_emigrated() && person.time_of_migration() == context.time_now()) { + if (person.has_emigrated() && person.time_of_migration() == current_time) { calculated_stats_[index + channel_index_.at("emigrations")]++; } }