def calc_dg_bar(LPs_map, Es, nfr, T=300): dg = [0] ddg2 = [] OIs = [] for temp_Es, temp_nfr in LPs_map.for_neigh_Es_nfr(Es, nfr): Deltaf_ij, dDeltaf_ij, mbar = calc_dg_mbar(temp_Es, temp_nfr, T=T) OIs.append(calc_OI_BAR(temp_Es, temp_nfr)[0]) dg.append(dg[-1] + Deltaf_ij[0][1]) ddg2.append(dDeltaf_ij[0][1]**2) return np.array(dg), np.array(ddg2), OIs
def update_LPs_2(LPs, seg_score_flag, converged_segments, dl_min): segs2sim = [] W_segs = [] for i in range(len(LPs) - 1): temp_seg = LPs[i], LPs[i + 1] if not check_fullseg_in_segs(temp_seg, converged_segments): W_segs.append(seg_score_flag[temp_seg][0]) segs2sim.append(temp_seg) W_segs = np.array(W_segs).T for i in range(len(W_segs)): W_segs[i] /= float(sum(W_segs[i])) new_LPs_weights = {} for i in range(len(segs2sim)): temp_seg = segs2sim[i] LPs_2_sim = [temp_seg[0]] temp_new_lp = Real.fix_float(np.mean(temp_seg)) if dl_min <= Real.fix_float(temp_seg[1] - temp_new_lp): LPs_2_sim.append(temp_new_lp) LPs_2_sim.append(temp_seg[1]) temp_w = np.average(W_segs.T[i]) / len(LPs_2_sim) for temp_l in LPs_2_sim: if temp_l not in new_LPs_weights: new_LPs_weights[temp_l] = 0 new_LPs_weights[temp_l] += temp_w return new_LPs_weights
def calc_dg_exTI_mbar(mbar, dEs, LPs_pred, flag_calc_w=True, fnc2integrate=integrate.simps): # calculates dG from exTI in combination with MBAR dhdl_pred_mbar = _calc_exTI_pred_mbar(mbar, dEs, LPs_pred, flag_calc_w=flag_calc_w) return _calc_dg_TI(dhdl_pred_mbar, LPs_pred, fnc2integrate=fnc2integrate), dhdl_pred_mbar dg = [0] for i in range(2, len(LPs_pred) + 1): dg.append(fnc2integrate(dhdl_pred_mbar[:i], LPs_pred[:i])) return np.array(dg), np.array(dhdl_pred_mbar)
def _parse_header(f_path, dl_max, comments): f = open(f_path) c_lines_no_comm, c_lines = 0, 0 sim_l, step, t = None, None, None for l in f: if _not_start_with_comm(l, comments): if c_lines_no_comm==0: N_LPs = int(l) elif c_lines_no_comm==1: LPs = np.array(l.split(), dtype=float) c_lines_no_comm+=1 else: if c_lines==0: temp = l.split() step, t = int(temp[-2]), float(temp[-1]) elif c_lines==1 and 'lam_s' in l: sim_l = float(l.split()[-1]) if c_lines_no_comm==3: break c_lines+=1 LP_offset = 0 if len(LPs)!=N_LPs: assert len(LPs) == N_LPs + 1 sim_l = LPs[0] LP_offset = 1 lp2use, lp2use_pos = _get_LPs2use(LPs, dl_max, sim_l, LP_offset) return c_lines, lp2use, lp2use_pos, LPs, sim_l, step, t
def get_nfr_mul_from_NFRs(NFRs, LPs, LPs_pred): nfr_mul = [] for l in LPs_pred: if l in LPs: nfr_mul.append(np.array(NFRs[l])) else: nfr_mul.append([0]) return nfr_mul
def get_nfr_from_NFRs(NFRs, LPs, LPs_pred): nfr = [] for l in LPs_pred: if l in LPs: nfr.append(sum(NFRs[l])) else: nfr.append(0) return np.array(nfr)
def _calc_exTI_pred_mbar(mbar, dEs, LPs_pred, flag_calc_w=True): if flag_calc_w: w = mbar.getWeights().T else: w = mbar dhdl_pred_mbar = [] for i in range(len(LPs_pred)): dhdl_pred_mbar.append(np.dot(w[i], dEs[i])) return np.array(dhdl_pred_mbar)
def _calc_singleLP_exTI_pred_mbar(Es, dEs, LPs_pred, nfr, T=300): assert Es.shape == dEs.shape kT = kb * T mbar = MBAR(Es / kT, nfr) w = mbar.getWeights() wt = w.T dgdl_int = [] for i in range(len(LPs_pred)): dgdl_int.append(np.dot(wt[i], dEs[i])) return np.array(dgdl_int)
def _get_rot_v(v, n=1, init_pv=None, mrot=210.): if init_pv is None: temp = abs(v[0]) pos = 0 for i in range(1, 3): if abs(v[i]) < temp: temp = abs(v[i]) pos = i axis = [0., 0., 0.] axis[pos] = 1 pv = rot(v, 90, axis) pv[pos] = 0 else: pv = np.array(init_pv) pv /= np.linalg.norm(pv) rot_vs = np.array(pv) if n > 1: rot_angles = np.linspace(0, mrot, n) for a in rot_angles[1:]: rot_vs = np.stack([rot_vs, rot(pv, a, v)]) return rot_vs
def rot(v, theta=90, axis=None): if axis is None: axis = [0, 0, 1] axis = np.asarray(axis) theta = np.radians(theta) / 2.0 axis = axis / np.linalg.norm(axis) a = np.cos(theta) b, c, d = -axis * np.sin(theta) aa, bb, cc, dd = a * a, b * b, c * c, d * d bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d m = np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)], [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)], [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]]) return np.dot(m, v)
def calc_exTI_err(exTI_data, LPs_pred=None, flag_get_exTI_data_avg=True, fnc2call_left_right=np.mean): """ calculates the difference between predictions from neighbouring points :param exTI_data: :param LPs_pred: :param flag_get_exTI_data_avg: :param fnc2call_left_right: (np.mean by default); it could also be min, max, lambda x:x[0] (return left) :return: exTI_err excluding / including simulated LPs """ LPs, LPs_pred = _get_LPs_pred_exTI(exTI_data, LPs_pred) if flag_get_exTI_data_avg: exTI_data_avg = _get_exTI_data_avg(exTI_data) else: exTI_data_avg = exTI_data dhdl_err, dhdl_err2 = [], [] for lp in LPs_pred: pos = bisect_left(LPs, lp) if lp not in exTI_data: v1 = exTI_data_avg[LPs[pos - 1]][lp] v2 = exTI_data_avg[LPs[pos]][lp] err = abs(v1 - v2) dhdl_err.append(err) dhdl_err2.append(err) else: dhdl_err.append(0) temp_err2 = [] v = exTI_data_avg[LPs[pos]][lp] if pos != 0: temp_err2.append(abs(v - exTI_data_avg[LPs[pos - 1]][lp])) if pos + 1 != len(LPs): temp_err2.append(abs(v - exTI_data_avg[LPs[pos + 1]][lp])) dhdl_err2.append(fnc2call_left_right(temp_err2)) return np.array(dhdl_err), np.array(dhdl_err2)
def __E(coord_flat, l12, l13, lrest, d_12, d_13, Fk, rest_pow): E = 0 coord = coord_flat.reshape(coord_flat.shape[0] // 2, 2) temp_ind_lists = list(zip(*l12)) temp_distances = coord[temp_ind_lists[0], ] - coord[ temp_ind_lists[1], ] temp_distances = np.linalg.norm(temp_distances, axis=1) E += 0.5 * Fk[0] * np.linalg.norm( temp_distances - np.array([d_12] * temp_distances.shape[0])) if l13: temp_ind_lists = list(zip(*l13)) temp_distances = coord[temp_ind_lists[0], ] - coord[ temp_ind_lists[1], ] temp_distances = np.linalg.norm(temp_distances, axis=1) E += 0.5 * Fk[1] * np.linalg.norm( temp_distances - np.array([d_13] * temp_distances.shape[0])) if lrest: temp_ind_lists = list(zip(*lrest)) temp_distances = coord[temp_ind_lists[0], ] - coord[ temp_ind_lists[1], ] temp_distances = Fk[2] / np.sum(temp_distances**(2 * rest_pow), axis=1) E += np.sum(temp_distances) return E
def __init__(self, l0, l1, LPs, LPs_pred): self.l0 = Real(l0).value self.l1 = Real(l1).value self.LPs = LPs self.LPs_pred = LPs_pred self.LPs_interval = [] # LPs (simulated) in the interval self.LPs_pred_interval = [] # LPs_pred (predicted) in the interval self.pos_inLPs_LPs_interval = [ ] # position (in LPs) of LPs in the interval (before pos_LPs_interval_inLPs) self.pos_LPs_interval = [ ] # position (in LPs_pred) of LPs in the interval self.pos_LPs_pred_interval = [ ] # position (in LPs_pred) of LPs_pred in the interval self.pos_LPs = [] # position (in LPs_pred) of LPs (all) # full interval - for l0 / l1 not in LPs for i, lp in enumerate(LPs_pred): if self.l0 <= lp <= self.l1: self.LPs_pred_interval.append(lp) self.pos_LPs_pred_interval.append(i) if lp in LPs: self.LPs_interval.append(lp) self.pos_inLPs_LPs_interval.append(LPs.index(lp)) self.pos_LPs_interval.append(i) self.pos_LPs.append(i) elif lp in LPs: self.pos_LPs.append(i) self.pos_inLPs_pred_interval__LPs_interval = np.array( self.pos_LPs_interval) - self.pos_LPs_interval[0] # position (in LPs_pred_interval) of LPs_interval (before pos_LPs_interval_inLPs_pred_interval) if l0 not in LPs or l1 not in LPs: self.pos_inLPs_LPs_interval_full = list( self.pos_inLPs_LPs_interval ) # position (in LPs) of LPs in the interval if self.LPs_interval[0] != l0: self.pos_inLPs_LPs_interval_full.insert( 0, self.pos_inLPs_LPs_interval[0] - 1) if self.LPs_interval[-1] != l1: self.pos_inLPs_LPs_interval_full.append( self.pos_inLPs_LPs_interval[-1] + 1) self.LPs_interval_full = [ LPs[pos] for pos in self.pos_inLPs_LPs_interval_full ] # LPs (simulated) in the interval
def _calc_dg_TI(dhdl, LPs, fnc2integrate=integrate.simps): """calculates dG from TI""" dg = [0] for i in range(2, len(LPs) + 1): dg.append(fnc2integrate(dhdl[:i], LPs[:i])) return np.array(dg)
def rot_2D(v, theta=90): theta = np.radians(theta) sin = np.sin(theta) cos = np.cos(theta) R = np.array([[cos, -sin], [sin, cos]]) return R.dot(v)
def _add_midpoints(LPs, dl_min): LPs_mid = get_lset(np.array(LPs)[:-1] + np.diff(LPs) * 0.5) LPs_mid = set(LPs_mid) & set(get_lset(np.arange(0., 1.0000001, dl_min))) new_LPs = list(LPs) new_LPs.extend(LPs_mid) return get_lset(new_LPs)
def get_nfr_from_nfr_mul(nfr_mul): return np.array([sum(nfr_list) for nfr_list in nfr_mul])
def get_nfr_LPs(nfr): nfr_LPs = np.array(nfr) return nfr_LPs[nfr_LPs > 0]