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_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
def test_reporter(): # stolen/modified from MDTraj/tests/test_reporter.py ... thanks rmcgibbo tempdir = os.path.join(testdir, 'test_reporter') os.makedirs(tempdir) pdb = PDBFile( ref_file('ala2.pdb') ) forcefield = ForceField('amber99sbildn.xml', 'amber99_obc.xml') system = forcefield.createSystem(pdb.topology, nonbondedMethod=CutoffNonPeriodic, nonbondedCutoff=1.0*nanometers, constraints=HBonds, rigidWater=True) integrator = LangevinIntegrator(300*kelvin, 1.0/picoseconds, 2.0*femtoseconds) integrator.setConstraintTolerance(0.00001) platform = Platform.getPlatformByName('Reference') simulation = Simulation(pdb.topology, system, integrator, platform) simulation.context.setPositions(pdb.positions) simulation.context.setVelocitiesToTemperature(300*kelvin) reffile = os.path.join(tempdir, 'traj.h5') testfile = os.path.join(tempdir, 'traj-test.h5') ref_reporter = HDF5Reporter(reffile, 2, coordinates=True, time=True, cell=True, potentialEnergy=True, kineticEnergy=True, temperature=True, velocities=True) test_reporter = sample.MCReporter(testfile, 2, coordinates=True, time=True, cell=True, potentialEnergy=True, kineticEnergy=True, temperature=True, velocities=True) simulation.reporters.append(ref_reporter) simulation.reporters.append(test_reporter) simulation.step(100) ref_reporter.close() test_reporter.close() with HDF5TrajectoryFile(testfile) as f: got = f.read() yield lambda: eq(got.temperature.shape, (50,)) yield lambda: eq(got.potentialEnergy.shape, (50,)) yield lambda: eq(got.kineticEnergy.shape, (50,)) yield lambda: eq(got.coordinates.shape, (50, 22, 3)) yield lambda: eq(got.velocities.shape, (50, 22, 3)) yield lambda: eq(got.time, 0.002*2*(1+np.arange(50))) yield lambda: f.topology == mdtraj.load(ref_file('ala2.pdb')).top ref_traj = mdtraj.load(reffile) test_traj = mdtraj.load(testfile) yield lambda: eq(ref_traj.xyz, test_traj.xyz) yield lambda: eq(ref_traj.unitcell_vectors, test_traj.unitcell_vectors) yield lambda: eq(ref_traj.time, test_traj.time) yield lambda: eq(ref_traj.xyz, test_traj.xyz) yield lambda: eq(ref_traj.unitcell_vectors, test_traj.unitcell_vectors)
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
def setup(self): if not HAVE_OPENMM: return self.potential = potential.FlatPotential() self.prior = 'amber99sbildn.xml' self.pdb = mdtraj.load(ref_file('ala2.pdb')) self.topology = PDBFile(ref_file('ala2.pdb')).topology # NEED TO CHANGE LATER self.starting_positions = self.pdb.xyz[0] self.mdmc = sample.MDMC(self.potential, self.prior, self.topology, self.starting_positions, openmm_platform='Reference', platform_properties={}, steps_per_iter=10)
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 = 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): if not HAVE_OPENMM: return self.potential = potential.FlatPotential() self.prior = 'amber99sbildn.xml' self.pdb = mdtraj.load(ref_file('ala2.pdb')) self.topology = PDBFile( ref_file('ala2.pdb')).topology # NEED TO CHANGE LATER self.starting_positions = self.pdb.xyz[0] self.mdmc = sample.MDMC(self.potential, self.prior, self.topology, self.starting_positions, openmm_platform='Reference', platform_properties={}, steps_per_iter=10)
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 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 self.rings = xray.Rings.simulate(self.traj, 1, self.q_values, self.num_phi, self.num_shots) # 1 molec
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.createEArray(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 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_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 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.i = np.abs(np.random.randn(self.d.num_pixels)) self.t = trajectory.load(ref_file('ala2.pdb')) self.shot = xray.Shotset(self.i, self.d)
def test_rm_com(): t = trajectory.load( ref_file('ala2.pdb') ) r = structure.remove_COM(t) masses = [ a.element.mass for a in t.topology.atoms() ] for i in range(t.n_frames): assert_array_almost_equal(np.zeros(3), np.average(t.xyz[i,:,:], weights=masses, axis=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
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_python_call(self): print "testing python wrapper fxn..." traj = trajectory.load(ref_file('ala2.pdb')) num_molecules = 512 detector = Detector.generic() py_I = xray.simulate_shot(traj, num_molecules, detector, verbose=True)
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.i = np.abs( np.random.randn(self.d.num_pixels) ) self.t = trajectory.load(ref_file('ala2.pdb')) self.shot = xray.Shotset(self.i, self.d)
def test_plotiq(): if not MPL: raise SkipTest if TRAVIS: raise SkipTest cmd = 'plotiq -i %s -m 1.0 > /dev/null 2>&1' % ref_file('refshot.shot') subprocess.check_call(cmd, shell=True) if not os.path.exists('intensity_plot.pdf'): raise RuntimeError('no output produced') else: os.remove('intensity_plot.pdf')
def test_cbf2shot(): if TRAVIS: raise SkipTest cmd = 'cbf2shot -i %s -o test.shot > /dev/null 2>&1' % ref_file('test1.cbf') subprocess.check_call(cmd, shell=True) if not os.path.exists('test.shot'): raise RuntimeError('no output produced') else: o = xray.Shotset.load('test.shot') os.remove('test.shot')
def test_replicate(): if TRAVIS: raise SkipTest cmd = 'replicate -i %s -n 10 -d 0.1 > /dev/null 2>&1' % ref_file('goldBenchMark.coor') subprocess.check_call(cmd, shell=True) if not os.path.exists('replicated.pdb'): raise RuntimeError('no output produced') else: o = trajectory.load('replicated.pdb') os.remove('replicated.pdb')
def test_plotcorr(): if not MPL: raise SkipTest if TRAVIS: raise SkipTest cmd = 'plotcorr -i %s > /dev/null 2>&1' % ref_file('refshot.shot') subprocess.check_call(cmd, shell=True) if not os.path.exists('correlation_plot.pdf'): raise RuntimeError('no output produced') else: os.remove('correlation_plot.pdf')
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_cbf2shot(): if TRAVIS: raise SkipTest cmd = 'cbf2shot -i %s -o test.shot > /dev/null 2>&1' % ref_file('test_cbf.cbf') subprocess.check_call(cmd, shell=True) if not os.path.exists('test.shot'): raise RuntimeError('no output produced') else: o = xray.Shotset.load('test.shot') os.remove('test.shot')
def test_plotcorr(): if not MPL: raise SkipTest if TRAVIS: raise SkipTest cmd = 'plotcorr -i %s > /dev/null 2>&1' % ref_file('reference_shot.shot') subprocess.check_call(cmd, shell=True) if not os.path.exists('correlation_plot.pdf'): raise RuntimeError('no output produced') else: os.remove('correlation_plot.pdf')
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_solvate(): if not OPENMM: raise SkipTest if TRAVIS: raise SkipTest cmd = 'solvate -i %s > /dev/null 2>&1' % ref_file('ala2.pdb') subprocess.check_call(cmd, shell=True) if not os.path.exists('solvated.pdb'): raise RuntimeError('no output produced') else: o = trajectory.load('solvated.pdb') os.remove('solvated.pdb')
def setup(self): self.t = trajectory.load( ref_file('ala2.pdb') ) # make a fake restraint array self.restraint_array = np.zeros((2,4)) self.restraint_array[0,:] = np.array([0, 5, 1.0, 1]) self.restraint_array[1,:] = np.array([4, 10, 10.0, 0]) self.dr = exptdata.DistanceRestraint(self.restraint_array)
def setup(self): self.t = trajectory.load(ref_file('ala2.pdb')) # make a fake restraint array self.restraint_array = np.zeros((2, 4)) self.restraint_array[0, :] = np.array([0, 5, 1.0, 1]) self.restraint_array[1, :] = np.array([4, 10, 10.0, 0]) self.dr = exptdata.DistanceRestraint(self.restraint_array)
def test_predictions(self): ala2 = mdtraj.trajectory.load(ref_file('ala2.pdb')) p = self.wep.predictions(ala2) assert p.shape == (1,5) #print 'pred0', self.expt1.predict(ala2) #print 'prediction', p
def test_plotiq(): if not MPL: raise SkipTest if TRAVIS: raise SkipTest cmd = 'odin.xray.plotiq -i %s -m 1.0 > /dev/null 2>&1' % ref_file( 'reference_shot.shot') subprocess.check_call(cmd, shell=True) if not os.path.exists('intensity_plot.pdf'): raise RuntimeError('no output produced') else: os.remove('intensity_plot.pdf')
def test_gpu_scatter(self): print "testing c code..." xyzQ = np.loadtxt(ref_file('512_atom_benchmark.xyz')) xyzlist = xyzQ[:,:3] atomic_numbers = xyzQ[:,3].flatten() q_grid = np.loadtxt(ref_file('512_q.xyz'))[:self.nq] rfloats = np.loadtxt(ref_file('512_x_3_random_floats.txt')) num_molecules = rfloats.shape[0] gpu_I = call_gpuscatter(xyzlist, atomic_numbers, num_molecules, q_grid, rfloats) self.ref_I = ref_simulate_shot(xyzlist, atomic_numbers, num_molecules, q_grid, rfloats) print "GPU", gpu_I print "CPU", self.ref_I assert_allclose(gpu_I, self.ref_I, rtol=1e-03, err_msg='scatter: gpu/cpu reference mismatch')
def test_all_on_many_img(self): image = imread(ref_file('chough-test2.png')) CM = om.CircularHough(radii=np.arange(10, 40, 2)) maxima = CM(image, mode='all') print "many circles:", maxima # the reference was confirmed visually TJL 12.27.12, the last # circle is, in fact, a false positive... (not sure how to fix it) ref = [(20, 62, 272), (20, 63, 168), (20, 64, 64), (22, 59, 376), (22, 60, 482), (26, 57, 691), (26, 58, 587), (26, 59, 589), (30, 56, 795), (34, 52, 952)] assert_allclose(maxima, ref)
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_all_on_many_img(self): image = imread(ref_file('chough-test2.png')) CM = om.CircularHough(radii=np.arange(10,40,2)) maxima = CM(image, mode='all') print "many circles:", maxima # the reference was confirmed visually TJL 12.27.12, the last # circle is, in fact, a false positive... (not sure how to fix it) ref = [(20, 62, 272), (20, 63, 168), (20, 64, 64), (22, 59, 376), (22, 60, 482), (26, 57, 691), (26, 58, 587), (26, 59, 589), (30, 56, 795), (34, 52, 952)] assert_allclose(maxima, ref)
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
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 setup(self): restraint_array = np.zeros((2,4)) restraint_array[0,:] = np.array([0, 5, 1.0, 1]) restraint_array[1,:] = np.array([4, 10, 10.0, 0]) dr = exptdata.DistanceRestraint(restraint_array) self.expts = [dr] self.confs = mdtraj.load(ref_file('ala3_3frames.h5')) self.meem = models.MaxEntEnsembleModel('IdealGas.xml', *self.expts) self.meem._conformations = self.confs # NEED TO REPLACE self.lambdas = np.ones(self.meem.num_measurements) return
def setup(self): restraint_array = np.zeros((2, 4)) restraint_array[0, :] = np.array([0, 5, 1.0, 1]) restraint_array[1, :] = np.array([4, 10, 10.0, 0]) dr = exptdata.DistanceRestraint(restraint_array) self.expts = [dr] self.confs = mdtraj.load(ref_file('ala3_3frames.h5')) self.meem = models.MaxEntEnsembleModel('IdealGas.xml', *self.expts) self.meem._conformations = self.confs # NEED TO REPLACE self.lambdas = np.ones(self.meem.num_measurements) return
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 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_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 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_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_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 setup(self): self.file = ref_file('ala2.pdb')