Ejemplo n.º 1
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)
        results = mbar.computeExpectations(x_n, return_dict=True)
        mu_t, sigma_t = mbar.computeExpectations(x_n, return_dict=False)
        mu = results['mu']
        sigma = results['sigma']

        eq(mu, mu_t)
        eq(sigma, sigma_t)

        mu0 = test.analytical_observable(observable='position')

        z = (mu0 - mu) / sigma
        eq(z / z_scale_factor, np.zeros(len(z)), decimal=0)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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_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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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():
    """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)
Ejemplo n.º 9
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():
    """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_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_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():
    """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)
Ejemplo n.º 14
0
  
  # observable for estimation is the position
  elif observe == 'position':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]

  # observable for estimation is the position^2
  elif observe == 'position^2':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2

  (A_k_estimated, dA_k_estimated) = mbar.computeExpectations(A_kn, state_dependent = state_dependent)

  # need to additionally transform to get the square root
  if observe == 'RMS displacement':
    A_k_estimated = numpy.sqrt(A_k_estimated)
    # Compute error from analytical observable estimate.
    dA_k_estimated = dA_k_estimated/(2*A_k_estimated)

  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages - not defined if no samples
  nonzeros = numpy.arange(K)[Nk_ne_zero]

  totaln = 0
  for k in nonzeros:
#MRS: 1001 for state 0, 1.00 for state 2, 985 for state 3

# MRS: The observable we are interested in is U, internal energy.  The
# question is, WHICH internal energy.  We are interested in the
# internal energy generated from the ith potential.  So there are
# actually _three_ observables.

# Now, the confusing thing, we can estimate the expectation of the
# three observables in three different states. We can estimate the
# observable of U_0 in states 0, 1 and 2, the observable of U_1 in
# states 0, 1, and 2, etc.

EAk = np.zeros([3, 3])
dEAk = np.zeros([3, 3])
(EAk[:, 0], dEAk[:, 0]) = mbar.computeExpectations(
    U_00
)  # potential energy of 0, estimated in state 0:2 (sampled from just 0)
(EAk[:, 1], dEAk[:, 1]) = mbar.computeExpectations(
    U_01
)  # potential energy of 1, estimated in state 0:2 (sampled from just 0)
(EAk[:, 2], dEAk[:, 2]) = mbar.computeExpectations(
    U_02
)  # potential energy of 2, estimated in state 0:2 (sampled from just 0)

# MRS: Some of these are of no practical importance.  We are most
# interested in the observable of U_0 in the 0th state, U_1 in the 1st
# state, and U_2 in the 2nd state, or the diagonal of the matrix EA.
EA = EAk.diagonal()
dEA = dEAk.diagonal()

#RM: Perhaps I am implementing MBAR improperly. Here I have reevaluated the expectation values
Ejemplo n.º 16
0
  
  # observable for estimation is the position
  elif observe == 'position':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]

  # observable for estimation is the position^2
  elif observe == 'position^2':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2

  results = mbar.computeExpectations(A_kn, state_dependent = state_dependent)
  A_k_estimated = results['mu']
  dA_k_estimated = results['sigma']

  # need to additionally transform to get the square root
  if observe == 'RMS displacement':
    A_k_estimated = numpy.sqrt(A_k_estimated)
    # Compute error from analytical observable estimate.
    dA_k_estimated = dA_k_estimated/(2*A_k_estimated)

  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages - not defined if no samples
  nonzeros = numpy.arange(K)[Nk_ne_zero]
Ejemplo n.º 17
0
        print "Loading from %s." % path_dir
        path_LogL = os.path.join(path_dir, 'LogLikelihood.dat')
        path_w = os.path.join(path_dir, 'w_params.dat')
        path_v = os.path.join(path_dir, 'v_params.dat')
        energies_LogL[k, :] = np.loadtxt(path_LogL)[-N:]
        obs_w[k, :] = np.loadtxt(path_w)[-N:, 0]
        obs_v[k, :] = np.loadtxt(path_v)[-N:, 0]

print "Preparing data..."
u_kln, N_k = prepare_mbar_input(energies=energies_LogL,
                                temps=temps[:ntemps],
                                N_max=N)

print "Running MBAR..."
mbar = MBAR(u_kln,
            N_k,
            relative_tolerance=2.0e-2,
            verbose=True,
            method='self-consistent-iteration')
# compute observable expectations
A_k = np.zeros(N * K, np.float64)
index = 0
for i in range(len(obs_w)):
    A_k[index:len(obs_w[i])] = obs_w[i]
    index = index + len(obs_w[i])
P, dP = mbar.computeExpectations(obs_w, uncertainty_method='approximate')
print "P:"
print P
print "dP"
print dP
Ejemplo n.º 18
0
    # observable for estimation is the position
    elif observe == 'position':
        state_dependent = False
        A_kn = numpy.zeros([K, N_max], dtype=numpy.float64)
        for k in range(0, K):
            A_kn[k, 0:N_k[k]] = x_kn[k, 0:N_k[k]]

    # observable for estimation is the position^2
    elif observe == 'position^2':
        state_dependent = False
        A_kn = numpy.zeros([K, N_max], dtype=numpy.float64)
        for k in range(0, K):
            A_kn[k, 0:N_k[k]] = x_kn[k, 0:N_k[k]]**2

    results = mbar.computeExpectations(A_kn, state_dependent=state_dependent)
    A_k_estimated = results['mu']
    dA_k_estimated = results['sigma']

    # need to additionally transform to get the square root
    if observe == 'RMS displacement':
        A_k_estimated = numpy.sqrt(A_k_estimated)
        # Compute error from analytical observable estimate.
        dA_k_estimated = dA_k_estimated / (2 * A_k_estimated)

    As_k_estimated = numpy.zeros([K], numpy.float64)
    dAs_k_estimated = numpy.zeros([K], numpy.float64)

    # 'standard' expectation averages - not defined if no samples
    nonzeros = numpy.arange(K)[Nk_ne_zero]
Ejemplo n.º 19
0
    def _reweight_observables(
        self,
        weights: ObservableArray,
        mbar: pymbar.MBAR,
        target_reduced_potentials: ObservableArray,
        **observables: ObservableArray,
    ) -> typing.Union[ObservableArray, Observable]:
        """A function which computes the average value of an observable using
        weights computed from MBAR and from a set of component observables.

        Parameters
        ----------
        weights
            The MBAR weights
        observables
            The component observables which may be combined to yield the final
            average observable of interest.
        mbar
            A pre-computed MBAR object encoded information from the reference states.
            This will be used to compute the std error when not bootstrapping.
        target_reduced_potentials
            The reduced potentials at the target state. This will be used to compute
            the std error when not bootstrapping.

        Returns
        -------
            The re-weighted average observable.
        """

        observable = observables.pop("observable")
        assert len(observables) == 0

        return_type = ObservableArray if observable.value.shape[
            1] > 1 else Observable

        weighted_observable = weights * observable

        average_value = weighted_observable.value.sum(axis=0)
        average_gradients = [
            ParameterGradient(key=gradient.key,
                              value=gradient.value.sum(axis=0))
            for gradient in weighted_observable.gradients
        ]

        if return_type == Observable:

            average_value = average_value.item()
            average_gradients = [
                ParameterGradient(key=gradient.key,
                                  value=gradient.value.item())
                for gradient in average_gradients
            ]

        else:

            average_value = average_value.reshape(1, -1)
            average_gradients = [
                ParameterGradient(key=gradient.key,
                                  value=gradient.value.reshape(1, -1))
                for gradient in average_gradients
            ]

        if self.bootstrap_uncertainties is False:

            # Unfortunately we need to re-compute the average observable for now
            # as pymbar does not expose an easier way to compute the average
            # uncertainty.
            observable_dimensions = observable.value.shape[1]
            assert observable_dimensions == 1

            results = mbar.computeExpectations(
                observable.value.T.magnitude,
                target_reduced_potentials.value.T.magnitude,
                state_dependent=True,
            )

            uncertainty = results[1][-1] * observable.value.units
            average_value = average_value.plus_minus(uncertainty)

        return return_type(value=average_value, gradients=average_gradients)
Ejemplo n.º 20
0
                msmle.solve_uwham(f1)
                f1_bs[trial] = msmle.f
                f1_bs[trial] -= msmle.f[0]
                xbar1_bs[trial] = msmle.compute_expectations(test.x_jn, False)[0]
            ferr1_bs = f1_bs.std(axis=0)[1:]
            varxbar1_bs = xbar1_bs.var(axis=0)
            msmle.revert_sample()
        f1 = f1[1:]

        if do_pymbar:
            try:
                mbar = MBAR(test.data, test.data_size)
                f2, ferr2, t = mbar.getFreeEnergyDifferences()
                f2 = f2[0][1:]
                ferr2 = ferr2[0][1:]
                xbar2, varxbar2 = mbar.computeExpectations(test.x_jn)
                skipmbar = False
            except:
                print('MBAR choked!')
                skipmbar = True
                pass
        else:
            skipmbar = True

        def print_float_array(msg, arr):
            print('%-16s '%msg + ' '.join(('% 6.4f'%x for x in arr)))

        print('samples:', test.data_size)
        print_float_array('actual energies', f3)

        print_float_array('uwham energies', f1)
Ejemplo n.º 21
0
u_kn[1,len(u00):] = u11    
u_kn[2,:len(u00)] = u20
u_kn[2,len(u00):] = u21   
     
N_kn = np.zeros(len(u00)+len(u11))
N_kn[:len(u00)] = N0
N_kn[len(u00):] = N1     
              
mbar = MBAR(u_kn,N_k)

Deltaf_ij = mbar.getFreeEnergyDifferences(return_theta=False)
print "effective sample numbers"
print (mbar.computeEffectiveSampleNumber())
print('\nWhich is approximately '+str(mbar.computeEffectiveSampleNumber()/N_K*100.)+'%')

NAk, dNAk = mbar.computeExpectations(N_kn) # Average number of molecules
NAk_alt = np.zeros(len(N_k))
for i in range(len(N_k)):
    NAk_alt[i] = np.sum(mbar.W_nk[:,i]*N_kn)
    
print(NAk)

Nscan = np.arange(60,100)

sqdeltaW0 = np.zeros(len(Nscan))

for iN, Ni in enumerate(Nscan):

    sumWliq = np.sum(mbar.W_nk[:,0][N_kn>Ni])
    sumWvap = np.sum(mbar.W_nk[:,0][N_kn<=Ni])
    sqdeltaW0[iN] = (sumWliq - sumWvap)**2
# Save the uncertainty in the free energy
   uncertainties_for_each_num_states.extend([uncertainty_free_energies])
# Get the dimensionless free energy differences and uncertainties for the uniform sampling approach
   free_energies,uncertainty_free_energies = mbar_same_total_samples.getFreeEnergyDifferences()[0],mbar_same_total_samples.getFreeEnergyDifferences()[1]
# Save the data
   free_energies_for_each_num_states_same_total_samples.extend([free_energies])
   uncertainties_for_each_num_states_same_total_samples.extend([uncertainty_free_energies])

#############
#
# 7) Calculate < R_Na-Cl > with MBAR
#
#############

# Get < R_Na-Cl >, and uncertainty, with MBAR
   averages, uncertainty = mbar.computeExpectations(distances)[0],mbar.computeExpectations(distances)[1]
# Save < R_Na-Cl >, and uncertainty
   distance_averages_for_each_num_states.extend([averages]) 
   uncertainty_distances_for_each_num_states.extend([uncertainty])
# Get < R_Na-Cl >, and uncertainty, with uniform sampling
   averages, uncertainty = mbar_same_total_samples.computeExpectations(distances_same_total_samples)[0],mbar_same_total_samples.computeExpectations(distances_same_total_samples)[1]
# Save the data
   distance_averages_for_each_num_states_same_total_samples.extend([averages])
   uncertainty_distances_for_each_num_states_same_total_samples.extend([uncertainty])

#############
#
# 8) Analyze uncertainty in < R_Na-Cl > with MBAR as a function of ensemble size
#
#############
Ejemplo n.º 23
0
    def MBAR_analysis(self, debug=False):
        """MBAR analysis for populations and BICePs score"""

        # load necessary data first
        self.load_data()

        # Suppose the energies sampled from each simulation are u_kln, where u_kln[k,l,n] is the reduced potential energy
        #   of snapshot n \in 1,...,N_k of simulation k \in 1,...,K evaluated at reduced potential for state l.
        self.K = self.nlambda  # number of thermodynamic ensembles
        # N_k[k] will denote the number of correlated snapshots from state k
        N_k = np.array(
            [len(self.traj[i]['trajectory']) for i in range(self.nlambda)])
        nsnaps = N_k.max()
        u_kln = np.zeros((self.K, self.K, nsnaps))
        nstates = int(self.states)
        print('nstates', nstates)
        states_kn = np.zeros((self.K, nsnaps, self.nreplicas))

        # special treatment for neglogP function
        temp_parameters_indices = self.traj[0]['trajectory'][0][4:][0]
        #print temp_parameters_indices
        original_index = [
        ]  # keep tracking the original index of the parameters
        for ind in range(len(temp_parameters_indices)):
            for in_ind in temp_parameters_indices[ind]:
                original_index.append(ind)

        # Get snapshot energies rescored in the different ensembles
        """['step', 'E', 'accept', 'state', [nuisance parameters]]"""
        for n in range(nsnaps):
            for k in range(self.K):
                for l in range(self.K):
                    if debug:
                        print('step',
                              self.traj[k]['trajectory'][n][0],
                              end=' ')
                    if k == l:
                        u_kln[k, k, n] = self.traj[k]['trajectory'][n][1]
                    state, sigma_index = self.traj[k]['trajectory'][n][3:]
                    for r in range(self.nreplicas):
                        states_kn[k, n, r] = state[r]
                    temp_parameters = []
                    new_parameters = [
                        [] for i in range(len(temp_parameters_indices))
                    ]
                    temp_parameter_indices = np.concatenate(sigma_index)
                    for ind in range(len(temp_parameter_indices)):
                        temp_parameters.append(
                            self.traj[k]['allowed_parameters'][ind][
                                temp_parameter_indices[ind]])
                    for m in range(len(original_index)):
                        new_parameters[original_index[m]].append(
                            temp_parameters[m])
                    u_kln[k, l,
                          n] = self.sampler[l].neglogP(state, new_parameters,
                                                       sigma_index)
                    if debug:
                        print('E_%d evaluated in model_%d' % (k, l),
                              u_kln[k, l, n])

        # Initialize MBAR with reduced energies u_kln and number of uncorrelated configurations from each state N_k.
        # u_kln[k,l,n] is the reduced potential energy beta*U_l(x_kn), where U_l(x) is the potential energy function for state l,
        # beta is the inverse temperature, and and x_kn denotes uncorrelated configuration n from state k.
        # N_k[k] is the number of configurations from state k stored in u_knm
        # Note that this step may take some time, as the relative dimensionless free energies f_k are determined at this point.
        mbar = MBAR(u_kln, N_k)

        # Extract dimensionless free energy differences and their statistical uncertainties.
        #       (Deltaf_ij, dDeltaf_ij) = mbar.getFreeEnergyDifferences()
        #(Deltaf_ij, dDeltaf_ij, Theta_ij) = mbar.getFreeEnergyDifferences(uncertainty_method='svd-ew')
        (Deltaf_ij, dDeltaf_ij, Theta_ij) = mbar.getFreeEnergyDifferences(
            uncertainty_method='approximate')
        #print 'Deltaf_ij', Deltaf_ij
        #print 'dDeltaf_ij', dDeltaf_ij
        beta = 1.0  # keep in units kT
        #print 'Unit-bearing (units kT) free energy difference f_1K = f_K - f_1: %f +- %f' % ( (1./beta) * Deltaf_ij[0,K-1], (1./beta) * dDeltaf_ij[0,K-1])
        self.f_df = np.zeros(
            (self.nlambda, 2)
        )  # first column is Deltaf_ij[0,:], second column is dDeltaf_ij[0,:]
        self.f_df[:, 0] = Deltaf_ij[0, :]
        self.f_df[:, 1] = dDeltaf_ij[0, :]

        # Compute the expectation of some observable A(x) at each state i, and associated uncertainty matrix.
        # Here, A_kn[k,n] = A(x_{kn})
        #(A_k, dA_k) = mbar.computeExpectations(A_kn)
        self.P_dP = np.zeros(
            (nstates, 2 * self.K))  # left columns are P, right columns are dP
        if debug:
            print('state\tP\tdP')
        states_kn = np.array(states_kn)
        for i in range(nstates):
            sampled = np.array([
                np.where(states_kn[:, :, r] == i, 1, 0)
                for r in range(self.nreplicas)
            ])
            A_kn = sampled.sum(axis=0)
            (p_i,
             dp_i) = mbar.computeExpectations(A_kn,
                                              uncertainty_method='approximate')
            self.P_dP[i, 0:self.K] = p_i
            self.P_dP[i, self.K:2 * self.K] = dp_i
        pops, dpops = self.P_dP[:, 0:self.K], self.P_dP[:, self.K:2 * self.K]

        # save results
        self.save_MBAR()
Ejemplo n.º 24
0
  
  # observable for estimation is the position
  elif observe == 'position':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]

  # observable for estimation is the position^2
  elif observe == 'position^2':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2

  results = mbar.computeExpectations(A_kn, state_dependent = state_dependent, return_dict=True)
  A_k_estimated = results['mu']
  dA_k_estimated = results['sigma']

  # need to additionally transform to get the square root
  if observe == 'RMS displacement':
    A_k_estimated = numpy.sqrt(A_k_estimated)
    # Compute error from analytical observable estimate.
    dA_k_estimated = dA_k_estimated/(2*A_k_estimated)

  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages - not defined if no samples
  nonzeros = numpy.arange(K)[Nk_ne_zero]
Ejemplo n.º 25
0
  # thermodynamic state
  elif observe == 'potential energy':
    A_kn = U_kln

  # observable for estimation is the position
  elif observe == 'position': 
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]  

  elif observe == 'position^2': 
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2   

  results = mbar.computeExpectations(A_kn)
  A_k_estimated = results['mu']
  dA_k_estimated = results['sigma']
  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages
  ifzero = numpy.array(N_k != 0)

  for k in range(K):
    if (ifzero[k]):
      if (observe == 'position') or (observe == 'position^2'):
        As_k_estimated[k] = numpy.average(A_kn[k,0:N_k[k]])
        dAs_k_estimated[k]  = numpy.sqrt(numpy.var(A_kn[k,0:N_k[k]])/(N_k[k]-1))
      elif (observe == 'RMS displacement' ) or (observe == 'potential energy'):
        As_k_estimated[k] = numpy.average(A_kn[k,k,0:N_k[k]])
Ejemplo n.º 26
0
f_df[:,0] = Deltaf_ij[0,:]
f_df[:,1] = dDeltaf_ij[0,:]
print 'Writing %s...'%args.bayesfactorfile
savetxt(args.bayesfactorfile, f_df)
print '...Done.'


# Compute the expectation of some observable A(x) at each state i, and associated uncertainty matrix.
# Here, A_kn[k,n] = A(x_{kn})
#(A_k, dA_k) = mbar.computeExpectations(A_kn)
P_dP = np.zeros( (nstates, 2*K) )  # left columns are P, right columns are dP
if (1):
    print 'state\tP\tdP'
    for i in range(nstates):
        A_kn = np.where(states_kn==i,1,0)
        (p_i, dp_i) = mbar.computeExpectations(A_kn, uncertainty_method='approximate')
        P_dP[i,0:K] = p_i
        P_dP[i,K:2*K] = dp_i
        print i,
        for p in p_i: print p,
        for dp in dp_i: print dp,
        print

pops, dpops = P_dP[:,0:K], P_dP[:,K:2*K]

print 'Writing %s...'%args.popsfile
savetxt(args.popsfile, P_dP)
print '...Done.'


Ejemplo n.º 27
0
# Using just the Model 0 mdrun samples
N_k = np.array([len(u_00), 0])  # The number of samples from the two states
u_kn = np.array([u_00, u_01])
U_kn = U_00
V_kn = V_00

mbar = MBAR(u_kn, N_k)

(Deltaf_ij, dDeltaf_ij,
 Theta_ij) = mbar.getFreeEnergyDifferences(return_theta=True)

# The first observable we are interested in is U, internal energy
A_kn = U_kn

(EA_k, dEA_k) = mbar.computeExpectations(A_kn)

# The second observable we are interested in is V, volume
A2_kn = V_kn

(EA2_k, dEA2_k) = mbar.computeExpectations(A2_kn)

# The third observable we are interested in is density
A3_kn = rho_calc(V_kn)
A3_kn = A3_kn[:]

(EA3_k, dEA3_k) = mbar.computeExpectations(A3_kn)

# The averages for the different mdruns and reruns

U_00_ave = np.mean(U_00)
Ejemplo n.º 28
0
    def MBAR_analysis(self, debug = False):
	"""MBAR analysis for populations and BICePs score"""
	# load necessary data first
	self.load_data()

	# Suppose the energies sampled from each simulation are u_kln, where u_kln[k,l,n] is the reduced potential energy
	#   of snapshot n \in 1,...,N_k of simulation k \in 1,...,K evaluated at reduced potential for state l.
	self.K = self.nlambda   # number of thermodynamic ensembles
	# N_k[k] will denote the number of correlated snapshots from state k
	N_k = np.array( [len(self.traj[i]['trajectory']) for i in range(self.nlambda)] )
	nsnaps = N_k.max()
	u_kln = np.zeros( (self.K, self.K, nsnaps) )
	nstates = int(self.states)
	print 'nstates', nstates
	states_kn = np.zeros( (self.K, nsnaps) )

	# Get snapshot energies rescored in the different ensembles
	"""['step', 'E', 'accept', 'state', 'sigma_noe', 'sigma_J', 'sigma_cs', 'sigma_pf''gamma']
	[int(step), float(self.E), int(accept), int(self.state), int(self.sigma_noe_index), int(self.sigma_J_index), int(self.sigma_cs_H_index), int(self.sigma_cs_Ha_index), int(self.sigma_cs_N_index), int(self.sigma_cs_Ca_index), int(self.sigma_pf_index), int(self.gamma_index)]               	"""

	for n in range(nsnaps):

  		for k in range(self.K):
    			for l in range(self.K):
				if debug:
      					print 'step', self.traj[k]['trajectory'][n][0],
      				if k==l:
          				print 'E%d evaluated in model %d'%(k,k), self.traj[k]['trajectory'][n][1],
          				u_kln[k,k,n] = self.traj[k]['trajectory'][n][1]
          			state, sigma_noe_index, sigma_J_index, sigma_cs_H_index, sigma_cs_Ha_index, sigma_cs_N_index, sigma_cs_Ca_index, sigma_pf_index, gamma_index = self.traj[k]['trajectory'][n][3:] 	# IMPORTANT: make sure the order of these parameters is the same as the way they are saved in PosteriorSampler
          			print 'state, sigma_noe_index, sigma_J_index, sigma_cs_H_index, sigma_cs_Ha_index, sigma_cs_N_index, sigma_cs_Ca_index, sigma_pf_index, gamma_index', state, sigma_noe_index, sigma_J_index, sigma_cs_H_index, sigma_cs_Ha_index, sigma_cs_N_index, sigma_cs_Ca_index, sigma_pf_index, gamma_index 
          			states_kn[k,n] = state
          			sigma_noe = self.traj[k]['allowed_sigma_noe'][sigma_noe_index]
          			sigma_J = self.traj[k]['allowed_sigma_J'][sigma_J_index]
          			sigma_cs_H = self.traj[k]['allowed_sigma_cs_H'][sigma_cs_H_index]
          			sigma_cs_Ha = self.traj[k]['allowed_sigma_cs_Ha'][sigma_cs_Ha_index]
          			sigma_cs_N = self.traj[k]['allowed_sigma_cs_N'][sigma_cs_N_index]
          			sigma_cs_Ca = self.traj[k]['allowed_sigma_cs_Ca'][sigma_cs_Ca_index]
          			sigma_pf = self.traj[k]['allowed_sigma_pf'][sigma_pf_index]
          			u_kln[k,l,n] = self.sampler[l].neglogP(0, state, sigma_noe, sigma_J, sigma_cs_H, sigma_cs_Ha, sigma_cs_N, sigma_cs_Ca, sigma_pf, gamma_index)
				if debug:
					print 'E_%d evaluated in model_%d'%(k,l), u_kln[k,l,n]


	# Initialize MBAR with reduced energies u_kln and number of uncorrelated configurations from each state N_k.
 	# u_kln[k,l,n] is the reduced potential energy beta*U_l(x_kn), where U_l(x) is the potential energy function for state l,
	# beta is the inverse temperature, and and x_kn denotes uncorrelated configuration n from state k.
	# N_k[k] is the number of configurations from state k stored in u_knm
	# Note that this step may take some time, as the relative dimensionless free energies f_k are determined at this point.
	mbar = MBAR(u_kln, N_k)

	# Extract dimensionless free energy differences and their statistical uncertainties.
#	(Deltaf_ij, dDeltaf_ij) = mbar.getFreeEnergyDifferences()
	#(Deltaf_ij, dDeltaf_ij, Theta_ij) = mbar.getFreeEnergyDifferences(uncertainty_method='svd-ew')
	(Deltaf_ij, dDeltaf_ij, Theta_ij) = mbar.getFreeEnergyDifferences(uncertainty_method='approximate')
	#print 'Deltaf_ij', Deltaf_ij
	#print 'dDeltaf_ij', dDeltaf_ij
	beta = 1.0 # keep in units kT
	#print 'Unit-bearing (units kT) free energy difference f_1K = f_K - f_1: %f +- %f' % ( (1./beta) * Deltaf_ij[0,K-1], (1./beta) * dDeltaf_ij[0,K-1])
	self.f_df = np.zeros( (self.nlambda, 2) )  # first column is Deltaf_ij[0,:], second column is dDeltaf_ij[0,:]
	self.f_df[:,0] = Deltaf_ij[0,:]
	self.f_df[:,1] = dDeltaf_ij[0,:]

	# Compute the expectation of some observable A(x) at each state i, and associated uncertainty matrix.
        # Here, A_kn[k,n] = A(x_{kn})
        #(A_k, dA_k) = mbar.computeExpectations(A_kn)
        self.P_dP = np.zeros( (nstates, 2*self.K) )  # left columns are P, right columns are dP
	if debug:
        	print 'state\tP\tdP'
    	for i in range(nstates):
        	A_kn = np.where(states_kn==i,1,0)
        	(p_i, dp_i) = mbar.computeExpectations(A_kn, uncertainty_method='approximate')
        	self.P_dP[i,0:self.K] = p_i
        	self.P_dP[i,self.K:2*self.K] = dp_i
		print i
        	for p in p_i: print p,
        	for dp in dp_i: print dp,
		print 
	pops, dpops = self.P_dP[:,0:self.K], self.P_dP[:,self.K:2*self.K]

	# save results
	self.save_MBAR()
Ejemplo n.º 29
0
def execute(nstates, q_samp_space, epsi_samp_space, sig_samp_space, singlestate=None, singleparms=None, useO=False, crunchset=False, bootstrap=False, bootstrapcount=200):
    #Initilize limts
    sig3_samp_space = sig_samp_space**3
    #These are the limits used to compute the constant matricies
    #They should match with LJ 0 and 5, ALWAYS
    q_min = -2.0
    q_max = +2.0
    epsi_min = epsi_samp_space[0]
    epsi_max = epsi_samp_space[5]
    sig_min = sig_samp_space[0]
    sig_max = sig_samp_space[5]
    lamto_epsi = lambda lam: (epsi_max - epsi_min)*lam + epsi_min
    lamto_sig3 = lambda lam: (sig_max**3 - sig_min**3)*lam + sig
    lamto_sig = lambda lam: lamto_sig3(lam)**(1.0/3)
    if spacing is logspace:
        StartSpace = -5
        EndSpace   = 0
        spacename='log'
        PlotStart = 10**StartSpace
        PlotEnd   = 10**EndSpace
    elif spacing is linspace:
        sigStartSpace = sig_min
        sigEndSpace   = sig_max
        epsiStartSpace = epsi_min
        qStartSpace = q_min
        qEndSpace = q_max
        if alle:
            epsiEndSpace   = 3.6 #!!! Manual set
        else:
            epsiEndSpace   = epsi_max
        spacename='linear'
        sigPlotStart = sigStartSpace**sig_factor
        sigPlotEnd   = sigEndSpace**sig_factor
        epsiPlotStart = epsiStartSpace
        epsiPlotEnd   = epsiEndSpace
        qPlotStart = qStartSpace
        qPlotEnd = qEndSpace



    #generate sample length
    g_en_start = 19 #Row where data starts in g_energy output
    g_en_energy = 1 #Column where the energy is located
    niterations_max = 30001
    #Min and max sigmas:
    fC12 = lambda epsi,sig: 4*epsi*sig**12
    fC6 = lambda epsi,sig: 4*epsi*sig**6
    fsig = lambda C12, C6: (C12/C6)**(1.0/6)
    fepsi = lambda C12, C6: C6**2/(4*C12)
    C12_delta = fC12(epsi_max, sig_max) - fC12(epsi_min, sig_min)
    C6_delta = fC6(epsi_max, sig_max) - fC6(epsi_min, sig_min)
    C12_delta_sqrt = fC12(epsi_max, sig_max)**.5 - fC12(epsi_min, sig_min)**.5
    C6_delta_sqrt = fC6(epsi_max, sig_max)**.5 - fC6(epsi_min, sig_min)**.5
    #Set up lambda calculation equations
    flamC6 = lambda epsi, sig: (fC6(epsi, sig) - fC6(epsi_min, sig_min))/C6_delta
    flamC12 = lambda epsi, sig: (fC12(epsi, sig) - fC12(epsi_min, sig_min))/C12_delta
    flamC6sqrt = lambda epsi, sig: (fC6(epsi, sig)**.5 - fC6(epsi_min, sig_min)**.5)/C6_delta_sqrt
    flamC12sqrt = lambda epsi, sig: (fC12(epsi, sig)**.5 - fC12(epsi_min, sig_min)**.5)/C12_delta_sqrt
    flamC1 = lambda q: q
    lamC12 = flamC12sqrt(epsi_samp_space, sig_samp_space)
    lamC6 = flamC6sqrt(epsi_samp_space, sig_samp_space)
    lamC1 = flamC1(q_samp_space)
    #Try to load u_kln
    lam_range = linspace(0,1,nstates)
    subsampled = numpy.zeros([nstates],dtype=numpy.bool)
    if load_ukln and os.path.isfile('esq_ukln_consts_n%i.npz'%nstates):
        energies = consts(nstates, file='esq_ukln_consts_n%i.npz'%nstates)
    else:
        #Initial u_kln
        energies = consts(nstates)
        g_t = numpy.zeros([nstates])
        #Read in the data
        for k in xrange(nstates):
            print "Importing LJ = %02i" % k
            energy_dic = {'full':{}, 'rep':{}}
            #Try to load the subsampled filenames
            try:
                energy_dic['null'] = open('lj%s/prod/subenergy%s_null.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the null energies (unaffected) of the K states
                for l in xrange(nstates):
                    energy_dic['full']['%s'%l] = open('lj%s/prod/subenergy%s_%s.xvg' %(k,k,l),'r').readlines()[g_en_start:] #Read in the full energies for each state at KxL
                    if l == 5 or l == 0:
                        energy_dic['rep']['%s'%l] = open('lj%s/prod/subenergy%s_%s_rep.xvg' %(k,k,l),'r').readlines()[g_en_start:] #Read in the repulsive energies at 0, nstates-1, and K
                energy_dic['q']  =  open('lj%s/prod/subenergy%s_q.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the charge potential energy
                energy_dic['q2']  =  open('lj%s/prod/subenergy%s_q2.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the charge potential energy
                iter = len(energy_dic['null'])
                subsampled[k] = True
                # Set the object to iterate over, since we want every frame of the subsampled proces, we just use every frame
                frames = xrange(iter)
            except: #Load the normal way
                energy_dic['null'] = open('lj%s/prod/energy%s_null.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the null energies (unaffected) of the K states
                for l in xrange(nstates):
                    energy_dic['full']['%s'%l] = open('lj%s/prod/energy%s_%s.xvg' %(k,k,l),'r').readlines()[g_en_start:] #Read in the full energies for each state at KxL
                    if l == 5 or l == 0:
                        energy_dic['rep']['%s'%l] = open('lj%s/prod/energy%s_%s_rep.xvg' %(k,k,l),'r').readlines()[g_en_start:] #Read in the repulsive energies at 0, nstates-1, and K
                energy_dic['q']  =  open('lj%s/prod/energy%s_q.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the charge potential energy
                energy_dic['q2']  =  open('lj%s/prod/energy%s_q2.xvg' %(k,k),'r').readlines()[g_en_start:] #Read in the charge potential energy
                iter = niterations_max
                #Subsample
                tempenergy = numpy.zeros(iter)
                for frame in xrange(iter):
                    tempenergy[frame] = float(energy_dic['full']['%s'%k][frame].split()[g_en_energy])
                frames, temp_series, g_t[k] = subsample_series(tempenergy, return_g_t=True)
                print "State %i has g_t of %i" % (k, g_t[k])
                iter = len(frames)
            #Update iterations if need be
            energies.updateiter(iter)
            #Fill in matricies
            n = 0
            for frame in frames:
                #Unaffected state
                energies.const_Un_matrix[k,n] = float(energy_dic['null'][frame].split()[g_en_energy])
                #Charge only state
                VI = float(energy_dic['q'][frame].split()[g_en_energy]) - energies.const_Un_matrix[k,n]
                VII = float(energy_dic['q2'][frame].split()[g_en_energy]) - energies.const_Un_matrix[k,n]
                q1 = 1
                q2 = 0.5
                QB = (q1**2*VII - q2**2*VI)/(q1**2*q2 - q2**2*q1)
                QA = (VI - q1*QB)/q1**2
                energies.const_q_matrix[k,n] = QB
                energies.const_q2_matrix[k,n] = QA
                #const_q_matrix[k,n] = float(energy_dic['q'][n].split()[g_en_energy]) - const_Un_matrix[k,n]
                #Isolate the data
                for l in xrange(nstates):
                    energies.u_kln[k,l,n] = float(energy_dic['full']['%s'%l][frame].split()[g_en_energy]) #extract the kln energy, get the line, split the line, get the energy, convert to float, store
                #Repulsive terms: 
                #R0 = U_rep[k,k,n] + dhdl[k,0,n] - Un[k,n]
                energies.const_R0_matrix[k,n] = float(energy_dic['rep']['%s'%(0)][frame].split()[g_en_energy]) - energies.const_Un_matrix[k,n]
                #R1 = U_rep[k,k,n] + dhdl[k,-1,n] - Un[k,n]
                energies.const_R1_matrix[k,n] = float(energy_dic['rep']['%s'%(5)][frame].split()[g_en_energy]) - energies.const_Un_matrix[k,n]
                energies.const_R_matrix[k,n] = energies.const_R1_matrix[k,n] - energies.const_R0_matrix[k,n]
                #Finish the total unaffected term
                #Total unaffected = const_Un + U0 = const_Un + (U_full[k,0,n] - const_Un) = U_full[k,0,n]
                energies.const_unaffected_matrix[k,n] = energies.u_kln[k,0,n]
                #Fill in the q matrix
                
                #Attractive term
                #u_A = U_full[k,n] - constR[k,n] - const_unaffected[k,n]
                energies.const_A0_matrix[k,n] = energies.u_kln[k,0,n] - energies.const_R0_matrix[k,n] - energies.const_Un_matrix[k,n]
                energies.const_A1_matrix[k,n] = energies.u_kln[k,5,n] - energies.const_R1_matrix[k,n] - energies.const_Un_matrix[k,n]
                energies.const_A_matrix[k,n] = energies.const_A1_matrix[k,n] - energies.const_A0_matrix[k,n]
                n += 1
        energies.determine_all_N_k()
        #write_g_t(g_t)
        constname = 'esq_ukln_consts_n%i.npz'%nstates
        if load_ukln and not os.path.isfile(constname):
            energies.save_consts(constname)
    #Sanity check
    sanity_kln = numpy.zeros(energies.u_kln.shape)
    #Round q to the the GROMACS precision, otherwise there is drift in q which can cause false positives
    lamC1r = numpy.around(lamC1, decimals=4)
    for l in xrange(nstates):
        #sanity_kln[:,l,:] = lamC12[l]*energies.const_R_matrix + lamC6[l]*energies.const_A_matrix + lamC1[l]*energies.const_q_matrix + lamC1[l]**2*energies.const_q2_matrix + energies.const_unaffected_matrix
        sanity_kln[:,l,:] = lamC12[l]*energies.const_R_matrix + lamC6[l]*energies.const_A_matrix + lamC1r[l]*energies.const_q_matrix + lamC1r[l]**2*energies.const_q2_matrix + energies.const_unaffected_matrix
    del_kln = numpy.abs(energies.u_kln - sanity_kln)
    del_tol = 1 #in kJ per mol
    print "Max Delta: %f" % numpy.nanmax(del_kln)
    if numpy.nanmax(del_kln) > 1: #Check for numeric error
        #Double check to see if the weight is non-zero.
        #Most common occurance is when small particle is tested in large particle properties
        #Results in energies > 60,000 kj/mol, which carry 0 weight to machine precision
        nonzero_weights = numpy.count_nonzero(numpy.exp(-energies.u_kln[numpy.where(del_kln > .2)] * kjpermolTokT))
        if nonzero_weights != 0:
            print "and there are %d nonzero weights! Stopping execution" % nonzero_weights
            pdb.set_trace()
        else:
            print "but these carry no weight and so numeric error does not change the answer"

    ##################################################
    ############### END DATA INPUT ###################
    ##################################################
    #Convert to dimless
    energies.dimless()
    #Set up regular grid
    sig_range = (spacing(sigStartSpace**3,sigEndSpace**3,Nparm))**(1.0/3)
    epsi_range = spacing(epsiStartSpace,epsiEndSpace,Nparm)
    q_range = spacing(qStartSpace,qEndSpace,Nparm)
    epsi_plot_range = spacing(epsiStartSpace,epsi_max,Nparm)
    #Load subsequent f_ki
    f_ki_loaded = False
    state_counter = nstates
    while not f_ki_loaded and state_counter != 0:
        #Load the largest f_ki you can
        try:
            f_ki_load = numpy.load('esq_f_k_{myint:{width}}.npy'.format(myint=state_counter, width=len(str(state_counter))))
            f_ki_loaded = True
            f_ki_n = state_counter
        except:
            pass
        state_counter -= 1
    try:
        if nstates >= f_ki_n:
            draw_ki = f_ki_n
        else:
            draw_ki = nstates
        #Copy the loaded data
        f_ki = numpy.zeros(nstates)
        f_ki[:draw_ki] = f_ki_load[:draw_ki]
        mbar = MBAR(energies.u_kln, energies.N_k, verbose = True, initial_f_k=f_ki, subsampling_protocol=[{'method':'L-BFGS-B','options':{'disp':True}}], subsampling=1)
    except:
        mbar = MBAR(energies.u_kln, energies.N_k, verbose = True, subsampling_protocol=[{'method':'L-BFGS-B','options':{'disp':True}}], subsampling=1)
    if not f_ki_loaded or f_ki_n != nstates:
        try:
            numpy.save_compressed('esq_f_k_{myint:{width}}.npy'.format(myint=nstates, width=len(str(nstates))), mbar.f_k)
        except:
            numpy.save('esq_f_k_{myint:{width}}.npy'.format(myint=nstates, width=len(str(nstates))), mbar.f_k)

    ######## Begin Computing RDF's #########

    basebins = genrdf.defbin
    plotx = linspace(genrdf.distmin, genrdf.distmax, basebins)/10.0 #put in nm
    #Generate blank
    rdfs = numpy.zeros([basebins, nstates, energies.itermax], dtype=numpy.float64)
    if useO:
        pathstr = 'lj%s/prod/rdfOhist%s.npz'
    else:
        pathstr = 'lj%s/prod/rdfhist%s.npz'
    for k in xrange(nstates):
        try: #Try to load in
            if useO:
                rdfk = numpy.load(pathstr%(k,k))['rdf']
            else:
                rdfk = numpy.load(pathstr%(k,k))['rdf']
        except:
            rdfk = genrdf.buildrdf(k, useO=useO)
        nkbin, nkiter = rdfk.shape
        rdfs[:,k,:nkiter] = rdfk
  
    if singleparms is not None:
        Npert = 1
        pertq = numpy.array([singleparms[0]])
        pertepsi = numpy.array([singleparms[1]])
        pertsig = numpy.array([singleparms[2]])
        pertrdfs = None
        if useO:
            filestring = 'pertrdfs/pertrdfOn%s' % nstates
        else:
            filestring = 'pertrdfs/pertrdfn%s' % nstates
        filestring += 'q%fe%fs%f.npz'
        fileobjs = [singleparms[0],singleparms[1],singleparms[2]] #Even though 0 and 1 get +l, the l =0 in this instance so its fine
    elif singlestate is not None:
        Npert = 1
        pertq = numpy.array([q_samp_space[singlestate]])
        pertepsi = numpy.array([epsi_samp_space[singlestate]])
        pertsig = numpy.array( [sig_samp_space[singlestate]])
        try:
            pertrdf = numpy.load(pathstr%(singlestate,singlestate))['rdf']
        except:
            pertrdf = genrdf.buildrdf(singlestate, useO=useO)
        pertbin, pertnk = pertrdf.shape
        quickrdf = numpy.zeros([pertbin,1,pertnk]) #Initilze the matrix I will pass into the graphing routine
        quickrdf[:,0,:] = pertrdf
        pertrdf = quickrdf
        del quickrdf #Cleanup a bit
        if useO:
            filestring = 'lj%s/rdfO%sfromn%s.npz'
        else:
            filestring = 'lj%s/rdf%sfromn%s.npz'
        fileobjs = [singlestate,singlestate,nstates]
    elif crunchset is True:
        #Joung/Cheatham params: 
        pertname =             ['Li+',      'Na+',       'K+',        'Rb+',       'Cs+',       'F-',        'Cl-',       'Br-',       'I-']
        pertq =    numpy.array([1,          1,           1,           1,           1,           -1,          -1,          -1,          -1])
        pertepsi = numpy.array([0.11709719,  0.365846031, 0.810369254, 1.37160683,  1.70096085,  0.014074976, 0.148912744, 0.245414194, 0.224603814])
        pertsig =  numpy.array([0.196549675, 0.24794308,  0.30389715,  0.323090391, 0.353170773, 0.417552588, 0.461739606, 0.482458908, 0.539622469])
        #pertname =             ['Br-']
        #pertq =    numpy.array([-1])
        #pertepsi = numpy.array([0.245414194])
        #pertsig =  numpy.array([0.482458908])
        Npert = len(pertq)
        if useO:
            filestring = 'pertrdfs/pertrdfOn%s%s.npz'
        else:
            filestring = 'pertrdfs/pertrdfn%s%s.npz'
    else:
        #For now, just use the back 5 of the qes set
        Npert = 5
        pertq = q_samp_space[-Npert:]
        pertepsi = epsi_samp_space[-Npert:]
        pertsig = sig_samp_space[-Npert:]
        pertNk = energies.N_k[-Npert:]
        pertrdfs = rdfs[:,-Npert:,:]
        if useO:
            filestring = 'lj%s/rdfO%sfromn%s.npz'
        else:
            filestring = 'lj%s/rdf%sfromn%s.npz'
        fileobjs = [nstates-Npert,nstates-Npert,nstates]

    
    #pertq = numpy.array([q_samp_space[1]])+0.25
    #pertepsi = numpy.array([epsi_samp_space[1]])
    #pertepsi *= 2
    #pertsig = numpy.array([sig_samp_space[1]])
    #pertsig *= 1.5
    #pertNk = numpy.array([energies.N_k[1]])
    #pertrdfs = rdfs[:,1,:]
 

    #Begin computing expectations
    Erdfs = numpy.zeros([Npert, basebins])
    dErdfs = numpy.zeros([Npert, basebins])
    eff_u_kln = numpy.zeros([energies.nstates, energies.nstates + Npert, energies.itermax])
    eff_u_kln[:energies.nstates, :energies.nstates, :] = energies.u_kln
    
    for l in xrange(Npert):
        #if not savedata or not os.path.isfile('lj%s/rdf%sfromn%s.npz'%(nstates-Npert+l, nstates-Npert+l, nstates)):
        if crunchset:
            filecheck = filestring % (nstates, pertname[l])
        else:
            filecheck = filestring % (fileobjs[0]+l, fileobjs[1]+l, fileobjs[2])
        if not savedata or not os.path.isfile(filecheck) or effnum:
            print "Working on state %d/%d" %(l+1, Npert)
            q = pertq[l]
            epsi = pertepsi[l]
            sig = pertsig[l]
            #u_kn_P = numpy.zeros([nstates,energies.itermax])
            u_kn_P = flamC12sqrt(epsi,sig)*energies.const_R_matrix + flamC6sqrt(epsi,sig)*energies.const_A_matrix + flamC1(q)*energies.const_q_matrix + flamC1(q)**2*energies.const_q2_matrix + energies.const_unaffected_matrix
            if effnum:
                eff_u_kln[:,energies.nstates+l,:] = u_kn_P
            else:
                for bin in xrange(basebins):
                    stdout.flush()
                    stdout.write('\rWorking on bin %d/%d'%(bin,basebins-1))
                    #Erdfs[l, bin], dErdfs[l, bin] = mbar.computePerturbedExpectation(u_kn_P, rdfs[bin,:,:])
                    Erdfs[l, bin], dErdfs[l, bin] = mbar.computeExpectations(rdfs[bin,:,:], u_kn=u_kn_P, compute_uncertainty=False)
                stdout.write('\n')
            if savedata:
                #savez('lj%s/rdf%sfromn%s.npz'%(nstates-Npert+l, nstates-Npert+l, nstates), Erdfs=Erdfs[l,:], dErdfs=dErdfs[l,:])
                if crunchset:
                    savez(filestring % (nstates, pertname[l]), Erdfs=Erdfs[l,:], dErdfs=dErdfs[l,:])
                else:
                    savez(filestring % (fileobjs[0]+l, fileobjs[1]+l, fileobjs[2]), Erdfs=Erdfs[l,:], dErdfs=dErdfs[l,:])
        else:
            #rdfdata = numpy.load('lj%s/rdf%sfromn%s.npz'%(nstates-Npert+l, nstates-Npert+l, nstates))
            if crunchset:
                rdfdata = numpy.load(filestring % (nstates, pertname[l]))
            else:
                rdfdata = numpy.load(filestring % (fileobjs[0]+l, fileobjs[1]+l, fileobjs[2]))
            Erdfs[l,:] = rdfdata['Erdfs']
            dErdfs[l,:] = rdfdata['dErdfs']

    #Effective number of samples
    if effnum:
        N_k = numpy.zeros(energies.nstates+Npert, dtype=numpy.int32)
        f_k = numpy.zeros(energies.nstates+Npert)
        N_k[:energies.nstates] = energies.N_k
        f_k[:mbar.K] = mbar.f_k
        eff_mbar = MBAR(eff_u_kln, N_k, verbose = True, initial_f_k=f_k, subsampling_protocol=[{'method':'L-BFGS-B','options':{'disp':True}}], subsampling=1)
        for l in xrange(Npert):
            w = numpy.exp(eff_mbar.Log_W_nk[:,mbar.K+l])
            neff = 1.0/numpy.sum(w**2)
            print "Effective number of samples for {0:s}: {1:f}".format(pertname[l], neff)
        pdb.set_trace()


    if bootstrap: #I spun this off into its own section, although it probably could have been folded into the previous
        from numpy.random import random_integers
        Nboot = bootstrapcount
        ErdfsB = numpy.zeros([Nboot, Npert, basebins])
        run_start_time = time.time()
        number_of_iterations = Npert*Nboot
        iteration = 0
        for l in xrange(Npert): #Go through each bootstrap
            print "Bootstraping on state %d/%d" %(l+1, Npert)
            q = pertq[l]
            epsi = pertepsi[l]
            sig = pertsig[l]
            #u_kn_P = numpy.zeros([nstates,energies.itermax])
            u_kn_P = flamC12sqrt(epsi,sig)*energies.const_R_matrix + flamC6sqrt(epsi,sig)*energies.const_A_matrix + flamC1(q)*energies.const_q_matrix + flamC1(q)**2*energies.const_q2_matrix + energies.const_unaffected_matrix
            for Nb in xrange(Nboot): #Bootstrap
                initial_time=time.time()
                iteration += 1
                u_kn_B = numpy.zeros(u_kn_P.shape)
                rdfsB = numpy.zeros(rdfs.shape)
                for k in xrange(nstates): #Roll random numbers
                    N_k = energies.N_k[k]
                    samplepool = random_integers(0,N_k-1,N_k)
                    for i in xrange(len(samplepool)):
                        u_kn_B[k,i] = u_kn_P[k,samplepool[i]] #Have to do this since slicing the first index alone reverses the return order for the last 2 indicies
                        rdfsB[:,k,i] = rdfs[:,k,samplepool[i]]
                #for bin in xrange(basebins):
                for bin in xrange(60): #Cut down for time
                    stdout.flush()
                    #stdout.write('\rWorking on bootstrap %d/%d and bin %d/%d'%(Nb,Nboot-1,bin,basebins-1))
                    stdout.write('\rWorking on bootstrap %d/%d and bin %d/%d'%(Nb,Nboot-1,bin,60-1))
                    ErdfsB[Nb, l, bin] = mbar.computePerturbedExpectation(u_kn_B, rdfsB[bin,:,:], compute_uncertainty=False)[0] #Dont compute error for speed
                laptime = time.clock()
                # Show timing statistics. copied from Repex.py, copywrite John Chodera
                final_time = time.time()
                elapsed_time = final_time - initial_time
                estimated_time_remaining = (final_time - run_start_time) / (iteration) * (number_of_iterations - iteration)
                estimated_total_time = (final_time - run_start_time) / (iteration) * (number_of_iterations)
                estimated_finish_time = final_time + estimated_time_remaining
                print "Iteration took %.3f s." % elapsed_time
                print "Estimated completion in %s, at %s (consuming total wall clock time %s)." % (str(datetime.timedelta(seconds=estimated_time_remaining)), time.ctime(estimated_finish_time), str(datetime.timedelta(seconds=estimated_total_time)))
        savez('BootstrapedIonRDF.npz', Erdfs = ErdfsB)
                    

    if singleparms is None and not crunchset:
        f,a = plt.subplots(Npert, 2)
        for l in xrange(Npert):
            Nl = pertNk[l]
            rdftraj = pertrdfs[:,l,:Nl]
            if Npert == 1:
                a[0].plot(plotx, Erdfs[l,:], '-k')
                #a[0].plot(plotx, Erdfs[l,:] + dErdfs[l,:], '--k')
                #a[0].plot(plotx, Erdfs[l,:] - dErdfs[l,:], '--k')
                a[1].plot(plotx, rdftraj.sum(axis=1)/float(Nl), '-k')
                alims0 = a[0].get_ylim()
                alims1 = a[1].get_ylim()
                a[0].set_ylim(0, numpy.amax(numpy.array([alims0[1], alims1[1]])))
                a[1].set_ylim(0, numpy.amax(numpy.array([alims0[1], alims1[1]])))
            else:
                a[l,0].plot(plotx, Erdfs[l,:], '-k')
                #a[l,0].plot(plotx, Erdfs[l,:] + dErdfs[l,:], '--k')
                #a[l,0].plot(plotx, Erdfs[l,:] - dErdfs[l,:], '--k')
                a[l,1].plot(plotx, rdftraj.sum(axis=1)/float(Nl), '-k')
                alims0 = a[l,0].get_ylim()
                alims1 = a[l,1].get_ylim()
                a[l,0].set_ylim(0, numpy.amax(numpy.array([alims0[1], alims1[1]])))
                a[l,1].set_ylim(0, numpy.amax(numpy.array([alims0[1], alims1[1]])))
    elif crunchset:
        f,a = plt.subplots(Npert)
        for l in xrange(Npert):
            a[l].plot(plotx, Erdfs[l,:], '-k')
            a[l].set_ylim([0,a[l].get_ylim()[1]])
            print "Peak for %s at %f" % (pertname[l], plotx[numpy.argmax(Erdfs[l,:])])
    else:
        f,a=plt.subplots(1,1)
        a.plot(plotx, Erdfs[0,:], '-k')
        
    plt.show()
    pdb.set_trace()
  # thermodynamic state
  elif observe == 'potential energy':
    A_kn = U_kln

  # observable for estimation is the position
  elif observe == 'position': 
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]  

  elif observe == 'position^2': 
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2   

  (A_k_estimated, dA_k_estimated) = mbar.computeExpectations(A_kn)

  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages

  ifzero = numpy.array(N_k != 0)

  for k in range(K):
    if (ifzero[k]):
      if (observe == 'position') or (observe == 'position^2'):
        As_k_estimated[k] = numpy.average(A_kn[k,0:N_k[k]])
        dAs_k_estimated[k]  = numpy.sqrt(numpy.var(A_kn[k,0:N_k[k]])/(N_k[k]-1))
      elif (observe == 'RMS displacement' ) or (observe == 'potential energy'):
        As_k_estimated[k] = numpy.average(A_kn[k,k,0:N_k[k]])
  
  # observable for estimation is the position
  elif observe == 'position':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]

  # observable for estimation is the position^2
  elif observe == 'position^2':
    state_dependent = False
    A_kn = numpy.zeros([K,N_max], dtype = numpy.float64)
    for k in range(0,K):
      A_kn[k,0:N_k[k]] = x_kn[k,0:N_k[k]]**2

  (A_k_estimated, dA_k_estimated) = mbar.computeExpectations(A_kn, state_dependent = state_dependent)

  # need to additionally transform to get the square root
  if observe == 'RMS displacement':
    A_k_estimated = numpy.sqrt(A_k_estimated)
    # Compute error from analytical observable estimate.
    dA_k_estimated = dA_k_estimated/(2*A_k_estimated)

  As_k_estimated = numpy.zeros([K],numpy.float64)
  dAs_k_estimated = numpy.zeros([K],numpy.float64)

  # 'standard' expectation averages - not defined if no samples
  nonzeros = numpy.arange(K)[Nk_ne_zero]

  totaln = 0
  for k in nonzeros: