def set_initial(posterior, pklfile, burnin=-10, max_distance=80): """Prepare input for refinement. """ samples = load(pklfile) coords = samples['coordinates'].reshape(len(samples), -1, 3)[burnin:] coords = coords.reshape(-1, 3) norm = np.linalg.norm(coords, axis=1) coords = coords[norm < max_distance] n_particles = len(posterior.params['coordinates'].get()) // 3 new_coords = coords[-n_particles:].copy() new_coords += np.random.randn(*new_coords.shape) posterior.params['coordinates'].set(new_coords) posterior.update() cc = [] for image in posterior.likelihoods: for param in (image.mock._rotation, image.mock._scale, image.mock._background): param.set(samples[param.name][-1]) image.update() image.precision.set(1 / np.var(image.data - image.mock.get())) cc.append(isd.crosscorr(image.data, image.mock.get())) print(np.min(cc), np.max(cc), np.mean(cc))
def load(self, aa, f_name): """ Load set of expansion coefficients from isd+. @param aa: Amino acid type @param f_name: File containing ramachandran definition """ import os from numpy import reshape, array from csb.io import load f_name = os.path.expanduser(f_name) params, _energies = load(f_name) params = params[self._n] a, b, c, d = params[aa] a, b, c, d = reshape(array(a), (self._n, self._n)).astype('d'), \ reshape(array(b), (self._n, self._n)).astype('d'), \ reshape(array(c), (self._n, self._n)).astype('d'), \ reshape(array(d), (self._n, self._n)).astype('d') # Not a typo, I accidently swichted cos*sin and sin*cos self._cc, self._cs, self._sc, self._ss = -a, -c, -b, -d
if not 'rex' in globals(): with open('./run_parallel.py') as script: exec script def calc_energy(state, rex): E = [] for x, s in zip(state,rex.samplers): s.set_replica_params() #s.model.params['coordinates'].set(x.positions) s.parameter.set(x.positions) E.append(-s.model.log_prob()) return E samples = load('/tmp/samples{}.pkl'.format(['',2][0])) E = np.array([[s.potential_energy for s in S] for S in samples]) X = np.array([[s.positions for s in S] for S in samples]) EE = [] for x in X: e = [] for k, y in enumerate(x): sampler = rex[k] sampler.parameter.set(y) sampler.set_replica_params() e.append(-sampler.model.log_prob()) EE.append(e) EE = np.array(EE)
prior = simulation.create_prior() posterior = isd.PosteriorCoordinates('ribosome', likelihoods=likelihoods, priors=[prior]) # predict radius of gyration Rg = 2.74 * (n_atoms / 10.)**0.34 # create prior term rgyr = simulation.create_radius_of_gyration(1., 'exponential') rgyr.rate = 10. # force Rg term to be treated as prior posterior.priors.append(rgyr) # spherical initial structure params['coordinates'].set(isd.random_sphere(n_particles, Rg)) pklfile = '../simulations/posterior_ribosome_200.pkl' if False and os.path.exists(pklfile): print('loading {}'.format(pklfile)) ensemble = load(pklfile) gibbs.set_params(posterior, ensemble) coords = params['coordinates'].get().reshape(-1, 3) coords += np.random.standard_normal(coords.shape) * diameter / 4. posterior.params['coordinates'].set(coords) else: print('{} not found'.format(pklfile)) posterior.update()
samples.append(rex.next()) if __name__ == '__main__': from csb.io import load, dump import os n_replicas = 50 schedule = np.transpose([ np.logspace(0., np.log10(1.06), n_replicas), np.linspace(1., 0.1, n_replicas) ]) try: schedule = load( os.path.expanduser('~/tmp/geo_rosetta_contacts_Rg_6_schedule.pkl')) except: schedule = load( '/space/users/mhabeck/tmp/geo_rosetta_contacts_Rg_6_schedule.pkl') schedule[0, :] = 1. posterior = create_posterior() replicas = [create_replica(q, beta, posterior) for q, beta in schedule] rex = ParallelReplicaExchange(replicas, n_cpus=25) samples = [] if False:
Test pickling of c-wrappers """ from csb.io import load, dump from isdhic import NBList resolution = 500 with open('../scripts/chrX_cell1_{}kb.py'.format(resolution)) as script: exec script forcefield = posterior['tsallis'].forcefield nblist = forcefield.nblist dump(nblist, '/tmp/nblist.pkl') nblist2 = load('/tmp/nblist.pkl') print nblist.ctype.update(posterior.params['coordinates'].get().reshape(-1, 3), 1) print nblist2.ctype.update( posterior.params['coordinates'].get().reshape(-1, 3), 1) dump(forcefield, '/tmp/forcefield.pkl') forcefield2 = load('/tmp/forcefield.pkl') print forcefield.energy(posterior.params['coordinates'].get().reshape(-1, 3)) print forcefield2.energy(posterior.params['coordinates'].get().reshape(-1, 3)) posterior['tsallis'].q = 1.03 posterior['rog'].beta = 0.2 posterior['contacts'].beta = 0.2