Exemplo n.º 1
0
    def test_evaluation_grid_linear_keepdims(self):
        """Test grid evaluation with keepdims"""

        # Default keepdims = False
        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      keepdims=False)

        # Default keepdims = True
        fk = FDataGrid(self.data_matrix_1_1,
                       sample_points=np.arange(10),
                       keepdims=True)

        t = [0.5, 1.5, 2.5]
        res = np.array([[0.5, 2.5, 6.5], [72.5, 56.5, 42.5]])
        res_keepdims = res.reshape(2, 3, 1)

        np.testing.assert_array_almost_equal(f(t, grid=True), res)
        np.testing.assert_array_almost_equal(
            f((t, ), grid=True, keepdims=True), res_keepdims)
        np.testing.assert_array_almost_equal(f([t], grid=True, keepdims=False),
                                             res)

        np.testing.assert_array_almost_equal(fk(t, grid=True), res_keepdims)
        np.testing.assert_array_almost_equal(
            fk((t, ), grid=True, keepdims=True), res_keepdims)
        np.testing.assert_array_almost_equal(
            fk([t], grid=True, keepdims=False), res)
Exemplo n.º 2
0
    def test_basis_fpca_transform_result(self):

        n_basis = 9
        n_components = 3

        fd_data = fetch_weather()['data'].coordinates[0]
        fd_data = FDataGrid(np.squeeze(fd_data.data_matrix),
                            np.arange(0.5, 365, 1))

        # initialize basis data
        basis = Fourier(n_basis=n_basis, domain_range=(0, 365))
        fd_basis = fd_data.to_basis(basis)

        fpca = FPCA(n_components=n_components,
                    regularization=TikhonovRegularization(
                        LinearDifferentialOperator(2),
                        regularization_parameter=1e5))
        fpca.fit(fd_basis)
        scores = fpca.transform(fd_basis)

        # results obtained using Ramsay's R package
        results = [[-7.68307641e+01, 5.69034443e+01, -1.22440149e+01],
                   [-9.02873996e+01, 1.46262257e+01, -1.78574536e+01],
                   [-8.21155683e+01, 3.19159491e+01, -2.56212328e+01],
                   [-1.14163637e+02, 3.66425562e+01, -1.00810836e+01],
                   [-6.97263223e+01, 1.22817168e+01, -2.39417618e+01],
                   [-6.41886364e+01, -1.07261045e+01, -1.10587407e+01],
                   [1.35824412e+02, 2.03484658e+01, -9.04815324e+00],
                   [-1.46816399e+01, -2.66867491e+01, -1.20233465e+01],
                   [1.02507511e+00, -2.29840736e+01, -9.06081296e+00],
                   [-3.62936903e+01, -2.09520442e+01, -1.14799951e+01],
                   [-4.20649313e+01, -1.13618094e+01, -6.24909009e+00],
                   [-7.38115985e+01, -3.18423866e+01, -1.50298626e+01],
                   [-6.69822456e+01, -3.35518632e+01, -1.25167352e+01],
                   [-1.03534763e+02, -1.29513941e+01, -1.49103879e+01],
                   [-1.04542036e+02, -1.36794907e+01, -1.41555965e+01],
                   [-7.35863347e+00, -1.41171956e+01, -2.97562788e+00],
                   [7.28804530e+00, -5.34421830e+01, -3.39823418e+00],
                   [5.59974094e+01, -4.02154080e+01, 3.78800103e-01],
                   [1.80778702e+02, 1.87798201e+01, -1.99043247e+01],
                   [-3.69700617e+00, -4.19441020e+01, 6.45820740e+00],
                   [3.76527216e+01, -4.23056953e+01, 1.04221757e+01],
                   [1.23850646e+02, -4.24648130e+01, -2.22336786e-01],
                   [-7.23588457e+00, -1.20579536e+01, 2.07502089e+01],
                   [-4.96871011e+01, 8.88483448e+00, 2.02882768e+01],
                   [-1.36726355e+02, -1.86472599e+01, 1.89076217e+01],
                   [-1.83878661e+02, 4.12118550e+01, 1.78960356e+01],
                   [-1.81568820e+02, 5.20817910e+01, 2.01078870e+01],
                   [-5.08775852e+01, 1.34600555e+01, 3.18602712e+01],
                   [-1.37633866e+02, 7.50809631e+01, 2.42320782e+01],
                   [4.98276375e+01, 1.33401270e+00, 3.50611066e+01],
                   [1.51149934e+02, -5.47417776e+01, 3.97592325e+01],
                   [1.58366096e+02, -3.80762686e+01, -5.62415023e+00],
                   [2.17139548e+02, 6.34055987e+01, -1.98853635e+01],
                   [2.33615480e+02, -7.90787574e-02, 2.69069525e+00],
                   [3.45371437e+02, 9.58703622e+01, 8.47570770e+00]]
        results = np.array(results)

        # compare results
        np.testing.assert_allclose(scores, results, atol=1e-7)
