def test_external_operation(): # Note that the test string has intentionally not uniform formatting with spaces op = elfi.tools.external_operation('echo 1 {0} 4 5 6 {seed}') constant = elfi.Constant(123) simulator = elfi.Simulator(op, constant) v = simulator.generate(1) assert np.array_equal(v[:5], [1, 123, 4, 5, 6]) # Can be pickled pickle.dumps(op)
def sleep_model(request): """The true param will be half of the given sleep time.""" ub_sec = request.param or .5 m = elfi.ElfiModel() elfi.Constant(ub_sec, model=m, name='ub') elfi.Prior('uniform', 0, m['ub'], model=m, name='sec') elfi.Simulator(sleeper, m['sec'], model=m, name='slept') elfi.Summary(no_op, m['slept'], model=m, name='summary') elfi.Distance('euclidean', m['summary'], model=m, name='d') m.observed['slept'] = ub_sec / 2 return m
def test_vectorized_and_external_combined(): constant = elfi.Constant(123) kwargs_sim = elfi.tools.external_operation( 'echo {seed} {batch_index} {index_in_batch} {submission_index}', process_result='int32') kwargs_sim = elfi.tools.vectorize(kwargs_sim) sim = elfi.Simulator(kwargs_sim, constant) with pytest.raises(Exception): sim.generate(3) sim.uses_meta = True g = sim.generate(3) # Test uniqueness of seeds assert len(np.unique(g[:, 0]) == 3) assert len(np.unique(g[:, 1]) == 1) # Test index_in_batch assert np.array_equal(g[:, 2], [0, 1, 2]) # Test submission_index (all belong to the same submission) assert len(np.unique(g[:, 3]) == 1)
def simple_model(): m = elfi.ElfiModel() elfi.Constant(10, model=m, name='tau') elfi.Prior('uniform', 0, m['tau'], size=1, model=m, name='k1') elfi.Prior('normal', m['k1'], size=3, model=m, name='k2') return m
if time.time() > timeout: raise TimeoutError("Could not find any cores after 600 seconds") time.sleep(10) logging.info(f"Ipyparallel client started with {num_cores} cores.") with open("../output/priors.pkl", "rb") as f: # Priors specified in priors.py priors = pickle.load(f) with open("../data/e3_phased.pkl", "rb") as f: y_obs = np.atleast_2d(pickle.load(f)) # Set up elfi model m = elfi.ElfiModel("m", observed={"sim": y_obs}) elfi.Constant(seq_length, name="length", model=m) elfi.Constant(1.8e-8, name="recombination_rate", model=m) elfi.Constant(6e-8, name="mutation_rate", model=m) for prior_name, prior in priors.items(): elfi.Prior(prior.sampling, name=prior_name, model=m) prior_args = [m[name] for name in m.parameter_names] elfi.Simulator(elfi_sim, *prior_args, m["length"], m["recombination_rate"], m["mutation_rate"], priors, name="sim",
def kisdi_model(population_filename): # (cumulative) deaths every day from 1st of march deaths = [0]*20 + [1, 1, 1, 1, 3, 4, 7, 9, 11, 13, 17, 17, 19, 20, 25, 27, 27, 34, 40, 42, 47, 49, 56, 59, 64, 72, 75] # In ICU # hospitalized = [np.nan]*24 + [22, 24, 32, 31, 41, 49, 56, 62, 65, 72, 73, # 76, 81, 83, 82, 82, 81, 80, 77, 74, 75, 75, 76] # total hospitalized hospitalized = [np.nan]*24 + [82, 96, 108, 112, 134, 143, 137, 159, 160, 180, 187, 209, 228, 231, 239, 244, 236, 235, 235, 230, 232, 226, 215] observed = np.array([list(zip(deaths, hospitalized))]) model = elfi.new_model() areas = set() with open(population_filename, newline='') as f: for row in csv.DictReader(f): areas.add(row['Area']) univariate_params = { 'beta_presymptomatic': stats.uniform(0, 1), 'beta_asymptomatic': stats.uniform(0, 1), 'beta_infected': stats.uniform(0, 1), 'pi': stats.beta(4, 6), 'kappa': stats.beta(2, 8), 'reciprocal_eta': stats.gamma(2.34, 5.0), 'reciprocal_alpha': stats.gamma(2., 5.0), 'reciprocal_theta': stats.gamma(8.0, 5.0), 'reciprocal_nu': stats.gamma(2.86, 5.0), 'reciprocal_rho': stats.gamma(5.0, 5.0), 'reciprocal_chi': stats.gamma(10.0, 5.0), 'reciprocal_delta': stats.gamma(7.0, 5.0), } multivar_params = { 'contact_y': stats.dirichlet([1, 1, 1]), 'contact_m': stats.dirichlet([1, 1, 1]), 'contact_o': stats.dirichlet([1, 1, 1]), } parameters = {} for k, v in itertools.chain( univariate_params.items(), multivar_params.items()): parameters[k] = elfi.Prior(v, name=k, model=model) initial_condition_parameters = {} for area in list(areas): for compartment in ['Exposed', 'Presymptomatic', 'Asymptomatic', 'Infected']: for age_group in ['Young', 'Adults', 'Elderly']: key = (area, compartment, age_group) initial_condition_parameters[key] = \ elfi.Constant(1, name=' '.join(key), model=model) # elfi.Prior(stats.uniform(0, 1000), name=' '.join(key), # model=model) sim_fun = elfi.tools.vectorize(simulate, constants=[0, 1, 2, 3, 4], dtype=np.float_) sim = elfi.Simulator(sim_fun, population_filename, list(areas), observed.shape[1], list(parameters), list(initial_condition_parameters), *parameters.values(), *initial_condition_parameters.values(), observed = observed) deaths = elfi.Summary(deaths_column, sim, model=model) hospitalized = elfi.Summary(hospitalized_column, sim, model=model) dist = elfi.Discrepancy(time_series_discrepancy, deaths, hospitalized, model=model) return model, dist,\ univariate_params, multivar_params, initial_condition_parameters
def constant_prior(val, name, **kwargs): return elfi.Constant(val, name=name)