From a572e0d4479fc290c36943bffec31354077c99e7 Mon Sep 17 00:00:00 2001 From: Sven Willner Date: Fri, 7 Jul 2017 20:35:31 +0200 Subject: [PATCH] fixed csv parser and minor style adjustments --- include/Disaggregation.h | 2 +- lib/cpp-library | 2 +- src/Disaggregation.cpp | 153 +++++++++++++++++++-------------------- 3 files changed, 77 insertions(+), 80 deletions(-) diff --git a/include/Disaggregation.h b/include/Disaggregation.h index 1563219..9850239 100644 --- a/include/Disaggregation.h +++ b/include/Disaggregation.h @@ -117,7 +117,7 @@ class Disaggregation { void approximate(const int& d); void adjust(const int& d); - void read_proxy_file(const std::string& filename, const int d, const int year); + void read_proxy_file(const std::string& filename, int d, I year); public: Disaggregation(const Table* basetable_p); diff --git a/lib/cpp-library b/lib/cpp-library index 05c0b02..22db0cf 160000 --- a/lib/cpp-library +++ b/lib/cpp-library @@ -1 +1 @@ -Subproject commit 05c0b02edad6a91574e5175fe774404523a614c3 +Subproject commit 22db0cf7c3cccafe6eee31fe3d2cdf2156efb6f1 diff --git a/src/Disaggregation.cpp b/src/Disaggregation.cpp index 199c1b2..e15f9d2 100644 --- a/src/Disaggregation.cpp +++ b/src/Disaggregation.cpp @@ -59,7 +59,7 @@ void Disaggregation::initialize(const settings::SettingsNode& settings) { } for (const auto& d : settings.as_sequence()) { for (const auto& proxy : d["proxies"].as_sequence()) { - read_proxy_file(proxy["file"].as(), proxy["level"].as(), proxy["year"].as()); + read_proxy_file(proxy["file"].as(), proxy["level"].as(), proxy["year"].as()); } } } @@ -109,7 +109,7 @@ const Region* Disaggregation::read_subregion(csv::Parser& in) { } template -void Disaggregation::read_proxy_file(const std::string& filename, const int d, const int year) { +void Disaggregation::read_proxy_file(const std::string& filename, int d, I year) { std::ifstream file(filename); if (!file) { throw std::runtime_error("Could not open proxy file"); @@ -306,13 +306,11 @@ void for_all_sub( const Region* r, const Sector* j, const Region* s, - std::function* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, const bool j_sub, const bool s_sub)> - func, - const bool i_sub = false, - const bool r_sub = false, - const bool j_sub = false, - const bool s_sub = false) { + std::function* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, bool j_sub, bool s_sub)> func, + bool i_sub = false, + bool r_sub = false, + bool j_sub = false, + bool s_sub = false) { if (i->has_sub()) { for (const auto& i_mu : i->as_super()->sub()) { for_all_sub(i_mu, r, j, s, func, true, r_sub, j_sub, s_sub); @@ -354,8 +352,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& js : table->index_set().super_indices) { for_all_sub( ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool r_sub, bool /* j_sub */, + bool s_sub) { T sum1 = 1, value1 = 1, sum2 = 1, value2 = 1; if (r_sub) { sum1 = (*proxy_sums[d])(r->parent()); @@ -395,8 +393,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& js : table->index_set().super_indices) { for_all_sub( ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool /* r_sub */, bool j_sub, + bool /* s_sub */) { T sum1 = 1, value1 = 1, sum2 = 1, value2 = 1; if (i_sub) { sum1 = (*proxy_sums[d])(i->parent(), r->super()); @@ -440,8 +438,7 @@ void Disaggregation::approximate(const int& d) { for (const auto& js : table->index_set().super_indices) { for_all_sub( ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, bool j_sub, bool s_sub) { T sum1 = 1, value1 = 1, sum2 = 1, value2 = 1; if (i_sub && r_sub) { sum1 = (*proxy_sums[d])(i->parent(), r->parent()); @@ -467,8 +464,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool /* r_sub */, + bool /* j_sub */, bool /* s_sub */) { if (i_sub) { const T& value = (*proxies[d])(i, s->super()); if (!std::isnan(value)) { @@ -487,8 +484,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool /* j_sub */, bool s_sub) { if (s_sub) { const T& value = (*proxies[d])(i->super(), s); if (!std::isnan(value)) { @@ -507,8 +504,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool /* r_sub */, + bool /* j_sub */, bool s_sub) { if (i_sub && s_sub) { const T& value = (*proxies[d])(i, s); if (!std::isnan(value)) { @@ -527,17 +524,17 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { const T& sum = basetable->basesum(ir.sector, ir.region, nullptr, nullptr); for (const auto& js : table->index_set().super_indices) { - for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { - if (i_sub && r_sub && j_sub == s_sub) { - const T& value = (*proxies[d])(i, r); - if (!std::isnan(value)) { - (*table)(i, r, j, s) = last_table->sum(i->parent(), r->parent(), j, s) * value / sum; - (*quality)(i, r, j, s) = d; - } - } - }); + for_all_sub( + ir.sector, ir.region, js.sector, js.region, + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, bool j_sub, bool s_sub) { + if (i_sub && r_sub && j_sub == s_sub) { + const T& value = (*proxies[d])(i, r); + if (!std::isnan(value)) { + (*table)(i, r, j, s) = last_table->sum(i->parent(), r->parent(), j, s) * value / sum; + (*quality)(i, r, j, s) = d; + } + } + }); } } break; @@ -547,8 +544,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool /* r_sub */, + bool /* j_sub */, bool /* s_sub */) { if (i_sub) { const T& value = (*proxies[d])(i, j->super(), s->super()); if (!std::isnan(value)) { @@ -567,8 +564,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool /* j_sub */, bool s_sub) { if (s_sub) { const T& value = (*proxies[d])(i->super(), r->super(), s); if (!std::isnan(value)) { @@ -587,8 +584,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool /* r_sub */, + bool j_sub, bool s_sub) { if (i_sub && j_sub && s_sub) { const T& value = (*proxies[d])(i, j, s); if (!std::isnan(value)) { @@ -607,8 +604,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, + bool /* j_sub */, bool /* s_sub */) { if (i_sub && r_sub) { const T& value = (*proxies[d])(i, r, s->super()); if (!std::isnan(value)) { @@ -627,8 +624,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool j_sub, bool s_sub) { if (j_sub && s_sub) { const T& value = (*proxies[d])(i->super(), j, s); if (!std::isnan(value)) { @@ -647,8 +644,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, + bool /* j_sub */, bool s_sub) { if (i_sub && r_sub && s_sub) { const T& value = (*proxies[d])(i, r, s); if (!std::isnan(value)) { @@ -667,8 +664,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, + bool /* j_sub */, bool /* s_sub */) { if (i_sub && r_sub) { const T& value1 = (*proxies[LEVEL_IMPORT_SUBSECTOR_5])(i, s->super()); const T& value2 = (*proxies[LEVEL_IMPORT_SUBSECTOR_BY_REGIONAL_SECTOR_9])(i, j->super(), s->super()); @@ -688,8 +685,8 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool j_sub, bool s_sub) { if (j_sub && s_sub) { const T& value1 = (*proxies[LEVEL_IMPORT_SUBREGION_6])(i->super(), s); const T& value2 = (*proxies[LEVEL_EXPORT_SUBREGION_10])(i->super(), r->super(), s); @@ -708,19 +705,19 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { - for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { - if (i_sub && r_sub && j_sub && s_sub) { - const T& value1 = (*proxies[LEVEL_INTERREGIONAL_SUBSECTOR_INPUT_7])(i, s); - const T& value2 = (*proxies[LEVEL_SUBREGIONAL_SUBSECTOR_INPUT_11])(i, j, s); - const T& value3 = (*proxies[LEVEL_EXPORT_SUBREGIONAL_SUBSECTOR_TO_SUBREGION_14])(i, r, s); - if (!std::isnan(value1) && !std::isnan(value2) && !std::isnan(value3)) { - (*table)(i, r, j, s) = value2 * value3 / value1; - (*quality)(i, r, j, s) = d; - } - } - }); + for_all_sub( + ir.sector, ir.region, js.sector, js.region, + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, bool j_sub, bool s_sub) { + if (i_sub && r_sub && j_sub && s_sub) { + const T& value1 = (*proxies[LEVEL_INTERREGIONAL_SUBSECTOR_INPUT_7])(i, s); + const T& value2 = (*proxies[LEVEL_SUBREGIONAL_SUBSECTOR_INPUT_11])(i, j, s); + const T& value3 = (*proxies[LEVEL_EXPORT_SUBREGIONAL_SUBSECTOR_TO_SUBREGION_14])(i, r, s); + if (!std::isnan(value1) && !std::isnan(value2) && !std::isnan(value3)) { + (*table)(i, r, j, s) = value2 * value3 / value1; + (*quality)(i, r, j, s) = d; + } + } + }); } } break; @@ -729,17 +726,17 @@ void Disaggregation::approximate(const int& d) { for (const auto& ir : table->index_set().super_indices) { for (const auto& js : table->index_set().super_indices) { - for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { - if (i_sub && r_sub && j_sub && s_sub) { - const T& value = (*proxies[d])(i, r, j, s); - if (!std::isnan(value)) { - (*table)(i, r, j, s) = value; - (*quality)(i, r, j, s) = d; - } - } - }); + for_all_sub( + ir.sector, ir.region, js.sector, js.region, + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool i_sub, bool r_sub, bool j_sub, bool s_sub) { + if (i_sub && r_sub && j_sub && s_sub) { + const T& value = (*proxies[d])(i, r, j, s); + if (!std::isnan(value)) { + (*table)(i, r, j, s) = value; + (*quality)(i, r, j, s) = d; + } + } + }); } } break; @@ -760,8 +757,8 @@ void Disaggregation::adjust(const int& d) { T sum_of_exact = 0; T sum_of_non_exact = 0; for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool /* j_sub */, bool /* s_sub */) { if ((*quality)(i, r, j, s) == d) { sum_of_exact += (*table)(i, r, j, s); } else { @@ -771,16 +768,16 @@ void Disaggregation::adjust(const int& d) { T correction_factor = base / (sum_of_exact + sum_of_non_exact); if (base > sum_of_exact && sum_of_non_exact > 0) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool /* j_sub */, bool /* s_sub */) { if ((*quality)(i, r, j, s) != d) { (*table)(i, r, j, s) = (base - sum_of_exact) * (*table)(i, r, j, s) / sum_of_non_exact; } }); } else if (correction_factor < 1 || correction_factor > 1) { for_all_sub(ir.sector, ir.region, js.sector, js.region, - [&](const Sector* i, const Region* r, const Sector* j, const Region* s, const bool i_sub, const bool r_sub, - const bool j_sub, const bool s_sub) { (*table)(i, r, j, s) = correction_factor * (*table)(i, r, j, s); }); + [&](const Sector* i, const Region* r, const Sector* j, const Region* s, bool /* i_sub */, bool /* r_sub */, + bool /* j_sub */, bool /* s_sub */) { (*table)(i, r, j, s) = correction_factor * (*table)(i, r, j, s); }); } } }