Exemplo n.º 1
0
    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')
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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}
Exemplo n.º 4
0
 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]
Exemplo n.º 5
0
 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}
Exemplo n.º 6
0
 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]
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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]
Exemplo n.º 9
0
 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]
Exemplo n.º 10
0
 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')
Exemplo n.º 11
0
    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')
Exemplo n.º 12
0
    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.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
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
    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.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
Exemplo n.º 16
0
    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')
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
    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)