Exemple #1
0
    def test_inverse_transform(self):

        reg = ShiftRegistration()
        fd = reg.fit_transform(self.fd)
        fd = reg.inverse_transform(fd)

        np.testing.assert_array_almost_equal(fd.data_matrix,
                                             self.fd.data_matrix,
                                             decimal=3)
Exemple #2
0
    def test_fit_and_transform(self):
        """Test wrapper of shift_registration_deltas"""

        fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=10)

        reg = ShiftRegistration()
        response = reg.fit(self.fd)

        # Check attributes and returned value
        self.assertTrue(hasattr(reg, 'template_'))
        self.assertTrue(response is reg)

        fd_registered = reg.transform(fd)
        deltas = reg.deltas_.round(3)
        np.testing.assert_allclose(deltas, [0.071, -0.072])
Exemple #3
0
    def test_fit_transform(self):

        reg = ShiftRegistration()

        # Test fit transform with FDataGrid
        fd_reg = reg.fit_transform(self.fd)

        # Check attributes fitted
        self.assertTrue(hasattr(reg, 'deltas_'))
        self.assertTrue(hasattr(reg, 'template_'))
        self.assertTrue(hasattr(reg, 'n_iter_'))
        self.assertTrue(isinstance(fd_reg, FDataGrid))

        deltas = reg.deltas_.round(3)
        np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03])

        # Test with Basis
        fd = self.fd.to_basis(Fourier())
        reg.fit_transform(fd)
        deltas = reg.deltas_.round(3)
        np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03])
Exemple #4
0
    def test_restrict_domain(self):
        reg = ShiftRegistration(restrict_domain=True)
        fd_registered_1 = reg.fit_transform(self.fd)

        np.testing.assert_array_almost_equal(
            np.array(fd_registered_1.domain_range).round(3), [[0.022, 0.969]])

        reg2 = ShiftRegistration(restrict_domain=True, template=reg.template_)
        fd_registered_2 = reg2.fit_transform(self.fd)

        np.testing.assert_array_almost_equal(fd_registered_2.data_matrix,
                                             fd_registered_1.data_matrix,
                                             decimal=3)

        reg3 = ShiftRegistration(restrict_domain=True, template=mean)
        fd_registered_3 = reg3.fit_transform(self.fd)

        np.testing.assert_array_almost_equal(fd_registered_3.data_matrix,
                                             fd_registered_1.data_matrix)
Exemple #5
0
# is essential due to the use of derivatives in the optimization process.
# Because of their sinusoidal nature we will use a Fourier basis.

fd_basis = fd.to_basis(Fourier(n_basis=11))
fd_basis.plot()

##############################################################################
# We will use the
# :func:`~skfda.preprocessing.registration.ShiftRegistration` transformer,
# which is suitable due to the periodicity of the dataset and the small
# amount of amplitude variation.
#
# We can observe how the sinusoidal pattern is easily distinguishable
# once the alignment has been made.

shift_registration = ShiftRegistration()
fd_registered = shift_registration.fit_transform(fd_basis)

fd_registered.plot()

##############################################################################
# We will plot the mean of the original smoothed curves and the registered
# ones, and we will compare with the original sinusoidal process without
# noise.
#
# We can see how the phase variation affects to the mean of the original
# curves varying their amplitude with respect to the original process,
# however, this effect is mitigated after the registration.

