transform_SIR.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pylab as plt
  4. dataset_path = 'Covid_19_DINN/datasets/COVID-19-Todesfaelle_in_Deutschland/'
  5. def transform_general_data():
  6. """Function to generate the SIR split from the data in the COVID-19-Todesfaelle_in_Deutschland dataset.
  7. """
  8. # read the data
  9. df = pd.read_csv(dataset_path + 'COVID-19-Todesfaelle_Deutschland.csv')
  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. # plot graphs
  35. t = np.arange(0, df.shape[0], 1)
  36. plt.plot(t, S, label='Susceptible')
  37. plt.plot(t, I, label='Infectionous')
  38. plt.plot(t, R, label='Removed')
  39. plt.legend()
  40. # plt.yscale('log')
  41. plt.savefig('Covid_19_DINN/visualizations/SIR.png')
  42. def transform_state_data(state='Sachsen', N=4100000):
  43. df = pd.read_csv(dataset_path + 'COVID-19-Todesfaelle_Bundeslaender.csv')
  44. S, I, R = np.zeros(df.shape[0]), np.zeros(df.shape[0]), np.zeros(df.shape[0])
  45. S[0] = N - df['Faelle_gesamt'][0]
  46. I[0] = df['Faelle_gesamt'][0] - df['Todesfaelle_gesamt'][0]
  47. R[0] = df['Todesfaelle_gesamt'][0]
  48. recovery_queue = np.zeros(14)
  49. for day in range(1, df.shape[0]):
  50. infections = df['Faelle_gesamt'][day] - df['Faelle_gesamt'][day-1]
  51. deaths = df['Todesfaelle_neu'][day]
  52. recoveries = recovery_queue[0]
  53. S[day] = S[day-1] - infections
  54. I[day] = I[day-1] + infections - deaths - recoveries
  55. R[day] = R[day-1] + deaths + recoveries
  56. if I[day] < 0:
  57. recovery_queue[-1] -= I[day]
  58. I[day] = 0
  59. recovery_queue[:-1] = recovery_queue[1:]
  60. recovery_queue[-1] = infections
  61. t = np.arange(0, df.shape[0], 1)
  62. plt.plot(t, S, label='Susceptible')
  63. plt.plot(t, I, label='Infectionous')
  64. plt.plot(t, R, label='Removed')
  65. plt.legend()
  66. # plt.yscale('log')
  67. plt.savefig('Covid_19_DINN/visualizations/SIR.png')
  68. transform_general_data()