def test_plotting(): sc.heading('Testing plotting') fig_path = 'plotting_test.png' # Create sim with data and interventions ce = cv.clip_edges(**{'start_day': 10, 'change': 0.5}) sim = cv.Sim(pop_size=100, n_days=60, datafile=csv_file, interventions=ce, verbose=verbose) sim.run(do_plot=True) # Handle lesser-used plotting options sim.plot(to_plot=['cum_deaths', 'new_infections'], sep_figs=True, font_family='Arial', log_scale=['Number of new infections'], interval=5, do_save=True, fig_path=fig_path) print('↑ May print a warning about zero values') # Handle Plotly functions cv.plotly_sim(sim) cv.plotly_people(sim) cv.plotly_animate(sim) # Tidy up remove_files(fig_path) return
def test_plotting(): sc.heading('Testing plotting') fig_paths = ['plotting_test1.png', 'plotting_test2.png'] # Create sim with data and interventions ce = cv.clip_edges(**{'days': 10, 'changes': 0.5}) sim = cv.Sim(pop_size=100, n_days=60, datafile=csv_file, interventions=ce, verbose=verbose) sim.run(do_plot=True) # Handle lesser-used plotting options sim.plot(to_plot=['cum_deaths', 'new_infections'], sep_figs=True, log_scale=['Number of new infections'], do_save=True, fig_path=fig_paths) # Handle Plotly functions try: cv.plotly_sim(sim) cv.plotly_people(sim) cv.plotly_animate(sim) except Exception as E: print( f'Plotly plotting failed ({str(E)}), but not essential so continuing' ) # Tidy up remove_files(*fig_paths) return
''' Test Plotly plotting outside of the webapp. ''' import plotly.io as pio import covasim as cv pio.renderers.default = "browser" # Or can use a Jupyter notebook ce = cv.clip_edges(**{'start_day': 10, 'change': 0.5}) sim = cv.Sim(pop_size=100, n_days=60, datafile='../example_data.csv', interventions=ce, verbose=0) sim.run() f1list = cv.plotly_sim(sim, do_show=True) f2 = cv.plotly_people(sim, do_show=True) f3 = cv.plotly_animate(sim, do_show=True)
def run_sim(sim_pars=None, epi_pars=None, int_pars=None, datafile=None, show_animation=False, n_days=90, location=None, verbose=True, die=die): ''' Create, run, and plot everything ''' errs = [] try: web_pars = parse_parameters(sim_pars=sim_pars, epi_pars=epi_pars, int_pars=int_pars, n_days=n_days, location=location, verbose=verbose, errs=errs, die=die) if verbose: print('Input parameters:') print(web_pars) except Exception as E: errs.append(log_err('Parameter conversion failed!', E)) if die: raise # Create the sim and update the parameters try: extra_pars = dict(pop_type='hybrid') pars = sc.mergedicts(extra_pars, web_pars) sim = cv.Sim(pars=pars, datafile=datafile) except Exception as E: errs.append(log_err('Sim creation failed!', E)) if die: raise # Core algorithm try: sim.run(do_plot=False) except TimeoutError as TE: err = f"The simulation stopped on day {sim.t} because run time limit ({sim['timelimit']} seconds) was exceeded. Please reduce the population size and/or number of days simulated." errs.append(log_err(err, TE)) if die: raise except Exception as E: errs.append(log_err('Sim run failed!', E)) if die: raise # Core plotting def process_graphs(figs): jsons = [] for fig in sc.promotetolist(figs): fig.update_layout(paper_bgcolor=bgcolor, plot_bgcolor=plotbg) output = {'json': fig.to_json(), 'id': str(sc.uuid())} d = json.loads(output['json']) d['config'] = {'responsive': True} output['json'] = json.dumps(d) jsons.append(output) return jsons graphs = [] try: graphs += process_graphs(cv.plotly_sim(sim)) graphs += process_graphs(cv.plotly_people(sim)) if show_animation: graphs += process_graphs(cv.plotly_animate(sim)) except Exception as E: errs.append(log_err('Plotting failed!', E)) if die: raise # Create and send output files (base64 encoded content) try: files, summary = get_output_files(sim) except Exception as E: files = {} summary = {} errs.append(log_err('Unable to save output files!', E)) if die: raise output = {} output['errs'] = errs output['sim_pars'] = sim_pars output['epi_pars'] = epi_pars output['int_pars'] = int_pars output['graphs'] = graphs output['files'] = files output['summary'] = summary return output