takeshimeonerespect / take_shimeone

0 stars 0 forks source link

GUDHIへのpull request後の対応 #2

Closed takeshimeonerespect closed 4 years ago

takeshimeonerespect commented 4 years ago

Marcからtime series dataからpoint cloudを生成するソースコード等について、
以下の修正を依頼された。

takeshimeonerespect commented 4 years ago

Docker container環境で動作確認、100%で終了することを確認済

takeshimeonerespect commented 4 years ago

Marcにreply済

takeshimeonerespect commented 4 years ago

2020-2-12

class TimeDelayEmbedding:
    """Attractor transformation class for a single time-series data.

    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):
        ndts = np.array(ts)
        if ndts.ndim == 1:
            return self._transform(*args, **kwargs)
        else:
            return self.transform(*args, **kwargs)

    def fit(self, ts, y=None):
        return self

    def _transform(self, ts):
        """Guts of transform method.

        Parameters
        ----------
        ts : ndarray, shape=[n_features]
            A single time-series data.

        Returns
        -------
        attractor : ndarray, shape=[n_points, d]
            `n_points` is the number of points in the R^d.

        """
        ndts = np.array(ts)
        return ndts[
            np.add.outer(
                np.arange(0, len(ndts)-self._delay*(self._dim-1), self._skip),
                np.arange(0, self._dim*self._delay, self._delay))
        ]

    def transform(self, ts):
        """Transform method."""
        ndts = np.array(ts)
        return np.apply_along_axis(self._transform, 1, ndts)
takeshimeonerespect commented 4 years ago

2020-2-13

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.
        Given delay=3 and skip=2, an point cloud which is obtained by embedding 
        a single time-series data into R^3 is as follows.

    .. code-block:: none

    time-series = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    point clouds = [[1, 4, 7], 
                    [3, 6, 9]]

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

    def __call__(self, ts):
        """Transform method for single time-series data.
        Parameters
        ----------
        ts : list[float]
            A single time-series data.
        Returns
        -------
        point clouds : list[list[float, float, float]]
            Makes point cloud every a single time-series data.
        Raises
        -------
        TypeError
            If the parameter's type does not match the desired type.
        """
        ndts = np.array(ts)
        if ndts.ndim == 1:
            return self._transform(ndts)
        else:
            raise TypeError("Expects 1-dimensional array.")

    def fit(self, ts, y=None):
        return self

    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 for multiple time-series data.
        Parameters
        ----------
        ts : list[list[float]]
            Multiple time-series data.
        Attributes
        ----------
        ndts : 
             The ndts means that all time series need to have exactly 
             the same size. 
        Returns
        -------
        point clouds : list[list[list[float, float, float]]]
            Makes point cloud every a single time-series data.
        Raises
        -------
        TypeError
            If the parameter's type does not match the desired type.
        """
        ndts = np.array(ts)
        if ndts.ndim == 2:
            return np.apply_along_axis(self._transform, 1, ndts)
        else:
            raise TypeError("Expects 2-dimensional array.")
takeshimeonerespect commented 4 years ago

2020-2-14

def test_normal():

Sample array

ts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Normal case.
prep = TimeDelayEmbedding()
attractor = prep(ts)
assert (attractor[0] == [1, 2, 3])
assert (attractor[1] == [2, 3, 4])
assert (attractor[2] == [3, 4, 5])
assert (attractor[3] == [4, 5, 6])
assert (attractor[4] == [5, 6, 7])
assert (attractor[5] == [6, 7, 8])
assert (attractor[6] == [7, 8, 9])
assert (attractor[7] == [8, 9, 10])
# Delay = 3
prep = TimeDelayEmbedding(delay=3)
attractor = prep(ts)
assert (attractor[0] == [1, 4, 7])
assert (attractor[1] == [2, 5, 8])
assert (attractor[2] == [3, 6, 9])
assert (attractor[3] == [4, 7, 10])
# Skip = 3
prep = TimeDelayEmbedding(skip=3)
attractor = prep(ts)
assert (attractor[0] == [1, 2, 3])
assert (attractor[1] == [4, 5, 6])
assert (attractor[2] == [7, 8, 9])
# Delay = 2 / Skip = 2
prep = TimeDelayEmbedding(delay=2, skip=2)
attractor = prep(ts)
assert (attractor[0] == [1, 3, 5])
assert (attractor[1] == [3, 5, 7])
assert (attractor[2] == [5, 7, 9])
- np.arrayを追記
  - ValueError: The truth value of an array with more than one element is ambiguous. 
