def test_mbar_computeOverlap(): # tests with identical states, which gives analytical results. d = len(N_k) even_O_k = 2.0 * np.ones(d) even_K_k = 0.5 * np.ones(d) even_N_k = 100 * np.ones(d) name, test = generate_ho(O_k=even_O_k, K_k=even_K_k) x_n, u_kn, N_k_output, s_n = test.sample(even_N_k, mode='u_kn') mbar = MBAR(u_kn, even_N_k) overlap_scalar, eigenval, O = mbar.computeOverlap() reference_matrix = np.matrix((1.0 / d) * np.ones([d, d])) reference_eigenvalues = np.zeros(d) reference_eigenvalues[0] = 1.0 reference_scalar = np.float64(1.0) eq(O, reference_matrix, decimal=precision) eq(eigenval, reference_eigenvalues, decimal=precision) eq(overlap_scalar, reference_scalar, decimal=precision) # test of more straightforward examples for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn, N_k) overlap_scalar, eigenval, O = mbar.computeOverlap() # rows of matrix should sum to one sumrows = np.array(np.sum(O, axis=1)) eq(sumrows, np.ones(np.shape(sumrows)), decimal=precision) eq(eigenval[0], np.float64(1.0), decimal=precision)
def test_protocols(): ''' Test that free energy is moderately equal to analytical solution, independent of solver protocols ''' # Importing the hacky fix to asert that free energies are moderately correct from pymbar.tests.test_mbar import z_scale_factor name, u_kn, N_k, s_n, test = load_oscillators(50, 100, provide_test=True) fa = test.analytical_free_energies() fa = fa[1:] - fa[0] with suppress_derivative_warnings_for_tests(): # scipy.optimize.minimize methods, same ones that are checked for in mbar_solvers.py # subsampling_protocols = ['adaptive', 'L-BFGS-B', 'dogleg', 'CG', 'BFGS', 'Newton-CG', 'TNC', 'trust-ncg', 'SLSQP'] # scipy.optimize.root methods. Omitting methods which do not use the Jacobian. Adding the custom adaptive protocol. solver_protocols = ['adaptive', 'hybr', 'lm', 'L-BFGS-B', 'dogleg', 'CG', 'BFGS', 'Newton-CG', 'TNC', 'trust-ncg', 'SLSQP'] for solver_protocol in solver_protocols: # Solve MBAR with zeros for initial weights mbar = pymbar.MBAR(u_kn, N_k, solver_protocol=({'method': solver_protocol},)) # Solve MBAR with the correct f_k used for the inital weights mbar = pymbar.MBAR(u_kn, N_k, initial_f_k=mbar.f_k, solver_protocol=({'method': solver_protocol},)) results = mbar.getFreeEnergyDifferences() fe = results['Delta_f'][0,1:] fe_sigma = results['dDelta_f'][0,1:] z = (fe - fa) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeMultipleExpectations(): """Can MBAR calculate E(u_kn)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) A = np.zeros([2, len(x_n)]) A[0, :] = x_n A[1, :] = x_n**2 state = 1 results = mbar.computeMultipleExpectations(A, u_kn[state, :], return_dict=True) mu_t, sigma_t = mbar.computeMultipleExpectations(A, u_kn[state, :], return_dict=False) mu = results['mu'] sigma = results['sigma'] eq(mu, mu_t) eq(sigma, sigma_t) mu0 = test.analytical_observable(observable='position')[state] mu1 = test.analytical_observable(observable='position^2')[state] z = (mu0 - mu[0]) / sigma[0] eq(z / z_scale_factor, 0 * z, decimal=0) z = (mu1 - mu[1]) / sigma[1] eq(z / z_scale_factor, 0 * z, decimal=0)
def test_mbar_computePMF(): """ testing computePMF """ name, test = generate_ho() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn, N_k) #do a 1d PMF of the potential in the 3rd state: refstate = 2 dx = 0.25 xmin = test.O_k[refstate] - 1 xmax = test.O_k[refstate] + 1 within_bounds = (x_n >= xmin) & (x_n < xmax) bin_centers = dx * np.arange(np.int(xmin / dx), np.int(xmax / dx)) + dx / 2 bin_n = np.zeros(len(x_n), int) bin_n[within_bounds] = 1 + np.floor((x_n[within_bounds] - xmin) / dx) # 0 is reserved for samples outside the domain. We will ignore this state range = np.max(bin_n) + 1 results = mbar.computePMF(u_kn[refstate, :], bin_n, range, uncertainties='from-specified', pmf_reference=1) f_i = results['f_i'] df_i = results['df_i'] f0_i = 0.5 * test.K_k[refstate] * (bin_centers - test.O_k[refstate])**2 f_i, df_i = f_i[2:], df_i[ 2:] # first state is ignored, second is zero, with zero uncertainty normf0_i = f0_i[1:] - f0_i[0] # normalize to first state z = (f_i - normf0_i) / df_i eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computePMF(): """ testing computePMF """ name, test = generate_ho() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn,N_k) #do a 1d PMF of the potential in the 3rd state: refstate = 2 dx = 0.25 xmin = test.O_k[refstate] - 1 xmax = test.O_k[refstate] + 1 within_bounds = (x_n >= xmin) & (x_n < xmax) bin_centers = dx*np.arange(np.int(xmin/dx),np.int(xmax/dx)) + dx/2 bin_n = np.zeros(len(x_n),int) bin_n[within_bounds] = 1 + np.floor((x_n[within_bounds]-xmin)/dx) # 0 is reserved for samples outside the domain. We will ignore this state range = np.max(bin_n)+1 results = mbar.computePMF(u_kn[refstate,:], bin_n, range, uncertainties = 'from-specified', pmf_reference = 1) f_i = results['f_i'] df_i = results['df_i'] f0_i = 0.5*test.K_k[refstate]*(bin_centers-test.O_k[refstate])**2 f_i, df_i = f_i[2:], df_i[2:] # first state is ignored, second is zero, with zero uncertainty normf0_i = f0_i[1:] - f0_i[0] # normalize to first state z = (f_i - normf0_i) / df_i eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_protocols(): '''Test that free energy is moderatley equal to analytical solution, independent of solver protocols''' #Supress the warnings when jacobian and Hessian information is not used in a specific solver import warnings warnings.filterwarnings('ignore', '.*does not use the jacobian.*') warnings.filterwarnings('ignore', '.*does not use Hessian.*') from pymbar.tests.test_mbar import z_scale_factor # Importing the hacky fix to asert that free energies are moderatley correct name, u_kn, N_k, s_n, test = load_oscillators(50, 100, provide_test=True) fa = test.analytical_free_energies() fa = fa[1:] - fa[0] #scipy.optimize.minimize methods, same ones that are checked for in mbar_solvers.py subsampling_protocols = ["L-BFGS-B", "dogleg", "CG", "BFGS", "Newton-CG", "TNC", "trust-ncg", "SLSQP"] solver_protocols = ['hybr', 'lm'] #scipy.optimize.root methods. Omitting methods which do not use the Jacobian for subsampling_protocol in subsampling_protocols: for solver_protocol in solver_protocols: #Solve MBAR with zeros for initial weights mbar = pymbar.MBAR(u_kn, N_k, subsampling_protocol=({'method':subsampling_protocol},), solver_protocol=({'method':solver_protocol},)) #Solve MBAR with the correct f_k used for the inital weights mbar = pymbar.MBAR(u_kn, N_k, initial_f_k=mbar.f_k, subsampling_protocol=({'method':subsampling_protocol},), solver_protocol=({'method':solver_protocol},)) fe, fe_sigma, Theta_ij = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0,1:], fe_sigma[0,1:] z = (fe - fa) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0) #Clear warning filters warnings.resetwarnings()
def test_protocols(): ''' Test that free energy is moderately equal to analytical solution, independent of solver protocols ''' # Importing the hacky fix to asert that free energies are moderately correct from pymbar.tests.test_mbar import z_scale_factor name, u_kn, N_k, s_n, test = load_oscillators(50, 100, provide_test=True) fa = test.analytical_free_energies() fa = fa[1:] - fa[0] with suppress_derivative_warnings_for_tests(): # scipy.optimize.minimize methods, same ones that are checked for in mbar_solvers.py # subsampling_protocols = ['adaptive', 'L-BFGS-B', 'dogleg', 'CG', 'BFGS', 'Newton-CG', 'TNC', 'trust-ncg', 'SLSQP'] # scipy.optimize.root methods. Omitting methods which do not use the Jacobian. Adding the custom adaptive protocol. solver_protocols = ['adaptive', 'hybr', 'lm', 'L-BFGS-B', 'dogleg', 'CG', 'BFGS', 'Newton-CG', 'TNC', 'trust-ncg', 'SLSQP'] for solver_protocol in solver_protocols: # Solve MBAR with zeros for initial weights mbar = pymbar.MBAR(u_kn, N_k, solver_protocol=({'method': solver_protocol},)) # Solve MBAR with the correct f_k used for the inital weights mbar = pymbar.MBAR(u_kn, N_k, initial_f_k=mbar.f_k, solver_protocol=({'method': solver_protocol},)) results = mbar.getFreeEnergyDifferences(return_dict=True) fe = results['Delta_f'][0,1:] fe_sigma = results['dDelta_f'][0,1:] z = (fe - fa) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeOverlap(): # tests with identical states, which gives analytical results. d = len(N_k) even_O_k = 2.0*np.ones(d) even_K_k = 0.5*np.ones(d) even_N_k = 100*np.ones(d) name, test = generate_ho(O_k = even_O_k, K_k = even_K_k) x_n, u_kn, N_k_output, s_n = test.sample(even_N_k, mode='u_kn') mbar = MBAR(u_kn, even_N_k) overlap_scalar, eigenval, O = mbar.computeOverlap() reference_matrix = np.matrix((1.0/d)*np.ones([d,d])) reference_eigenvalues = np.zeros(d) reference_eigenvalues[0] = 1.0 reference_scalar = np.float64(1.0) eq(O, reference_matrix, decimal=precision) eq(eigenval, reference_eigenvalues, decimal=precision) eq(overlap_scalar, reference_scalar, decimal=precision) # test of more straightforward examples for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn, N_k) overlap_scalar, eigenval, O = mbar.computeOverlap() # rows of matrix should sum to one sumrows = np.array(np.sum(O,axis=1)) eq(sumrows, np.ones(np.shape(sumrows)), decimal=precision) eq(eigenval[0], np.float64(1.0), decimal=precision)
def test_logsumexp(): a = np.random.normal(size=(200, 500, 5)) for axis in range(a.ndim): ans_ne = pymbar.utils.logsumexp(a, axis=axis) ans_no_ne = pymbar.utils.logsumexp(a, axis=axis, use_numexpr=False) ans_scipy = scipy.misc.logsumexp(a, axis=axis) eq(ans_ne, ans_no_ne) eq(ans_ne, ans_scipy)
def test_logsumexp(): a = np.random.normal(size=(200, 500, 5)) for axis in range(a.ndim): ans_ne = pymbar.utils.logsumexp(a, axis=axis) ans_no_ne = pymbar.utils.logsumexp(a, axis=axis, use_numexpr=False) ans_scipy = logsumexp(a, axis=axis) eq(ans_ne, ans_no_ne) eq(ans_ne, ans_scipy)
def test_mbar_getWeights(): """ testing getWeights """ for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn, N_k) # rows should be equal to zero W = mbar.getWeights() sumrows = np.sum(W, axis=0) eq(sumrows, np.ones(len(sumrows)), decimal=precision)
def test_mbar_getWeights(): """ testing getWeights """ for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') mbar = MBAR(u_kn, N_k) # rows should be equal to zero W = mbar.getWeights() sumrows = np.sum(W,axis=0) eq(sumrows, np.ones(len(sumrows)), decimal=precision)
def test_mbar_computeExpectationsInner(): """Can MBAR calculate general expectations inner code (note: this just tests completion)""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) A_in = np.array([x_n, x_n**2, x_n**3]) u_n = u_kn[:2, :] state_map = np.array([[0, 0], [1, 0], [2, 0], [2, 1]], int) _ = mbar.computeExpectationsInner(A_in, u_n, state_map)
def test_mbar_computeExpectations_position_differences(): """Can MBAR calculate E(x_n)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu_ij, sigma_ij = mbar.computeExpectations(x_n, output='differences') mu0 = test.analytical_observable(observable='position') z = convert_to_differences(mu_ij, sigma_ij, mu0) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0)
def test_mbar_computeExpectations_position_differences(): """Can MBAR calculate E(x_n)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu_ij, sigma_ij = mbar.computeExpectations(x_n, output = 'differences') mu0 = test.analytical_observable(observable = 'position') z = convert_to_differences(mu_ij, sigma_ij, mu0) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0)
def test_mbar_computeExpectationsInner(): """Can MBAR calculate general expectations inner code (note: this just tests completion)""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) A_in = np.array([x_n, x_n ** 2, x_n ** 3]) u_n = u_kn[:2,:] state_map = np.array([[0,0],[1,0],[2,0],[2,1]],int) [A_i, d2A_ij] = mbar.computeExpectationsInner(A_in, u_n, state_map)
def test_mbar_computeExpectations_position_averages(): """Can MBAR calculate E(x_n)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu, sigma = mbar.computeExpectations(x_n) mu0 = test.analytical_observable(observable='position') z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeEntropyAndEnthalpy(): """Can MBAR calculate f_k, <u_k> and s_k ??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) results = mbar.computeEntropyAndEnthalpy(u_kn) f_ij = results['Delta_f'] df_ij = results['dDelta_f'] u_ij = results['Delta_u'] du_ij = results['dDelta_u'] s_ij = results['Delta_s'] ds_ij = results['dDelta_s'] fa = test.analytical_free_energies() ua = test.analytical_observable('potential energy') sa = test.analytical_entropies() fa_ij = np.array(np.matrix(fa) - np.matrix(fa).transpose()) ua_ij = np.array(np.matrix(ua) - np.matrix(ua).transpose()) sa_ij = np.array(np.matrix(sa) - np.matrix(sa).transpose()) z = convert_to_differences(f_ij, df_ij, fa) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0) z = convert_to_differences(u_ij, du_ij, ua) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0) z = convert_to_differences(s_ij, ds_ij, sa) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0)
def test_mbar_computeExpectations_potential(): """Can MBAR calculate E(u_kn)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu, sigma = mbar.computeExpectations(u_kn, state_dependent=True) mu0 = test.analytical_observable(observable='potential energy') print(mu) print(mu0) z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_xkn_squared(): """Harmonic Oscillators Test: can MBAR calculate E(x_kn^2)??""" test = harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k) x_kn, u_kln, N_k_output = test.sample(N_k) eq(N_k, N_k_output) mbar = MBAR(u_kln, N_k) mu, sigma = mbar.computeExpectations(x_kn ** 2) mu0 = test.analytical_x_squared() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_xkn_squared(): """Exponential Distribution Test: can MBAR calculate E(x_kn^2)""" test = exponential_distributions.ExponentialTestCase(rates) x_kn, u_kln, N_k_output = test.sample(N_k) eq(N_k, N_k_output) mbar = MBAR(u_kln, N_k) mu, sigma = mbar.computeExpectations(x_kn ** 2) mu0 = test.analytical_x_squared() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeExpectations_position_averages(): """Can MBAR calculate E(x_n)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu, sigma = mbar.computeExpectations(x_n) mu0 = test.analytical_observable(observable = 'position') z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeEffectiveSampleNumber(): """ testing computeEffectiveSampleNumber """ for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) # one mathematical effective sample numbers should be between N_k and sum_k N_k N_eff = mbar.computeEffectiveSampleNumber() sumN = np.sum(N_k) assert all(N_eff > N_k) assert all(N_eff < sumN)
def test_mbar_computeEntropyAndEnthalpy(): """Can MBAR calculate f_k, <u_k> and s_k ??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) f_ij, df_ij, u_ij, du_ij, s_ij, ds_ij = mbar.computeEntropyAndEnthalpy(u_kn) fa = test.analytical_free_energies() ua = test.analytical_observable('potential energy') sa = test.analytical_entropies() fa_ij = np.array(np.matrix(fa) - np.matrix(fa).transpose()) ua_ij = np.array(np.matrix(ua) - np.matrix(ua).transpose()) sa_ij = np.array(np.matrix(sa) - np.matrix(sa).transpose()) z = convert_to_differences(f_ij,df_ij,fa) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0) z = convert_to_differences(u_ij,du_ij,ua) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0) z = convert_to_differences(s_ij,ds_ij,sa) eq(z / z_scale_factor, np.zeros(np.shape(z)), decimal=0)
def test_mbar_computeExpectations_potential(): """Can MBAR calculate E(u_kn)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) mu, sigma = mbar.computeExpectations(u_kn, state_dependent = True) mu0 = test.analytical_observable(observable = 'potential energy') print(mu) print(mu0) z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeExpectations_position2(): """Can MBAR calculate E(x_n^2)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) results = mbar.computeExpectations(x_n**2, return_dict=True) mu = results['mu'] sigma = results['sigma'] mu0 = test.analytical_observable(observable='position^2') z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_free_energies(): """Exponential Distribution Test: can MBAR calculate correct free energy differences?""" test = exponential_distributions.ExponentialTestCase(rates) x_kn, u_kln, N_k_output = test.sample(N_k, mode='u_kln') eq(N_k, N_k_output) mbar = MBAR(u_kln, N_k) fe, fe_sigma = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0,1:], fe_sigma[0,1:] fe0 = test.analytical_free_energies() fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_protocols(): '''Test that free energy is moderatley equal to analytical solution, independent of solver protocols''' #Supress the warnings when jacobian and Hessian information is not used in a specific solver import warnings warnings.filterwarnings('ignore', '.*does not use the jacobian.*') warnings.filterwarnings('ignore', '.*does not use Hessian.*') from pymbar.tests.test_mbar import z_scale_factor # Importing the hacky fix to asert that free energies are moderatley correct name, u_kn, N_k, s_n, test = load_oscillators(50, 100, provide_test=True) fa = test.analytical_free_energies() fa = fa[1:] - fa[0] #scipy.optimize.minimize methods, same ones that are checked for in mbar_solvers.py subsampling_protocols = [ "L-BFGS-B", "dogleg", "CG", "BFGS", "Newton-CG", "TNC", "trust-ncg", "SLSQP" ] solver_protocols = [ 'hybr', 'lm' ] #scipy.optimize.root methods. Omitting methods which do not use the Jacobian for subsampling_protocol in subsampling_protocols: for solver_protocol in solver_protocols: #Solve MBAR with zeros for initial weights mbar = pymbar.MBAR(u_kn, N_k, subsampling_protocol=({ 'method': subsampling_protocol }, ), solver_protocol=({ 'method': solver_protocol }, )) #Solve MBAR with the correct f_k used for the inital weights mbar = pymbar.MBAR(u_kn, N_k, initial_f_k=mbar.f_k, subsampling_protocol=({ 'method': subsampling_protocol }, ), solver_protocol=({ 'method': solver_protocol }, )) fe, fe_sigma, Theta_ij = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0, 1:], fe_sigma[0, 1:] z = (fe - fa) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0) #Clear warning filters warnings.resetwarnings()
def test_harmonic_oscillators_mbar_free_energies(): """Harmonic Oscillators Test: can MBAR calculate correct free energy differences?""" test = harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k) x_kn, u_kln, N_k_output = test.sample(N_k) eq(N_k, N_k_output) mbar = MBAR(u_kln, N_k) fe, fe_sigma = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0,1:], fe_sigma[0,1:] fe0 = test.analytical_free_energies() fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_free_energies(): """Can MBAR calculate moderately correct free energy differences?""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) fe, fe_sigma, Theta_ij = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0, 1:], fe_sigma[0, 1:] fe0 = test.analytical_free_energies() fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_harmonic_oscillators_mbar_free_energies(): """Harmonic Oscillators Test: can MBAR calculate correct free energy differences?""" test = harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) fe, fe_sigma = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0], fe_sigma[0] fe0 = test.analytical_free_energies() z = (fe - fe0) / fe_sigma z = z[1:] # First component is undetermined. eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_free_energies(): """Exponential Distribution Test: can MBAR calculate correct free energy differences?""" test = exponential_distributions.ExponentialTestCase(rates) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) fe, fe_sigma = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0], fe_sigma[0] fe0 = test.analytical_free_energies() z = (fe - fe0) / fe_sigma z = z[1:] # First component is undetermined. eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computePerturbedFreeEnergeies(): """ testing computePerturbedFreeEnergies """ for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') numN = np.sum(N_k[:2]) mbar = MBAR(u_kn[:2, :numN], N_k[:2]) # only do MBAR with the first and last set fe, fe_sigma = mbar.computePerturbedFreeEnergies(u_kn[2:, :numN]) fe, fe_sigma = fe[0, 1:], fe_sigma[0, 1:] print(fe, fe_sigma) fe0 = test.analytical_free_energies()[2:] fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computePerturbedFreeEnergeies(): """ testing computePerturbedFreeEnergies """ for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') numN = np.sum(N_k[:2]) mbar = MBAR(u_kn[:2,:numN], N_k[:2]) # only do MBAR with the first and last set fe, fe_sigma = mbar.computePerturbedFreeEnergies(u_kn[2:,:numN]) fe, fe_sigma = fe[0,1:], fe_sigma[0,1:] print(fe, fe_sigma) fe0 = test.analytical_free_energies()[2:] fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_free_energies(): """Can MBAR calculate moderately correct free energy differences?""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) fe, fe_sigma, Theta_ij = mbar.getFreeEnergyDifferences() fe, fe_sigma = fe[0,1:], fe_sigma[0,1:] fe0 = test.analytical_free_energies() fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_compare_detectEquil(show_hist=False): """ compare detectEquilibration implementations (with and without binary search + fft) """ t_res = [] N=100 for _ in xrange(100): A_t = testsystems.correlated_timeseries_example(N=N, tau=5.0) + 2.0 B_t = testsystems.correlated_timeseries_example(N=N, tau=5.0) + 1.0 C_t = testsystems.correlated_timeseries_example(N=N*2, tau=5.0) D_t = np.concatenate([A_t, B_t, C_t, np.zeros(20)]) #concatenate and add flat region to one end (common in MC data) bs_de = timeseries.detectEquilibration_binary_search(D_t, bs_nodes=10) std_de = timeseries.detectEquilibration(D_t, fast=False, nskip=1) t_res.append(bs_de[0]-std_de[0]) t_res_mode = float(stats.mode(t_res)[0][0]) eq(t_res_mode,0.,decimal=1) if show_hist: import matplotlib.pyplot as plt plt.hist(t_res) plt.show()
def test_exponential_mbar_xkn_squared(): """Exponential Distribution Test: can MBAR calculate E(x_kn^2)""" test = exponential_distributions.ExponentialTestCase(rates) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) x_kn = convert_xn_to_x_kn(x_n) ** 2.0 x_kn = x_kn.values # Convert to numpy for MBAR x_kn[np.isnan(x_kn)] = 0.0 # Convert nans to 0.0 mu, sigma = mbar.computeExpectations(x_kn) mu0 = test.analytical_x_squared() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_compare_detectEquil(show_hist=False): """ compare detectEquilibration implementations (with and without binary search + fft) """ t_res = [] N = 100 for _ in xrange(100): A_t = testsystems.correlated_timeseries_example(N=N, tau=5.0) + 2.0 B_t = testsystems.correlated_timeseries_example(N=N, tau=5.0) + 1.0 C_t = testsystems.correlated_timeseries_example(N=N * 2, tau=5.0) D_t = np.concatenate([A_t, B_t, C_t]) bs_de = timeseries.detectEquilibration_binary_search(D_t, bs_nodes=10) std_de = timeseries.detectEquilibration(D_t, fast=False, nskip=1) t_res.append(bs_de[0] - std_de[0]) t_res_mode = float(stats.mode(t_res)[0][0]) eq(t_res_mode, 0., decimal=1) if show_hist: import matplotlib.pyplot as plt plt.hist(t_res) plt.show()
def test_exponential_mbar__xkn(): """Harmonic Oscillators Test: can MBAR calculate E(x_kn)??""" test = harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) x_kn = convert_xn_to_x_kn(x_n) x_kn = x_kn.values # Convert to numpy for MBAR x_kn[np.isnan(x_kn)] = 0.0 # Convert nans to 0.0 mu, sigma = mbar.computeExpectations(x_kn) mu0 = test.analytical_means() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_xkn_squared(): """Harmonic Oscillators Test: can MBAR calculate E(x_kn^2)??""" test = harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) x_kn = convert_xn_to_x_kn(x_n) ** 2. x_kn = x_kn.values # Convert to numpy for MBAR x_kn[np.isnan(x_kn)] = 0.0 # Convert nans to 0.0 mu, sigma = mbar.computeExpectations(x_kn) mu0 = test.analytical_x_squared() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_exponential_mbar_xkn(): """Exponential Distribution Test: can MBAR calculate E(x_kn)?""" test = exponential_distributions.ExponentialTestCase(rates) x_n, u_kn, origin = test.sample(N_k) u_ijn, N_k_output = convert_ukn_to_uijn(u_kn) eq(N_k, N_k_output.values) mbar = MBAR(u_ijn.values, N_k) x_kn = convert_xn_to_x_kn(x_n) x_kn = x_kn.values # Convert to numpy for MBAR x_kn[np.isnan(x_kn)] = 0.0 # Convert nans to 0.0 mu, sigma = mbar.computeExpectations(x_kn) mu0 = test.analytical_means() z = (mu0 - mu) / sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_statistical_inefficiency_fft(): X, Y, energy = generate_data() timeseries.statisticalInefficiency_fft(X[0]) timeseries.statisticalInefficiency_fft(X[0]**2) timeseries.statisticalInefficiency_fft(energy[0]) g0 = timeseries.statisticalInefficiency_fft(X[0]) g1 = timeseries.statisticalInefficiency(X[0]) g2 = timeseries.statisticalInefficiency(X[0], X[0]) g3 = timeseries.statisticalInefficiency(X[0], fft=True) eq(g0, g1) eq(g0, g2) eq(g0, g3)
def test_statistical_inefficiency_fft(): X, Y, energy = generate_data() timeseries.statisticalInefficiency_fft(X[0]) timeseries.statisticalInefficiency_fft(X[0] ** 2) timeseries.statisticalInefficiency_fft(energy[0]) g0 = timeseries.statisticalInefficiency_fft(X[0]) g1 = timeseries.statisticalInefficiency(X[0]) g2 = timeseries.statisticalInefficiency(X[0], X[0]) g3 = timeseries.statisticalInefficiency(X[0], fft=True) eq(g0, g1) eq(g0, g2) eq(g0, g3)
def test_mbar_free_energies(): """Can MBAR calculate moderately correct free energy differences?""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) results = mbar.getFreeEnergyDifferences(return_dict=True) fe_t, dfe_t = mbar.getFreeEnergyDifferences(return_dict=False) fe = results['Delta_f'] fe_sigma = results['dDelta_f'] eq(fe, fe_t) eq(fe_sigma, dfe_t) fe, fe_sigma = fe[0, 1:], fe_sigma[0, 1:] fe0 = test.analytical_free_energies() fe0 = fe0[1:] - fe0[0] z = (fe - fe0) / fe_sigma eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
def test_mbar_computeMultipleExpectations(): """Can MBAR calculate E(u_kn)??""" for system_generator in system_generators: name, test = system_generator() x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn') eq(N_k, N_k_output) mbar = MBAR(u_kn, N_k) A = np.zeros([2,len(x_n)]) A[0,:] = x_n A[1,:] = x_n**2 state = 1 mu, sigma, covariances = mbar.computeMultipleExpectations(A,u_kn[state,:]) mu0 = test.analytical_observable(observable = 'position')[state] mu1 = test.analytical_observable(observable = 'position^2')[state] z = (mu0 - mu[0]) / sigma[0] eq(z / z_scale_factor, 0*z, decimal=0) z = (mu1 - mu[1]) / sigma[1] eq(z / z_scale_factor, 0*z, decimal=0)
def test_statistical_inefficiency_fft_gaussian(): # Run multiple times to get things with and without negative "spikes" at C(1) for i in range(5): x = np.random.normal(size=100000) g0 = timeseries.statisticalInefficiency(x, fast=False) g1 = timeseries.statisticalInefficiency(x, x, fast=False) g2 = timeseries.statisticalInefficiency_fft(x) g3 = timeseries.statisticalInefficiency(x, fft=True) eq(g0, g1, decimal=5) eq(g0, g2, decimal=5) eq(g0, g3, decimal=5) eq(np.log(g0), np.log(1.0), decimal=1) for i in range(5): x = np.random.normal(size=100000) x = np.repeat( x, 3 ) # e.g. Construct correlated gaussian e.g. [a, b, c] -> [a, a, a, b, b, b, c, c, c] g0 = timeseries.statisticalInefficiency(x, fast=False) g1 = timeseries.statisticalInefficiency(x, x, fast=False) g2 = timeseries.statisticalInefficiency_fft(x) g3 = timeseries.statisticalInefficiency(x, fft=True) eq(g0, g1, decimal=5) eq(g0, g2, decimal=5) eq(g0, g3, decimal=5) eq(np.log(g0), np.log(3.0), decimal=1)
def _test(data_generator): name, U, N_k, s_n = data_generator() print(name) mbar = pymbar.MBAR(U, N_k) eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8) eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10) eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10) eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10) # Test against old MBAR code. with suppress_derivative_warnings_for_tests(): mbar0 = pymbar.old_mbar.MBAR(U, N_k) eq(mbar.f_k, mbar0.f_k, decimal=8) eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5)
def _test(data_generator): try: name, U, N_k, s_n = data_generator() except IOError as exc: raise(SkipTest("Cannot load dataset; skipping test. Try downloading pymbar-datasets GitHub repository and setting PYMBAR-DATASETS environment variable. Error was '%s'" % exc)) except ImportError as exc: raise(SkipTest("Error importing pytables to load dataset; skipping test. Error was '%s'" % exc)) print(name) mbar = pymbar.MBAR(U, N_k) fij1, dfij1, Theta_ij = mbar.getFreeEnergyDifferences(uncertainty_method="svd") fij2, dfij2, Theta_ij = mbar.getFreeEnergyDifferences(uncertainty_method="svd-ew") eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8) eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10) eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10) eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10) # Test against old MBAR code. mbar0 = pymbar.old_mbar.MBAR(U, N_k) fij0, dfij0 = mbar0.getFreeEnergyDifferences(uncertainty_method="svd") eq(mbar.f_k, mbar0.f_k, decimal=8) eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5) eq(fij0, fij1, decimal=8) eq(dfij0, dfij1, decimal=8) eq(fij0, fij2, decimal=8) eq(dfij0, dfij2, decimal=8)
def test_logsum(): u = np.random.normal(size=(200)) y1 = pymbar.utils.logsumexp(u) y2 = pymbar.utils._logsum(u) eq(y1, y2, decimal=12)
def _test(data_generator): name, U, N_k, s_n = data_generator() print(name) mbar = pymbar.MBAR(U, N_k) fij1, dfij1, Theta_ij = mbar.getFreeEnergyDifferences( uncertainty_method="svd") fij2, dfij2, Theta_ij = mbar.getFreeEnergyDifferences( uncertainty_method="svd-ew") eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8) eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10) eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10) eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10) # Test against old MBAR code. mbar0 = pymbar.old_mbar.MBAR(U, N_k) fij0, dfij0 = mbar0.getFreeEnergyDifferences(uncertainty_method="svd") eq(mbar.f_k, mbar0.f_k, decimal=8) eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5) eq(fij0, fij1, decimal=8) eq(dfij0, dfij1, decimal=8) eq(fij0, fij2, decimal=8) eq(dfij0, dfij2, decimal=8)
def _test(data_generator): name, U, N_k, s_n = data_generator() print(name) mbar = pymbar.MBAR(U, N_k) results1 = mbar.getFreeEnergyDifferences(uncertainty_method="svd", return_dict=True) fij1_t, dfij1_t = mbar.getFreeEnergyDifferences(uncertainty_method="svd", return_dict=False) results2 = mbar.getFreeEnergyDifferences(uncertainty_method="svd-ew", return_dict=True) fij1 = results1['Delta_f'] dfij1 = results1['dDelta_f'] fij2 = results2['Delta_f'] dfij2 = results2['dDelta_f'] # Check to make sure the returns from with and w/o dict are the same eq(fij1, fij1_t) eq(dfij1, dfij1_t) eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8) eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10) eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10) eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10) # Test against old MBAR code. with suppress_derivative_warnings_for_tests(): mbar0 = pymbar.old_mbar.MBAR(U, N_k) fij0, dfij0 = mbar0.getFreeEnergyDifferences(uncertainty_method="svd") eq(mbar.f_k, mbar0.f_k, decimal=8) eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5) eq(fij0, fij1, decimal=8) eq(dfij0, dfij1, decimal=8) eq(fij0, fij2, decimal=8) eq(dfij0, dfij2, decimal=8)
def test_statistical_inefficiency_fft_gaussian(): # Run multiple times to get things with and without negative "spikes" at C(1) for i in range(5): x = np.random.normal(size=100000) g0 = timeseries.statisticalInefficiency(x, fast=False) g1 = timeseries.statisticalInefficiency(x, x, fast=False) g2 = timeseries.statisticalInefficiency_fft(x) g3 = timeseries.statisticalInefficiency(x, fft=True) eq(g0, g1, decimal=5) eq(g0, g2, decimal=5) eq(g0, g3, decimal=5) eq(np.log(g0), np.log(1.0), decimal=1) for i in range(5): x = np.random.normal(size=100000) x = np.repeat(x, 3) # e.g. Construct correlated gaussian e.g. [a, b, c] -> [a, a, a, b, b, b, c, c, c] g0 = timeseries.statisticalInefficiency(x, fast=False) g1 = timeseries.statisticalInefficiency(x, x, fast=False) g2 = timeseries.statisticalInefficiency_fft(x) g3 = timeseries.statisticalInefficiency(x, fft=True) eq(g0, g1, decimal=5) eq(g0, g2, decimal=5) eq(g0, g3, decimal=5) eq(np.log(g0), np.log(3.0), decimal=1)
def test_subsampling(): name, u_kn, N_k, s_n = load_exponentials(5, 20000) mbar = pymbar.MBAR(u_kn, N_k) u_kn_sub, N_k_sub = pymbar.mbar_solvers.subsample_data(u_kn, N_k, s_n, 2) mbar_sub = pymbar.MBAR(u_kn_sub, N_k_sub) eq(mbar.f_k, mbar_sub.f_k, decimal=2)
def _test(data_generator): name, U, N_k, s_n = data_generator() print(name) mbar = pymbar.MBAR(U, N_k) results1 = mbar.getFreeEnergyDifferences(uncertainty_method="svd") results2 = mbar.getFreeEnergyDifferences(uncertainty_method="svd-ew") fij1 = results1['Delta_f'] dfij1 = results1['dDelta_f'] fij2 = results2['Delta_f'] dfij2 = results2['dDelta_f'] eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8) eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10) eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10) eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10) # Test against old MBAR code. with suppress_derivative_warnings_for_tests(): mbar0 = pymbar.old_mbar.MBAR(U, N_k) fij0, dfij0 = mbar0.getFreeEnergyDifferences(uncertainty_method="svd") eq(mbar.f_k, mbar0.f_k, decimal=8) eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5) eq(fij0, fij1, decimal=8) eq(dfij0, dfij1, decimal=8) eq(fij0, fij2, decimal=8) eq(dfij0, dfij2, decimal=8)