Ejemplo n.º 1
0
 def setUp(self):
     argvals = {'input_dim_0': {0: np.array([1, 2, 3, 4]),
                                1: np.array([2, 4]),
                                2: np.array([0, 2, 3])}}
     values = {0: np.array([1, 2, 3, 4]),
               1: np.array([5, 6]),
               2: np.array([8, 9, 7],)}
     self.irregu_fd = IrregularFunctionalData(argvals, values)
Ejemplo n.º 2
0
class TestIrregularFunctionalData1D(unittest.TestCase):
    """Test class for the class IrregularFunctionalData in one dimension."""

    def setUp(self):
        argvals = {'input_dim_0': {0: np.array([1, 2, 3, 4]),
                                   1: np.array([2, 4]),
                                   2: np.array([0, 2, 3])}}
        values = {0: np.array([1, 2, 3, 4]),
                  1: np.array([5, 6]),
                  2: np.array([8, 9, 7],)}
        self.irregu_fd = IrregularFunctionalData(argvals, values)

    def test_argvals_stand(self):
        is_equal = [np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][0],
                                np.array([0.25, 0.5, 0.75, 1.])),
                    np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][1],
                                np.array([0.5, 1.])),
                    np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][2],
                                np.array([0., 0.5, 0.75]))]
        self.assertTrue(np.all(is_equal))

    def test_n_obs(self):
        self.assertEqual(self.irregu_fd.n_obs, 3)

    def test_n_dim(self):
        self.assertEqual(self.irregu_fd.n_dim, 1)

    def test_range_obs(self):
        self.assertEqual(self.irregu_fd.range_obs, (1, 9))

    def test_range_dim(self):
        self.assertEqual(self.irregu_fd.range_dim, {'input_dim_0': (0, 4)})

    def test_shape(self):
        self.assertEqual(self.irregu_fd.shape, {'input_dim_0': 5})

    def test_subset(self):
        new_irregu_fd = self.irregu_fd[2]
        self.assertIsInstance(new_irregu_fd, IrregularFunctionalData)
        self.assertEqual(new_irregu_fd.n_obs, 1)
        new_irregu_fd = self.irregu_fd[:2]
        self.assertIsInstance(new_irregu_fd, IrregularFunctionalData)
        self.assertEqual(new_irregu_fd.n_obs, 2)

    def test_as_dense(self):
        dense_fd = self.irregu_fd.as_dense()
        self.assertIsInstance(dense_fd, DenseFunctionalData)
        self.assertEqual(dense_fd.n_obs, 3)

    def test_is_compatible(self):
        self.assertTrue(self.irregu_fd.is_compatible(self.irregu_fd))

    def test_mean(self):
        mean_fd = self.irregu_fd.mean()
        is_equal = np.allclose(mean_fd.values,
                               np.array([[8., 1., 5.33333333, 5., 5.]]))
        self.assertTrue(is_equal)
Ejemplo n.º 3
0
 def setUp(self):
     argvals = {'input_dim_0': {0: np.array([1, 2, 3, 4]),
                                1: np.array([2, 4]),
                                2: np.array([4, 5, 6])},
                'input_dim_1': {0: np.array([5, 6, 7]),
                                1: np.array([1, 2, 3]),
                                2: np.array([8, 9])}}
     values = {0: np.array([[1, 2, 3], [4, 1, 2], [3, 4, 1], [2, 3, 4]]),
               1: np.array([[1, 2, 3], [1, 2, 3]]),
               2: np.array([[8, 9], [8, 9], [8, 9]])}
     self.irregu_fd = IrregularFunctionalData(argvals, values)
Ejemplo n.º 4
0
def read_csv_irregular(data: pd.DataFrame,
                       argvals: np.ndarray) -> IrregularFunctionalData:
    """Load a csv file into an IrregularFunctionalData object.

    Parameters
    ----------
    data: pd.DataFrame
        Input dataframe.
    argvals: np.ndarray
        An array of argvals.

    Returns
    -------
    obj: IrregularFunctionalData
        The loaded csv file.

    """
    argvals = {
        'input_dim_0': {
            idx: np.array(argvals[~np.isnan(row)])
            for idx, row in enumerate(data.values)
        }
    }
    values = {idx: row[~np.isnan(row)] for idx, row in enumerate(data.values)}
    return IrregularFunctionalData(argvals, values)
    def test_check_compatible_irregular(self):
        argvals = {
            'input_dim_0': {
                0: np.array([1, 2, 3, 4])
            },
            'input_dim_1': {
                0: np.array([5, 6, 7])
            }
        }
        values = {0: np.array([[1, 2, 3], [4, 1, 2], [3, 4, 1], [2, 3, 4]])}
        irregu_fd = IrregularFunctionalData(argvals, values)

        argvals = {
            'input_dim_0': {
                0: np.array([1, 2, 3, 4]),
                1: np.array([2, 4])
            }
        }
        values = {0: np.array([1, 2, 3, 4]), 1: np.array([5, 6])}
        irregu_fd2 = IrregularFunctionalData(argvals, values)
        self.assertRaises(ValueError, _check_same_nobs, irregu_fd, irregu_fd2)
        self.assertRaises(ValueError, _check_same_ndim, irregu_fd, irregu_fd2)