Exemplo n.º 3
0
    def test_evaluation_composed_linear_keepdims(self):
        """Test parameter keepdims with composed evaluation"""

        # Default keepdims = False
        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      keepdims=False)

        # Default keepdims = True
        fk = FDataGrid(self.data_matrix_1_1,
                       sample_points=np.arange(10),
                       keepdims=True)

        t = np.array([1, 2, 3])
        t = [t, 9 - t]
        res = np.array([[1., 4., 9.], [1., 4., 9.]])
        res_keepdims = res.reshape((2, 3, 1))

        # Test combinations of keepdims with list
        np.testing.assert_array_almost_equal(f(t, aligned_evaluation=False),
                                             res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=False), res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=True), res_keepdims)

        np.testing.assert_array_almost_equal(fk(t, aligned_evaluation=False),
                                             res_keepdims)
        np.testing.assert_array_almost_equal(
            fk(t, aligned_evaluation=False, keepdims=False), res)
        np.testing.assert_array_almost_equal(
            fk(t, aligned_evaluation=False, keepdims=True), res_keepdims)
Exemplo n.º 4
0
    def setUp(self):
        """Initialization of samples"""

        self.time = np.linspace(-1, 1, 50)
        interpolation = SplineInterpolation(3, monotone=True)
        self.polynomial = FDataGrid([self.time**3, self.time**5],
                                    self.time,
                                    interpolation=interpolation)
Exemplo n.º 5
0
    def test_monary_ufunc(self):
        data_matrix = np.arange(15).reshape(3, 5)

        fd = FDataGrid(data_matrix)

        fd_sqrt = np.sqrt(fd)

        fd_sqrt_build = FDataGrid(np.sqrt(data_matrix))

        self.assertEqual(fd_sqrt, fd_sqrt_build)
Exemplo n.º 6
0
 def setUp(self):
     grid_points = [1, 2, 3, 4, 5]
     self.fd = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                         grid_points=grid_points)
     basis = Monomial(n_basis=3, domain_range=(1, 5))
     self.fd_basis = FDataBasis(basis, [[1, 1, 0], [0, 0, 1]])
     self.fd_curve = self.fd.concatenate(self.fd, as_coordinates=True)
     self.fd_surface = make_multimodal_samples(n_samples=3,
                                               dim_domain=2,
                                               random_state=0)
Exemplo n.º 7
0
    def test_out_ufunc(self):
        data_matrix = np.arange(15.).reshape(3, 5)
        data_matrix_copy = np.copy(data_matrix)

        fd = FDataGrid(data_matrix)

        np.sqrt(fd, out=fd)

        fd_sqrt_build = FDataGrid(np.sqrt(data_matrix_copy))

        self.assertEqual(fd, fd_sqrt_build)
Exemplo n.º 8
0
    def test_warping_distance(self):
        """Test of warping distance"""
        t = np.linspace(0, 1)
        w1 = FDataGrid([t**5], t)
        w2 = FDataGrid([t**3], t)

        d = warping_distance(w1, w2)
        np.testing.assert_allclose(d, np.arccos(np.sqrt(15) / 4), atol=1e-3)

        d = warping_distance(w2, w2)
        np.testing.assert_allclose(d, 0, atol=2e-2)
Exemplo n.º 9
0
def _get_group_fd(X, group_mask, basis, grid_points=None):
    _check_skfda()
    group_X = np.copy(X[:, group_mask])
    if grid_points is None:
        group_grid_points = np.arange(0, group_X.shape[1])
    else:
        group_grid_points = np.copy(grid_points[group_mask])
    fd = FDataGrid(group_X, group_grid_points)
    if basis is not None:
        fd = fd.to_basis(basis)
    return fd
