Exemplo n.º 1
0
def test_line_scan():
    start = (0, 0)
    end = (1, 1)
    scan = LineScan(start, end, gpts=5)

    positions = scan.get_positions()
    assert np.allclose(positions[0], start)
    assert np.allclose(positions[-1], end)
    assert np.allclose(positions[2], np.mean([start, end], axis=0))
    assert np.allclose(np.linalg.norm(np.diff(positions, axis=0), axis=1), scan.sampling[0])

    scan = LineScan(start, end, gpts=5, endpoint=False)
    positions = scan.get_positions()
    assert np.allclose(positions[0], start)
    assert np.allclose(positions[-1], (end[0] - (end[0] - start[0]) / 5, end[1] - (end[1] - start[1]) / 5))
Exemplo n.º 2
0
    def interpolate_line(self, start, end, gpts=None, sampling=None):
        from abtem.scan import LineScan

        if not (self.dimensions == 2):
            raise RuntimeError()

        if (self.calibrations[0] is None) or (self.calibrations[1] is None):
            raise RuntimeError()

        if self.calibrations[0].units != self.calibrations[1].units:
            raise RuntimeError()

        if (gpts is None) & (sampling is None):
            sampling = (self.calibrations[0].sampling +
                        self.calibrations[1].sampling) / 2.

        x = np.linspace(self.calibrations[0].offset,
                        self.shape[0] * self.calibrations[0].sampling,
                        self.shape[0])
        y = np.linspace(self.calibrations[1].offset,
                        self.shape[1] * self.calibrations[1].sampling,
                        self.shape[1])

        scan = LineScan(start=start, end=end, gpts=gpts, sampling=sampling)

        interpolated_array = interpn((x, y), self.array, scan.get_positions())

        calibration = Calibration(offset=0,
                                  sampling=scan.sampling[0],
                                  units=self.calibrations[0].units,
                                  name=self.calibrations[0].name)
        return Measurement(interpolated_array, calibration)
Exemplo n.º 3
0
    def interpolate_line(self, start, end, gpts=None, sampling=None, width=None, interpolation='splinef2d'):
        from abtem.scan import LineScan
        self = self.squeeze()

        if (self.calibrations[0] is None) or (self.calibrations[1] is None):
            raise RuntimeError()

        if self.calibrations[0].units != self.calibrations[1].units:
            raise RuntimeError()

        if (gpts is None) & (sampling is None):
            sampling = (self.calibrations[0].sampling + self.calibrations[1].sampling) / 2.

        x = np.linspace(self.calibrations[0].offset, self.shape[0] * self.calibrations[0].sampling, self.shape[0])
        y = np.linspace(self.calibrations[1].offset, self.shape[1] * self.calibrations[1].sampling, self.shape[1])

        scan = LineScan(start=start, end=end, gpts=gpts, sampling=sampling)

        if width is not None:
            direction = scan.direction
            perpendicular_direction = np.array([-direction[1], direction[0]])
            n = int(np.ceil(width / scan.sampling[0]))
            perpendicular_positions = np.linspace(-width, width, n)[:, None] * perpendicular_direction[None]
            positions = scan.get_positions()[None] + perpendicular_positions[:, None]
            positions = positions.reshape((-1, 2))
            interpolated_array = interpn((x, y), self.array, positions, method=interpolation, bounds_error=False,
                                         fill_value=0)
            interpolated_array = interpolated_array.reshape((n, -1)).mean(0)

        else:
            interpolated_array = interpn((x, y), self.array, scan.get_positions(), method=interpolation,
                                         bounds_error=False, fill_value=0)

        calibration = Calibration(offset=0, sampling=scan.sampling[0],
                                  units=self.calibrations[0].units,
                                  name=self.calibrations[0].name)
        return Measurement(interpolated_array, calibration)
