def test_interpolate(): detector = PixelatedDetector() array = np.random.rand(2, 32, 32) gpts = array.shape[-2:] new_gpts = (16, 9) v, u, vw, uw = detector._bilinear_nodes_and_weight(gpts, new_gpts, (1 / 32, 1 / 32), (1 / 16, 1 / 9), np) interpolated = interpolate_bilinear_cpu(array, v, u, vw, uw) kx, ky = spatial_frequencies(gpts, (1, 1)) kx = np.fft.fftshift(kx) ky = np.fft.fftshift(ky) kx_new, ky_new = spatial_frequencies(new_gpts, (1, 1)) kx_new = np.fft.fftshift(kx_new) ky_new = np.fft.fftshift(ky_new) px, py = np.meshgrid(kx_new, ky_new, indexing='ij') p = np.array([px.ravel(), py.ravel()]).T interpolated2 = interpn((kx, ky), array[0], p) interpolated2 = interpolated2.reshape(new_gpts) assert np.allclose(interpolated[0], interpolated2)
def test_resample_diffraction_patterns(): for extent in [(5, 10), (5, 8)]: for gpts in [(256, 256), (256, 296)]: probe = Probe(energy=60e3, extent=extent, gpts=gpts, semiangle_cutoff=80, rolloff=0.2) detector = PixelatedDetector(max_angle='valid', resample='uniform') wave = probe.build() measurement = detector.detect(wave) measurement /= measurement.max() probe = Probe(energy=60e3, extent=(5, 5), gpts=(256, 256), semiangle_cutoff=80, rolloff=0.2) wave = probe.build() measurement2 = detector.detect(wave) measurement2 /= measurement2.max() s1 = (measurement2.shape[-2] - measurement.shape[-2]) // 2 s2 = (measurement2.shape[-1] - measurement.shape[-1]) // 2 measurement2 = measurement2[..., s1:s1 + measurement.shape[-2], s2:s2 + measurement.shape[-1]] assert np.all(np.abs(measurement2[0] - measurement[0]) < .5)
def test_gridscan_to_file(tmp_path): d = tmp_path / 'sub' d.mkdir() path = d / 'measurement2.hdf5' atoms = read(_set_path('orthogonal_graphene.cif')) potential = Potential(atoms=atoms, sampling=.05) probe = Probe(energy=200e3, semiangle_cutoff=30) probe.grid.match(potential) scan = GridScan(start=[0, 0], end=[0, potential.extent[1]], gpts=(10, 9)) detector = PixelatedDetector() export_detector = PixelatedDetector(save_file=path) measurements = probe.scan(scan, [detector, export_detector], potential, pbar=False) measurement = measurements[0] imported_measurement = Measurement.read(measurements[1]) assert np.allclose(measurement.array, imported_measurement.array) assert measurement.calibrations[0] == imported_measurement.calibrations[0] assert measurement.calibrations[1] == imported_measurement.calibrations[1]
def test_detector_consistency(): atoms = read('data/srtio3_100.cif') atoms *= (4, 4, 1) potential = Potential( atoms, gpts=256, projection='infinite', slice_thickness=.5, parametrization='kirkland', ).build(pbar=False) probe = Probe(energy=300e3, semiangle_cutoff=9.4, rolloff=0.05) flexible_detector = FlexibleAnnularDetector() annular_detector = AnnularDetector(inner=20, outer=40) pixelated_detector = PixelatedDetector() end = (potential.extent[0] / 4, potential.extent[1] / 4) gridscan = GridScan(start=[0, 0], end=end, sampling=.5) measurements = probe.scan( gridscan, [flexible_detector, pixelated_detector, annular_detector], potential, pbar=False) assert np.allclose(measurements[0].integrate(20, 40).array, measurements[2].array) assert np.allclose( annular_detector.integrate(measurements[1]).array, measurements[2].array)
def test_preallocated_measurement(): atoms = read( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/amorphous_carbon.cif')) potential = Potential(atoms, gpts=256, slice_thickness=1, projection='infinite', parametrization='kirkland').build(pbar=False) scan = GridScan(start=[0, 0], end=potential.extent, gpts=4) detector1 = AnnularDetector(inner=70, outer=100) probe = Probe(semiangle_cutoff=15, energy=300e3, extent=potential.extent, gpts=512) measurement = detector1.allocate_measurement(probe, scan) probe.scan(scan, detector1, potential, measurement, pbar=False) assert np.any(measurement.array > 0) detector2 = PixelatedDetector() measurement1 = detector1.allocate_measurement(probe, scan) measurement2 = detector2.allocate_measurement(probe, scan) with pytest.raises(ValueError) as e: probe.scan(scan, [detector1, detector2], potential, measurement1, pbar=False) probe.scan(scan, [detector1, detector2], potential, { detector1: measurement1, detector2: measurement2 }, pbar=False)
def test_pixelated_detector(): gpts = (512, 512) extent = (12, 12) probe = Probe(energy=60e3, extent=extent, gpts=gpts, semiangle_cutoff=80, rolloff=0.2) detector = PixelatedDetector(max_angle=30, resample='uniform') wave = probe.build().downsample(max_angle=30) measurement = detector.detect(wave) assert measurement.shape == wave.array.shape detector = PixelatedDetector(max_angle='valid', resample='uniform') measurement = detector.detect(wave) assert measurement.shape == wave.array.shape detector = PixelatedDetector(max_angle='limit', resample='uniform') measurement = detector.detect(wave) assert measurement.shape == wave.array.shape gpts = (512, 512) extent = (10, 12) probe = Probe(energy=60e3, extent=extent, gpts=gpts, semiangle_cutoff=80, rolloff=0.2) detector = PixelatedDetector(max_angle='valid', resample='uniform') wave = probe.build() measurement = detector.allocate_measurement(wave) assert measurement.array.shape[0] == measurement.array.shape[1]