Beispiel #1
0
    def test_one_to_one_alignment(self):
        """Test alignment to 1 sample to a template"""
        reg = ElasticRegistration(template=self.template)
        register = reg.fit_transform(self.unimodal_samples[0])
        distances = metric(self.template, register)

        np.testing.assert_allclose(distances, 0, atol=12e-3)
Beispiel #2
0
    def test_set_alignment(self):
        """Test alignment 3 curves to set with 3 templates"""
        # Should give same result than test_template_alignment
        reg = ElasticRegistration(template=self.template_rep)
        register = reg.fit_transform(self.unimodal_samples)
        distances = metric(self.template, register)

        np.testing.assert_allclose(distances, 0, atol=12e-3)
Beispiel #3
0
    def test_simmetry_of_aligment(self):
        """Check registration using inverse composition"""
        reg = ElasticRegistration(template=self.template)
        reg.fit_transform(self.unimodal_samples)
        warping = reg.warping_
        inverse = invert_warping(warping)
        register = self.template_rep.compose(inverse)
        distances = np.diag(metric(self.unimodal_samples, register))

        np.testing.assert_allclose(distances, 0, atol=12e-3)
Beispiel #4
0
    def test_callable_alignment(self):
        """Test alignment by default"""
        # Should give same result than test_template_alignment
        reg = ElasticRegistration(template=elastic_mean)
        register = reg.fit_transform(self.unimodal_samples)

        values = register([-.25, -.1, 0, .1, .25])
        expected = [[[0.599058],  [0.997427],  [0.772248],
                     [0.412342],  [0.064725]],
                    [[0.626875],  [0.997155],  [0.791649],
                     [0.382181],  [0.050098]],
                    [[0.620992],  [0.997369],  [0.785886],
                     [0.376556],  [0.048804]]]

        np.testing.assert_allclose(values, expected, atol=1e-4)
Beispiel #5
0
                             random_state=1, start=0, mode_std=.01)

fig = fd.plot()
fig.axes[0].legend(['$f$', '$g$'])


##############################################################################
# In this example :math:`g` will be used as template and :math:`f` will be
# aligned to it. In the following figure it is shown the result of the
# registration process, wich can be computed using
# :class:`~skfda.preprocessing.registration.ElasticRegistration`.
#

f, g = fd[0], fd[1]

elastic_registration = ElasticRegistration(template=g)


# Aligns f to g
f_align = elastic_registration.fit_transform(f)

fig = fd.plot()
f_align.plot(fig=fig, color='C0', linestyle='--')


# Legend
fig.axes[0].legend(['$f$', '$g$', '$f \\circ \\gamma $'])


##############################################################################
# The non-linear transformation :math:`\gamma` applied to :math:`f` in
Beispiel #6
0
# under the :math:`\mathbb{L}^2` distance.
#
# It can be seen how the elastic mean better captures the geometry of the
# curves compared to the standard mean, since it is not affected by the
# deformations of the curves.


fig = fd.mean().plot(label="L2 mean")
elastic_mean(fd).plot(fig=fig, label="Elastic mean")
fig.legend()

##############################################################################
# In this case, the alignment completely reduces the amplitude variability
# between the samples, aligning the maximum points correctly.

elastic_registration = ElasticRegistration()

fd_align = elastic_registration.fit_transform(fd)

fd_align.plot()


##############################################################################
# In general these type of alignments are not possible, in the following
# figure it is shown how it works with a real dataset.
# The :func:`berkeley growth dataset<skfda.datasets.fetch_growth>`
# contains the growth curves of a set children, in this case will be used only
# the males. The growth curves will be resampled using cubic interpolation and
# derived to obtain the velocity curves.
#
# First we show the original curves:
Beispiel #7
0
 def test_score(self):
     """Test score method of the transformer"""
     reg = ElasticRegistration()
     reg.fit(self.unimodal_samples)
     score = reg.score(self.unimodal_samples)
     np.testing.assert_almost_equal(score,  0.9994225)
Beispiel #8
0
    def test_raises(self):
        reg = ElasticRegistration()

        # X not in fit, but template is not an FDataGrid
        with np.testing.assert_raises(ValueError):
            reg.fit()

        # Inverse transform without previous transform
        with np.testing.assert_raises(ValueError):
            reg.inverse_transform(self.unimodal_samples)

        # Inverse transform with different number of samples than transform
        reg.fit_transform(self.unimodal_samples)
        with np.testing.assert_raises(ValueError):
            reg.inverse_transform(self.unimodal_samples[0])

        # FDataGrid as template with n != 1 and n!= n_samples to transform
        reg = ElasticRegistration(template=self.unimodal_samples).fit()
        with np.testing.assert_raises(ValueError):
            reg.transform(self.unimodal_samples[0])