transform_SIR.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import numpy as np
  2. import pandas as pd
  3. from src.plotter import Plotter
  4. def transform_general_to_SIR(plotter:Plotter, dataset_path='datasets/COVID-19-Todesfaelle_in_Deutschland/', plot_name='', plot_title='', sample_rate=1, exclude=[], plot_size=(12,6), yscale_log=False, plot_legend=True):
  5. """Function to generate the SIR split from the data in the COVID-19-Todesfaelle_in_Deutschland dataset.
  6. """
  7. # read the data
  8. df = pd.read_csv(dataset_path + 'COVID-19-Todesfaelle_Deutschland.csv')
  9. df = df.drop(df.index[1200:])
  10. # population of germany at the end of 2019
  11. N = 83100000
  12. S, I, R = np.zeros(df.shape[0]), np.zeros(df.shape[0]), np.zeros(df.shape[0])
  13. # S_0 = N - I_0
  14. S[0] = N - df['Faelle_gesamt'][0]
  15. # I_0 = overall cases at the day - overall death cases at the day
  16. I[0] = df['Faelle_gesamt'][0] - df['Todesfaelle_gesamt'][0]
  17. # R_0 = overall death cases at the day
  18. R[0] = df['Todesfaelle_gesamt'][0]
  19. # the recovery time is 14 days
  20. recovery_queue = np.zeros(14)
  21. for day in range(1, df.shape[0]):
  22. infections = df['Faelle_gesamt'][day] - df['Faelle_gesamt'][day-1]
  23. deaths = df['Todesfaelle_neu'][day]
  24. recoveries = recovery_queue[0]
  25. S[day] = S[day-1] - infections
  26. I[day] = I[day-1] + infections - deaths - recoveries
  27. R[day] = R[day-1] + deaths + recoveries
  28. # update recovery queue
  29. if I[day] < 0:
  30. recovery_queue[-1] -= I[day]
  31. I[day] = 0
  32. recovery_queue[:-1] = recovery_queue[1:]
  33. recovery_queue[-1] = infections
  34. t = np.arange(0, df.shape[0], 1)
  35. if plotter != None:
  36. # plot graphs
  37. plots = []
  38. labels = []
  39. if 'S' not in exclude:
  40. plots.append(S)
  41. labels.append('S')
  42. if 'I' not in exclude:
  43. plots.append(I)
  44. labels.append('I')
  45. if 'R' not in exclude:
  46. plots.append(R)
  47. labels.append('R')
  48. plotter.plot(t, plots, labels, plot_name, plot_title, plot_size, y_log_scale=yscale_log, plot_legend=plot_legend, xlabel='time / days', ylabel='amount of poeple')
  49. COVID_Data = np.asarray([t[0::sample_rate],
  50. S[0::sample_rate],
  51. I[0::sample_rate],
  52. R[0::sample_rate]])
  53. np.savetxt(f"datasets/SIR_RKI_{sample_rate}.csv", COVID_Data, delimiter=",")
  54. def state_based_data(dataset_path='datasets/state_data/Aktuell_Deutschland_SarsCov2_Infektionen.csv/'):
  55. pass