LCOV - code coverage report
Current view: top level - models/ode_secir - analyze_result.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 76 76 100.0 %
Date: 2024-11-18 12:45:26 Functions: 52 52 100.0 %

          Line data    Source code
       1             : /* 
       2             : * Copyright (C) 2020-2024 MEmilio
       3             : *
       4             : * Authors: Daniel Abele
       5             : *
       6             : * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
       7             : *
       8             : * Licensed under the Apache License, Version 2.0 (the "License");
       9             : * you may not use this file except in compliance with the License.
      10             : * You may obtain a copy of the License at
      11             : *
      12             : *     http://www.apache.org/licenses/LICENSE-2.0
      13             : *
      14             : * Unless required by applicable law or agreed to in writing, software
      15             : * distributed under the License is distributed on an "AS IS" BASIS,
      16             : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      17             : * See the License for the specific language governing permissions and
      18             : * limitations under the License.
      19             : */
      20             : #ifndef ODESECIR_ANALYZE_RESULT_H
      21             : #define ODESECIR_ANALYZE_RESULT_H
      22             : 
      23             : #include "ode_secir/infection_state.h"
      24             : #include "ode_secir/parameters.h"
      25             : 
      26             : namespace mio
      27             : {
      28             : 
      29             : namespace osecir
      30             : {
      31             : 
      32             : /**
      33             :  * @brief computes the p percentile of the parameters for each node.
      34             :  * @param ensemble_result graph of multiple simulation runs
      35             :  * @param p percentile value in open interval (0, 1)
      36             :  * @return p percentile of the parameters over all runs
      37             :  */
      38             : template <class Model>
      39          19 : std::vector<Model> ensemble_params_percentile(const std::vector<std::vector<Model>>& ensemble_params, double p)
      40             : {
      41          19 :     assert(p > 0.0 && p < 1.0 && "Invalid percentile value.");
      42             : 
      43          19 :     auto num_runs   = ensemble_params.size();
      44          19 :     auto num_nodes  = ensemble_params[0].size();
      45          19 :     auto num_groups = (size_t)ensemble_params[0][0].parameters.get_num_groups();
      46             : 
      47          38 :     std::vector<double> single_element_ensemble(num_runs);
      48             : 
      49             :     // lambda function that calculates the percentile of a single parameter
      50          57 :     std::vector<Model> percentile(num_nodes, Model((int)num_groups));
      51       29375 :     auto param_percentil = [&ensemble_params, p, num_runs, &percentile](auto n, auto get_param) mutable {
      52        6276 :         std::vector<double> single_element(num_runs);
      53        9088 :         for (size_t run = 0; run < num_runs; run++) {
      54        9808 :             auto const& params  = ensemble_params[run][n];
      55        4904 :             single_element[run] = get_param(params);
      56             :         }
      57        2092 :         std::sort(single_element.begin(), single_element.end());
      58        4184 :         auto& new_params = get_param(percentile[n]);
      59        4184 :         new_params       = single_element[static_cast<size_t>(num_runs * p)];
      60        2092 :     };
      61             : 
      62          57 :     for (size_t node = 0; node < num_nodes; node++) {
      63         124 :         for (auto i = AgeGroup(0); i < AgeGroup(num_groups); i++) {
      64             :             //Population
      65         946 :             for (size_t compart = 0; compart < (size_t)InfectionState::Count; ++compart) {
      66         860 :                 param_percentil(
      67        2880 :                     node, [ compart, i ](auto&& model) -> auto& {
      68        5760 :                         return model.populations[{i, (InfectionState)compart}];
      69             :                     });
      70             :             }
      71             :             // times
      72          86 :             param_percentil(
      73         288 :                 node, [i](auto&& model) -> auto& { return model.parameters.template get<TimeExposed<double>>()[i]; });
      74          86 :             param_percentil(
      75         288 :                 node, [i](auto&& model) -> auto& {
      76         288 :                     return model.parameters.template get<TimeInfectedNoSymptoms<double>>()[i];
      77             :                 });
      78          86 :             param_percentil(
      79         288 :                 node, [i](auto&& model) -> auto& {
      80         288 :                     return model.parameters.template get<TimeInfectedSymptoms<double>>()[i];
      81             :                 });
      82          86 :             param_percentil(
      83         288 :                 node, [i](auto&& model) -> auto& {
      84         288 :                     return model.parameters.template get<TimeInfectedSevere<double>>()[i];
      85             :                 });
      86          86 :             param_percentil(
      87         288 :                 node, [i](auto&& model) -> auto& {
      88         288 :                     return model.parameters.template get<TimeInfectedCritical<double>>()[i];
      89             :                 });
      90             :             //probs
      91          86 :             param_percentil(
      92         288 :                 node, [i](auto&& model) -> auto& {
      93         288 :                     return model.parameters.template get<TransmissionProbabilityOnContact<double>>()[i];
      94             :                 });
      95          86 :             param_percentil(
      96         288 :                 node, [i](auto&& model) -> auto& {
      97         288 :                     return model.parameters.template get<RelativeTransmissionNoSymptoms<double>>()[i];
      98             :                 });
      99          86 :             param_percentil(
     100         288 :                 node, [i](auto&& model) -> auto& {
     101         288 :                     return model.parameters.template get<RiskOfInfectionFromSymptomatic<double>>()[i];
     102             :                 });
     103          86 :             param_percentil(
     104         288 :                 node, [i](auto&& model) -> auto& {
     105         288 :                     return model.parameters.template get<MaxRiskOfInfectionFromSymptomatic<double>>()[i];
     106             :                 });
     107          86 :             param_percentil(
     108         288 :                 node, [i](auto&& model) -> auto& {
     109         288 :                     return model.parameters.template get<RecoveredPerInfectedNoSymptoms<double>>()[i];
     110             :                 });
     111          86 :             param_percentil(
     112         288 :                 node, [i](auto&& model) -> auto& {
     113         288 :                     return model.parameters.template get<SeverePerInfectedSymptoms<double>>()[i];
     114             :                 });
     115          86 :             param_percentil(
     116         288 :                 node, [i](auto&& model) -> auto& {
     117         288 :                     return model.parameters.template get<CriticalPerSevere<double>>()[i];
     118             :                 });
     119          86 :             param_percentil(
     120         288 :                 node, [i](auto&& model) -> auto& {
     121         288 :                     return model.parameters.template get<DeathsPerCritical<double>>()[i];
     122             :                 });
     123             :         }
     124             :         // group independent params
     125          38 :         param_percentil(
     126         124 :             node, [](auto&& model) -> auto& { return model.parameters.template get<Seasonality<double>>(); });
     127          38 :         param_percentil(
     128         124 :             node, [](auto&& model) -> auto& { return model.parameters.template get<TestAndTraceCapacity<double>>(); });
     129          38 :         param_percentil(
     130         124 :             node, [](auto&& model) -> auto& {
     131         124 :                 return model.parameters.template get<DynamicNPIsImplementationDelay<double>>();
     132             :             });
     133             : 
     134         124 :         for (size_t run = 0; run < num_runs; run++) {
     135          86 :             auto const& params = ensemble_params[run][node];
     136          86 :             single_element_ensemble[run] =
     137          86 :                 params.parameters.template get<ICUCapacity<double>>() * params.populations.get_total();
     138             :         }
     139          38 :         std::sort(single_element_ensemble.begin(), single_element_ensemble.end());
     140          76 :         percentile[node].parameters.template set<ICUCapacity<double>>(
     141          38 :             single_element_ensemble[static_cast<size_t>(num_runs * p)]);
     142             :     }
     143          38 :     return percentile;
     144          19 : }
     145             : 
     146             : } // namespace osecir
     147             : } // namespace mio
     148             : 
     149             : #endif //ODESECIR_ANALYZE_RESULT_H

Generated by: LCOV version 1.14