Exemple #1
0
def test_oval_radial_background_symmetry():
    radius, angle = m.polar_map(centerX=10,
                                centerY=10,
                                imageSizeX=21,
                                imageSizeY=21,
                                stretchY=1.5,
                                angle=0.)

    radius2, angle2 = m.polar_map(centerX=10,
                                  centerY=10,
                                  imageSizeX=21,
                                  imageSizeY=21,
                                  stretchY=1.5,
                                  angle=np.pi)

    radius3, angle3 = m.polar_map(centerX=10,
                                  centerY=10,
                                  imageSizeX=21,
                                  imageSizeY=21,
                                  stretchY=1.5,
                                  angle=np.pi / 4)

    radius4, angle4 = m.polar_map(centerX=10,
                                  centerY=10,
                                  imageSizeX=21,
                                  imageSizeY=21,
                                  stretchY=1.5,
                                  angle=-np.pi / 4)

    assert np.allclose(radius, radius2)
    assert np.allclose(radius3, np.flip(radius4, axis=1))
Exemple #2
0
        def stack():
            rings = masks.radial_bins(centerX=cx,
                                      centerY=cy,
                                      imageSizeX=detector_x,
                                      imageSizeY=detector_y,
                                      radius=ro,
                                      radius_inner=ri,
                                      n_bins=n_bins,
                                      use_sparse=use_sparse,
                                      dtype=np.complex64)

            orders = np.arange(max_order + 1)

            r, phi = masks.polar_map(centerX=cx,
                                     centerY=cy,
                                     imageSizeX=detector_x,
                                     imageSizeY=detector_y)
            modulator = np.exp(phi * orders[:, np.newaxis, np.newaxis] * 1j)

            if use_sparse:
                rings = rings.reshape((rings.shape[0], 1, *rings.shape[1:]))
                ring_stack = [rings] * len(orders)
                ring_stack = sparse.concatenate(ring_stack, axis=1)
                ring_stack *= modulator
            else:
                ring_stack = rings[:, np.newaxis, ...] * modulator
            return ring_stack.reshape((-1, detector_y, detector_x))
Exemple #3
0
def test_oval_radial_background_balance():
    radius, angle = m.polar_map(centerX=10,
                                centerY=10,
                                imageSizeX=21,
                                imageSizeY=21,
                                stretchY=1.5,
                                angle=np.pi / 4)
    template = m.radial_gradient_background_subtraction(r=radius,
                                                        r0=5,
                                                        r_outer=7)
    template = m.balance(template)
    outside = radius > 7
    inside = (radius < 5) * (radius > 0)

    assert np.allclose(template.sum(), 0)
    assert np.allclose(template[outside], 0)
    assert np.all(template[inside] > 0)

    assert np.allclose(template[0, :], 0)
    assert np.allclose(template[20, :], 0)
    assert np.allclose(template[:, 0], 0)
    assert np.allclose(template[:, 20], 0)

    diag = int(np.sqrt(5))

    # Confirm stretched gradient in 45 diagonal
    assert template[10 + diag, 10 + diag] > 0
    assert template[10 - diag, 10 + diag] < template[10 + diag, 10 + diag]
Exemple #4
0
    def __init__(self,
                 radius,
                 search=None,
                 radius_outer=None,
                 delta=1,
                 radial_map=None):
        '''
        See :meth:`~libertem.masks.radial_gradient_background_subtraction` for details.

        Parameters
        ----------

        radius : float
            Radius of the circular pattern in px
        search : float, optional
            Range from the center point in px to include in the correlation.
            :code:`max(2*radius, radius_outer)` by default
            Defining the size of the square correlation pattern.
        radius_outer : float, optional
            Radius of the negative region in px. 1.5x radius by default.
        delta : float, optional
            Width of the transition region between positive and negative in px
        radial_map : numpy.ndarray, optional
            Radius value of each pixel in px. This can be used to distort the shape as needed
            or work in physical coordinates instead of pixels.
            A suitable map can be generated with :meth:`libertem.masks.polar_map`.

        Example
        -------

        >>> import matplotlib.pyplot as plt

        >>> (radius, phi) = libertem.masks.polar_map(
        ...     centerX=64, centerY=64,
        ...     imageSizeX=128, imageSizeY=128,
        ...     stretchY=2., angle=np.pi/4
        ... )

        >>> template = RadialGradientBackgroundSubtraction(
        ...     radius=30, radial_map=radius)

        >>> # This shows an elliptical template that is stretched
        >>> # along the 45 ° bottom-left top-right diagonal
        >>> plt.imshow(template.get_mask(sig_shape=(128, 128)))
        <matplotlib.image.AxesImage object at ...>
        >>> plt.show() # doctest: +SKIP
        '''
        if radius_outer is None:
            radius_outer = radius * 1.5
        if search is None:
            search = max(2 * radius, radius_outer)
        if radial_map is None:
            r = max(radius, radius_outer)
            radial_map, _ = masks.polar_map(
                centerX=r + 1,
                centerY=r + 1,
                imageSizeX=int(np.ceil(2 * r + 2)),
                imageSizeY=int(np.ceil(2 * r + 2)),
            )
        self.radius = radius
        self.radius_outer = radius_outer
        self.delta = delta
        self.radial_map = radial_map
        template = masks.radial_gradient_background_subtraction(
            r=self.radial_map,
            r0=self.radius,
            r_outer=self.radius_outer,
            delta=self.delta)
        super().__init__(template=template, search=search)