summaryrefslogtreecommitdiff
path: root/src/python/gudhi/point_cloud/timedelay.py
blob: 5c7ba5425c3a8dfa4afedd6e53de2d43810054f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np

class TimeDelayEmbedding:
    """Point cloud transformation class.

    Embeds time-series data in the R^d according to Takens' Embedding Theorem
    and obtains the coordinates of each point.

    Parameters
    ----------
    dim : int, optional (default=3)
        `d` of R^d to be embedded.

    delay : int, optional (default=1)
        Time-Delay embedding.

    skip : int, optional (default=1)
        How often to skip embedded points.

    """
    def __init__(self, dim=3, delay=1, skip=1):
        self._dim = dim
        self._delay = delay
        self._skip = skip

    def __call__(self, *args, **kwargs):
        return self.transform(*args, **kwargs)

    def _transform(self, ts):
        """Guts of transform method."""
        return ts[
            np.add.outer(
                np.arange(0, len(ts)-self._delay*(self._dim-1), self._skip),
                np.arange(0, self._dim*self._delay, self._delay))
        ]

    def transform(self, ts):
        """Transform method.

        Parameters
        ----------
        ts : list[float] or list[list[float]]
            A single or multiple time-series data.

        Returns
        -------
        point clouds : list[list[float, float, float]] or list[list[list[float, float, float]]]
            Makes point cloud every a single time-series data.
        """
        ndts = np.array(ts)
        if ndts.ndim == 1:
            # for single.
            return self._transform(ndts).tolist()
        else:
            # for multiple.
            return np.apply_along_axis(self._transform, 1, ndts).tolist()