# sinusoidal process without variation and noise
sine = make_sinusoidal_process(n_samples=1,
Exemple #6
0
 def setUp(self):
     """Initialization of samples"""
     self.X = make_sinusoidal_process(error_std=0, random_state=0)
     self.shift_registration = ShiftRegistration().fit(self.X)
Exemple #7
0
class TestRegistrationValidation(unittest.TestCase):
    """Test shift registration"""
    def setUp(self):
        """Initialization of samples"""
        self.X = make_sinusoidal_process(error_std=0, random_state=0)
        self.shift_registration = ShiftRegistration().fit(self.X)

    def test_amplitude_phase_score(self):
        scorer = AmplitudePhaseDecomposition()
        score = scorer(self.shift_registration, self.X)
        np.testing.assert_allclose(score, 0.972095, rtol=1e-6)

    def test_amplitude_phase_score_with_output_points(self):
        eval_points = self.X.grid_points[0]
        scorer = AmplitudePhaseDecomposition(eval_points=eval_points)
        score = scorer(self.shift_registration, self.X)
        np.testing.assert_allclose(score, 0.972095, rtol=1e-6)

    def test_amplitude_phase_score_with_basis(self):
        scorer = AmplitudePhaseDecomposition()
        X = self.X.to_basis(Fourier())
        score = scorer(self.shift_registration, X)
        np.testing.assert_allclose(score, 0.995087, rtol=1e-6)

    def test_default_score(self):

        score = self.shift_registration.score(self.X)
        np.testing.assert_allclose(score, 0.972095, rtol=1e-6)

    def test_least_squares_score(self):
        scorer = LeastSquares()
        score = scorer(self.shift_registration, self.X)
        np.testing.assert_allclose(score, 0.795933, rtol=1e-6)

    def test_sobolev_least_squares_score(self):
        scorer = SobolevLeastSquares()
        score = scorer(self.shift_registration, self.X)
        np.testing.assert_allclose(score, 0.76124, rtol=1e-6)

    def test_pairwise_correlation(self):
        scorer = PairwiseCorrelation()
        score = scorer(self.shift_registration, self.X)
        np.testing.assert_allclose(score, 1.816228, rtol=1e-6)

    def test_mse_decomposition(self):

        fd = make_multimodal_samples(n_samples=3, random_state=1)
        landmarks = make_multimodal_landmarks(n_samples=3, random_state=1)
        landmarks = landmarks.squeeze()
        warping = landmark_registration_warping(fd, landmarks)
        fd_registered = fd.compose(warping)
        scorer = AmplitudePhaseDecomposition(return_stats=True)
        ret = scorer.score_function(fd, fd_registered, warping=warping)
        np.testing.assert_allclose(ret.mse_amp, 0.0009866997121476962)
        np.testing.assert_allclose(ret.mse_pha, 0.11576935495450151)
        np.testing.assert_allclose(ret.r_squared, 0.9915489952877273)
        np.testing.assert_allclose(ret.c_r, 0.999999, rtol=1e-6)

    def test_raises_amplitude_phase(self):
        scorer = AmplitudePhaseDecomposition()

        # Inconsistent number of functions registered
        with np.testing.assert_raises(ValueError):
            scorer.score_function(self.X, self.X[:2])

        # Inconsistent number of functions registered
        with np.testing.assert_raises(ValueError):
            scorer.score_function(self.X, self.X, warping=self.X[:2])
Exemple #8
0
 def test_custom_output_points(self):
     reg = ShiftRegistration(output_points=np.linspace(0, 1, 50))
     reg.fit_transform(self.fd)
Exemple #9
0
    def test_initial_estimation(self):
        reg = ShiftRegistration(initial=[-0.02161235, 0.03032652])
        reg.fit_transform(self.fd)

        # Only needed 1 iteration until convergence
        self.assertEqual(reg.n_iter_, 1)
Exemple #10
0
    def test_template(self):

        reg = ShiftRegistration()
        fd_registered_1 = reg.fit_transform(self.fd)

        reg_2 = ShiftRegistration(template=reg.template_)
        fd_registered_2 = reg_2.fit_transform(self.fd)

        reg_3 = ShiftRegistration(template=mean)
        fd_registered_3 = reg_3.fit_transform(self.fd)

        reg_4 = ShiftRegistration(template=reg.template_)
        fd_registered_4 = reg_4.fit(self.fd).transform(self.fd)

        np.testing.assert_array_almost_equal(fd_registered_1.data_matrix,
                                             fd_registered_3.data_matrix)

        # With the template fixed could vary the convergence
        np.testing.assert_array_almost_equal(fd_registered_1.data_matrix,
                                             fd_registered_2.data_matrix,
                                             decimal=3)

        np.testing.assert_array_almost_equal(fd_registered_2.data_matrix,
                                             fd_registered_4.data_matrix)
Exemple #11
0
    def test_raises(self):

        reg = ShiftRegistration()

        # Test not fitted
        with np.testing.assert_raises(NotFittedError):
            reg.transform(self.fd)

        reg.fit(self.fd)
        reg.set_params(restrict_domain=True)

        # Test use fit or transform with restrict_domain=True
        with np.testing.assert_raises(AttributeError):
            reg.transform(self.fd)

        with np.testing.assert_raises(AttributeError):
            reg.fit(self.fd)

        # Test inverse_transform without previous transformation
        with np.testing.assert_raises(AttributeError):
            reg.inverse_transform(self.fd)

        reg.fit_transform(self.fd)

        # Test inverse transform with different number of sample
        with np.testing.assert_raises(ValueError):
            reg.inverse_transform(self.fd[:1])

        fd = make_multimodal_samples(dim_domain=2, random_state=0)

        with np.testing.assert_raises(ValueError):
            reg.fit_transform(fd)

        reg.set_params(initial=[0.])

        # Wrong initial estimation
        with np.testing.assert_raises(ValueError):
            reg.fit_transform(self.fd)