def test_lnprob_calculates_multi_phase_probability_for_success(datasets_db): """lnprob() successfully calculates the probability for equilibrium """ dbf = Database.from_string(CU_MG_TDB, fmt='tdb') datasets_db.insert(CU_MG_DATASET_ZPF_WORKING) comps = ['CU', 'MG', 'VA'] phases = ['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'] param = 'VV0001' orig_val = dbf.symbols[param].args[0].expr models = instantiate_models(dbf, comps, phases, parameters={param: orig_val}) eq_callables = build_callables(dbf, comps, phases, models, parameter_symbols=[param], output='GM', build_gradients=True, build_hessians=False, additional_statevars={v.N, v.P, v.T}) zpf_kwargs = { 'dbf': dbf, 'phases': phases, 'zpf_data': get_zpf_data(comps, phases, datasets_db), 'phase_models': models, 'callables': eq_callables, 'data_weight': 1.0, } opt = EmceeOptimizer(dbf) res = opt.predict([10], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert np.isreal(res) assert np.isclose(res, -31.309645520830344, rtol=1e-6) res_2 = opt.predict([10000000], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert not np.isclose(res_2, -31.309645520830344, rtol=1e-6)
def test_lnprob_calculates_multi_phase_probability_for_success(datasets_db): """lnprob() successfully calculates the probability for equilibrium """ dbf = Database.from_string(CU_MG_TDB, fmt='tdb') datasets_db.insert(CU_MG_DATASET_ZPF_WORKING) comps = ['CU', 'MG', 'VA'] phases = ['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'] param = 'VV0001' orig_val = dbf.symbols[param].args[0].expr initial_params = {param: orig_val} zpf_kwargs = { 'zpf_data': get_zpf_data(dbf, comps, phases, datasets_db, initial_params), 'data_weight': 1.0, } opt = EmceeOptimizer(dbf) res = opt.predict([10], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert np.isreal(res) assert np.isclose(res, -31.309645520830344, rtol=1e-4) res_2 = opt.predict([10000000], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert not np.isclose(res_2, -31.309645520830344, rtol=1e-6)
def test_equilibrium_thermochemical_correct_probability(datasets_db): """Integration test for equilibrium thermochemical error.""" dbf = Database(CU_MG_TDB) opt = EmceeOptimizer(dbf) datasets_db.insert(CU_MG_EQ_HMR_LIQUID) ctx = setup_context(dbf, datasets_db, ['VV0017']) ctx.update(opt.get_priors(None, ['VV0017'], [0])) prob = opt.predict(np.array([-31626.6]), **ctx) expected_prob = norm(loc=0, scale=500).logpdf([-31626.6 * 0.5 * 0.5]).sum() assert np.isclose(prob, expected_prob) # change to -40000 prob = opt.predict(np.array([-40000], dtype=np.float_), **ctx) expected_prob = norm(loc=0, scale=500).logpdf([-40000 * 0.5 * 0.5]).sum() assert np.isclose(prob, expected_prob)
def test_equilibrium_thermochemical_context_is_pickleable(datasets_db): """Test that the context for equilibrium thermochemical data is pickleable""" datasets_db.insert(CU_MG_EQ_HMR_LIQUID) dbf = Database(CU_MG_TDB) symbols_to_fit = database_symbols_to_fit(dbf) initial_guess = np.array([unpack_piecewise(dbf.symbols[s]) for s in symbols_to_fit]) prior_dict = EmceeOptimizer.get_priors(None, symbols_to_fit, initial_guess) ctx = setup_context(dbf, datasets_db) ctx.update(prior_dict) ctx_pickle = pickle.dumps(ctx) ctx_unpickled = pickle.loads(ctx_pickle) regular_predict = EmceeOptimizer.predict(initial_guess, **ctx) unpickle_predict = EmceeOptimizer.predict(initial_guess, **ctx_unpickled) assert np.isclose(regular_predict, unpickle_predict)
def test_zpf_context_is_pickleable(datasets_db): """Test that the context for ZPF data is pickleable""" datasets_db.insert(CU_MG_DATASET_ZPF_ZERO_ERROR) dbf = Database(CU_MG_TDB) symbols_to_fit = database_symbols_to_fit(dbf) initial_guess = np.array([unpack_piecewise(dbf.symbols[s]) for s in symbols_to_fit]) prior_dict = EmceeOptimizer.get_priors(None, symbols_to_fit, initial_guess) ctx = setup_context(dbf, datasets_db) ctx.update(prior_dict) ctx_pickle = pickle.dumps(ctx) ctx_unpickled = pickle.loads(ctx_pickle) regular_predict = EmceeOptimizer.predict(initial_guess, **ctx) unpickle_predict = EmceeOptimizer.predict(initial_guess, **ctx_unpickled) assert np.isclose(regular_predict, unpickle_predict)
def test_lnprob_does_not_raise_on_ValueError(datasets_db): """lnprob() should catch ValueError raised by equilibrium and return -np.inf""" dbf = Database.from_string(CU_MG_TDB, fmt='tdb') opt = EmceeOptimizer(dbf) comps = ['CU', 'MG', 'VA'] phases = ['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'] datasets_db.insert(CU_MG_DATASET_ZPF_WORKING) zpf_kwargs = {'dbf': dbf, 'phases': phases, 'zpf_data': get_zpf_data(comps, phases, datasets_db), 'data_weight': 1.0} res = opt.predict([10], prior_rvs=[rv_zero()], symbols_to_fit=['VV0001'], zpf_kwargs=zpf_kwargs) assert np.isneginf(res)
def test_lnprob_calculates_single_phase_probability_for_success(datasets_db): """lnprob() succesfully calculates the probability from single phase data""" dbf = Database.from_string(CU_MG_TDB_FCC_ONLY, fmt='tdb') datasets_db.insert(CU_MG_HM_MIX_SINGLE_FCC_A1) comps = ['CU', 'MG', 'VA'] phases = ['FCC_A1'] param = 'VV0003' orig_val = -14.0865 opt = EmceeOptimizer(dbf) thermochemical_data = get_thermochemical_data(dbf, comps, phases, datasets_db, symbols_to_fit=[param]) thermochemical_kwargs = { 'dbf': dbf, 'comps': comps, 'thermochemical_data': thermochemical_data } res_orig = opt.predict([orig_val], prior_rvs=[rv_zero()], symbols_to_fit=[param], thermochemical_kwargs=thermochemical_kwargs) assert np.isreal(res_orig) assert np.isclose(res_orig, -9.119484935312146, rtol=1e-6) res_10 = opt.predict([10], prior_rvs=[rv_zero()], symbols_to_fit=[param], thermochemical_kwargs=thermochemical_kwargs) assert np.isreal(res_10) assert np.isclose(res_10, -9.143559131626864, rtol=1e-6) res_1e5 = opt.predict([1e5], prior_rvs=[rv_zero()], symbols_to_fit=[param], thermochemical_kwargs=thermochemical_kwargs) assert np.isreal(res_1e5) assert np.isclose(res_1e5, -1359.1335466316268, rtol=1e-6)
def test_lnprob_calculates_associate_tdb(datasets_db): """lnprob() successfully calculates the probability for equilibrium """ dbf = Database.from_string(CU_MG_TDB_ASSOC, fmt='tdb') datasets_db.insert(CU_MG_DATASET_ZPF_WORKING) comps = ['CU', 'MG', 'VA'] phases = ['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'] param = 'VV0001' orig_val = dbf.symbols[param].args[0] initial_params = {param: orig_val} zpf_kwargs = { 'zpf_data': get_zpf_data(dbf, comps, phases, datasets_db, initial_params), 'data_weight': 1.0, } opt = EmceeOptimizer(dbf) res = opt.predict([10], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert np.isreal(res) assert not np.isinf(res) assert np.isclose(res, -31.309645520830344, rtol=1e-6) # The purpose of this part is to test that the driving forces (and probability) # are different than the case of VV0001 = 10. res_2 = opt.predict([-10000000], prior_rvs=[rv_zero()], symbols_to_fit=[param], zpf_kwargs=zpf_kwargs) assert np.isreal(res_2) assert not np.isinf(res_2) # Accept a large rtol becuase the results should be _very_ different assert not np.isclose(res_2, -31.309645520830344, rtol=1e-2)