def test_misc(): sc.heading('Testing miscellaneous functions') sim_path = 'test_misc.sim' json_path = 'test_misc.json' gitinfo_path = 'test_misc.gitinfo' fig_path = 'test_misc.png' fig_comments = 'Test comment' # Data loading cv.load_data(csv_file) cv.load_data(xlsx_file) with pytest.raises(NotImplementedError): cv.load_data('example_data.unsupported_extension') with pytest.raises(ValueError): cv.load_data(xlsx_file, columns=['missing_column']) # Dates d1 = cv.date('2020-04-04') d2 = cv.date(sc.readdate('2020-04-04')) ds = cv.date('2020-04-04', d2) assert d1 == d2 assert d2 == ds[0] with pytest.raises(ValueError): cv.date([(2020, 4, 4)]) # Raises a TypeError which raises a ValueError with pytest.raises(ValueError): cv.date('Not a date') cv.daydiff('2020-04-04') # Run sim for more investigations sim = cv.Sim(pop_size=500, verbose=0) sim.run() sim.plot(do_show=False) # Saving and loading cv.savefig(fig_path, comments=fig_comments) cv.save(filename=sim_path, obj=sim) cv.load(filename=sim_path) # Version checks cv.check_version('0.0.0') # Nonsense version print('↑ Should complain about version') with pytest.raises(ValueError): cv.check_version('0.0.0', die=True) # Git checks cv.git_info(json_path) cv.git_info(json_path, check=True) # Poisson tests c1 = 5 c2 = 8 for alternative in ['two-sided', 'larger', 'smaller']: cv.poisson_test(c1, c2, alternative=alternative) for method in ['score', 'wald', 'sqrt', 'exact-cond']: cv.poisson_test(c1, c2, method=method) with pytest.raises(ValueError): cv.poisson_test(c1, c2, method='not a method') # Test locations for location in [None, 'viet-nam']: cv.data.show_locations(location) # Test versions with pytest.raises(ValueError): cv.check_save_version('1.3.2', die=True) cv.check_save_version(cv.__version__, filename=gitinfo_path, comments='Test') # Test PNG try: metadata = cv.get_png_metadata(fig_path, output=True) assert metadata['Covasim version'] == cv.__version__ assert metadata['Covasim comments'] == fig_comments except ImportError as E: print( f'Cannot test PNG function since pillow not installed ({str(E)}), skipping' ) # Tidy up remove_files(sim_path, json_path, fig_path, gitinfo_path) return
import sciris as sc import covasim as cv import covasim.base as cvb import covasim.parameters as cvp import pylab as pl import numpy as np import matplotlib as mplt import utils_vac ######################################################################## # Settings and initialisation ######################################################################## # Check version cv.check_version('3.0.2') cv.git_info('covasim_version.json') # Saving and plotting settings do_plot = 1 do_save = 1 save_sim = 1 plot_hist = 0 # Whether to plot an age histogram do_show = 0 verbose = 1 seed = 1 n_runs = 200 to_plot = sc.objdict({ 'Cumulative tests': ['cum_tests'], 'Cumulative diagnoses': ['cum_diagnoses'], 'Cumulative hospitalisations': ['cum_severe'], 'Cumulative deaths': ['cum_deaths'], 'New infections': ['new_infections'],
def test_misc(): sc.heading('Testing miscellaneous functions') sim_path = 'test_misc.sim' json_path = 'test_misc.json' # Data loading cv.load_data(csv_file) cv.load_data(xlsx_file) with pytest.raises(NotImplementedError): cv.load_data('example_data.unsupported_extension') with pytest.raises(ValueError): cv.load_data(xlsx_file, columns=['missing_column']) # Dates d1 = cv.date('2020-04-04') d2 = cv.date(sc.readdate('2020-04-04')) ds = cv.date('2020-04-04', d2) assert d1 == d2 assert d2 == ds[0] with pytest.raises(ValueError): cv.date([(2020, 4, 4)]) # Raises a TypeError which raises a ValueError with pytest.raises(ValueError): cv.date('Not a date') cv.daydiff('2020-04-04') # Saving and loading sim = cv.Sim() cv.save(filename=sim_path, obj=sim) cv.load(filename=sim_path) # Version checks cv.check_version('0.0.0') # Nonsense version print('↑ Should complain about version') with pytest.raises(ValueError): cv.check_version('0.0.0', die=True) # Git checks cv.git_info(json_path) cv.git_info(json_path, check=True) # Poisson tests c1 = 5 c2 = 8 for alternative in ['two-sided', 'larger', 'smaller']: cv.poisson_test(c1, c2, alternative=alternative) for method in ['score', 'wald', 'sqrt', 'exact-cond']: cv.poisson_test(c1, c2, method=method) with pytest.raises(ValueError): cv.poisson_test(c1, c2, method='not a method') # Tidy up remove_files(sim_path, json_path) return