Use a.any() or a.all() numpy  
  - `np.array`を追記しても、errorは解消されず

from gudhi.point_cloud.timedelay import TimeDelayEmbedding import numpy as np

def test_normal():

Sample array

ts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Normal case.
prep = TimeDelayEmbedding()
attractor = prep(ts)
assert (attractor[0] == np.array([1, 2, 3]))
assert (attractor[1] == np.array([2, 3, 4]))
assert (attractor[2] == np.array([3, 4, 5]))
assert (attractor[3] == np.array([4, 5, 6]))
assert (attractor[4] == np.array([5, 6, 7]))
assert (attractor[5] == np.array([6, 7, 8]))
assert (attractor[6] == np.array([7, 8, 9]))
assert (attractor[7] == np.array([8, 9, 10]))
# Delay = 3
prep = TimeDelayEmbedding(delay=3)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 4, 7]))
assert (attractor[1] == np.array([2, 5, 8]))
assert (attractor[2] == np.array([3, 6, 9]))
assert (attractor[3] == np.array([4, 7, 10]))
# Skip = 3
prep = TimeDelayEmbedding(skip=3)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 2, 3]))
assert (attractor[1] == np.array([4, 5, 6]))
assert (attractor[2] == np.array([7, 8, 9]))
# Delay = 2 / Skip = 2
prep = TimeDelayEmbedding(delay=2, skip=2)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 3, 5]))
assert (attractor[1] == np.array([3, 5, 7]))
assert (attractor[2] == np.array([5, 7, 9]))

- `.all()`を追記することによりerror解消

from gudhi.point_cloud.timedelay import TimeDelayEmbedding import numpy as np

def test_normal():

Sample array

ts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Normal case.
prep = TimeDelayEmbedding()
attractor = prep(ts)
assert (attractor[0] == np.array([1, 2, 3])).all()
assert (attractor[1] == np.array([2, 3, 4])).all()
assert (attractor[2] == np.array([3, 4, 5])).all()
assert (attractor[3] == np.array([4, 5, 6])).all()
assert (attractor[4] == np.array([5, 6, 7])).all()
assert (attractor[5] == np.array([6, 7, 8])).all()
assert (attractor[6] == np.array([7, 8, 9])).all()
assert (attractor[7] == np.array([8, 9, 10])).all()
# Delay = 3
prep = TimeDelayEmbedding(delay=3)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 4, 7])).all()
assert (attractor[1] == np.array([2, 5, 8])).all()
assert (attractor[2] == np.array([3, 6, 9])).all()
assert (attractor[3] == np.array([4, 7, 10])).all()
# Skip = 3
prep = TimeDelayEmbedding(skip=3)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 2, 3])).all()
assert (attractor[1] == np.array([4, 5, 6])).all()
assert (attractor[2] == np.array([7, 8, 9])).all()
# Delay = 2 / Skip = 2
prep = TimeDelayEmbedding(delay=2, skip=2)
attractor = prep(ts)
assert (attractor[0] == np.array([1, 3, 5])).all()
assert (attractor[1] == np.array([3, 5, 7])).all()
assert (attractor[2] == np.array([5, 7, 9])).all()
takeshimeonerespect commented 4 years ago

2020-2-19

implementation

ndts = np.array(ts)
        if ndts.ndim == 2:
            join_array = np.apply_along_axis(self._transform, 1, ndts)
            return np.hstack([i for i in join_array])
        else:
            raise TypeError("Expects 2-dimensional array.")
takeshimeonerespect commented 4 years ago

2020-2-25

takeshimeonerespect commented 4 years ago

2020-2-26

takeshimeonerespect commented 4 years ago

Close the issue because it was merged on March 16.