Esempio n. 1
0
    def setUp(self, shape=(7, 5, 5)):
        """Load a dataset for reconstruction."""

        self.operator = Alignment()
        self.operator.__enter__()
        self.xp = self.operator.xp

        padded_shape = shape + np.asarray((0, 41, 32))
        flow = (self.xp.random.rand(*padded_shape, 2, dtype='float32') -
                0.5) * 9

        np.random.seed(0)
        self.m = self.xp.asarray(random_complex(*shape), dtype='complex64')
        self.m_name = 'unpadded'
        self.d = self.xp.asarray(random_complex(*padded_shape),
                                 dtype='complex64')
        self.d_name = 'rotated'
        self.kwargs = {
            'flow': flow,
            'padded_shape': padded_shape,
            'unpadded_shape': shape,
            'angle': np.random.rand() * 2 * np.pi,
            'cval': 0,
        }
        print(self.operator)
Esempio n. 2
0
class TestAlignment(unittest.TestCase, OperatorTests):
    """Test the Alignment operator."""

    def setUp(self, shape=(7, 5, 5)):
        """Load a dataset for reconstruction."""

        self.operator = Alignment()
        self.operator.__enter__()
        self.xp = self.operator.xp

        padded_shape = shape + np.asarray((0, 41, 32))
        flow = (self.xp.random.rand(*padded_shape, 2, dtype='float32') -
                0.5) * 9

        np.random.seed(0)
        self.m = self.xp.asarray(random_complex(*shape), dtype='complex64')
        self.m_name = 'unpadded'
        self.d = self.xp.asarray(random_complex(*padded_shape),
                                 dtype='complex64')
        self.d_name = 'rotated'
        self.kwargs = {
            'flow': flow,
            'padded_shape': padded_shape,
            'unpadded_shape': shape,
            'angle': np.random.rand() * 2 * np.pi,
            'cval': 0,
        }
        print(self.operator)

    @unittest.skip('FIXME: This operator is not scaled.')
    def test_scaled(self):
        pass
Esempio n. 3
0
def invert(
        original,
        **kwargs
):  # yapf: disable
    """Return original shifted by shift."""
    with Alignment() as operator:
        for key, value in kwargs.items():
            if not isinstance(value, tuple) and np.ndim(value) > 0:
                kwargs[key] = operator.asarray(value)
        unaligned = operator.inv(
            operator.asarray(original, dtype='complex64'),
            **kwargs,
        )
        assert unaligned.dtype == 'complex64', unaligned.dtype
        return operator.asnumpy(unaligned)
Esempio n. 4
0
def reconstruct(
        original,
        unaligned,
        algorithm,
        num_iter=1, rtol=-1, **kwargs
):  # yapf: disable
    """Solve the alignment problem; returning either the original or the shift.

    Parameters
    ----------
    unaligned, original: (..., H, W) complex64
        The images to be aligned.
    rtol : float
        Terminate early if the relative decrease of the cost function is
        less than this amount.

    """
    if algorithm in solvers.__all__:
        with Alignment() as operator:
            for key, value in kwargs.items():
                if not isinstance(value, tuple) and np.ndim(value) > 0:
                    kwargs[key] = operator.asarray(value)
            logger.info("{} on {:,d} - {:,d} by {:,d} images for {:,d} "
                        "iterations.".format(algorithm, *unaligned.shape,
                                             num_iter))
            result = getattr(solvers, algorithm)(
                operator,
                original=operator.asarray(original, dtype='complex64'),
                unaligned=operator.asarray(unaligned, dtype='complex64'),
                num_iter=num_iter,
                **kwargs,
            )
        return {k: operator.asnumpy(v) for k, v in result.items()}
    else:
        raise ValueError(
            "The '{}' algorithm is not an available.".format(algorithm))