pandemaths.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. PandeMaths - 2020 - by psy (epsylon@riseup.net)
  5. You should have received a copy of the GNU General Public License along
  6. with PandeMaths; if not, write to the Free Software Foundation, Inc., 51
  7. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8. """
  9. VERSION = "v0.4_beta"
  10. RELEASE = "02042020"
  11. SOURCE1 = "https://code.03c8.net/epsylon/pandemaths"
  12. SOURCE2 = "https://github.com/epsylon/pandemaths"
  13. CONTACT = "epsylon@riseup.net - (https://03c8.net)"
  14. pandemic_model_variables_path = "model/pandemia.txt" # pandemia variables file
  15. extended_model_variables_path = "model/extended.txt" # extended model variables file
  16. simulation_templates_path = "templates/" # templates files
  17. reports_path = "reports/" # reports files
  18. import json, datetime, os, random, sys
  19. import matplotlib.pyplot as plt
  20. def model_maths():
  21. print("[Info] Reviewing Model ...\n")
  22. try:
  23. print(" "+"-"*5+"\n")
  24. f = open(pandemic_model_variables_path, "r")
  25. model_variables = f.readlines()
  26. f.close()
  27. for v in model_variables:
  28. print(" - "+str(v.replace("\n", "")))
  29. except:
  30. pass
  31. try:
  32. print("\n "+"-"*5+"\n")
  33. f = open(extended_model_variables_path, "r")
  34. extended_variables = f.readlines()
  35. f.close()
  36. for v in extended_variables:
  37. print(" - "+str(v.replace("\n", "")))
  38. except:
  39. pass
  40. print("\n "+"-"*5+"\n")
  41. def simulation():
  42. print("[Info] Defining ecosystem ...\n")
  43. total_population = input(" + Total population (default: 100000): ")
  44. try:
  45. total_population = int(total_population)
  46. except:
  47. total_population = 100000
  48. if not total_population:
  49. total_population = 100000
  50. starting_population = total_population
  51. infected_starting = input(" + Infected (at the beginning) population (default: 1): ")
  52. try:
  53. infected_starting = int(infected_starting)
  54. except:
  55. infected_starting = 1
  56. if not infected_starting or infected_starting < 1:
  57. infected_starting = 1
  58. infected = infected_starting
  59. print("\n "+"-"*5+"\n")
  60. print("[Info] Establishing time units ...\n")
  61. days = input(" + Number of days (default: 200): ")
  62. try:
  63. days = int(days)
  64. except:
  65. days = 200
  66. if not days:
  67. days = 200
  68. daily_rate_interaction = input(" + Daily rate of interaction between individuals (default: 2.50): ")
  69. try:
  70. daily_rate_interaction = int(daily_rate_interaction)
  71. except:
  72. daily_rate_interaction = 2.50
  73. if not daily_rate_interaction:
  74. daily_rate_interaction = 2.50
  75. print("\n "+"-"*5+"\n")
  76. template = input("+ CHOOSE: (O)pen Simulation or (L)oad template: ").upper()
  77. if template == "O": # New Simulation
  78. average_rate_duration = None
  79. probability_of_contagion = None
  80. recovery_rate = None
  81. simulation_name = "OPEN"
  82. new_simulation(total_population, infected_starting, days, daily_rate_interaction, average_rate_duration, probability_of_contagion, recovery_rate, simulation_name, starting_population)
  83. else: # Load template
  84. load_template(total_population, infected_starting, days, daily_rate_interaction, starting_population)
  85. def load_template(total_population, infected_starting, days, daily_rate_interaction, starting_population):
  86. print("\n "+"-"*5+"\n")
  87. print("[Info] Generating templates ...\n")
  88. import glob
  89. templates = {}
  90. i = 0
  91. for file in glob.iglob(simulation_templates_path + '*', recursive=False):
  92. if(file.endswith(".txt")):
  93. i = i +1
  94. f=open(file, 'r')
  95. template = f.read().replace('\n',' ')
  96. templates[i] = file.replace("templates/",""), template.upper() # add template to main dict
  97. f.close()
  98. for k,v in templates.items():
  99. print (" ["+str(k)+"] - "+str(v[0].replace(".txt","")))
  100. print("\n "+"-"*5+"\n")
  101. template_set = input("+ CHOOSE: Number of template (ex: 1): ").upper()
  102. try:
  103. template_set = int(template_set)
  104. except:
  105. template_set = 1
  106. if not template_set or template_set > len(templates) or template_set < 1:
  107. template_set = 1
  108. for k,v in templates.items():
  109. if template_set == k:
  110. simulation_name = v[0].replace(".txt","")
  111. average_rate_duration = int(v[1].split("DURATION:")[1].split(" ")[0])
  112. probability_of_contagion = int(v[1].split("CONTAGION:")[1].split(" ")[0])
  113. recovery_rate = int(v[1].split("RECOV:")[1].split(" ")[0])
  114. new_simulation(total_population, infected_starting, days, daily_rate_interaction, average_rate_duration, probability_of_contagion, recovery_rate, simulation_name, starting_population)
  115. def new_simulation(total_population, infected_starting, days, daily_rate_interaction, average_rate_duration, probability_of_contagion, recovery_rate, simulation_name, starting_population):
  116. print("\n "+"-"*5+"\n")
  117. print("[Info] Generating variables ...\n")
  118. if average_rate_duration == None:
  119. average_rate_duration = input(" + Average duration of illness (default: 12) (days): ")
  120. try:
  121. if average_rate_duration == 0:
  122. pass
  123. else:
  124. average_rate_duration = int(average_rate_duration)
  125. except:
  126. average_rate_duration = 12
  127. if average_rate_duration < 0 or average_rate_duration > 100:
  128. average_rate_duration = 12
  129. else:
  130. print(" + Average duration of illness: "+str(average_rate_duration)+" days")
  131. if probability_of_contagion == None:
  132. probability_of_contagion = input(" + Infection rate (default: 14%): ")
  133. try:
  134. if probability_of_contagion == 0:
  135. pass
  136. else:
  137. probability_of_contagion = int(probability_of_contagion)
  138. except:
  139. probability_of_contagion = 14
  140. if probability_of_contagion < 0 or probability_of_contagion > 100:
  141. probability_of_contagion = 14
  142. else:
  143. print(" + Infection rate: "+str(probability_of_contagion)+"%")
  144. if recovery_rate == None:
  145. recovery_rate = input(" + Recovery rate (default: 95%): ")
  146. try:
  147. if recovery_rate == 0:
  148. pass
  149. else:
  150. recovery_rate = int(recovery_rate)
  151. except:
  152. recovery_rate = 95
  153. if recovery_rate < 0 or recovery_rate > 100:
  154. recovery_rate = 95
  155. else:
  156. print(" + Recovery rate: "+str(recovery_rate)+"%")
  157. mortality = 100 - recovery_rate
  158. print("\n "+"-"*5+"\n")
  159. print("[Info] Building parameters ...\n")
  160. print(" + Mortality rate: "+str(mortality)+"%")
  161. mortality = mortality / 100
  162. recovery_rate = recovery_rate / 100
  163. probability_of_contagion = probability_of_contagion / 100
  164. infected = infected_starting
  165. susceptible_starting = int(total_population) - int(infected)
  166. susceptible = susceptible_starting # susceptitble at start
  167. recoveries = 0 # recoveries individuals at start
  168. print(" + Susceptible: "+str(susceptible))
  169. print("\n"+"="*50+"\n")
  170. print("[Info] Launching Simulation: [ "+str(simulation_name)+" ] ...")
  171. print("\n"+"="*50+"\n")
  172. current_time = datetime.datetime.now() # current datetime
  173. if not os.path.exists(reports_path): # create folder for reports
  174. os.makedirs(reports_path)
  175. data = {
  176. 'METADATA': [
  177. {
  178. 'Simulation Name': str(simulation_name),
  179. 'Datetime': str(current_time)
  180. }
  181. ],
  182. 'ECOSYSTEM': [
  183. {
  184. 'Total Population': str(total_population),
  185. 'Infected (at the beginning)': str(infected_starting),
  186. 'Number of days': str(days),
  187. 'Daily rate of interaction between individuals': str(daily_rate_interaction),
  188. 'Average duration of illness': str(average_rate_duration),
  189. 'Infection rate': str(probability_of_contagion*100)+"%",
  190. 'Recovery rate': str(recovery_rate*100)+"%",
  191. 'Mortality': str(mortality*100)+"%",
  192. 'Susceptible': str(susceptible),
  193. }
  194. ],
  195. 'SIMULATION': [
  196. {}
  197. ]
  198. }
  199. if not os.path.exists(reports_path+"PandeMaths-report_"+str(current_time)): # create folder for reports
  200. os.makedirs(reports_path+"PandeMaths-report_"+str(current_time))
  201. with open(reports_path+"PandeMaths-report_"+str(current_time)+"/"+str("PandeMaths-report_"+str(current_time)+".txt"), 'a', encoding='utf-8') as f: # append into txt
  202. f.write("="*50+os.linesep)
  203. f.write("Simulation Name:"+str(simulation_name)+os.linesep)
  204. f.write("Infected (at the beginning):"+str(infected_starting)+os.linesep)
  205. f.write("Number of days:"+str(days)+os.linesep)
  206. f.write("Daily rate of interaction between individuals:"+str(daily_rate_interaction)+os.linesep)
  207. f.write("Average duration of illness:"+str(average_rate_duration)+os.linesep)
  208. f.write("Infection rate:"+str(probability_of_contagion*100)+"%"+os.linesep)
  209. f.write("Recovery rate:"+str(recovery_rate*100)+"%"+os.linesep)
  210. f.write("Mortality:"+str(mortality*100)+"%"+os.linesep)
  211. f.write("Susceptible:"+str(susceptible)+os.linesep)
  212. f.write("="*50+os.linesep)
  213. entire_population_infected = 0
  214. plot_starting_population = []
  215. plot_days = []
  216. plot_contagion = []
  217. plot_recoveries = []
  218. plot_deaths = []
  219. plot_susceptible = []
  220. plot_infected = []
  221. plot_total_population = []
  222. plot_total_contagion = []
  223. plot_total_recovered = []
  224. plot_total_deceased = []
  225. entire_population_infected = False
  226. average_end_duration = 1
  227. infected_resolving_situation = False
  228. for i in range(0, days):
  229. if i > 0:
  230. try:
  231. status_rate = round(int(infected*100/total_population))
  232. except:
  233. status_rate = 100
  234. if status_rate < 11: # ENDEMIA (-11%)
  235. if susceptible > 0:
  236. status = "IMPACT LEVEL: [ ENDEMIC! ]"
  237. else:
  238. if int(total_deceased*100/starting_population) > 49:
  239. status = "IMPACT LEVEL: VACCINED! [ ERRADICATED BUT AT LEAST HALF OF THE POPULATION HAS DIED! ]"
  240. else:
  241. status = "IMPACT LEVEL: VACCINED! [ ERRADICATED! ]"
  242. elif status_rate > 10 and status_rate < 25: # EPIDEMIA (>10%<25%)
  243. if susceptible > 0:
  244. status = "IMPACT LEVEL: [ EPIDEMIC! ]"
  245. else:
  246. status = "IMPACT LEVEL: [ FOCUS OF INCUBATION! ]"
  247. else: # PANDEMIA (>25%)
  248. if susceptible > 0:
  249. status = "IMPACT LEVEL: [ PANDEMIC! ]"
  250. else:
  251. status = "IMPACT LEVEL: [ MOSTLY INCUBATING! ]"
  252. sir = susceptible+infected+recoveries # S-I-R model
  253. try:
  254. contagion = round(infected*daily_rate_interaction*susceptible/sir*probability_of_contagion) # contagion rounded rate
  255. except:
  256. contagion = 100
  257. recoveries = int(infected*recovery_rate/average_rate_duration) # recoveries rounded rate
  258. deaths = int(infected*mortality/average_rate_duration) # deaths rounded rate
  259. susceptible = int(susceptible - contagion + recoveries - deaths)
  260. infected = int(infected+contagion-recoveries-deaths)
  261. else: # related to the first day
  262. status = "[ SIMULATION START! ]"
  263. contagion = 0
  264. recoveries = 0
  265. deaths = 0
  266. total_recovered = 0
  267. total_deceased = 0
  268. susceptible = total_population - infected
  269. total_contagion = infected_starting
  270. total_contagion = total_contagion + contagion
  271. total_deceased = total_deceased + deaths
  272. total_population = total_population - total_deceased
  273. total_recovered = total_recovered + recoveries
  274. if total_recovered > starting_population:
  275. total_recovered = total_population
  276. print("-"*75+"\n")
  277. if total_population > 0: # some population still alive
  278. if contagion == 0 and recoveries == 0 and deaths == 0 and total_population != starting_population: # no more interactions after starting
  279. status = "IMPACT LEVEL: [ INFECTED RESOLVING THEIR SITUATION! ]"
  280. if infected == 0: # no more interactions + max average_rate_duration -> end!
  281. print("="*50+"\n")
  282. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  283. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  284. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  285. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  286. else:
  287. infected_resolving_situation = True
  288. average_end_duration = average_end_duration + 1
  289. if average_end_duration < average_rate_duration:
  290. res = random.randrange(2)
  291. if res == 1: # more recoveries!
  292. res_rec = random.randrange(infected)
  293. recoveries = res_rec
  294. deaths = infected - recoveries
  295. else: # more deaths!
  296. res_dea = random.randrange(infected)
  297. deaths = res_dea
  298. recoveries = infected - deaths
  299. infected = 0
  300. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  301. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  302. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  303. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  304. break
  305. if total_contagion >= starting_population: # all are infected
  306. total_contagion = starting_population
  307. susceptible = 0
  308. if entire_population_infected == False: # adding output markers to this event
  309. infected = total_population
  310. print("="*50+"\n")
  311. status = "IMPACT LEVEL: [ THE ENTIRE POPULATION IS INFECTED! ]"
  312. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  313. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  314. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  315. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  316. entire_population_infected = True
  317. print("="*50+"\n")
  318. else:
  319. if infected_resolving_situation == False:
  320. status = "IMPACT LEVEL: [ SOME POPULATION IS INCUBATING! ]"
  321. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  322. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  323. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  324. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  325. else: # more population susceptible than infected
  326. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  327. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  328. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  329. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  330. else: # no more population exposed
  331. total_population = 0
  332. contagion = 0
  333. susceptible = 0
  334. recoveries = 0
  335. deaths = 0
  336. if int(susceptible) > 0 and int(infected) > 0: # some have survived
  337. status = "IMPACT LEVEL: VACCINED! [ BUT WITH MANY CASUALTIES! ]"
  338. else: # no survivors!
  339. status = "IMPACT LEVEL: [ INFECTED RESOLVING THEIR SITUATION! ]"
  340. infected_resolving_situation = True
  341. average_end_duration = average_end_duration + 1
  342. if average_end_duration < average_rate_duration:
  343. res = random.randrange(2)
  344. if res == 1: # more recoveries!
  345. res_rec = random.randrange(infected)
  346. recoveries = res_rec
  347. deaths = infected - recoveries
  348. else: # more deaths!
  349. res_dea = random.randrange(infected)
  350. deaths = res_dea
  351. recoveries = infected - deaths
  352. infected = 0
  353. print("="*75+"\n")
  354. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  355. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  356. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  357. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  358. break
  359. print("="*50+"\n")
  360. status = "[ SIMULATION END! ]"
  361. i = i + 1
  362. contagion = 0
  363. recoveries = 0
  364. deaths = 0
  365. susceptible = 0
  366. infected = 0
  367. report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased)
  368. export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  369. export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  370. export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased)
  371. generate_graph(starting_population, simulation_name, infected_starting, daily_rate_interaction, average_rate_duration, probability_of_contagion, recovery_rate, mortality, total_population, plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time) # generate final graph
  372. print("="*75+"\n")
  373. print ("[Info] [REPORTS] (txt|json|png) -> [SAVED!] at: '"+str(reports_path+"PandeMaths-report_"+str(current_time)+"/'")+"\n")
  374. def extract_rates_current_day(starting_population, total_population, contagion, recoveries, deaths, total_contagion, total_recovered, total_deceased, infected, susceptible):
  375. try:
  376. rate_contagion = "%0.2f" % ((contagion/total_population)*100)
  377. except:
  378. rate_contagion = 0.00
  379. try:
  380. rate_recoveries = "%0.2f" % ((recoveries/infected)*100)
  381. except:
  382. rate_recoveries = 0.00
  383. try:
  384. rate_deaths = "%0.2f" % ((deaths/infected)*100)
  385. except:
  386. rate_deaths = 0.00
  387. try:
  388. rate_total_contagion = "%0.2f" % ((total_contagion/starting_population)*100)
  389. except:
  390. rate_total_contagion = 0.00
  391. try:
  392. rate_total_recovered = "%0.2f" % ((total_recovered/starting_population)*100)
  393. except:
  394. rate_total_recovered = 0.00
  395. try:
  396. rate_total_deceased = "%0.2f" % ((total_deceased/starting_population)*100)
  397. except:
  398. rate_total_deceased = 0.00
  399. try:
  400. rate_final_population = (starting_population-total_deceased)*100/starting_population
  401. except:
  402. rate_final_population = 0.00
  403. try:
  404. rate_infected = "%0.2f" % ((infected/total_population)*100)
  405. except:
  406. rate_infected = 0.00
  407. try:
  408. rate_susceptible = "%0.2f" % ((susceptible/total_population)*100)
  409. except:
  410. rate_susceptible = 0.00
  411. if rate_recoveries == 0 and rate_deaths == 0:
  412. if recoveries > 0 or deaths > 0:
  413. total_alive = recoveries + deaths
  414. if recoveries > 0:
  415. rate_recoveries = "%0.2f" % (recoveries*100/total_alive)
  416. if deaths > 0:
  417. rate_deaths = "%0.2f" % (deaths*100/total_alive)
  418. return rate_contagion, rate_recoveries, rate_deaths, rate_total_contagion, rate_total_recovered, rate_total_deceased, rate_final_population, rate_infected, rate_susceptible
  419. def report_current_day(i, status, contagion, total_population, recoveries, deaths, susceptible, infected, starting_population, total_contagion, total_recovered, total_deceased):
  420. if susceptible > total_population:
  421. susceptible = total_population
  422. if infected > starting_population:
  423. infected = starting_population
  424. rate_contagion, rate_recoveries, rate_deaths, rate_total_contagion, rate_total_recovered, rate_total_deceased, rate_final_population, rate_infected, rate_susceptible = extract_rates_current_day(starting_population, total_population, contagion, recoveries, deaths, total_contagion, total_recovered, total_deceased, infected, susceptible) # extract current daily rates
  425. print(" -> [DAY: "+str(i)+"]\n")
  426. print(" -> Contagion: ("+str(int(contagion))+")["+str(rate_contagion)+"%] - [ Recoveries: ("+str(int(recoveries))+")["+str(rate_recoveries)+"%] - Deaths: ("+str(int(deaths))+")["+str(rate_deaths)+"%] ]\n")
  427. print(" -> Status: "+str(status))
  428. print(" * Total Population: ("+str(int(starting_population)-int(total_deceased))+"/"+str(starting_population)+")["+str(int(rate_final_population))+"%] - [ Susceptible: ("+str(int(susceptible))+")["+str(rate_susceptible)+"%] - Infected: ("+str(int(infected))+")["+str(rate_infected)+"%] ]")
  429. print(" * Total Contagion: ("+str(int(total_contagion))+")["+str(rate_total_contagion)+"%] - Total Recovered: (" +str(int(total_recovered))+")["+str(rate_total_recovered)+"%] - Total Deceased: ("+str(int(total_deceased))+")["+str(rate_total_deceased)+"%]\n")
  430. def export_to_txt(current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased):
  431. if not os.path.exists(reports_path+"PandeMaths-report_"+str(current_time)): # create folder for reports
  432. os.makedirs(reports_path+"PandeMaths-report_"+str(current_time))
  433. with open(reports_path+"PandeMaths-report_"+str(current_time)+"/"+str("PandeMaths-report_"+str(current_time)+".txt"), 'a', encoding='utf-8') as f: # append into txt
  434. f.write(os.linesep)
  435. f.write("Day:"+str(i)+os.linesep)
  436. f.write("Status:"+str(status)+os.linesep)
  437. f.write("Contagion:"+str(contagion)+os.linesep)
  438. f.write("Recoveries:"+str(recoveries)+os.linesep)
  439. f.write("Deaths:"+str(deaths)+os.linesep)
  440. f.write("Susceptible:"+str(susceptible)+os.linesep)
  441. f.write("Infected:"+str(infected)+os.linesep)
  442. f.write("Total Population:"+str(total_population)+os.linesep)
  443. f.write("Total Contagion:"+str(total_contagion)+os.linesep)
  444. f.write("Total Recovered:"+str(total_recovered)+os.linesep)
  445. f.write("Total Deceased:"+str(total_deceased)+os.linesep)
  446. def export_to_json(data, current_time, i, status, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased):
  447. data['SIMULATION'][0]['DAY'] = str(i)
  448. data['SIMULATION'][0]['Status'] = str(status)
  449. data['SIMULATION'][0]['Contagion'] = str(int(contagion))
  450. data['SIMULATION'][0]['Recoveries'] = str(int(recoveries))
  451. data['SIMULATION'][0]['Deaths'] = str(int(deaths))
  452. data['SIMULATION'][0]['Susceptible'] = str(int(susceptible))
  453. data['SIMULATION'][0]['Infected'] = str(int(infected))
  454. data['SIMULATION'][0]['Total Population'] = str(int(total_population))
  455. data['SIMULATION'][0]['Total Contagion'] = str(int(total_contagion))
  456. data['SIMULATION'][0]['Total Recovered'] = str(int(total_recovered))
  457. data['SIMULATION'][0]['Total Deceased'] = str(int(total_deceased))
  458. if not os.path.exists(reports_path+"PandeMaths-report_"+str(current_time)): # create folder for reports
  459. os.makedirs(reports_path+"PandeMaths-report_"+str(current_time))
  460. with open(reports_path+"PandeMaths-report_"+str(current_time)+"/"+str("PandeMaths-report_"+str(current_time)+".json"), 'a', encoding='utf-8') as f: # append into json
  461. json.dump(data, f, ensure_ascii=False, sort_keys=False, indent=4)
  462. def export_to_graph(plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time, starting_population, i, contagion, recoveries, deaths, susceptible, infected, total_population, total_contagion, total_recovered, total_deceased):
  463. plot_starting_population = starting_population
  464. plot_days.append(i)
  465. plot_contagion.append(contagion)
  466. plot_recoveries.append(recoveries)
  467. plot_deaths.append(deaths)
  468. plot_susceptible.append(susceptible)
  469. plot_infected.append(infected)
  470. plot_total_population.append(total_population)
  471. plot_total_contagion.append(total_contagion)
  472. plot_total_recovered.append(total_recovered)
  473. plot_total_deceased.append(total_deceased)
  474. def generate_graph(starting_population, simulation_name, infected_starting, daily_rate_interaction, average_rate_duration, probability_of_contagion, recovery_rate, mortality, total_population, plot_starting_population, plot_days, plot_contagion, plot_recoveries, plot_deaths, plot_susceptible, plot_infected, plot_total_population, plot_total_contagion, plot_total_recovered, plot_total_deceased, current_time):
  475. plt.plot(plot_days, plot_contagion, "blue", label="Contagion")
  476. plt.plot(plot_days, plot_recoveries, "grey", label="Recoveries")
  477. plt.plot(plot_days, plot_deaths, "orange", label="Deaths")
  478. plt.plot(plot_days, plot_susceptible, "cyan", label="Susceptible")
  479. plt.plot(plot_days, plot_infected, "purple", label="Infected")
  480. plt.plot(plot_days, plot_total_population, "pink", label="Total Population")
  481. plt.plot(plot_days, plot_total_contagion, "red", label="Total Contagion")
  482. plt.plot(plot_days, plot_total_recovered, "yellow", label="Total Recovered")
  483. plt.plot(plot_days, plot_total_deceased, "black", label="Total Deceased")
  484. plt.plot(plot_starting_population)
  485. plt.title("SIMULATION: '"+str(simulation_name)+"' = Av_Ill: ["+str(average_rate_duration)+" days] - Inf_R: ["+str(probability_of_contagion*100)+"%] - Rec_R: ["+str(recovery_rate*100)+"%] - Mort: ["+str(mortality*100)+"%]\n\nTotal Population: ["+str(total_population)+"/"+str(starting_population)+"] - Infected (at the beginning): ["+str(infected_starting)+"] - Interaction (rate): ["+str(daily_rate_interaction)+"]\n")
  486. plt.xlabel('Day(s)')
  487. plt.ylabel('Individual(s)')
  488. plt.legend(loc='center left', fancybox=True, bbox_to_anchor=(1, 0.5))
  489. if not os.path.exists(reports_path+"PandeMaths-report_"+str(current_time)): # create folder for reports
  490. os.makedirs(reports_path+"PandeMaths-report_"+str(current_time))
  491. plt.savefig(reports_path+"PandeMaths-report_"+str(current_time)+"/"+str("PandeMaths-report_"+str(current_time)+".png"), bbox_inches='tight')
  492. def print_banner():
  493. print("\n"+"="*50)
  494. print(" ____ _ __ __ _ _ ")
  495. print("- _ \ __ _ _ __ __- - ___- \/ - __ _- -_- -__ ___ ")
  496. print("- -_) / _` - '_ \ / _` -/ _ \ -\/- -/ _` - __- '_ \/ __--2020")
  497. print("- __/ (_- - - - - (_- - __/ - - - (_- - -_- - - \__ /")
  498. print("-_- \__,_-_- -_-\__,_-\___-_- -_-\__,_-\__-_- -_-___/-by psy")
  499. print('\n"Pandemics Extensible Mathematical Model"')
  500. print("\n"+"-"*15+"\n")
  501. print(" * VERSION: ")
  502. print(" + "+VERSION+" - (rev:"+RELEASE+")")
  503. print("\n * SOURCES:")
  504. print(" + "+SOURCE1)
  505. print(" + "+SOURCE2)
  506. print("\n * CONTACT: ")
  507. print(" + "+CONTACT+"\n")
  508. print("-"*15+"\n")
  509. print("="*50)
  510. # sub_init #
  511. print_banner() # show banner
  512. try:
  513. option = input("\n+ CHOOSE: (M)odel or (S)imulation: ").upper()
  514. except:
  515. print("\n"+"="*50 + "\n")
  516. print ("[Info] Try to run the tool with Python3.x.y... (ex: python3 pandemaths) -> [EXITING!]\n")
  517. sys.exit()
  518. print("")
  519. print("="*50+"\n")
  520. if option == "S": # simulation
  521. simulation()
  522. else: # model
  523. model_maths()
  524. print ("="*50+"\n")