def test_fromfiles(self): ss = xray.Shotset.fromfiles( [ref_file('test_cbf.cbf'), ref_file('test_cbf2.cbf')] ) assert ss.num_shots == 2 n = 0 for i in ss.intensities: n += 1 assert ss.num_shots == n assert np.sum(ss.mask) > 1000
def setup(self): self.nq = 2 # number of detector vectors to do xyzQ = np.loadtxt(ref_file('512_atom_benchmark.xyz')) self.xyzlist = xyzQ[:,:3] * 10.0 # nm -> ang. self.atomic_numbers = xyzQ[:,3].flatten() self.q_grid = np.loadtxt(ref_file('512_q.xyz'))[:self.nq] self.rfloats = np.loadtxt(ref_file('512_x_3_random_floats.txt')) self.num_molecules = self.rfloats.shape[0]
def setup(self): self.nq = 5 # number of detector vectors to do self.nr = 10 # number of atoms to use xyzZ = np.loadtxt(ref_file('512_atom_benchmark.xyz')) self.xyzlist = xyzZ[:self.nr,:3] * 10.0 # nm -> ang. self.atomic_numbers = xyzZ[:self.nr,3].flatten() self.q_grid = np.loadtxt(ref_file('512_q.xyz'))[:self.nq] self.num_molecules = 512 self.ref_A = ref_simulate_shot(self.xyzlist, self.atomic_numbers, self.num_molecules, self.q_grid)
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, 20, 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_simulate_density(self): # generate a rings object both from an atomic and density model and # ensure the correlations match num_shots = 100 num_phi = 1024 nq = 100 # number of q vectors q_values = [1.0, 2.0] # atomic model traj = mdtraj.load(ref_file('pentagon.pdb')) r1 = xray.Rings.simulate(traj, 1, q_values, num_phi, num_shots) # density model grid_dimensions = [151,] * 3 grid_spacing = 1.0 # Angstroms grid = structure.atomic_to_density(traj, grid_dimensions, grid_spacing) r2 = xray.Rings.simulate_density(grid, grid_spacing, num_shots, q_values, num_phi) # compute correlations & ensure match c1 = r1.correlate_intra(1.0, 1.0) c2 = r2.correlate_intra(1.0, 1.0) R = np.corrcoef(c1, c2)[0,1] assert R > 0.95 c1 = r1.correlate_intra(2.0, 2.0) c2 = r2.correlate_intra(2.0, 2.0) R = np.corrcoef(c1, c2)[0,1] assert R > 0.95
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_from_atomic(self): """ simulate both from a grid and from an atomic model and ensure match """ num_molecules = 1 nq = 100 # number of q vectors q_grid = np.zeros((nq, 3)) q_grid[:,1] = np.linspace(.01, 2.0, nq) traj = mdtraj.load(ref_file('pentagon.pdb')) atomic_numbers = np.array([ a.element.atomic_number for a in traj.topology.atoms ]) cp, at = get_cromermann_parameters(atomic_numbers) rxyz = traj.xyz[0] * 10.0 ref_A = ref_simulate_shot(rxyz, atomic_numbers, num_molecules, q_grid, dont_rotate=True) grid_dimensions = [125,] * 3 grid_spacing = 0.1 # Angstroms grid = structure.atomic_to_density(traj, grid_dimensions, grid_spacing) A = scatter.simulate_density(grid, grid_spacing, num_molecules, q_grid, dont_rotate=True) tst = np.abs(A) ref = np.abs(ref_A) R = np.corrcoef(tst, ref)[0,1] assert R > 0.95, 'atomic and grid models significantly different'
def setup(self): self.q_values = np.array([1.0, 2.0]) self.num_phi = 360 self.traj = Trajectory.load(ref_file('ala2.pdb')) self.num_shots = 2 # generate the tables file on disk, then re-open it intensities = np.abs( np.random.randn(self.num_shots, len(self.q_values), self.num_phi) / 100.0 + \ np.cos( np.linspace(0.0, 4.0*np.pi, self.num_phi) ) ) if os.path.exists('tmp_tables.h5'): os.remove('tmp_tables.h5') hdf = tables.File('tmp_tables.h5', 'w') a = tables.Atom.from_dtype(np.dtype(np.float64)) node = hdf.create_earray(where='/', name='data', shape=(0, len(self.q_values), self.num_phi), atom=a, filters=io.COMPRESSION) node.append(intensities) hdf.close() self.tables_file = tables.File('tmp_tables.h5', 'r+') pi = self.tables_file.root.data pm = np.random.binomial(1, 0.9, size=(len(self.q_values), self.num_phi)) k = 1.0 self.rings = xray.Rings(self.q_values, pi, k, pm) return
def setup(self): self.q_values = np.array([1.0, 2.0]) self.num_phi = 360 self.traj = Trajectory.load(ref_file('ala2.pdb')) self.num_shots = 4 self.rings = xray.Rings.simulate(self.traj, 1, self.q_values, self.num_phi, self.num_shots) # 1 molec
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, self.num_phi) ip = np.sum(pi[0,:,:], axis=1) assert ip[1] > ip[0] assert ip[1] > ip[2]
def setup(self): self.q_values = np.array([1.0, 2.0]) self.num_phi = 360 self.l = 50.0 self.d = xray.Detector.generic(spacing=0.4, l=self.l) self.num_shots = 2 self.i = np.abs( np.random.randn(self.num_shots, self.d.num_pixels) ) self.t = Trajectory.load(ref_file('ala2.pdb')) self.shot = xray.Shotset(self.i, self.d)
def test_kabsch(): fn = ref_file('gold1k.coor') obj = structure.load_coor(fn).xyz[0,:,:] rot_obj = structure.rand_rotate_molecule2(obj) U = math2.kabsch(rot_obj, obj) obj2 = np.dot(rot_obj, U) assert_almost_equal(obj, obj2)
def setup(self): self.nq = 100 # number of detector vectors to do self.q_grid = np.loadtxt(ref_file('512_q.xyz'))[:self.nq] self.traj = mdtraj.load(ref_file('ala2.pdb')) atomic_numbers = np.array([ a.element.atomic_number for a in self.traj.topology.atoms ]) cp, at = get_cromermann_parameters(atomic_numbers) self.num_molecules = 32 rxyz = self.traj.xyz[0] * 10.0 self.ref_A = ref_simulate_shot(rxyz, atomic_numbers, self.num_molecules, self.q_grid) self.cpp_A = _cppscatter.cpp_scatter(self.num_molecules, rxyz, self.q_grid, at, cp, random_state=np.random.RandomState(RANDOM_SEED))
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-300) ) > 1e-3) print np.sum(diff) assert np.sum(diff) < 300
def test_sph_harm(): # ----------------------- traj = Trajectory.load(ref_file('pentagon.pdb')) q_magnitudes = [1.6] num_coefficients = 44 num_molecules = 1 num_shots = 20000 num_phi = 2048 # ----------------------- q = q_magnitudes[0] # compute the Kam-theoretic values of the Legendre coefficients C_ell, which # we will call "coeffsh" coeffsh_even = scatter.sph_harm_coefficients(traj, q_magnitudes, num_coefficients=num_coefficients/2) coeffsh_even = np.nan_to_num(coeffsh_even) coeffsh_even /= coeffsh_even[1] coeffsh = np.zeros(num_coefficients) coeffsh[0::2] = coeffsh_even.flatten() # next, preform a simulation of the scattering and empirically compute the # correlation function rings = xray.Rings.simulate(traj, num_molecules, q_magnitudes, num_phi, num_shots) c = rings.correlate_intra(q, q, mean_only=True) # it seems best to compare the solutions in the expanded basis c_sh = np.polynomial.legendre.legval(rings.cospsi(q, q), coeffsh.flatten()) c = c - c.mean() c_sh = c_sh - c_sh.mean() # plt.figure() # plt.plot(c_sh / c_sh[0]) # plt.plot(c / c[0]) # plt.show() # if these are more than 10% different, fail the test error = (np.sum(np.abs( (c_sh / c_sh[0]) - (c / c[0]) )) / float(num_phi)) assert error < 0.1, 'simulation and analytical computation >10%% different (%f %%)' % error return
def test_py_cpu_smoke(self): traj = Trajectory.load(ref_file('ala2.pdb')) num_molecules = 1 detector = xray.Detector.generic() detector.beam.photons_scattered_per_shot = 1e3 I = scatter.simulate_shot(traj, num_molecules, detector, finite_photon=True) # simple statistical sanity check assert np.abs(I.sum() - detector.beam.photons_scattered_per_shot) < \ np.sqrt(detector.beam.photons_scattered_per_shot)*6.0
def test_python_call(self): """ Test the GPU scattering simulation interface (scatter.simulate) """ if not GPU: raise SkipTest print "testing python wrapper fxn..." traj = Trajectory.load(ref_file('ala2.pdb')) num_molecules = 512 detector = xray.Detector.generic() py_I = scatter.simulate_shot(traj, num_molecules, detector) assert not np.all( py_I == 0.0 ) assert not np.isnan(np.sum( py_I ))
def test_to_rings_on_disk(self): # this test uses the Rings `rings_filename` flag t = structure.load_coor(ref_file('gold1k.coor')) shot = xray.Shotset.simulate(t, self.d, 1, 1) q_values = [1.0, 2.0] rings_ref = shot.to_rings(q_values) if os.path.exists('tmp.ring'): os.remove('tmp.ring') shot.to_rings(q_values, rings_filename='tmp.ring') rings = xray.Rings.load('tmp.ring') assert_array_almost_equal(rings_ref.polar_intensities, rings.polar_intensities) if os.path.exists('tmp.ring'): os.remove('tmp.ring')
def setup(self): self.q_values = np.array([1.0, 2.0]) self.num_phi = 360 self.l = 50.0 self.d = xray.Detector.generic(spacing=0.4, l=self.l) self.t = Trajectory.load(ref_file('ala2.pdb')) self.num_shots = 2 intensities = np.abs(np.random.randn(self.num_shots, self.d.num_pixels)) io.saveh('tmp_tables.h5', data=intensities) self.tables_file = tables.File('tmp_tables.h5') self.i = self.tables_file.root.data self.shot = xray.Shotset(self.i, self.d) return
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_no_hydrogens(): traj = Trajectory.load(ref_file('ala2.pdb')) num_molecules = 1 detector = xray.Detector.generic() detector.beam.photons_scattered_per_shot = 1e3 I_noH = scatter.simulate_shot(traj, num_molecules, detector, ignore_hydrogens=True, dont_rotate=True) I_wH = scatter.simulate_shot(traj, num_molecules, detector, ignore_hydrogens=False, dont_rotate=True) assert not np.all(I_noH == I_wH) # compute the differece -- we're not setting random numbers here so just # looking at radially averaged stuff... diff = np.sum(np.abs(I_noH - I_wH) / I_wH) / float(len(I_wH)) print diff assert diff < 1.0, 'ignoring hydrogens makes too big of a difference...'
def test_to_rings(self): t = structure.load_coor(ref_file('gold1k.coor')) shot = xray.Shotset.simulate(t, self.d, 1, 2) shot_ip = shot.intensity_profile(0.1) q_values = shot_ip[:,0] rings = shot.to_rings(q_values) assert rings.num_shots == shot.num_shots 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_multimodel(self): """ regression test """ t = mdtraj.load(ref_file('3LYZ_x2.pdb')) A = scatter.simulate_atomic(t, 1, self.q_grid)