Exemplo n.º 10
0
    def test_binary_ufunc(self):
        data_matrix = np.arange(15).reshape(3, 5)
        data_matrix2 = 2 * np.arange(15).reshape(3, 5)

        fd = FDataGrid(data_matrix)
        fd2 = FDataGrid(data_matrix2)

        fd_mul = np.multiply(fd, fd2)

        fd_mul_build = FDataGrid(data_matrix * data_matrix2)

        self.assertEqual(fd_mul, fd_mul_build)
Exemplo n.º 11
0
    def test_error_degree(self):

        with np.testing.assert_raises(ValueError):
            interpolation = SplineInterpolation(7)
            f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10),
                          interpolation=interpolation)
            f(1)

        with np.testing.assert_raises(ValueError):
            interpolation = SplineInterpolation(0)
            f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10),
                          interpolation=interpolation)
            f(1)
Exemplo n.º 12
0
    def test_discretized_fpca_fit_attributes(self):
        fpca = FPCA()
        with self.assertRaises(AttributeError):
            fpca.fit(None)

        # check that if n_components is bigger than the number of samples then
        # an exception should be thrown
        fd = FDataGrid([[0.5], [0.1]], grid_points=[0])
        with self.assertRaises(AttributeError):
            fpca.fit(fd)

        # check that n_components must be smaller than the number of attributes
        # in the FDataGrid object
        fd = FDataGrid([[0.9], [0.7], [0.5]], grid_points=[0])
        with self.assertRaises(AttributeError):
            fpca.fit(fd)
Exemplo n.º 13
0
    def test_fisher_rao_invariance(self):
        """Test invariance of fisher rao metric: d(f,g)= d(foh, goh)"""

        t = np.linspace(0, np.pi)
        id = FDataGrid([t], t)
        cos = np.cos(id)
        sin = np.sin(id)
        gamma = normalize_warping(np.sqrt(id), (0, np.pi))
        gamma2 = normalize_warping(np.square(id), (0, np.pi))

        distance_original = fisher_rao_distance(cos, sin)

        # Construction of 2 warpings
        distance_warping = fisher_rao_distance(cos.compose(gamma),
                                               sin.compose(gamma))
        distance_warping2 = fisher_rao_distance(cos.compose(gamma2),
                                                sin.compose(gamma2))

        # The error ~0.001 due to the derivation
        np.testing.assert_almost_equal(distance_original,
                                       distance_warping,
                                       decimal=2)

        np.testing.assert_almost_equal(distance_original,
                                       distance_warping2,
                                       decimal=2)
Exemplo n.º 14
0
    def test_add(self):
        fd1 = FDataGrid([[1, 2, 3, 4], [2, 3, 4, 5]])

        fd2 = fd1 + fd1
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[2, 4, 6, 8], [4, 6, 8, 10]])

        fd2 = fd1 + 2
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[3, 4, 5, 6], [4, 5, 6, 7]])

        fd2 = fd1 + np.array(2)
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[3, 4, 5, 6], [4, 5, 6, 7]])

        fd2 = fd1 + np.array([2])
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[3, 4, 5, 6], [4, 5, 6, 7]])

        fd2 = fd1 + np.array([1, 2, 3, 4])
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[2, 4, 6, 8], [3, 5, 7, 9]])

        fd2 = fd1 + fd1.data_matrix
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[2, 4, 6, 8], [4, 6, 8, 10]])

        fd2 = fd1 + fd1.data_matrix[..., 0]
        np.testing.assert_array_equal(fd2.data_matrix[..., 0],
                                      [[2, 4, 6, 8], [4, 6, 8, 10]])
def load_datagrid_sim(folder, arr, x):
    '''
    Load cif files from folder into ase crystal objects, simulate crystal
    diffraction and represent data in timeseries
    Args:
        folder (string): Where we load the data from.
        arr (list): List of cif filenames in folder.
        x (nparray): representation of 1/angström for powder simulation.

    Returns:
        fd (datagrid): representation of intensity values and 1/angström
        values in datagrid object.

    '''
    filename_arr = []
    crys_arr = []
    for file in arr:
        filename = os.sep.join([folder, file])
        filename_arr.append(file)
        crys = read(filename)
        crys = Crystal.from_ase(crys)

        diff = powdersim(crys, x, fwhm_l=50)
        diff_norm = diff / diff.max()
        crys_arr.append(diff_norm)

    crys_arr = np.array(crys_arr)
    fd = FDataGrid(crys_arr,
                   x,
                   dataset_name='Diffraction Curves',
                   argument_names=[r'$q (1/\AA)$'],
                   coordinate_names=['Diffracted intensity (A.u.)'])
    print(fd)

    return fd
