def GetdG0(self, nH, z, nMg, T=default_T): key = self._MakeKey(nH, z, nMg) if key not in self.dgs: return None dG0_list = self.dgs[key] return -(R*T) * log_sum_exp([x/(-R*T) for x in dG0_list])
def Squeeze(self, T=default_T): """Groups together all pseudoisomers that have the same key""" squeezed_dgs = {} for k, dG0_list in self.dgs.iteritems(): squeezed_dG0 = -(R*T) * log_sum_exp([x/(-R*T) for x in dG0_list]) squeezed_dgs[k] = [squeezed_dG0] self.dgs = squeezed_dgs
def log_det(X): if (X.shape[0] != X.shape[1]): raise Exception("X is not a square matrix so its determinant cannot be calculated") if (X.shape[0] == 1): return X[0,0] if (X.shape[0] == 2): # X is a 2x2 matrix return(log_subt_exp(X[0,0]+X[1,1], X[1,0]+X[0,1])) if (X.shape[0] == 3): s_plus = log_sum_exp([X[0,0]+X[1,1]+X[2,2], X[0,2]+X[1,0]+X[2,1], X[0,1]+X[1,2]+X[2,0]]) s_minus = log_sum_exp([X[0,1]+X[1,0]+X[2,2], X[0,2]+X[1,1]+X[2,0], X[0,0]+X[1,2]+X[2,1]]) return log_subt_exp(s_plus, s_minus) raise Exception("log_det is only implemented for matrices of size < 4")
def log_dot(X,Y): if (X.shape[1] != Y.shape[0]): raise Exception("The dimensions of the matrices do not match: (%d,%d) and (%d,%d)" % (X.shape[0], X.shape[1], Y.shape[0], Y.shape[1])) R = zeros((X.shape[0], Y.shape[1])) for i in range(X.shape[0]): for j in range(Y.shape[1]): R[i,j] = log_sum_exp([(X[i,k] + Y[k,j]) for k in range(X.shape[1])]) return R
def array_transform(dG0, nH, z, nMg, pH, pMg, I, T): """ dG0, nH and z - are the species parameters (can be vectors) pH and I - are the conditions, must be scalars returns the transformed gibbs energy: dG0' """ from toolbox.util import log_sum_exp ddG0 = correction_function(nH, z, nMg, pH, pMg, I, T) dG0_tag = dG0 + ddG0 return -R * T * log_sum_exp(dG0_tag / (-R * T))
def Transform(self, pH, pMg, I, T): """ Transform this set of pseudoisomers to a dG value at the specified conditions. """ if not self.dgs: raise ValueError("Cannot run Transform on an empty pseudoisomer map") _v_dG0, v_dG0_tag, _v_nH, _v_z, _v_nMg = self._Transform(pH, pMg, I, T) return -R * T * log_sum_exp(v_dG0_tag / (-R*T))
def array_transform(dG0, nH, z, nMg, pH, pMg, I, T): """ dG0, nH and z - are the species parameters (can be vectors) pH and I - are the conditions, must be scalars returns the transformed gibbs energy: dG0' """ from toolbox.util import log_sum_exp ddG0 = correction_function(nH, z, nMg, pH, pMg, I, T) dG0_tag = dG0 + ddG0 return -R * T * log_sum_exp(dG0_tag / (-R*T))
def log_dot(X, Y): if (X.shape[1] != Y.shape[0]): raise Exception( "The dimensions of the matrices do not match: (%d,%d) and (%d,%d)" % (X.shape[0], X.shape[1], Y.shape[0], Y.shape[1])) R = zeros((X.shape[0], Y.shape[1])) for i in range(X.shape[0]): for j in range(Y.shape[1]): R[i, j] = log_sum_exp([(X[i, k] + Y[k, j]) for k in range(X.shape[1])]) return R
def log_det(X): if (X.shape[0] != X.shape[1]): raise Exception( "X is not a square matrix so its determinant cannot be calculated") if (X.shape[0] == 1): return X[0, 0] if (X.shape[0] == 2): # X is a 2x2 matrix return (log_subt_exp(X[0, 0] + X[1, 1], X[1, 0] + X[0, 1])) if (X.shape[0] == 3): s_plus = log_sum_exp([ X[0, 0] + X[1, 1] + X[2, 2], X[0, 2] + X[1, 0] + X[2, 1], X[0, 1] + X[1, 2] + X[2, 0] ]) s_minus = log_sum_exp([ X[0, 1] + X[1, 0] + X[2, 2], X[0, 2] + X[1, 1] + X[2, 0], X[0, 0] + X[1, 2] + X[2, 1] ]) return log_subt_exp(s_plus, s_minus) raise Exception("log_det is only implemented for matrices of size < 4")
def GetDeltaDeltaG0(self, pH, I, pMg, T, nH=None, nMg=0): """ Return: the transformed ddG0 = dG0'_f - dG0_f Note: assume that the dG0_f of one of the psuedoisomers (according to the given nH) is 0 """ if nH is None: nH = self.min_nH pseudoisomer_matrix = self.GetTransformedDeltaGs(pH, I, pMg, T, nH=nH, nMg=nMg) ddG0_f = -R * T * log_sum_exp([dG0_tag / (-R*T) for (_nH, _z, _nMg, dG0_tag) in pseudoisomer_matrix]) return ddG0_f
def _Bolzmann(self, pH, pMg, I, T): """ Returns a dictionary with the same keys as self.dgs, but the values are the relative abundance of that species at the specified conditions """ _v_dG0, v_dG0_tag, v_nH, v_z, v_nMg = self._Transform(pH, pMg, I, T) minus_E_over_RT = [-E / (R*T) for E in v_dG0_tag] total = log_sum_exp(minus_E_over_RT) species2abundance = {} for i in xrange(len(v_dG0_tag)): key = PseudoisomerMap._MakeKey(v_nH[i], v_z[i], v_nMg[i]) abundance = pylab.exp(minus_E_over_RT[i] - total) species2abundance[key] = species2abundance.get(key, 0.0) + abundance return species2abundance
def GetDeltaDeltaG0(self, pH, I, pMg, T, nH=None, nMg=0): """ Return: the transformed ddG0 = dG0'_f - dG0_f Note: assume that the dG0_f of one of the psuedoisomers (according to the given nH) is 0 """ if nH is None: nH = self.min_nH pseudoisomer_matrix = self.GetTransformedDeltaGs(pH, I, pMg, T, nH=nH, nMg=nMg) ddG0_f = -R * T * log_sum_exp([ dG0_tag / (-R * T) for (_nH, _z, _nMg, dG0_tag) in pseudoisomer_matrix ]) return ddG0_f