def test_q_values(self): q_values = np.array([1.0, 2.0, 3.0]) num_phi = 360 k = 2.0 * np.pi / 1.4 qxyz = xray._q_grid_as_xyz(q_values, num_phi, k) # assert that the above vectors are the correct length assert np.all( np.abs( np.sqrt( np.sum( np.power(qxyz,2), axis=1 ) ) - \ np.repeat(q_values, num_phi)) < 1e-6 )
def test_multi_panel_interp(self): # regression test ensuring detectors w/multiple basisgrid panels # are handled correctly t = structure.load_coor(ref_file('gold1k.coor')) q_values = np.array([2.66]) multi_d = xray.Detector.load(ref_file('lcls_test.dtc')) num_phi = 1080 num_molecules = 1 xyzlist = t.xyz[0, :, :] * 10.0 # convert nm -> ang. / first snapshot atomic_numbers = np.array( [a.element.atomic_number for a in t.topology.atoms()]) # generate a set of random numbers that we can use to make sure the # two simulations have the same molecular orientation (and therefore) # output rfloats = np.random.rand(num_molecules, 3) # --- first, scatter onto a perfect ring q_grid = xray._q_grid_as_xyz(q_values, num_phi, multi_d.k) ring_i = cpuscatter.simulate(num_molecules, q_grid, xyzlist, atomic_numbers, rfloats=rfloats) perf = xray.Rings(q_values, ring_i[None, None, :], multi_d.k) # --- next, to the full detector q_grid2 = multi_d.reciprocal real_i = cpuscatter.simulate(num_molecules, q_grid2, xyzlist, atomic_numbers, rfloats=rfloats) # interpolate ss = xray.Shotset(real_i, multi_d) real = ss.to_rings(q_values, num_phi) # count the number of points that differ significantly between the two diff = ( np.abs((perf.polar_intensities[0,0,:] - real.polar_intensities[0,0,:]) \ / real.polar_intensities[0,0,:]) > 1e-3) print np.sum(diff) assert np.sum(diff) < 300
def test_multi_panel_interp(self): # regression test ensuring detectors w/multiple basisgrid panels # are handled correctly t = structure.load_coor(ref_file('gold1k.coor')) q_values = np.array([2.66]) multi_d = xray.Detector.load(ref_file('lcls_test.dtc')) num_phi = 1080 num_molecules = 1 xyzlist = t.xyz[0,:,:] * 10.0 # convert nm -> ang. / first snapshot atomic_numbers = np.array([ a.element.atomic_number for a in t.topology.atoms() ]) # generate a set of random numbers that we can use to make sure the # two simulations have the same molecular orientation (and therefore) # output rfloats = np.random.rand(num_molecules, 3) # --- first, scatter onto a perfect ring q_grid = xray._q_grid_as_xyz(q_values, num_phi, multi_d.k) ring_i = _cpuscatter.simulate(num_molecules, q_grid, xyzlist, atomic_numbers, rfloats=rfloats) perf = xray.Rings(q_values, ring_i[None,None,:], multi_d.k) # --- next, to the full detector q_grid2 = multi_d.reciprocal real_i = _cpuscatter.simulate(num_molecules, q_grid2, xyzlist, atomic_numbers, rfloats=rfloats) # interpolate ss = xray.Shotset(real_i, multi_d) real = ss.to_rings(q_values, num_phi) # count the number of points that differ significantly between the two diff = ( np.abs((perf.polar_intensities[0,0,:] - real.polar_intensities[0,0,:]) \ / real.polar_intensities[0,0,:]) > 1e-3) print np.sum(diff) assert np.sum(diff) < 300
def simulate(self, n_repeats, compare='rmsd', plot_rings=False): """ Simualtes one molecule on both a perfect "ring" detector, and also the real `detector` object that the class owns. Then apply whatever "issues" we're going to look at, interpolate to polar, and compare the results. Repeat `n_repeats` times. Parameters ---------- n_repeats : int How many times to repeat the simulation compare : function A function that takes two one-d arrays of variable length, and returns a float that indicates how well the two simulations match. Can also be a str to note some common functions: {'rmsd','corr_rmsd'} """ xyzlist = self.traj.xyz[0,:,:] * 10.0 # convert nm -> ang. / first snapshot atomic_numbers = np.array([ a.element.atomic_number for a in self.traj.topology.atoms() ]) comparison = np.zeros(n_repeats) for i in range(n_repeats): print "Simulation %d/%d" % (i+1, n_repeats) # generate a set of random numbers that we can use to make sure the # two simulations have the same molecular orientation (and therefore) # output rfloats = np.random.rand(self.num_molecules, 3) # --- first, scatter onto a perfect ring q_grid = xray._q_grid_as_xyz(self.q_values, self.num_phi, self.detector.k) ring_i = _cpuscatter.simulate(self.num_molecules, q_grid, xyzlist, atomic_numbers, rfloats=rfloats) perf = xray.Rings(self.q_values, ring_i[None,None,:], self.detector.k) # --- next, to the full detector q_grid2 = self.detector.reciprocal real_i = _cpuscatter.simulate(self.num_molecules, q_grid2, xyzlist, atomic_numbers, rfloats=rfloats) # apply "issues" # for issue in self.issues: # real_i = self.__dict__['_' + issue](real_i) # interpolate ss = xray.Shotset(real_i, self.detector) real = ss.to_rings(self.q_values, self.num_phi) # --- plot if requested (mostly for debugging) if plot_rings: plt.figure() plt.plot(perf.polar_intensities[0,0,:]) plt.plot(real.polar_intensities[0,0,:]) plt.legend(['perfect', 'interpolated from detector']) plt.xlabel(r'$\phi$') plt.ylabel('Intensity') plt.show() # --- compare them on some grounds if compare == 'rmsd': comparison[i] = self._rmsd(perf.polar_intensities[0,0,:], real.polar_intensities[0,0,:]) elif compare == 'corr_rmsd': c1 = perf.correlate_intra(self.q_values[0], self.q_values[0], mean_only=True) c2 = real.correlate_intra(self.q_values[0], self.q_values[0], mean_only=True) comparison[i] = self._rmsd(c1, c2) else: try: comparison[i] = compare(perf.polar_intensities[0,0,:], real.polar_intensities[0,0,:]) except: raise Exception('bad compare function') return comparison
from odin import xray from odin import _cpuscatter from mdtraj import trajectory import matplotlib.pyplot as plt q_values = [1.4] traj = trajectory.load('3LYZ.pdb') xyzlist = traj.xyz[0] atomic_numbers = np.array([ a.element.atomic_number for a in traj.topology.atoms() ]) b = xray.Beam(None, energy=10.) # same as was used to make reference.dtc pol_q_grid = xray._q_grid_as_xyz(q_values, 360, b.k) dtc = xray.load('reference.dtc') dtc_q_grid = dtc.reciprocal num_molecules = 1 rfloats = np.random.rand(num_molecules, 3) pol_i = _cpuscatter.simulate(num_molecules, pol_q_grid, xyzlist, atomic_numbers, rfloats=rfloats) dtc_i = _cpuscatter.simulate(num_molecules, dtc_q_grid, xyzlist, atomic_numbers, rfloats=rfloats) ss = xray.Shotset(dtc_i, dtc) r = ss.to_rings(q_values, num_phi=360)