pik-copan / pyunicorn

Unified Complex Network and Recurrence Analysis Toolbox
http://pik-potsdam.de/~donges/pyunicorn/
Other
195 stars 86 forks source link

JointRecurrencePlot input array format #173

Closed astrolitterbox closed 8 months ago

astrolitterbox commented 1 year ago

I think it is an error on my part, but I can't make the JointRecurrencePlot work:

jrp = JointRecurrencePlot(x, y, threshold=(0.1,0.1))
print(jrp.recurrence_matrix())

Calculating recurrence plot at fixed threshold...
Calculating the supremum distance matrix...
Calculating joint recurrence plot at fixed threshold...

[[1 1]
 [1 1]]

My input arrays x and y have a shape of (561, 2), where the first column are the timepoints (identical for both timeseries), and the second columns are the measurements. I've tried using synthetic data as well, but I think the problem lies with the data dimensions/shapes. Perhaps you have a minimal working example available? That would help a lot. Thank you! Simona

zugnachpankow commented 1 year ago

Hey @astrolitterbox / Simona,

thank you for reaching out with your problem!

Just for clearity, your input arrays are classical python or numpy arrays right? When you say you have shape (561, 2) arrays, that would mean an array of 561 arrays with length 2, in which the first entry is the timepoint and the second is your measurement. As far as i know, joint recurrence plot would consider your timepoints as part of a 2d data field then.

Therefore i would suggest converting your arrays into two (561, ) arrays, omitting the timepoints, as they are the same for both series. Please try to convert x and y in this way:

x_new = x[ : , 1] y_new = y[ : , 1]

Then feed them to the method and let me know if that helped.

astrolitterbox commented 1 year ago

Hi @zugnachpankow! Thank you so much for your quick response. Indeed, the arrays should have been flipped. I've tried doing that before. Unfortunately, having 1D Numpy arrays does not change the result:


(150,)
(150,)
Calculating recurrence plot at fixed threshold...
Calculating the supremum distance matrix...
Calculating joint recurrence plot at fixed threshold...
[[1 1]
 [1 1]]

Here's a cleaned-up script that produces it: https://gist.github.com/astrolitterbox/54cdbcaf02f15e39d919078332397646

Many thanks for your help!

fkuehlein commented 9 months ago

Another class inheritance problem? @ntfrgl please help:

As described above, for two timeseries of length N, JointRecurrencePlot should produce a recurrence matrix of shape (N, N), but will end up with a matrix of shape (2, 2) irrespective of the input timeseries (see minimal example below).

In the example case, the recurrence matrix is calculated in JointRecurrencePlot.set_fixed_threshold(), which will call RecurrencePlot.distance_matrix(), which will call RecurrencePlot.supremum_distance_matrix().

The latter is fine, as with jrp = JointRecurrencePlot(), manually calling RecurrencePlot.supremum_distance_matrix(jrp) will produce a correctly shaped (50,50)-matrix.
Instead, manually calling RecurrencePlot.distance_matrix(jrp, jrp.x_embedded, metric="supremum") will produce a (2,2) shaped matrix (see below). Why?

Is there a problem with initialising a RecurrencePlot from an empty array in JointRecurrencePlot.__init__() maybe?


minimal example:

from pyunicorn.timeseries import JointRecurrencePlot
from pyunicorn.funcnet import CouplingAnalysis
# test timeseries of length N = 50
ts = CouplingAnalysis.test_data()[:50,0]
# create JointRecurrencePlot
jrp = JointRecurrencePlot(ts, ts, threshold=(.1,.1))
# compare input and output shapes
print(f"Timeseries shape:        {ts.shape}")
print(f"Recurrence Matrix shape: {jrp.recurrence_matrix().shape} <-- this is not right!\n")
# directly call parent class methods for comparison
print(f"the matrix calculation itself is ok:\
      {RecurrencePlot.supremum_distance_matrix(jrp, jrp.x_embedded).shape}")
print(f"but its not retrieved correctly:    \
      {RecurrencePlot.distance_matrix(jrp, jrp.x_embedded, metric='supremum').shape}")

output:

Calculating recurrence plot at fixed threshold...
Calculating the supremum distance matrix...
Calculating joint recurrence plot at fixed threshold...
Timeseries shape:        (50,)
Recurrence Matrix shape: (2, 2) <-- this is not right!

Calculating the supremum distance matrix...
the matrix calculation itself is ok:      (50, 50)
but its not retrieved correctly:          (2, 2)
fkuehlein commented 8 months ago

@ntfrgl, thanks for fixing and for providing a test that sets a good example!