Exemplo n.º 16
0
 def test_fdboxplot_multivariate(self):
     data_matrix = [[[1, 0.3], [2, 0.4], [3, 0.5], [4, 0.6]],
                    [[2, 0.5], [3, 0.6], [4, 0.7], [5, 0.7]],
                    [[3, 0.2], [4, 0.3], [5, 0.4], [6, 0.5]]]
     sample_points = [2, 4, 6, 8]
     fd = FDataGrid(data_matrix, sample_points)
     fdataBoxplot = Boxplot(fd, prob=[0.75, 0.5, 0.25])
     np.testing.assert_array_equal(fdataBoxplot.median,
                                   np.array([[2., 3., 4., 5.],
                                             [0.3, 0.4, 0.5, 0.6]]))
     np.testing.assert_array_equal(fdataBoxplot.central_envelope, np.array(
         [[[2., 3., 4., 5.], [1., 2., 3., 4.]],
          [[0.5, 0.6, 0.7, 0.7],
           [0.3, 0.4, 0.5, 0.6]]]))
     np.testing.assert_array_equal(fdataBoxplot.outlying_envelope, np.array(
         [[[3., 4., 5., 6.], [1., 2., 3., 4.]],
          [[0.5, 0.6, 0.7, 0.7],
           [0.2, 0.3, 0.4, 0.5]]]))
     np.testing.assert_array_equal(fdataBoxplot.central_regions, np.array(
         [[[3., 4., 5., 6.], [1., 2., 3., 4.]],
          [[2., 3., 4., 5.], [1., 2., 3., 4.]],
          [[2., 3., 4., 5.], [2., 3., 4., 5.]],
          [[0.5, 0.6, 0.7, 0.7],
           [0.2, 0.3, 0.4, 0.5]],
          [[0.5, 0.6, 0.7, 0.7],
           [0.3, 0.4, 0.5, 0.6]],
          [[0.3, 0.4, 0.5, 0.6],
           [0.3, 0.4, 0.5, 0.6]]]))
     np.testing.assert_array_equal(fdataBoxplot.outliers,
                                   np.array([[0., 0., 0.],
                                             [0., 0., 0.]]))
Exemplo n.º 17
0
 def test_directional_outlyingness(self):
     data_matrix = [[[1, 0.3], [2, 0.4], [3, 0.5], [4, 0.6]],
                    [[2, 0.5], [3, 0.6], [4, 0.7], [5, 0.7]],
                    [[3, 0.2], [4, 0.3], [5, 0.4], [6, 0.5]]]
     sample_points = [2, 4, 6, 8]
     fd = FDataGrid(data_matrix, sample_points)
     dir_outlyingness, mean_dir_outl, variation_dir_outl = directional_outlyingness(
         fd)
     np.testing.assert_allclose(dir_outlyingness,
                                np.array([[[0., 0.], [0., 0.], [0., 0.],
                                           [0., 0.]],
                                          [[0.19611614, 0.03922323],
                                           [0.19611614, 0.03922323],
                                           [0.19611614, 0.03922323],
                                           [0.19900744, 0.01990074]],
                                          [[0.49937617, -0.02496881],
                                           [0.49937617, -0.02496881],
                                           [0.49937617, -0.02496881],
                                           [0.49937617, -0.02496881]]]),
                                rtol=1e-06)
     np.testing.assert_allclose(mean_dir_outl,
                                np.array([[0., 0.],
                                          [0.29477656, 0.05480932],
                                          [0.74906425, -0.03745321]]),
                                rtol=1e-06)
     np.testing.assert_allclose(variation_dir_outl,
                                np.array([0., 0.01505136, 0.09375]))
