Пример #1
0
def test_ialign_misaligned_canned_images():
    """shift images from skimage.data by entire pixels.
    We don't expect perfect alignment."""
    reference = camera()
    shifts = [(randint(-4, 0), randint(0, 4)) for _ in range(5)]

    mask = np.zeros_like(reference, dtype=bool)
    rr1, cc1 = ellipse(129, 127, r_radius=63, c_radius=50, shape=reference.shape)
    mask[rr1, cc1] = True

    misaligned = (ndi.shift(camera(), shift=s) for s in shifts)

    for aligned, (sx, sy) in zip(
        ialign(misaligned, reference=reference, mask=mask), shifts
    ):
        assert np.allclose(reference[sx::, 0:-sy], aligned[sx::, 0:-sy])
Пример #2
0
    def test_misaligned_canned_images(self):
        """ shift images from skimage.data by entire pixels.
	   We don't expect perfect alignment."""
        original = TEST_IMAGE
        misaligned = [
            ndi.shift(original, (randint(-4, 4), randint(-4, 4)))
            for _ in range(5)
        ]

        aligned = ialign(misaligned, reference=original)

        # TODO: find a better figure-of-merit for alignment
        for im in aligned:
            # edge will be filled with zeros, we ignore
            diff = np.abs(original[5:-5, 5:-5] - im[5:-5, 5:-5])

            # Want less than 1% difference
            percent_diff = np.sum(diff) / (diff.size *
                                           (original.max() - original.min()))
            self.assertLess(percent_diff, 1)
Пример #3
0
def _raw_combine(timedelay, raw, exclude_scans, normalize, align, valid_mask,
                 dtype):

    images = raw.itertime(timedelay, exclude_scans=exclude_scans)

    if align:
        # Note : the fast = False fixes issue #11, where single crystal images were not successfully aligned.
        images = ialign(images, mask=valid_mask)

    # Set up normalization
    if normalize:
        images, images2 = itercopy(images, copies=2)

        # Compute the total intensity of first image
        # This will be the reference point
        first2, images2 = peek(images2)
        initial_weight = np.sum(first2[valid_mask])
        weights = (initial_weight / np.sum(image[valid_mask])
                   for image in images2)
    else:
        weights = None

    return average(images, weights=weights).astype(dtype)
Пример #4
0
    def test_trivial(self):
        """ Test alignment of identical images """
        aligned = tuple(ialign([TEST_IMAGE for _ in range(5)]))

        self.assertEqual(len(aligned), 5)
        self.assertSequenceEqual(TEST_IMAGE.shape, aligned[0].shape)
Пример #5
0
def test_ialign_trivial():
    """ Test alignment of identical images """
    aligned = tuple(ialign([camera() for _ in range(5)]))

    assert len(aligned) == 5
    assert camera().shape == aligned[0].shape
Пример #6
0
    def test_trivial(self):
        """ Test alignment of identical images """
        aligned = tuple(ialign([data.camera() for _ in range(5)]))

        self.assertEqual(len(aligned), 5)
        self.assertSequenceEqual(data.camera().shape, aligned[0].shape)
Пример #7
0
    def reduced(
        self,
        exclude_scans=None,
        align=True,
        normalize=True,
        mask=None,
        processes=1,
        dtype=np.float,
    ):
        """
        Generator of reduced dataset. The reduced diffraction patterns are generated in order of time-delay.

        This particular implementation normalizes diffracted intensity of pictures acquired at the same time-delay
        while rejecting masked pixels.

        Parameters
        ----------
        exclude_scans : iterable or None, optional
            These scans will be skipped when reducing the dataset.
        align : bool, optional
            If True (default), raw diffraction patterns will be aligned using the masked normalized
            cross-correlation approach. See `skued.align` for more information.
        normalize : bool, optional
            If True (default), equivalent diffraction pictures (e.g. same time-delay, different scans) 
            are normalized to the same diffracted intensity.
        mask : array-like of bool or None, optional
            If not None, pixels where ``mask = True`` are ignored for certain operations (e.g. alignment).
        processes : int or None, optional
            Number of Processes to spawn for processing. 
        dtype : numpy.dtype or None, optional
            Reduced patterns will be cast to ``dtype``.

        Yields
        ------
        pattern : `~numpy.ndarray`, ndim 2
        """
        # Convention for masks is different for scikit-ued
        # For backwards compatibility, we cannot change the definition
        # in iris-ued
        valid_mask = np.logical_not(mask)

        kwargs = {
            "raw": self,
            "exclude_scans": exclude_scans,
            "align": align,
            "normalize": normalize,
            "valid_mask": valid_mask,
            "dtype": dtype,
        }

        combined = pmap(
            _raw_combine,
            self.time_points,
            kwargs=kwargs,
            processes=processes,
            ntotal=len(self.time_points),
        )

        # Each image at the same time-delay are aligned to each other. This means that
        # the reference image is different for each time-delay. We align the reduced images
        # to each other as well.
        if align:
            yield from ialign(combined, mask=valid_mask)
        else:
            yield from combined