Skip to content

Commit

Permalink
MAINT: Fixup daf187e
Browse files Browse the repository at this point in the history
Resolves #173.
  • Loading branch information
ntfrgl committed Oct 19, 2023
1 parent 940efa1 commit ed8b158
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/pyunicorn/timeseries/joint_recurrence_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,18 @@ def __init__(self, x, y, metric=("supremum", "supremum"),
threshold_std = kwds.get("threshold_std")
recurrence_rate = kwds.get("recurrence_rate")

assert len(metric) == 2
assert all(m in ("manhattan", "euclidean", "supremum") for m in metric)

# initialise base class using dummy values (bad OO design)
RecurrencePlot.__init__(
self, np.empty((2, 0)), metric=metric[0], normalize=normalize,
threshold=threshold[0] if threshold else 0,
recurrence_rate=recurrence_rate, silence_level=silence_level)

# Store type of metric
self.metric = metric

self._distance_matrix_cached = False
self.JR = None
"""The joint recurrence matrix."""
Expand Down
4 changes: 3 additions & 1 deletion src/pyunicorn/timeseries/recurrence_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def __init__(self, time_series, metric="supremum", normalize=False,
self.time_series.shape = (self.time_series.shape[0], -1)

# Store type of metric
assert metric in ("manhattan", "euclidean", "supremum")
self.metric = metric
"""The metric used for measuring distances in phase space."""

Expand Down Expand Up @@ -282,7 +283,6 @@ def distance_matrix(self, embedding, metric):
:rtype: 2D square array
:return: the phase space distance matrix :math:`D`
"""

if not self._distance_matrix_cached:
# Return distance matrix according to chosen metric:
if metric == "manhattan":
Expand All @@ -294,6 +294,8 @@ def distance_matrix(self, embedding, metric):
elif metric == "supremum":
self._distance_matrix = \
RecurrencePlot.supremum_distance_matrix(self, embedding)
else:
raise ValueError(f"unknown metric: {metric}")

self._distance_matrix_cached = True

Expand Down
37 changes: 37 additions & 0 deletions tests/test_timeseries/test_joint_recurrence_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is part of pyunicorn.
# Copyright (C) 2008--2023 Jonathan F. Donges and pyunicorn authors
# URL: <http://www.pik-potsdam.de/members/donges/software>
# License: BSD (3-clause)
#
# Please acknowledge and cite the use of this software and its authors
# when results are used in publications or published elsewhere.
#
# You can use the following reference:
# J.F. Donges, J. Heitzig, B. Beronov, M. Wiedermann, J. Runge, Q.-Y. Feng,
# L. Tupikina, V. Stolbova, R.V. Donner, N. Marwan, H.A. Dijkstra,
# and J. Kurths, "Unified functional network and nonlinear time series analysis
# for complex systems science: The pyunicorn package"

"""
Simple tests for the JointRecurrencePlot class.
"""

import numpy as np

import pytest

from pyunicorn.timeseries import JointRecurrencePlot
from pyunicorn.funcnet import CouplingAnalysis


@pytest.mark.parametrize("met", ["supremum", "euclidean", "manhattan"])
@pytest.mark.parametrize("n", [2, 10, 50])
def test_recurrence(met: str, n: int):
ts = CouplingAnalysis.test_data()[:n, 0]
jrp = JointRecurrencePlot(ts, ts, threshold=(.1, .1), metric=(met, met))
dist = {
i: jrp.distance_matrix(getattr(jrp, f"{i}_embedded"), metric=met)
for i in "xy"}
assert all(d.shape == (n, n) for d in dist.values())
assert np.allclose(*dist.values())
assert jrp.recurrence_matrix().shape == (n, n)

0 comments on commit ed8b158

Please sign in to comment.