def free_energy_calculation(dHdl, u_nk): logger("Fitting TI on dHdl ...") ti = TI().fit(dHdl) logger("Fitting BAR on u_nk ...") bar = BAR().fit(u_nk) logger("Fitting MBAR on u_nk ...\n") try: mbar_stop = False mbar = MBAR().fit(u_nk) except pymbar.utils.ParameterError: mbar_stop = True logger( "\sum_n W_nk is not equal to 1, probably due to insufficient overlap between states." ) logger("Stop using MBAR ...") logger("------ Results based on the whole dataset ------") if hasattr(dHdl, 'statineff') is True: logger(f'Statistical inefficiency of dHdl: {dHdl.statineff}') if hasattr(u_nk, 'statineff') is True: logger(f'Statistical inefficiency of u_nk: {u_nk.statineff}\n') logger(f"TI: {ti.delta_f_.iloc[0, -1]} +/- {ti.d_delta_f_.iloc[0, -1]} kT") logger(f"BAR: {bar.delta_f_.iloc[0, -1]} +/- unknown kT") if mbar_stop is False: logger( f"MBAR: {mbar.delta_f_.iloc[0, -1]} +/- {mbar.d_delta_f_.iloc[0, -1]} kT" ) logger("------------------------------------------------") return ti, bar, mbar else: logger("------------------------------------------------") return ti, bar
def test_plot_dF_state(): '''Just test if the plot runs''' bz = load_benzene().data u_nk_coul = pd.concat([extract_u_nk(xvg, T=300) for xvg in bz['Coulomb']]) dHdl_coul = pd.concat([extract_dHdl(xvg, T=300) for xvg in bz['Coulomb']]) u_nk_vdw = pd.concat([extract_u_nk(xvg, T=300) for xvg in bz['VDW']]) dHdl_vdw = pd.concat([extract_dHdl(xvg, T=300) for xvg in bz['VDW']]) ti_coul = TI().fit(dHdl_coul) ti_vdw = TI().fit(dHdl_vdw) bar_coul = BAR().fit(u_nk_coul) bar_vdw = BAR().fit(u_nk_vdw) mbar_coul = MBAR().fit(u_nk_coul) mbar_vdw = MBAR().fit(u_nk_vdw) dhdl_data = [ (ti_coul, ti_vdw), (bar_coul, bar_vdw), (mbar_coul, mbar_vdw), ] fig = plot_dF_state(dhdl_data, orientation='portrait') assert isinstance(fig, matplotlib.figure.Figure) fig = plot_dF_state(dhdl_data, orientation='landscape') assert isinstance(fig, matplotlib.figure.Figure) fig = plot_dF_state(dhdl_data, labels=['MBAR', 'TI', 'BAR']) assert isinstance(fig, matplotlib.figure.Figure) with pytest.raises(ValueError): fig = plot_dF_state(dhdl_data, labels=[ 'MBAR', 'TI', ]) fig = plot_dF_state(dhdl_data, colors=['#C45AEC', '#33CC33', '#F87431']) assert isinstance(fig, matplotlib.figure.Figure) with pytest.raises(ValueError): fig = plot_dF_state(dhdl_data, colors=['#C45AEC', '#33CC33']) with pytest.raises(NameError): fig = plot_dF_state(dhdl_data, orientation='xxx') fig = plot_dF_state(ti_coul, orientation='landscape') assert isinstance(fig, matplotlib.figure.Figure) fig = plot_dF_state(ti_coul, orientation='portrait') assert isinstance(fig, matplotlib.figure.Figure) fig = plot_dF_state([ti_coul, bar_coul]) assert isinstance(fig, matplotlib.figure.Figure) fig = plot_dF_state([(ti_coul, ti_vdw)]) assert isinstance(fig, matplotlib.figure.Figure)
def test_bar_namd(): """Test BAR calculation on NAMD data. """ from alchemlyb.estimators import BAR import numpy as np # load data dataset = load_tyr2ala() u_nk1 = extract_u_nk(dataset['data']['forward'][0]) u_nk2 = extract_u_nk(dataset['data']['backward'][0]) # combine dataframes of fwd and rev directions u_nk1.replace(0, np.nan, inplace=True) u_nk1[u_nk1.isnull()] = u_nk2 u_nk1.replace(np.nan, 0, inplace=True) u_nk = u_nk1.sort_index(level=u_nk1.index.names[1:]) # after loading BOTH fwd and rev data, do BAR calculation bar = BAR() bar.fit(u_nk) dg = (bar.delta_f_.iloc[0].iloc[-1]) assert_almost_equal(dg, 6.03126982925, decimal=4)
def test_bar(self, u_nk): bar = BAR().fit(u_nk) assert bar.delta_f_.attrs['temperature'] == 300 assert bar.delta_f_.attrs['energy_unit'] == 'kT' assert bar.d_delta_f_.attrs['temperature'] == 300 assert bar.d_delta_f_.attrs['energy_unit'] == 'kT'
#for TI estimator #print("Working on TI method ...") dHdl = pd.concat([extract_dHdl(f, T=temprature) for f in files]) ti = TI().fit(dHdl) sum_ti, sum_ds_ti = get_delta(ti) tis.append(sum_ti) #for MBAR estimator #print("Working on MBAR method ...") u_nk = pd.concat([extract_u_nk(f, T=temprature) for f in files]) mbar = MBAR().fit(u_nk) sum_mbar, sum_ds_mbar = get_delta(mbar) mbars.append(sum_mbar) #for BAR estimator #print("Working on BAR method ...") bar = BAR().fit(u_nk) sum_bar, sum_ds_bar = get_delta2(bar) bars.append(sum_bar) print("Replica-%d, %4s, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f" % (nr, com, sum_ti, sum_ds_ti, sum_mbar, sum_ds_mbar, sum_bar, sum_ds_bar)) tis = np.array(tis) mbars = np.array(mbars) bars = np.array(bars) print("Average, %4s, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f" % (com, np.average(tis), np.std(tis), np.average(mbars), np.std(mbars), np.average(bars), np.std(bars)))