Ejemplo n.º 6
0
def read_ts_irregular(data):
    """Load a ts file into an IrregularFunctionalData object.

    Parameters
    ----------
    data: pd.DataFrame
        Input dataframe

    Returns
    -------
    obj: IrregularFunctionalData
        The loaded ts file.

    """
    argvals, values = {}, {}
    for idx, row in data.iterrows():
        argvals[idx] = row['dim_0'].index.values
        values[idx] = row['dim_0'].values
    return IrregularFunctionalData({'input_dim_0': argvals}, values)
    def test_check_same_type(self):
        argvals = {'input_dim_0': np.array([1, 2, 3, 4])}
        values = np.array([[1, 2, 3, 4], [5, 6, 7, 9], [3, 4, 5, 7]])
        dense_fd = DenseFunctionalData(argvals, values)

        argvals = {
            'input_dim_0': {
                0: np.array([1, 2, 3, 4]),
                1: np.array([2, 4]),
                2: np.array([0, 2, 3])
            }
        }
        values = {
            0: np.array([1, 2, 3, 4]),
            1: np.array([5, 6]),
            2: np.array([8, 9, 7])
        }
        irregu_fd = IrregularFunctionalData(argvals, values)
        self.assertRaises(TypeError, _check_same_type, dense_fd, irregu_fd)
class TestIrregularFunctionalData2D(unittest.TestCase):
    """Test class for the class IrregularFunctionalData in two dimension."""
    def setUp(self):
        argvals = {
            'input_dim_0': {
                0: np.array([1, 2, 3, 4]),
                1: np.array([2, 4]),
                2: np.array([4, 5, 6])
            },
            'input_dim_1': {
                0: np.array([5, 6, 7]),
                1: np.array([1, 2, 3]),
                2: np.array([8, 9])
            }
        }
        values = {
            0: np.array([[1, 2, 3], [4, 1, 2], [3, 4, 1], [2, 3, 4]]),
            1: np.array([[1, 2, 3], [1, 2, 3]]),
            2: np.array([[8, 9], [8, 9], [8, 9]])
        }
        self.irregu_fd = IrregularFunctionalData(argvals, values)

    def test_argvals_stand(self):
        is_equal = [
            np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][0],
                        np.array([0., 0.2, 0.4, 0.6])),
            np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][1],
                        np.array([0.2, 0.6])),
            np.allclose(self.irregu_fd.argvals_stand['input_dim_0'][2],
                        np.array([0.6, 0.8, 1.])),
            np.allclose(self.irregu_fd.argvals_stand['input_dim_1'][0],
                        np.array([0.5, 0.625, 0.75])),
            np.allclose(self.irregu_fd.argvals_stand['input_dim_1'][1],
                        np.array([0., 0.125, 0.25])),
            np.allclose(self.irregu_fd.argvals_stand['input_dim_1'][2],
                        np.array([0.875, 1.]))
        ]
        self.assertTrue(np.all(is_equal))

    def test_n_obs(self):
        self.assertEqual(self.irregu_fd.n_obs, 3)

    def test_n_dim(self):
        self.assertEqual(self.irregu_fd.n_dim, 2)

    def test_range_obs(self):
        self.assertEqual(self.irregu_fd.range_obs, (1, 9))

    def test_range_dim(self):
        self.assertEqual(self.irregu_fd.range_dim, {
            'input_dim_0': (1, 6),
            'input_dim_1': (1, 9)
        })

    def test_shape(self):
        self.assertEqual(self.irregu_fd.shape, {
            'input_dim_0': 6,
            'input_dim_1': 8
        })

    def test_subset(self):
        new_irregu_fd = self.irregu_fd[2]
        self.assertIsInstance(new_irregu_fd, IrregularFunctionalData)
        self.assertEqual(new_irregu_fd.n_obs, 1)
        new_irregu_fd = self.irregu_fd[:2]
        self.assertIsInstance(new_irregu_fd, IrregularFunctionalData)
        self.assertEqual(new_irregu_fd.n_obs, 2)

    def test_as_dense(self):
        dense_fd = self.irregu_fd.as_dense()
        self.assertIsInstance(dense_fd, DenseFunctionalData)
        self.assertEqual(dense_fd.n_obs, 3)

    def test_is_compatible(self):
        self.assertTrue(self.irregu_fd.is_compatible(self.irregu_fd))

    def test_mean(self):
        N = np.nan
        mean_fd = self.irregu_fd.mean()
        is_equal = np.allclose(mean_fd.values,
                               np.array([[[N, N, N, 1., 2., 3., N, N],
                                          [1., 2., 3., 4., 1., 2., N, N],
                                          [N, N, N, 3., 4., 1., N, N],
                                          [1., 2., 3., 2., 3., 4., 8., 9.],
                                          [N, N, N, N, N, N, 8., 9.],
                                          [N, N, N, N, N, N, 8., 9.]]]),
                               equal_nan=True)
        self.assertTrue(is_equal)