Exemplo n.º 4
0
def interpolate_line(measurement: Measurement,
                     start: Sequence[float],
                     end: Sequence[float],
                     sampling: Sequence[float] = None):
    from abtem.scan import LineScan

    array = np.squeeze(measurement.array)

    if not (len(array.shape) == 2):
        raise RuntimeError()

    if (measurement.calibrations[-1] is None) or (measurement.calibrations[-2]
                                                  is None):
        raise RuntimeError()

    if measurement.calibrations[-2].units != measurement.calibrations[-1].units:
        raise RuntimeError()

    if (sampling is None):
        sampling = (measurement.calibrations[-2].sampling +
                    measurement.calibrations[-1].sampling) / 2.

    x = np.linspace(
        measurement.calibrations[-2].offset,
        measurement.shape[-2] * measurement.calibrations[-2].sampling,
        measurement.shape[-2])
    y = np.linspace(
        measurement.calibrations[1].offset,
        measurement.shape[-1] * measurement.calibrations[-1].sampling,
        measurement.shape[-1])

    line_scan = LineScan(start=start, end=end, sampling=sampling)

    interpolated_array = interpn((x, y), array, line_scan.get_positions())

    return Measurement(
        interpolated_array,
        Calibration(offset=0,
                    sampling=line_scan.sampling[0],
                    units=measurement.calibrations[-2].units,
                    name=measurement.calibrations[-2].name))
Exemplo n.º 5
0
    def interpolate_line(
            self,
            start: Tuple[float, float],
            end: Tuple[float, float],
            gpts: int = None,
            sampling: float = None,
            width: float = None,
            interpolation_method: str = 'splinef2d') -> 'Measurement':
        """
        Interpolate 2d measurement along a line.

        Parameters
        ----------
        start : two float
            Start point on line [Å].
        end : two float
            End point on line [Å].
        gpts : int
            Number of grid points along line.
        sampling : float
            Sampling rate of grid points along line [1 / Å].
        width : float
            The interpolation will be averaged across
        interpolation_method : str


        Returns
        -------
        Measurement
            Line profile measurement.
        """
        from abtem.scan import LineScan
        measurement = self.squeeze()

        if measurement.dimensions != 2:
            raise RuntimeError('measurement must be 2d')

        if measurement.calibrations[0].units != measurement.calibrations[
                1].units:
            raise RuntimeError(
                'the units of the interpolation dimensions must match')

        if (gpts is None) & (sampling is None):
            sampling = (measurement.calibrations[0].sampling +
                        measurement.calibrations[1].sampling) / 2.

        x = np.linspace(
            measurement.calibrations[0].offset,
            measurement.shape[0] * measurement.calibrations[0].sampling +
            measurement.calibrations[0].offset, measurement.shape[0])
        y = np.linspace(
            measurement.calibrations[1].offset,
            measurement.shape[1] * measurement.calibrations[1].sampling +
            measurement.calibrations[1].offset, measurement.shape[1])

        scan = LineScan(start=start, end=end, gpts=gpts, sampling=sampling)

        if width is not None:
            direction = scan.direction
            perpendicular_direction = np.array([-direction[1], direction[0]])
            n = int(np.ceil(width / scan.sampling[0]))
            perpendicular_positions = np.linspace(
                -width, width, n)[:, None] * perpendicular_direction[None]
            positions = scan.get_positions(
            )[None] + perpendicular_positions[:, None]
            positions = positions.reshape((-1, 2))
            interpolated_array = interpn((x, y),
                                         measurement.array,
                                         positions,
                                         method=interpolation_method,
                                         bounds_error=False,
                                         fill_value=0)
            # import matplotlib.pyplot as plt
            # plt.plot(*positions.T,'ro')
            # plt.show()

            interpolated_array = interpolated_array.reshape((n, -1)).mean(0)

        else:
            interpolated_array = interpn((x, y),
                                         measurement.array,
                                         scan.get_positions(),
                                         method=interpolation_method,
                                         bounds_error=False,
                                         fill_value=0)

        calibration = Calibration(offset=0,
                                  sampling=scan.sampling[0],
                                  units=measurement.calibrations[0].units,
                                  name=measurement.calibrations[0].name)
        return Measurement(interpolated_array, calibration)