Skip to content

Commit 761c798

Browse files
authored
default theiler=1; 0 for cross-recurrence matrices (#51)
* default theiler=1, 0 for cross-recurrence matrices * conditional theiler for recurrencerate * `deftheiler` function for default Theiler window
1 parent d93fe0c commit 761c798

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

src/histograms.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ function _linehistograms(rows::T, cols::T, lmin::Integer=1, theiler::Integer=0,
103103
return (bins, bins_d)
104104
end
105105

106-
function diagonalhistogram(x::ARM; lmin::Integer=2, theiler::Integer=0, kwargs...)
106+
deftheiler(x::ARM) = 1
107+
deftheiler(x::CrossRecurrenceMatrix) = 0
108+
109+
function diagonalhistogram(x::ARM; lmin::Integer=2, theiler::Integer=deftheiler(x), kwargs...)
107110
(theiler < 0) && throw(ErrorException(
108111
"Theiler window length must be greater than or equal to 0"))
109112
(lmin < 1) && throw(ErrorException("lmin must be 1 or greater"))
@@ -140,9 +143,9 @@ function diagonalhistogram(x::ARM; lmin::Integer=2, theiler::Integer=0, kwargs..
140143
end
141144
return dh
142145
end
143-
146+
144147
function verticalhistograms(x::ARM;
145-
lmin::Integer=2, theiler::Integer=0, distances=true, kwargs...)
148+
lmin::Integer=2, theiler::Integer=deftheiler(x), distances=true, kwargs...)
146149
(theiler < 0) && throw(ErrorException(
147150
"Theiler window length must be greater than or equal to 0"))
148151
(lmin < 1) && throw(ErrorException("lmin must be 1 or greater"))
@@ -191,7 +194,8 @@ recurrence quantifications", in: Webber, C.L. & N. Marwan (eds.), *Recurrence
191194
Quantification Analysis. Theory and Best Practices*, Springer, pp. 3-43 (2015).
192195
"""
193196
function recurrencestructures(x::ARM;
194-
diagonal=true, vertical=true, recurrencetimes=true, lmin=1, theiler=0, kwargs...)
197+
diagonal=true, vertical=true, recurrencetimes=true,
198+
lmin=1, theiler=deftheiler(x), kwargs...)
195199

196200
# Parse arguments for diagonal and vertical structures
197201
histograms = Dict{String,Vector{Int}}()
@@ -211,3 +215,4 @@ function recurrencestructures(x::ARM;
211215
end
212216
return histograms
213217
end
218+

src/rqa.jl

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# Recurrence rate
44

55
"""
6-
recurrencerate(x; theiler=0)
6+
recurrencerate(x[; theiler::Integer])
77
8-
Calculate the recurrence rate of the recurrence matrix `x`, ruling out
9-
the points within the Theiler window of size `theiler`.
8+
Calculate the recurrence rate of the recurrence matrix `x`.
9+
10+
The line of identity (main diagonal) is excluded by default for matrices of type
11+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
12+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
13+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
1014
"""
11-
function recurrencerate(x::ARM; theiler::Integer=0, kwargs...)::Float64
15+
function recurrencerate(x::ARM; theiler::Integer=deftheiler(x), kwargs...)::Float64
1216
(theiler < 0) && throw(ErrorException(
1317
"Theiler window length must be greater than or equal to 0"))
1418
if theiler == 0
@@ -52,11 +56,15 @@ macro histogram_params(keyword, description, hist_fun)
5256
_fname = Symbol("_$(keyword)_$(name)")
5357
fbody = function_bodies[name]
5458
doc = """
55-
$fname(x; lmin=2, theiler=0)
59+
$fname(x[; lmin=2, theiler])
5660
5761
Calculate the $(param) contained in the recurrence matrix `x`,
58-
ruling out the points within the Theiler window of size `theiler`
59-
and diagonals shorter than `lmin`.
62+
ruling out the lines shorter than `lmin`.
63+
64+
The line of identity (main diagonal) is excluded by default for matrices of type
65+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
66+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
67+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
6068
"""
6169
push!(ret.args, quote
6270
@doc $doc ->
@@ -80,11 +88,15 @@ end
8088
@deprecate rqaentropy dl_entropy
8189

8290
"""
83-
determinism(x; lmin=2, theiler=0)
91+
determinism(x[; lmin=2, theiler])
8492
8593
Calculate the determinism of the recurrence matrix `x`, ruling out
86-
the points within the Theiler window of size `theiler` and diagonals shorter
87-
than `lmin`.
94+
the diagonal lines shorter than `lmin`.
95+
96+
The line of identity (main diagonal) is excluded by default for matrices of type
97+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
98+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
99+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
88100
"""
89101
function determinism(x::ARM; kwargs...)
90102
npoints = recurrencerate(x; kwargs...)*length(x)
@@ -99,32 +111,37 @@ end
99111

100112

101113
"""
102-
divergence(x; theiler=0)
114+
divergence(x[; theiler])
103115
104116
Calculate the divergence of the recurrence matrix `x`
105-
(actually the inverse of [`dl_max`](@ref)), ruling out
106-
the points within the Theiler window of size `theiler`.
117+
(actually the inverse of [`dl_max`](@ref)).
107118
"""
108119
divergence(x::ARM; kwargs...) = ( 1.0/dl_max(x; kwargs...) )
109120

110121

111122
"""
112-
trend(x; theiler=0, border=10)
123+
trend(x[; border=10, theiler])
113124
114-
Calculate the trend of recurrences in the recurrence matrix `x`
115-
towards its edges, ruling out the points within the Theiler window of size `theiler`.
125+
Calculate the trend of recurrences in the recurrence matrix `x`.
116126
117-
Keyword `border` can also exclude outermost diagonals
118-
(i.e. counting from the corners of the matrix).
127+
The 10 outermost diagonals (counting from the corners of the matrix)
128+
are excluded by default to avoid "border effects". Use the keyword argument
129+
`border` to define a different number of excluded lines.
130+
131+
The line of identity (main diagonal) is excluded by default for matrices of type
132+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
133+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
134+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
119135
"""
120-
trend(x::ARM; kwargs...) = _trend(tau_recurrence(x); kwargs...)
136+
trend(x::ARM; theiler=deftheiler(x), kwargs...) =
137+
_trend(tau_recurrence(x); theiler=theiler, kwargs...)
121138

122139
function tau_recurrence(x::ARM)
123140
n = minimum(size(x))
124141
[count(!iszero, diag(x,d))/(n-d) for d in (0:n-1)]
125142
end
126143

127-
function _trend(npoints::Vector; theiler=0, border=10, kwargs...)::Float64
144+
function _trend(npoints::Vector; theiler=1, border=10, kwargs...)::Float64
128145
nmax = length(npoints)
129146
rrk = npoints./collect(nmax:-1:1)
130147
a = 1+theiler
@@ -150,11 +167,15 @@ end
150167
@deprecate maxvert vl_max
151168

152169
"""
153-
laminarity(x; lmin=2, theiler=0)
170+
laminarity(x[; lmin=2, theiler])
154171
155172
Calculate the laminarity of the recurrence matrix `x`, ruling out the
156-
points within the Theiler window of size `theiler` and lines shorter
157-
than `lmin`.
173+
lines shorter than `lmin`.
174+
175+
The line of identity (main diagonal) is excluded by default for matrices of type
176+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
177+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
178+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
158179
"""
159180
function laminarity(x::ARM; kwargs...)
160181
npoints = recurrencerate(x)*length(x)
@@ -167,8 +188,12 @@ _laminarity(vert_hist::Vector{<:Integer}, npoints) = _determinism(vert_hist, npo
167188
trappingtime(x; lmin=2, theiler=0)
168189
169190
Calculate the trapping time of the recurrence matrix `x`, ruling out the
170-
points within the Theiler window of size `theiler` and lines shorter
171-
than `lmin`.
191+
lines shorter than `lmin`.
192+
193+
The line of identity (main diagonal) is excluded by default for matrices of type
194+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
195+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
196+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
172197
173198
The trapping time is the average of the vertical line structures and thus equal
174199
to [`vl_average`](@ref).
@@ -184,8 +209,12 @@ trappingtime(x::ARM; kwargs...) = vl_average(x; kwargs...)
184209
meanrecurrencetime(x; lmin=2, theiler=0)
185210
186211
Calculate the mean recurrence time of the recurrence matrix `x`, ruling out the
187-
points within the Theiler window of size `theiler` and lines shorter
188-
than `lmin`.
212+
lines shorter than `lmin`.
213+
214+
The line of identity (main diagonal) is excluded by default for matrices of type
215+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
216+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
217+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
189218
190219
Equivalent to [`rt_average`](@ref).
191220
"""
@@ -196,8 +225,12 @@ meanrecurrencetime(x::ARM; kwargs...) = rt_average(x; kwargs...)
196225
nmprt(x; lmin=2, theiler=0)
197226
198227
Calculate the number of the most probable recurrence time (NMPRT), ruling out the
199-
points within the Theiler window of size `theiler` and lines shorter
200-
than `lmin`.
228+
lines shorter than `lmin`.
229+
230+
The line of identity (main diagonal) is excluded by default for matrices of type
231+
`RecurrenceMatrix` or `JointRecurrenceMatrix`, but included for matrices of type
232+
`CrossRecurrenceMatrix`. Use the keyword argument `theiler` to exclude the
233+
diagonals within a custom Theiler window (`theiler=0` to include all diagonals).
201234
"""
202235
nmprt(x::ARM; kwargs) = maximum(verticalhistograms(x; kwargs...)[2])
203236

0 commit comments

Comments
 (0)