Cyclip

COVID-19 statistics estimation

Sep 20th, 2020
896
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import os
  3. import time
  4. import json
  5. import time
  6. import math
  7. import datetime
  8.  
  9. '''
  10. By u/Cyclip (github.com/Cyclip)
  11. Very badly made code that allows you to estimate
  12. deaths based on modifiable statistics for
  13. the current pandemic, COVID-19.
  14. '''
  15.  
  16. ################### Variables ###################
  17. increment     = 5000000
  18. population    = 7813160000
  19. jsonIndent    = 4
  20. roundUp       = True
  21. generateFile  = True
  22. fileDirectory = 'cov-statistics'
  23. covStatistics = [
  24.                     {
  25.                         'infections': 30835922,
  26.                         'deaths': 957790,
  27.                         'apply': True,
  28.                         'postApply': True
  29.                     },
  30.                     {
  31.                         'infections': None,
  32.                         'deaths': None,
  33.                         'apply': False,
  34.                         'postApply': False
  35.                     }
  36.                 ]
  37. #################################################
  38.  
  39. def toWordsDec(dec):
  40.     if dec == '00' or dec == '0' or dec == '':
  41.         return ''
  42.     else:
  43.         return '.' + dec
  44.  
  45. def toWords(num):
  46.     # This does not round up or down
  47.     s = str(num)
  48.     if len(s) < 6:
  49.         return s
  50.     elif len(s) == 6:
  51.         return s[:3] + 'k'
  52.     elif len(s) == 7:
  53.         return s[0] + toWordsDec(s[1:3]) + ' million'
  54.     elif len(s) == 8:
  55.         return s[:2] + toWordsDec(s[3:5]) + ' million'
  56.     elif len(s) == 9:
  57.         return s[:3] + ' million'
  58.     elif len(s) == 10:
  59.         return s[0] + '.' + s[1:3] + ' billion'
  60.     return s
  61.  
  62.  
  63. def calculateMultiplier(stat):
  64.     stat = covStatistics
  65.     periMultipliers = []
  66.     postMultipliers = []
  67.     for s in stat:
  68.         if s['apply']:
  69.             tmp_m = s['deaths'] / s['infections']
  70.             if s['postApply']:
  71.                 periMultipliers.append(tmp_m)
  72.             else:
  73.                 postMultipliers.append(tmp_m)
  74.  
  75.     periM = sum(periMultipliers)/len(periMultipliers)
  76.     postMultipliers.append(periM)
  77.     finalMultiplier = sum(postMultipliers)/len(postMultipliers)
  78.     return finalMultiplier
  79.  
  80. print('Calculating..')
  81. start = time.time()
  82.  
  83. m = calculateMultiplier(covStatistics)
  84. if roundUp:
  85.     mround = math.ceil
  86. else:
  87.     mround = math.floor
  88.  
  89. infections = [i * increment for i in range(1, math.floor(7000000000 / increment))]
  90. deaths = [mround(i * m) for i in infections]
  91.  
  92. data = []
  93.  
  94. for i, infection in enumerate(infections):
  95.     try:
  96.         data.append({'infections': infection, 'deaths': deaths[i]})
  97.     except:
  98.         print(i, len(infections), len(deaths))
  99.  
  100. if generateFile:
  101.     now = datetime.datetime.now()
  102.     sfilename = f'{fileDirectory}/COV19 {now.day}-{now.month}-{now.year} {now.hour}-{now.minute}-{now.second}.json'
  103.  
  104.     con = {
  105.         'config': {
  106.             'increment': increment,
  107.             'population': population,
  108.             'jsonIndent': jsonIndent,
  109.             'round': 'up' if roundUp else 'down',
  110.             'generateFile': 'guess',
  111.             'geniusGuyWhoMadeThis': 'Cyclip'
  112.         },
  113.         'statistics': covStatistics,
  114.         'data': data
  115.     }
  116.            
  117.     if not os.path.isdir(fileDirectory):
  118.         os.mkdir(fileDirectory)
  119.  
  120.     with open(sfilename, 'w') as f:
  121.         f.write(json.dumps(con, indent=jsonIndent))
  122.  
  123. end = time.time() - start
  124.  
  125. a = 1.3
  126. previousInf = 0
  127. while a < 200:
  128.     if previousInf == data[mround(a)]['infections']:
  129.         a += 1
  130.     else:
  131.         previousInf = data[mround(a)]['infections']
  132.     # print(toWords(data[mround(a)]['infections']), toWords(data[mround(a)]['deaths']), a)
  133.     if a > len(data)/2:
  134.         a += 10
  135.     else:
  136.         a += 5
  137.  
  138. print(f'Calculated in {round(end, 3)}s')
  139. if generateFile:
  140.     print(f'Generated statistics file {fileDirectory}/{sfilename}')
  141. while True:
  142.     inp = input('Number (in millions): ')
  143.     try:
  144.         finp = float(inp)
  145.         print(f'[+] {toWords(finp)} infections: '.ljust(34) + f'{toWords(mround(finp * m))} deaths')
  146.     except KeyboardInterrupt:
  147.         sys.exit()
  148.     except ValueError:
  149.         print('[-] Enter a number')
  150.     except Exception as e:
  151.         print(f'[?] Unexpected error: {str(e)}')
  152.  
RAW Paste Data