def test_vs_reference(self): qs = np.arange(2, 3.52, 0.02) silver = structure.load_coor(ref_file('SilverSphere.coor')) cl = scatter.sph_hrm_coefficients(silver, q_magnitudes=qs, num_coefficients=2)[1,:,:] ref = np.loadtxt(ref_file('ag_kam.dat')) # computed in matlab assert_allclose(cl, ref)
def test_i_profile(self): # doubles as a test for intensity_maxima() t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 5, 1) p = s.intensity_profile() m = s.intensity_maxima() assert np.any(np.abs(p[m,0] - 2.67) < 1e-1) # |q| = 2.67 is in {maxima}
def test_intensity_profile(self): q_values = [2.4, 2.67, 3.0] # should be a peak at |q|=2.67 t = structure.load_coor(ref_file('gold1k.coor')) rings = xray.Rings.simulate(t, 10, q_values, self.num_phi, 1) # 3 molec, 1 shots ip = rings.intensity_profile() assert ip[1,1] > ip[0,1] assert ip[1,1] > ip[2,1]
def test_i_profile(self): # doubles as a test for intensity_maxima() t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 5, 1) p = s.intensity_profile() m = s.intensity_maxima() assert np.any( np.abs(p[m, 0] - 2.67) < 1e-1) # |q| = 2.67 is in {maxima}
def test_intensity_profile(self): q_values = [2.4, 2.67, 3.0] # should be a peak at |q|=2.67 t = structure.load_coor(ref_file('gold1k.coor')) rings = xray.Rings.simulate(t, 10, q_values, self.num_phi, 1) # 3 molec, 1 shots ip = rings.intensity_profile() assert ip[1, 1] > ip[0, 1] assert ip[1, 1] > ip[2, 1]
def test_interpolate_to_polar(self): # doubles as a test for _implicit_interpolation q_values = np.array([2.0, 2.67, 3.7]) # should be a peak at |q|=2.67 t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 3, 1) pi, pm = s.interpolate_to_polar(q_values=q_values) ip = np.sum(pi[0, :, :], axis=1) assert ip[1] > ip[0] assert ip[1] > ip[2]
def test_interpolate_to_polar(self): # doubles as a test for _implicit_interpolation q_values = np.array([2.0, 2.67, 3.7]) # should be a peak at |q|=2.67 t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 3, 1) pi, pm = s.interpolate_to_polar(q_values=q_values) ip = np.sum(pi[0,:,:], axis=1) assert ip[1] > ip[0] assert ip[1] > ip[2]
def test(): d = xray.Detector.load('ds1_jun2011.dtc') traj = structure.load_coor('gold_2nm.coor') tst = LclsTester(d, traj) print tst.simulate(1, plot_rings=True) return
def test_load_coor(): s = structure.load_coor( ref_file('goldBenchMark.coor') ) s.save('s.pdb') t = trajectory.load('s.pdb') assert_array_almost_equal(s.xyz, t.xyz, decimal=3) for a in t.topology.atoms(): assert a.element.symbol == 'Au' if os.path.exists('s.pdb'): os.remove('s.pdb')
def test_multiply_conformations(): traj = structure.load_coor(ref_file('goldBenchMark.coor')) n_samples = 150 otraj = structure.multiply_conformations(traj, n_samples, 0.1) # iterate over x,y,z and check if any of the bins are more than 3 STD from the mean for i in [0,1,2]: h = np.histogram(otraj.xyz[:,0,i])[0] cutoff = h.std() * 3.0 # chosen arbitrarily deviations = np.abs(h - h.mean()) print deviations / h.std() if np.any( deviations > cutoff ): raise RuntimeError('Highly unlikely centers of mass are randomly ' 'distributed in space. Test is stochastic, though, so' ' try running again to make sure you didn\'t hit a ' 'statistical anomaly')
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_iprofile_consistency(self): t = structure.load_coor(ref_file('gold1k.coor')) d = xray.Detector.generic() s = xray.Shotset.simulate(t, d, 5, 1) q_values = np.arange(1.0, 4.0, 0.02) num_phi = 360 # compute from polar interp pi, pm = s._implicit_interpolation(q_values, num_phi) pi = pi.reshape(len(q_values), num_phi) ip1 = np.zeros((len(q_values), 2)) ip1[:,0] = q_values ip1[:,1] = pi.sum(1) # compute from detector ip2 = s.intensity_profile(0.02) # compute from rings r = xray.Rings.simulate(t, 10, q_values, 360, 1) ip3 = r.intensity_profile() # make sure maxima are all similar ind1 = utils.maxima( math2.smooth(ip1[:,1], beta=15.0, window_size=21) ) ind2 = utils.maxima( math2.smooth(ip2[:,1], beta=15.0, window_size=21) ) ind3 = utils.maxima( math2.smooth(ip3[:,1], beta=15.0, window_size=21) ) m1 = ip1[ind1,0] m2 = ip2[ind2,0] m3 = ip3[ind3,0] # discard the tails of the sim -- they have weak/noisy peaks # there should be strong peaks at |q| ~ 2.66, 3.06 m1 = m1[(m1 > 2.0) * (m1 < 3.2)] m2 = m2[(m2 > 2.0) * (m2 < 3.2)] m3 = m3[(m3 > 2.0) * (m3 < 3.2)] # I'll let them be two q-brackets off assert_allclose(m1, m2, atol=0.045) assert_allclose(m1, m3, atol=0.045) assert_allclose(m2, m3, atol=0.045)
def test_iprofile_consistency(self): t = structure.load_coor(ref_file('gold1k.coor')) d = xray.Detector.generic() s = xray.Shotset.simulate(t, d, 5, 1) q_values = np.arange(1.0, 4.0, 0.02) num_phi = 360 # compute from polar interp pi, pm = s._implicit_interpolation(q_values, num_phi) pi = pi.reshape(len(q_values), num_phi) ip1 = np.zeros((len(q_values), 2)) ip1[:, 0] = q_values ip1[:, 1] = pi.sum(1) # compute from detector ip2 = s.intensity_profile(0.02) # compute from rings r = xray.Rings.simulate(t, 10, q_values, 360, 1) ip3 = r.intensity_profile() # make sure maxima are all similar ind1 = utils.maxima(math2.smooth(ip1[:, 1], beta=15.0, window_size=21)) ind2 = utils.maxima(math2.smooth(ip2[:, 1], beta=15.0, window_size=21)) ind3 = utils.maxima(math2.smooth(ip3[:, 1], beta=15.0, window_size=21)) m1 = ip1[ind1, 0] m2 = ip2[ind2, 0] m3 = ip3[ind3, 0] # discard the tails of the sim -- they have weak/noisy peaks # there should be strong peaks at |q| ~ 2.66, 3.06 m1 = m1[(m1 > 2.0) * (m1 < 3.2)] m2 = m2[(m2 > 2.0) * (m2 < 3.2)] m3 = m3[(m3 > 2.0) * (m3 < 3.2)] # I'll let them be two q-brackets off assert_allclose(m1, m2, atol=0.045) assert_allclose(m1, m3, atol=0.045) assert_allclose(m2, m3, atol=0.045)
def test_to_rings(self): t = structure.load_coor(ref_file('gold1k.coor')) shot = xray.Shotset.simulate(t, self.d, 1, 1) shot_ip = shot.intensity_profile(0.1) q_values = shot_ip[:,0] rings = shot.to_rings(q_values) rings_ip = rings.intensity_profile() # normalize to the 6th entry, and discard values before that # which are usually just large + uninformative rings_ip[:,1] /= rings_ip[5,1] shot_ip[:,1] /= shot_ip[5,1] # for some reason assert_allclose not working, but this is x = np.sum( np.abs(rings_ip[5:,1] - shot_ip[5:,1]) ) x /= float(len(rings_ip[5:,1])) print x assert x < 0.2 # intensity mismatch assert_allclose(rings_ip[:,0], shot_ip[:,0], err_msg='test impl error')
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_to_rings(self): t = structure.load_coor(ref_file('gold1k.coor')) shot = xray.Shotset.simulate(t, self.d, 1, 1) shot_ip = shot.intensity_profile(0.1) q_values = shot_ip[:, 0] rings = shot.to_rings(q_values) rings_ip = rings.intensity_profile() # normalize to the 6th entry, and discard values before that # which are usually just large + uninformative rings_ip[:, 1] /= rings_ip[5, 1] shot_ip[:, 1] /= shot_ip[5, 1] # for some reason assert_allclose not working, but this is x = np.sum(np.abs(rings_ip[5:, 1] - shot_ip[5:, 1])) x /= float(len(rings_ip[5:, 1])) print x assert x < 0.2 # intensity mismatch assert_allclose(rings_ip[:, 0], shot_ip[:, 0], err_msg='test impl error')
def test_rotated_beam(self): # shift a detector up (in x) a bit and test to make sure there's no diff t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 5, 1) sh = 50.0 # the shift mag xyz = self.d.xyz.copy() shift = np.zeros_like(xyz) shift[:,0] += sh beam_vector = np.array([ sh/self.l, 0.0, 1.0 ]) # note that the detector du is further from the interaction site du = xray.Detector(xyz + shift, self.d.k, beam_vector=beam_vector) su = xray.Shotset.simulate(t, du, 5, 1) p1 = s.intensity_profile(q_spacing=0.05) p2 = su.intensity_profile(q_spacing=0.05) p1 /= p1.max() p2 /= p2.max() p1 = p2[:10,:] p2 = p2[:p1.shape[0],:] assert_allclose(p1, p2, rtol=0.1)
def test_rotated_beam(self): # shift a detector up (in x) a bit and test to make sure there's no diff t = structure.load_coor(ref_file('gold1k.coor')) s = xray.Shotset.simulate(t, self.d, 5, 1) sh = 50.0 # the shift mag xyz = self.d.xyz.copy() shift = np.zeros_like(xyz) shift[:, 0] += sh beam_vector = np.array([sh / self.l, 0.0, 1.0]) # note that the detector du is further from the interaction site du = xray.Detector(xyz + shift, self.d.k, beam_vector=beam_vector) su = xray.Shotset.simulate(t, du, 5, 1) p1 = s.intensity_profile(q_spacing=0.05) p2 = su.intensity_profile(q_spacing=0.05) p1 /= p1.max() p2 /= p2.max() p1 = p2[:10, :] p2 = p2[:p1.shape[0], :] assert_allclose(p1, p2, rtol=0.1)
import numpy as np import matplotlib.pyplot as plt from odin import structure, scatter qs = np.arange(2, 3.52, 0.02) silver = structure.load_coor('reference/SilverSphere.coor') cl = scatter.sph_hrm_coefficients(silver, q_magnitudes=qs, num_coefficients=2)[1,:,:] plt.imshow(cl.T) plt.show()
from matplotlib.pylab import * from odin import structure, xray gold = structure.load_coor('reference/gold1k.coor') r = xray.Rings.simulate(gold, 10, [2.67], 360, 100) figure() c = r.correlate_intra(2.67, 2.67, 4, mean_only = True ) # plot(c, lw=2) cl = r._convert_to_kam(2.67, 2.67, c) plot(cl[:,0], cl[:,1], lw=2) xlabel(r'$\cos \psi$ / rad') ylabel('correlation') show()
in the simulated pattern! The output is a rings file, containing the simulated stuff. -- TJL May 14 2013 """ import numpy as np from odin import xray from odin import structure from odin.math2 import ER_rotation_matrix # --------------------------------------------------------- # # Manually set parameters -- change these # traj = structure.load_coor('gold1k.coor') # or whatever shp = (201,201) # choose number of pixels distance = 20.0 # detector distance, pixel units energy = 10.0 # keV detector_tilt = 1.0 # in degrees num_molecules = 10 num_shots = 1 q_values = [2.66, 3.07] # inv. Ang. num_phi = 3600 output_fn = 'tilted.ring'