def run_rolling_average(x, y, ct, g_id, avgsize, curdate, ylabel): rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' freport = f'report/{ct}-{g_id}.html' dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'7-Rolling average of cases for {rname} on {curdate}', f'Total {ylabel}', f'{avgsize}-rolling Average of {ylabel}', fdata, 2, f"{avgsize}-rolling average", opt='xrange [0<*:]\nset yrange [0<*:]', point=False, logx=True, logy=True) with open(freport, 'w') as f: table_info = f'<tr><td>Success on a {avgsize}-rolling average</td></tr>' table_stat = f'<tr><th>Day</th><th>{avgsize}-average of {ylabel}</th></tr>' for i, j in zip(x, y): table_stat += f'<tr><td>{i}</td><td>{j}</td></tr>' f.write( param_page(rname, table_info, table_stat, '<tr><td>None</td></tr>', fsvg)) return
def run_model_sigmoid(x, y, ct, g_id, ylabel, data_consolidated, model_consolidated, curdate, text): rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' freport = f'report/{ct}-{g_id}.html' result, model = find_fit_sigmoid(x, y) if result.success == False: data_consolidated.append('n.a.') data_consolidated.append('n.a.') dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, f"{rname} data", opt='colorsequence podo', txt1='NO FIT AVAILABLE FOR THE CURRENT DATA', point=True) #loggingg.info(f'run_model_sigmoid: cannot fit sigmoid for {ct}') else: chisqr = result.chisqr center = int(round(result.params['step_center'].value)) data_consolidated.append(center) data_consolidated.append(chisqr) model_consolidated.append(result) dump_xyz_dat(fdata, x, y, result.best_fit) dump_svg2D(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, 3, f"{rname} data", f'{text}', opt='yrange [0<*:]', txt1=f'Mid = {center} days', txt2=f'�² = {chisqr:9.2}') dump_report(freport, result, x, y, 7, fsvg, model, ct, ylabel) return
def run_data(x, y, ct, g_id, ylabel, curdate, text, txt1=''): rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, f"{rname} data", opt='colorsequence podo', txt1=txt1, point=True)
def run_socnet_model(x, y, ct, g_id, cur, tag, ylabel, data_consolidated, model_consolidated, curdate, text): import fitrs3 from scipy.stats import chisquare rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' freport = f'report/{ct}-{g_id}.html' file1 = f'scnlog/{ct}-p1.dat' file2 = f'scnlog/{ct}-p2.dat' partition = len(y) // 4 forecast = fitrs3.previsaoredeslp(y, 7, 200, 400, 100, file1, file2, partition, y[-1] + 50, y[-1] * 20, 4, 6, 0.2, 0.7, 0, 101) if forecast is None: data_consolidated.append('n.a.') data_consolidated.append('n.a.') dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, f"{rname} data", opt='colorsequence podo', txt1='NO FIT AVAILABLE FOR THE CURRENT DATA', point=True) else: chisqr = chisquare(y, f_exp=forecast[:len(y)])[0] data_consolidated.append(chisqr) model_consolidated.append('socnet-fitrs3') nx = x if forecast is None else np.arange(len(forecast)) dump_xyz_dat(fdata, nx, y, forecast) dump_svg2D(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, 3, f"{rname} data", f'{text}', opt='yrange [0<*:]', txt1=f'SOCNET', txt2=f'�² = {chisqr:9.2}') if forecast is not None: with open(freport, 'w') as f: table_info = f'<tr><td>Success status</td><td>Forecast calculated with socnet-fitrs3</td></tr>' table_info += f'<tr><td>Abort status</td><td>n.a.</td></tr>' table_info += f'<tr><td>Fit message</td><td>n.a.</td></tr>' table_stat = '<tr> <td>n.a</td></tr>' table_obs = f'<tr><th>Days from the first infected</th><th>{ylabel}</th><th>Model {ylabel}</th></tr>' if forecast != None: for i, j, k in itertools.zip_longest(nx, y, forecast, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>{k:.0f}</td></tr>' else: for i, j in itertools.zip_longest(x, y, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>n.a.</td></tr>' f.write(param_page(rname, table_info, table_stat, table_obs, fsvg)) return
def run_edo_model(ct, g_id, cur, tag, ylabel, data_consolidated, model_consolidated, curdate, text, paramp=None, forecast_days=14): rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fdatc = f'gpdata/dat/{ct}-{g_id+10}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' fsvg1 = f'svg/{ct}-{g_id+10}.svg' fsvg2 = f'svg/{ct}-{g_id+11}.svg' freport = f'report/{ct}-{g_id}.html' x, y, model, forecast = fit_edo_shape(ct, cur, tag, paramp=paramp, forecast_days=forecast_days) if model.success == False: data_consolidated.append('n.a.') model_consolidated.append(None) dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, f"{rname} data", opt='colorsequence podo', txt1='NO FIT AVAILABLE FOR THE CURRENT DATA', point=True) else: chisqr = model.chisqr param = model.params data_consolidated.append(chisqr) model_consolidated.append(model) if tag == 'accCombined': x = x[:len(x) // 2] y = y[:len(x) // 2] nx = x if forecast is None else np.arange(len(forecast) // 2) else: nx = x if forecast is None else np.arange(len(forecast)) dump_xyz_dat(fdata, nx, y, forecast) dump_svg2D(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, 3, f"{rname} data", f'{text}', opt='yrange [0<*:]', txt1=f'SIMDRQME', txt2=f'�² = {chisqr:9.2}') S, Q, E, A, I, D, R, M = edo.eval_edo(param, 250) pop = param['pop'].value Sr = [x / pop for x in S] Qr = [x / pop for x in Q] Er = [x / pop for x in E] Ar = [x / pop for x in A] Ir = [x / pop for x in I] Dr = [x / pop for x in D] Rr = [x / pop for x in R] Mr = [x / pop for x in M] svg.dump_8_dat(fdatc, Sr, Qr, Er, Ar, Ir, Dr, Rr, Mr) svg.dump_svg8D(fsvg1, fsvg2, fdatc, f'SIMDRQME Compartment Model for {rname} on {curdate}', 'days', 'perc. of pop.', ['S', 'Q', 'E', 'A', 'I', 'D', 'R', 'M']) if model != None: with open(freport, 'w') as f: table_info = f'<tr><td>Success status</td><td>{model.success}</td></tr>' table_info += f'<tr><td>Abort status</td><td>{model.aborted}</td></tr>' table_info += f'<tr><td>Fit message</td><td>{model.message}</td></tr>' table_stat = '<tr> <td>' + model._repr_html_() + '</td></tr>' table_obs = f'<tr><th>Days from the first infected</th><th>{ylabel}</th><th>Model {ylabel}</th></tr>' if forecast != None: for i, j, k in itertools.zip_longest(nx, y, forecast, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>{k:.0f}</td></tr>' else: for i, j in itertools.zip_longest(x, y, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>n.a.</td></tr>' f.write( param_page1(rname, table_info, table_stat, table_obs, fsvg, fsvg1, fsvg2)) return
def copy_edo_model(ct, g_id, cur, tag, ylabel, data_consolidated, model_consolidated, curdate, text, paramp=None, forecast_days=14): rname = ct.replace('_', ' ') fdata = f'gpdata/dat/{ct}-{g_id}.dat' fgplot = f'gpdata/{ct}-{g_id}.gp' fsvg = f'svg/{ct}-{g_id}.svg' freport = f'report/{ct}-{g_id}.html' x, y, model, forecast = copy_edo_shape(ct, cur, tag, model_consolidated[0], paramp, forecast_days) if model.success == False: data_consolidated.append('n.a.') model_consolidated.append(None) dump_xy_dat(fdata, x, y) dump_svg(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, f"{rname} data", opt='colorsequence podo', txt1='NO FIT AVAILABLE FOR THE CURRENT DATA', point=True) else: chisqr = model.chisqr data_consolidated.append(chisqr) model_consolidated.append(model) nx = x if forecast is None else np.arange(len(forecast)) dump_xyz_dat(fdata, nx, y, forecast) dump_svg2D(fgplot, fsvg, f'{text} for {rname} on {curdate}', 'Days from the first infected', f'{ylabel}', fdata, 2, 3, f"{rname} data", f'{text}', opt='yrange [0<*:]', txt1=f'EDO', txt2=f'�² = {chisqr:9.2}') if model != None: with open(freport, 'w') as f: table_info = f'<tr><td>Success status</td><td>{model.success}</td></tr>' table_info += f'<tr><td>Abort status</td><td>{model.aborted}</td></tr>' table_info += f'<tr><td>Fit message</td><td>{model.message}</td></tr>' table_stat = '<tr> <td>' + model._repr_html_() + '</td></tr>' table_obs = f'<tr><th>Days from the first infected</th><th>{ylabel}</th><th>Model {ylabel}</th></tr>' if forecast != None: for i, j, k in itertools.zip_longest(nx, y, forecast, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>{k:.0f}</td></tr>' else: for i, j in itertools.zip_longest(x, y, fillvalue='nan'): table_obs += f'<tr><td>{i}</td><td>{j}</td><td>n.a.</td></tr>' f.write(param_page(rname, table_info, table_stat, table_obs, fsvg)) return