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_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_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 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 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 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_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_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)
#!/usr/bin/env python import numpy as np from thor import structure from mayavi import mlab # grid parameters size = 30.0 # angstroms resolution = 0.25 # angstroms # compute the number of grid pts necessary grid_length = int(size / resolution) gd = (grid_length,)*3 print 'grid dimensions:', gd t = structure.load_coor('fcc_sphere_2nm.coor') m = structure.atomic_to_density(t, gd, resolution) np.save('fcc_sphere_2nm_map.npy', m) s = mlab.contour3d( m, contours=5, line_width=1.0, transparent=True ) mlab.show()