Exemplo n.º 18
0
 def test_magnitude_shape_plot(self):
     fd = fetch_weather()["data"]
     fd_temperatures = FDataGrid(data_matrix=fd.data_matrix[:, :, 0],
                                 sample_points=fd.sample_points,
                                 dataset_label=fd.dataset_label,
                                 axes_labels=fd.axes_labels[0:2])
     msplot = MagnitudeShapePlot(fd_temperatures, random_state=0)
     np.testing.assert_allclose(
         msplot.points,
         np.array([[0.28216472, 3.15069249], [1.43406267, 0.77729052],
                   [0.96089808, 2.7302293], [2.1469911, 7.06601804],
                   [0.89081951, 0.71098079], [1.22591999, 0.2363983],
                   [-2.65530111, 0.9666511], [0.47819535, 0.83989187],
                   [-0.11256072, 0.89035836], [0.99627103, 0.3255725],
                   [0.77889317, 0.32451932], [3.47490723, 12.5630275],
                   [3.14828582, 13.80605804], [3.51793514, 10.46943904],
                   [3.94435195, 15.24142224], [0., 0.],
                   [0.74574282, 6.68207165], [-0.82501844, 0.82694929],
                   [-3.4617439, 1.10389229], [0.44523944, 1.61262494],
                   [-0.52255157, 1.00486028], [-1.67260144, 0.74626351],
                   [-0.10133788, 0.96326946], [0.36576472, 0.93071675],
                   [7.57827303, 40.70985885], [7.51140842, 36.65641988],
                   [7.13000185, 45.56574331], [0.28166597, 1.70861091],
                   [1.55486533, 8.75149947], [-1.43363018, 0.36935927],
                   [-2.79138743, 4.80007762], [-2.39987853, 1.54992208],
                   [-5.87118328, 5.34300766], [-5.42854833, 5.1694065],
                   [-16.34459211, 0.9397118]]))
     np.testing.assert_array_almost_equal(
         msplot.outliers,
         np.array([
             0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,
             0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1
         ]))
Exemplo n.º 19
0
    def fit(self, Xtrain, ytrain):
        grid_fd = FDataGrid(Xtrain, self.sample_points)
        fpca_discretized = FPCA(n_components=self.Ncomps)
        theta = fpca_discretized.fit_transform(grid_fd)

        fpca = fpca_discretized.components_.data_matrix.squeeze()
        fpca_gxl = fpca_discretized.explained_variance_ratio_

        theta_m = theta - theta.mean(0)
        T0 = np.eye(theta.shape[1])
        fpca_lamda = self.COV_shrinkage_SCM(theta_m.T, T0)

        self.fpca = fpca
        self.fpca_lamda = fpca_lamda

        n, p = Xtrain.shape
        diff = self.sample_points[1] - self.sample_points[0]

        y = (Xtrain.reshape(n, 1, p) -
             np.tile(Xtrain,
                     (n, 1)).reshape(n, n, p)).reshape(n, n, 1, p) * (np.tile(
                         fpca, (n, 1)).reshape(n, self.Ncomps, p))
        Xtrain_p = (((diff * y[:, :, :, 1:]).sum(3))**2 /
                    fpca_lamda.squeeze()).sum(2)

        self.model.fit(Xtrain_p, ytrain)
        return self
Exemplo n.º 20
0
    def test_lp_error_grid_points(self):
        grid_points = [1, 2, 4, 4.3, 5]
        fd2 = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                        grid_points=grid_points)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, fd2)
Exemplo n.º 21
0
 def test_fdboxplot_univariate(self):
     data_matrix = [[1, 1, 2, 3, 2.5, 2], [0.5, 0.5, 1, 2, 1.5, 1],
                    [-1, -1, -0.5, 1, 1, 0.5],
                    [-0.5, -0.5, -0.5, -1, -1, -1]]
     grid_points = [0, 2, 4, 6, 8, 10]
     fd = FDataGrid(data_matrix, grid_points)
     fdataBoxplot = Boxplot(fd, depth_method=IntegratedDepth())
     np.testing.assert_array_equal(fdataBoxplot.median.ravel(),
                                   np.array([-1., -1., -0.5, 1., 1., 0.5]))
     np.testing.assert_array_equal(
         fdataBoxplot.central_envelope[0].ravel(),
         np.array([-1., -1., -0.5, -1., -1., -1.]))
     np.testing.assert_array_equal(
         fdataBoxplot.central_envelope[1].ravel(),
         np.array([-0.5, -0.5, -0.5, 1., 1., 0.5]))
     np.testing.assert_array_equal(
         fdataBoxplot.non_outlying_envelope[0].ravel(),
         np.array([-1., -1., -0.5, -1., -1., -1.]))
     np.testing.assert_array_equal(
         fdataBoxplot.non_outlying_envelope[1].ravel(),
         np.array([-0.5, -0.5, -0.5, 1., 1., 0.5]))
     self.assertEqual(len(fdataBoxplot.envelopes), 1)
     np.testing.assert_array_equal(fdataBoxplot.envelopes[0],
                                   fdataBoxplot.central_envelope)
     np.testing.assert_array_equal(fdataBoxplot.outliers,
                                   np.array([True, True, False, False]))
