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]
def get_mask(self, sig_shape): # Recalculate in case someone has changed parameters self.template = masks.radial_gradient_background_subtraction( r=self.radial_map, r0=self.radius, r_outer=self.radius_outer, delta=self.delta) return super().get_mask(sig_shape)
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)