Closed takeshimeonerespect closed 4 years ago
Docker container環境で動作確認、100%で終了することを確認済
Marcにreply済
__call__
で実行transform
で実行
__call__
にif文による条件分岐を使用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)
__call__
でif文による条件分岐としてしまい、1次元配列を__call__
で__call__
で実行
if文による条件分岐を使用transform
で実行
if文による条件分岐を使用__init__.py
追加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.")
from gudhi.point_cloud.timedelay import TimeDelayEmbedding
def test_normal():
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():
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():
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()
dim
に代わる新たな名称の提案
point clouds
の返却値(return)の修正
point clouds : list[list[list[float, float, float]]]
point clouds : list of n x 3 numpy arrays
np.hstack
を使うとできそう[式 for 任意の変数名 in イテラブルオブジェクト]
リストやタプルなどのイテラブルオブジェクト
の各要素を任意の変数名
で取り出し式
で評価、その結果が要素となる新たなリストが返却される.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.")
dim
は、結局、名称変更なし
- CMAKE_EXAMPLE='OFF' CMAKE_TEST='OFF' CMAKE_UTILITIES='OFF' CMAKE_PYTHON='OFF' MAKE_TARGET='doxygen' CTEST_COMMAND='echo No tests for doxygen target'
doxygen: プログラムのコメントに書かれた情報をもとにドキュメントを作成するツール
Close the issue because it was merged on March 16.
Marcからtime series dataからpoint cloudを生成するソースコード等について、
以下の修正を依頼された。