|
| 1 | +############################################################################# |
| 2 | +# Copyright (C) 2020-2023 German Aerospace Center (DLR-SC) |
| 3 | +# |
| 4 | +# Authors: Agatha Schmidt, Henrik Zunker |
| 5 | +# |
| 6 | +# Contact: Martin J. Kuehn <[email protected]> |
| 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 | +import copy |
| 21 | +import os |
| 22 | +import pickle |
| 23 | +import random |
| 24 | +from datetime import date |
| 25 | + |
| 26 | +import numpy as np |
| 27 | +import pandas as pd |
| 28 | +import tensorflow as tf |
| 29 | +from progress.bar import Bar |
| 30 | +from sklearn.preprocessing import FunctionTransformer |
| 31 | + |
| 32 | +from memilio.simulation import (ContactMatrix, Damping, LogLevel, |
| 33 | + UncertainContactMatrix, set_log_level) |
| 34 | +from memilio.simulation.secir import (AgeGroup, Index_InfectionState, |
| 35 | + InfectionState, Model, Simulation, |
| 36 | + interpolate_simulation_result, simulate) |
| 37 | + |
| 38 | + |
| 39 | +def run_secir_simulation(days): |
| 40 | + """! Uses an ODE SECIR model allowing for asymptomatic infection. The model is not stratified by region or demographic properties such as age. |
| 41 | + Virus-specific parameters are fixed and initial number of persons in the particular infection states are chosen randomly from defined ranges. |
| 42 | +
|
| 43 | + @param Days Describes how many days we simulate within a single run. |
| 44 | + @return List containing the populations in each compartment for each day of the simulation. |
| 45 | + """ |
| 46 | + set_log_level(LogLevel.Off) |
| 47 | + |
| 48 | + populations = [50_000] |
| 49 | + start_day = 1 |
| 50 | + start_month = 1 |
| 51 | + start_year = 2019 |
| 52 | + dt = 0.1 |
| 53 | + num_groups = 1 |
| 54 | + |
| 55 | + # Initialize Parameters |
| 56 | + model = Model(1) |
| 57 | + |
| 58 | + A0 = AgeGroup(0) |
| 59 | + |
| 60 | + # Set parameters |
| 61 | + # Compartment transition duration |
| 62 | + model.parameters.IncubationTime[A0] = 5.2 |
| 63 | + model.parameters.TimeInfectedSymptoms[A0] = 6. |
| 64 | + model.parameters.SerialInterval[A0] = 4.2 |
| 65 | + model.parameters.TimeInfectedSevere[A0] = 12. |
| 66 | + model.parameters.TimeInfectedCritical[A0] = 8. |
| 67 | + |
| 68 | + # Initial number of people in each compartment with random numbers |
| 69 | + model.populations[A0, Index_InfectionState( |
| 70 | + InfectionState.Exposed)] = 60 * random.uniform(0.2, 1) |
| 71 | + model.populations[A0, Index_InfectionState( |
| 72 | + InfectionState.InfectedNoSymptoms)] = 55 * random.uniform(0.2, 1) |
| 73 | + model.populations[A0, Index_InfectionState( |
| 74 | + InfectionState.InfectedSymptoms)] = 50 * random.uniform(0.2, 1) |
| 75 | + model.populations[A0, Index_InfectionState( |
| 76 | + InfectionState.InfectedSevere)] = 12 * random.uniform(0.2, 1) |
| 77 | + model.populations[A0, Index_InfectionState( |
| 78 | + InfectionState.InfectedCritical)] = 3 * random.uniform(0.2, 1) |
| 79 | + model.populations[A0, Index_InfectionState( |
| 80 | + InfectionState.Recovered)] = 50 * random.random() |
| 81 | + model.populations[A0, Index_InfectionState(InfectionState.Dead)] = 0 |
| 82 | + model.populations.set_difference_from_total( |
| 83 | + (A0, Index_InfectionState(InfectionState.Susceptible)), populations[0]) |
| 84 | + |
| 85 | + # Compartment transition propabilities |
| 86 | + model.parameters.RelativeTransmissionNoSymptoms[A0] = 0.5 |
| 87 | + model.parameters.TransmissionProbabilityOnContact[A0] = 0.1 |
| 88 | + model.parameters.RecoveredPerInfectedNoSymptoms[A0] = 0.09 |
| 89 | + model.parameters.RiskOfInfectionFromSymptomatic[A0] = 0.25 |
| 90 | + model.parameters.SeverePerInfectedSymptoms[A0] = 0.2 |
| 91 | + model.parameters.CriticalPerSevere[A0] = 0.25 |
| 92 | + model.parameters.DeathsPerCritical[A0] = 0.3 |
| 93 | + # twice the value of RiskOfInfectionFromSymptomatic |
| 94 | + model.parameters.MaxRiskOfInfectionFromSymptomatic[A0] = 0.5 |
| 95 | + |
| 96 | + model.parameters.StartDay = ( |
| 97 | + date(start_year, start_month, start_day) - date(start_year, 1, 1)).days |
| 98 | + |
| 99 | + model.parameters.ContactPatterns.cont_freq_mat[0].baseline = np.ones( |
| 100 | + (num_groups, num_groups)) * 10 |
| 101 | + model.parameters.ContactPatterns.cont_freq_mat[0].minimum = np.ones( |
| 102 | + (num_groups, num_groups)) * 0 |
| 103 | + |
| 104 | + # Apply mathematical constraints to parameters |
| 105 | + model.apply_constraints() |
| 106 | + |
| 107 | + # Run Simulation |
| 108 | + result = simulate(0, days, dt, model) |
| 109 | + # Interpolate simulation result on days time scale |
| 110 | + result = interpolate_simulation_result(result) |
| 111 | + |
| 112 | + # Using an array instead of a list to avoid problems with references |
| 113 | + result_array = result.as_ndarray() |
| 114 | + dataset = [] |
| 115 | + # Omit first column, as the time points are not of interest here. |
| 116 | + dataset_entries = copy.deepcopy(result_array[1:, :].transpose()) |
| 117 | + |
| 118 | + return dataset_entries.tolist() |
| 119 | + |
| 120 | + |
| 121 | +def generate_data( |
| 122 | + num_runs, path, input_width, label_width, normalize=True, |
| 123 | + save_data=True): |
| 124 | + """! Generate data sets of num_runs many equation-based model simulations and transforms the computed results by a log(1+x) transformation. |
| 125 | + Divides the results in input and label data sets and returns them as a dictionary of two TensorFlow Stacks. |
| 126 | +
|
| 127 | + In general, we have 8 different compartments. If we choose, |
| 128 | + input_width = 5 and label_width = 20, the dataset has |
| 129 | + - input with dimension 5 x 8 |
| 130 | + - labels with dimension 20 x 8 |
| 131 | +
|
| 132 | + @param num_runs Number of times, the function run_secir_simulation is called. |
| 133 | + @param path Path, where the dataset is saved to. |
| 134 | + @param input_width Int value that defines the number of time series used for the input. |
| 135 | + @param label_width Int value that defines the size of the labels. |
| 136 | + @param normalize [Default: true] Option to transform dataset by logarithmic normalization. |
| 137 | + @param save_data [Default: true] Option to save the dataset. |
| 138 | + @return Data dictionary of input and label data sets. |
| 139 | + """ |
| 140 | + data = { |
| 141 | + "inputs": [], |
| 142 | + "labels": [] |
| 143 | + } |
| 144 | + |
| 145 | + # The number of days is the same as the sum of input and label width. |
| 146 | + # Since the first day of the input is day 0, we still need to subtract 1. |
| 147 | + days = input_width + label_width - 1 |
| 148 | + |
| 149 | + # show progess in terminal for longer runs |
| 150 | + # Due to the random structure, theres currently no need to shuffle the data |
| 151 | + bar = Bar('Number of Runs done', max=num_runs) |
| 152 | + for _ in range(0, num_runs): |
| 153 | + data_run = run_secir_simulation(days) |
| 154 | + data['inputs'].append(data_run[:input_width]) |
| 155 | + data['labels'].append(data_run[input_width:]) |
| 156 | + bar.next() |
| 157 | + bar.finish() |
| 158 | + |
| 159 | + if normalize: |
| 160 | + # logarithmic normalization |
| 161 | + transformer = FunctionTransformer(np.log1p, validate=True) |
| 162 | + inputs = np.asarray(data['inputs']).transpose(2, 0, 1).reshape(8, -1) |
| 163 | + scaled_inputs = transformer.transform(inputs) |
| 164 | + scaled_inputs = scaled_inputs.transpose().reshape(num_runs, input_width, 8) |
| 165 | + scaled_inputs_list = scaled_inputs.tolist() |
| 166 | + |
| 167 | + labels = np.asarray(data['labels']).transpose(2, 0, 1).reshape(8, -1) |
| 168 | + scaled_labels = transformer.transform(labels) |
| 169 | + scaled_labels = scaled_labels.transpose().reshape(num_runs, label_width, 8) |
| 170 | + scaled_labels_list = scaled_labels.tolist() |
| 171 | + |
| 172 | + # cast dfs to tensors |
| 173 | + data['inputs'] = tf.stack(scaled_inputs_list) |
| 174 | + data['labels'] = tf.stack(scaled_labels_list) |
| 175 | + |
| 176 | + if save_data: |
| 177 | + # check if data directory exists. If necessary, create it. |
| 178 | + if not os.path.isdir(path): |
| 179 | + os.mkdir(path) |
| 180 | + |
| 181 | + # save dict to json file |
| 182 | + with open(os.path.join(path, 'data_secir_simple.pickle'), 'wb') as f: |
| 183 | + pickle.dump(data, f) |
| 184 | + return data |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + # Store data relative to current file two levels higher. |
| 189 | + path = os.path.dirname(os.path.realpath(__file__)) |
| 190 | + path_data = os.path.join(os.path.dirname(os.path.realpath( |
| 191 | + os.path.dirname(os.path.realpath(path)))), 'data') |
| 192 | + |
| 193 | + input_width = 5 |
| 194 | + label_width = 30 |
| 195 | + num_runs = 1000 |
| 196 | + data = generate_data(num_runs, path_data, input_width, |
| 197 | + label_width) |
0 commit comments