Exemplo n.º 22
0
class TestLpMetrics(unittest.TestCase):
    def setUp(self):
        grid_points = [1, 2, 3, 4, 5]
        self.fd = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                            grid_points=grid_points)
        basis = Monomial(n_basis=3, domain_range=(1, 5))
        self.fd_basis = FDataBasis(basis, [[1, 1, 0], [0, 0, 1]])
        self.fd_curve = self.fd.concatenate(self.fd, as_coordinates=True)
        self.fd_surface = make_multimodal_samples(n_samples=3,
                                                  dim_domain=2,
                                                  random_state=0)

    def test_lp_norm(self):

        np.testing.assert_allclose(lp_norm(self.fd, p=1), [16., 41.33333333])
        np.testing.assert_allclose(lp_norm(self.fd, p='inf'), [6, 25])

    def test_lp_norm_curve(self):

        np.testing.assert_allclose(lp_norm(self.fd_curve, p=1, p2=1),
                                   [32., 82.666667])
        np.testing.assert_allclose(lp_norm(self.fd_curve, p='inf', p2='inf'),
                                   [6, 25])

    def test_lp_norm_surface_inf(self):
        np.testing.assert_allclose(
            lp_norm(self.fd_surface, p='inf').round(5),
            [0.99994, 0.99793, 0.99868])

    def test_lp_norm_surface(self):
        # Integration of surfaces not implemented, add test case after
        # implementation
        self.assertEqual(lp_norm(self.fd_surface, p=1), NotImplemented)

    def test_lp_error_dimensions(self):
        # Case internal arrays
        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, self.fd_surface)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, self.fd_curve)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd_surface, self.fd_curve)

    def test_lp_error_domain_ranges(self):
        grid_points = [2, 3, 4, 5, 6]
        fd2 = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                        grid_points=grid_points)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, fd2)

    def test_lp_error_grid_points(self):
        grid_points = [1, 2, 4, 4.3, 5]
        fd2 = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                        grid_points=grid_points)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, fd2)
Exemplo n.º 23
0
 def test_magnitude_shape_plot(self):
     fd = fetch_weather()["data"]
     fd_temperatures = FDataGrid(data_matrix=fd.data_matrix[:, :, 0],
                                 sample_points=fd.sample_points,
                                 dataset_label=fd.dataset_label,
                                 axes_labels=fd.axes_labels[0:2])
     msplot = MagnitudeShapePlot(fd_temperatures,
                                 depth_method=modified_band_depth)
     np.testing.assert_allclose(
         msplot.points,
         np.array([[0.2591055, 3.15861149], [1.3811996, 0.91806814],
                   [0.94648379, 2.75695426], [2.11346208, 7.24045853],
                   [0.82557436, 0.82727771], [1.23249759, 0.22004329],
                   [-2.66259589, 0.96925352], [0.15827963, 1.00235557],
                   [-0.43751765, 0.66236714], [0.70695162, 0.66482897],
                   [0.73095525, 0.33165117], [3.48445368, 12.59745018],
                   [3.15539264, 13.85234879], [3.52759979, 10.49810783],
                   [3.95518808, 15.28317686], [-0.48486514, 0.5035343],
                   [0.64492781, 6.83385521], [-0.83185751, 0.81125541],
                   [-3.47125418, 1.10683451], [0.22241054, 1.76783493],
                   [-0.54402406, 0.95229119], [-1.71310618, 0.61875513],
                   [-0.44161441, 0.77815135], [0.13851408, 1.02560672],
                   [7.59909246, 40.82126568], [7.57868277, 36.03923856],
                   [7.12930634, 45.96866318], [0.05746528, 1.75817588],
                   [1.53092075, 8.85227], [-1.48696387, 0.22472872],
                   [-2.853082, 4.50814844], [-2.42297615, 1.46926902],
                   [-5.8873129, 5.35742609], [-5.44346193, 5.18338576],
                   [-16.38949483, 0.94027717]]))
     np.testing.assert_array_almost_equal(
         msplot.outliers,
         np.array([
             0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,
             0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1
         ]))
