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

Correct calculation of size of calculated_stats_ vector #466

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 26 additions & 11 deletions src/HealthGPS/analysis_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ AnalysisModule::AnalysisModule(AnalysisDefinition &&definition, WeightModel &&cl
residual_disability_weight_{create_age_gender_table<double>(age_range)},
comorbidities_{comorbidities} {}

// Overload constructor with additional parameter for calculated_factors_
// Overload constructor with additional parameter for calculated_stats_
AnalysisModule::AnalysisModule(AnalysisDefinition &&definition, WeightModel &&classifier,
const core::IntegerInterval age_range, unsigned int comorbidities,
std::vector<double> calculated_factors)
std::vector<double> calculated_stats)
: definition_{std::move(definition)}, weight_classifier_{std::move(classifier)},
residual_disability_weight_{create_age_gender_table<double>(age_range)},
comorbidities_{comorbidities}, calculated_factors_{std::move(calculated_factors)} {}
comorbidities_{comorbidities}, calculated_stats_{std::move(calculated_stats)} {}
SimulationModuleType AnalysisModule::type() const noexcept {
return SimulationModuleType::Analysis;
}
Expand Down Expand Up @@ -59,17 +59,32 @@ void AnalysisModule::initialise_vector(RuntimeContext &context) {
factor_bin_widths_.push_back((max_factor - min_factor) / factor_bins_.back());
}

// The number of factors to calculate is the number of factors minus the length of the `factors`
// vector.
size_t num_factors_to_calc = context.mapping().entries().size() - factors_to_calculate_.size();
// The number of factors to calculate stats for is the number of factors minus the length of the
// `factors` vector.
size_t num_stats_to_calc = context.mapping().entries().size() - factors_to_calculate_.size();

// But we calculate the mean and standard deviation of each factor, so we need to multiply by 2
num_stats_to_calc *= 2;

// We also want to calculate the prevalence and incidence of each disease
num_stats_to_calc += 2 * context.diseases().size();

// We also want to keep the count, deaths, and emigrations
num_stats_to_calc += 3;

// We also want to calculate the normal, overweight, obese, and above weight prevalence
num_stats_to_calc += 4;

// Finally, we want to calculate the mean and standard deviation of YLL, YLD, and DALY
num_stats_to_calc += 6;
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like there are a lot of hardcoded numbers here... Is there a way we could pass them in as various arguments so we don't have to remember to manually change them if we add/remove factors etc.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah you're right it's a bit horrible isn't it. I'll have a think about a nicer way to do this. Eventually the other "channels" stuff will be removed and with it the reference to what the calculated stats (column headings) actually are, so it might be good to have a human-readable list somewhere perhaps stored as a std::vector<std::string> as a data member in the class or something.


// The product of the number of bins for each factor can be used to calculate the size of the
// `calculated_factors_` in the next step
// `calculated_stats_` in the next step
size_t total_num_bins =
std::accumulate(factor_bins_.cbegin(), factor_bins_.cend(), size_t{1}, std::multiplies<>());

// Set the vector size and initialise all values to 0.0
calculated_factors_.resize(total_num_bins * num_factors_to_calc);
calculated_stats_.resize(total_num_bins * num_stats_to_calc);
}

const std::string &AnalysisModule::name() const noexcept { return name_; }
Expand Down Expand Up @@ -115,7 +130,7 @@ void AnalysisModule::initialise_population(RuntimeContext &context) {
void AnalysisModule::update_population(RuntimeContext &context) {

// Reset the calculated factors vector to 0.0
std::ranges::fill(calculated_factors_, 0.0);
std::ranges::fill(calculated_stats_, 0.0);

publish_result_message(context);
}
Expand Down Expand Up @@ -328,7 +343,7 @@ void AnalysisModule::calculate_population_statistics(RuntimeContext &context) {
bin_indices.push_back(bin_index);
}

// Calculate the index in the calculated_factors_ vector
// 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 =
Expand All @@ -342,7 +357,7 @@ void AnalysisModule::calculate_population_statistics(RuntimeContext &context) {
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_factors_[index++] += person.get_risk_factor_value(factor.key());
calculated_stats_[index++] += person.get_risk_factor_value(factor.key());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HealthGPS/analysis_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AnalysisModule final : public UpdatableModule {
unsigned int comorbidities_;
std::string name_{"Analysis"};
std::vector<core::Identifier> factors_to_calculate_ = {"Gender"_id, "Age"_id};
std::vector<double> calculated_factors_;
std::vector<double> calculated_stats_;
std::vector<size_t> factor_bins_;
std::vector<double> factor_bin_widths_;
std::vector<double> factor_min_values_;
Expand Down