Exemplo n.º 24
0
    def test_lp_error_domain_ranges(self):
        sample_points = [2, 3, 4, 5, 6]
        fd2 = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                        sample_points=sample_points)

        with np.testing.assert_raises(ValueError):
            lp_distance(self.fd, fd2)
Exemplo n.º 25
0
def test_groupfpca_errors():
    n_basis = 9
    n_components = 1

    grid_points = np.arange(0.5, 365, 1)
    fd_data = fetch_weather()["data"].coordinates[0]
    fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), grid_points)
    X = fd_data.data_matrix.squeeze()
    X = np.hstack([X, X])
    groups = [np.arange(0, 365), np.arange(365, 730)]

    with pytest.raises(ValueError):
        GroupFPCA(n_components=n_components,
                  n_basis=n_basis,
                  basis=object,
                  groups=groups).fit(X)

    with pytest.raises(ValueError):
        GroupFPCA(n_components=n_components,
                  n_basis=n_basis,
                  basis="error",
                  groups=groups).fit(X)

    with pytest.raises(TypeError):
        GroupFPCA(
            n_components=n_components,
            n_basis=n_basis,
            groups=groups,
            exclude_groups="error",
        ).fit(X)
Exemplo n.º 26
0
 def test_slice(self):
     t = 10
     fd = FDataGrid(data_matrix=numpy.ones(t))
     fd = fd[:, 0]
     numpy.testing.assert_array_equal(fd.data_matrix[..., 0],
                                      numpy.array([[1]]))
     numpy.testing.assert_array_equal(fd.sample_points, numpy.array([[0]]))
Exemplo n.º 27
0
def test_group_fpca_bases(basis, use_grid_points, exclude_groups):
    n_basis = 9
    n_components = 1

    grid_points = np.arange(0.5, 365, 1)
    fd_data = fetch_weather()["data"].coordinates[0]
    fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), grid_points)
    X = fd_data.data_matrix.squeeze()
    X = np.hstack([X, X])
    groups = [np.arange(0, 365), np.arange(365, 730)]

    if use_grid_points:
        grid_points = np.hstack([grid_points, grid_points])
    else:
        grid_points = None

    gfpca = GroupFPCA(
        n_components=n_components,
        n_basis=n_basis,
        basis_domain_range=(0, 365),
        basis=basis,
        groups=groups,
        exclude_groups=exclude_groups,
    ).fit(X, grid_points=grid_points)

    _ = gfpca.transform(X, grid_points=grid_points)
Exemplo n.º 28
0
    def test_evaluation_center_and_extreme_points_linear(self):
        """Test linear interpolation in the middle point of a grid square."""

        dim_codomain = 4
        n_samples = 2

        @np.vectorize
        def coordinate_function(*args):
            _, *domain_indexes, _ = args
            return np.sum(domain_indexes)

        for dim_domain in range(1, 6):
            grid_points = [np.array([0, 1]) for _ in range(dim_domain)]
            data_matrix = np.fromfunction(
                function=coordinate_function,
                shape=(n_samples,) + (2,) * dim_domain + (dim_codomain,))

            f = FDataGrid(data_matrix, grid_points=grid_points)

            evaluation = f([[0.] * dim_domain, [0.5] *
                            dim_domain, [1.] * dim_domain])

            self.assertEqual(evaluation.shape, (n_samples, 3, dim_codomain))

            for i in range(n_samples):
                for j in range(dim_codomain):
                    np.testing.assert_array_almost_equal(
                        evaluation[i, ..., j],
                        [0, dim_domain * 0.5, dim_domain])
Exemplo n.º 29
0
 def test_init(self):
     fd = FDataGrid([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])
     np.testing.assert_array_equal(
         fd.data_matrix[..., 0], np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5,
                                                             6]]))
     np.testing.assert_array_equal(fd.sample_range, [(0, 1)])
     np.testing.assert_array_equal(fd.sample_points,
                                   np.array([[0., 0.25, 0.5, 0.75, 1.]]))
Exemplo n.º 30
0
 def test_mean(self):
     fd = FDataGrid([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])
     mean = stats.mean(fd)
     np.testing.assert_array_equal(mean.data_matrix[0, ..., 0],
                                   np.array([1.5, 2.5, 3.5, 4.5, 5.5]))
     np.testing.assert_array_equal(fd.sample_range, [(0, 1)])
     np.testing.assert_array_equal(fd.sample_points,
                                   np.array([[0., 0.25, 0.5, 0.75, 1.]]))