Skip to content

Commit 4e38bed

Browse files
felixcremerheliosdrm
authored andcommitted
Change return value of rqa to named Tuple (#61)
* Change return value of rqa to named Tuple This should be faster than using a Dict. * Change docstring of rqa rqa now returns a NamedTuple with symbols and not a dictionary with string keys. * Change return type of windowed rqa to NamedTuple To be consistent with the change of the return type of rqa this needed to change. Also changes the retrieval of the local rqa values, because the are not in a dict anymore. Changes local rqa_dict variable to rqa_tuple to reflect the type. * Use :key to get values from the rqa return This is needed because now rqa returns a NamedTuple instead of a dict.
1 parent 361f308 commit 4e38bed

File tree

4 files changed

+120
-122
lines changed

4 files changed

+120
-122
lines changed

src/rqa.jl

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,27 @@ Using this function is much more efficient than calling all individual functions
346346
one by one.
347347
348348
## Return
349-
The returned value is a dictionary with the following keys:
350-
351-
* "RR": recurrence rate (see [`recurrencerate`](@ref))
352-
* "DET": determinsm (see [`determinism`](@ref))
353-
* "L": average length of diagonal structures (see [`dl_average`](@ref))
354-
* "Lmax": maximum length of diagonal structures (see [`dl_max`](@ref))
355-
* "DIV": divergence (see [`divergence`](@ref))
356-
* "ENTR": entropy of diagonal structures (see [`dl_entropy`](@ref))
357-
* "TREND": trend of recurrences (see [`trend`](@ref))
358-
* "LAM": laminarity (see [`laminarity`](@ref))
359-
* "TT": trapping time (see [`trappingtime`](@ref))
360-
* "Vmax": maximum length of vertical structures (see [`vl_max`](@ref))
361-
* "VENTR": entropy of vertical structures (see [`vl_entropy`](@ref))
362-
* "MRT": mean recurrence time (see [`meanrecurrencetime`](@ref))
363-
* "RTE" recurrence time entropy (see [`rt_entropy`](@ref))
364-
* "NMPRT": number of the most probable recurrence time (see [`nmprt`](@ref))
349+
The returned value is a NamedTuple with the following entries:
350+
351+
* ´RR´: recurrence rate (see [`recurrencerate`](@ref))
352+
* ´DET´: determinsm (see [`determinism`](@ref))
353+
* ´L´: average length of diagonal structures (see [`dl_average`](@ref))
354+
* ´Lmax´: maximum length of diagonal structures (see [`dl_max`](@ref))
355+
* ´DIV´: divergence (see [`divergence`](@ref))
356+
* ´ENTR´: entropy of diagonal structures (see [`dl_entropy`](@ref))
357+
* ´TREND´: trend of recurrences (see [`trend`](@ref))
358+
* ´LAM´: laminarity (see [`laminarity`](@ref))
359+
* ´TT´: trapping time (see [`trappingtime`](@ref))
360+
* ´Vmax´: maximum length of vertical structures (see [`vl_max`](@ref))
361+
* ´VENTR´: entropy of vertical structures (see [`vl_entropy`](@ref))
362+
* ´MRT´: mean recurrence time (see [`meanrecurrencetime`](@ref))
363+
* ´RTE´ recurrence time entropy (see [`rt_entropy`](@ref))
364+
* ´NMPRT´: number of the most probable recurrence time (see [`nmprt`](@ref))
365365
366366
In the case of empty histograms (e.g. no existing vertical lines
367367
less than the keyword `lminvert`) the average and maximum values
368-
("L", "Lmax", "TT", "Vmax", "MRT")
369-
are returned as `0.0` but their respective entropies ("ENTR", "VENTR", "RTE")
368+
(´L´, ´Lmax´, ´TT´, ´Vmax´, ´MRT´)
369+
are returned as `0.0` but their respective entropies (´ENTR´, ´VENTR´, ´RTE´)
370370
are returned as `NaN`.
371371
372372
## Keyword Arguments
@@ -388,7 +388,7 @@ i.e. `theiler`, `lmin`, and `border`:
388388
describe the distributions of diagonal or vertical lines (it is set as 2 by
389389
default).
390390
391-
* `border` is used to avoid border effects in the calculation of "TREND"
391+
* `border` is used to avoid border effects in the calculation of ´TREND´
392392
(cf. [`trend`](@ref)).
393393
394394
In addition `theilerdiag`, `lmindiag` may be used to
@@ -399,7 +399,7 @@ structures.
399399
400400
The keyword argument `onlydiagonal` (`false` by default) can be set to `true`
401401
in order to restrict the analysis to the recurrence rate and the parameters related
402-
to diagonal structures ("RR", "DET", "L", "Lmax", "DIV" and "ENTR"), which makes
402+
to diagonal structures (´RR´, ´DET´, ´L´, ´Lmax´, ´DIV´ and ´ENTR´), which makes
403403
this function slightly faster.
404404
"""
405405
function rqa(x; onlydiagonal=false, kwargs...)
@@ -410,33 +410,33 @@ function rqa(x; onlydiagonal=false, kwargs...)
410410
dhist = diagonalhistogram(x; kw_d...)
411411
rr_d = recurrencerate(x; kw_d...)
412412
if onlydiagonal
413-
return Dict("RR" => recurrencerate(x; kwargs...),
414-
"DET" => _determinism(dhist, rr_d*_rrdenominator(x; kw_d...)),
415-
"L" => _dl_average(dhist),
416-
"Lmax" => _dl_max(dhist),
417-
"DIV" => 1.0/_dl_max(dhist),
418-
"ENTR" => _dl_entropy(dhist)
413+
return (RR = recurrencerate(x; kwargs...),
414+
DET = _determinism(dhist, rr_d*_rrdenominator(x; kw_d...)),
415+
L = _dl_average(dhist),
416+
Lmax = _dl_max(dhist),
417+
DIV = 1.0/_dl_max(dhist),
418+
ENTR = _dl_entropy(dhist)
419419
)
420420
else
421421
kw_v = Dict(kwargs)
422422
haskey(kw_v, :theilervert) && (kw_v[:theiler] = kw_v[:theilervert])
423423
haskey(kw_v, :lminvert) && (kw_v[:lmin] = kw_v[:lminvert])
424424
vhist, rthist = verticalhistograms(x; kw_v...)
425425
rr_v = recurrencerate(x; kw_v...)
426-
return Dict("RR" => recurrencerate(x; kwargs...),
427-
"DET" => _determinism(dhist, rr_d*_rrdenominator(x; kw_v...)),
428-
"L" => _dl_average(dhist),
429-
"Lmax" => _dl_max(dhist),
430-
"DIV" => 1.0/_dl_max(dhist),
431-
"ENTR" => _dl_entropy(dhist),
432-
"TREND" => trend(x; kw_d...),
433-
"LAM" => _laminarity(vhist, rr_v*_rrdenominator(x; kw_v...)),
434-
"TT" => _vl_average(vhist),
435-
"Vmax" => _vl_max(vhist),
436-
"VENTR" => _vl_entropy(vhist),
437-
"MRT" => _rt_average(rthist),
438-
"RTE" => _rt_entropy(rthist),
439-
"NMPRT" => maximum(rthist)
426+
return (RR = recurrencerate(x; kwargs...),
427+
DET = _determinism(dhist, rr_d*_rrdenominator(x; kw_v...)),
428+
L = _dl_average(dhist),
429+
Lmax = _dl_max(dhist),
430+
DIV = 1.0/_dl_max(dhist),
431+
ENTR = _dl_entropy(dhist),
432+
TREND = trend(x; kw_d...),
433+
LAM = _laminarity(vhist, rr_v*_rrdenominator(x; kw_v...)),
434+
TT = _vl_average(vhist),
435+
Vmax = _vl_max(vhist),
436+
VENTR = _vl_entropy(vhist),
437+
MRT = _rt_average(rthist),
438+
RTE = _rt_entropy(rthist),
439+
NMPRT = maximum(rthist)
440440
)
441441
end
442442
end

src/windowed.jl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,30 @@ macro windowed(ex, options...)
152152
local nw = size($x,1) - $(dict_op[:width])
153153
local ni = div(nw, s)+1 # number of items
154154
local mtype = typeof($x)
155-
local rqa_dict = Dict(
156-
"RR" => zeros(Float64,ni),
157-
"DET" => zeros(Float64,ni),
158-
"L" => zeros(Float64,ni),
159-
"Lmax" => zeros(Int,ni),
160-
"DIV" => zeros(Float64,ni),
161-
"ENTR" => zeros(Float64,ni),
162-
"TREND" => zeros(Float64,ni),
163-
"LAM" => zeros(Float64,ni),
164-
"TT" => zeros(Float64,ni),
165-
"Vmax" => zeros(Int,ni),
166-
"VENTR" => zeros(Float64,ni),
167-
"MRT" => zeros(Float64,ni),
168-
"RTE" => zeros(Float64,ni),
169-
"NMPRT" => zeros(Int,ni)
155+
local rqa_tuple = (
156+
RR = zeros(Float64,ni),
157+
DET = zeros(Float64,ni),
158+
L = zeros(Float64,ni),
159+
Lmax = zeros(Int,ni),
160+
DIV = zeros(Float64,ni),
161+
ENTR = zeros(Float64,ni),
162+
TREND = zeros(Float64,ni),
163+
LAM = zeros(Float64,ni),
164+
TT = zeros(Float64,ni),
165+
Vmax = zeros(Int,ni),
166+
VENTR = zeros(Float64,ni),
167+
MRT = zeros(Float64,ni),
168+
RTE = zeros(Float64,ni),
169+
NMPRT = zeros(Int,ni)
170170
)
171171
for i=1:ni
172172
local rqa_i = $ex
173-
for (k,v) in rqa_i
174-
rqa_dict[k][i] = v
173+
#@show rqa_i
174+
for k in keys(rqa_i)
175+
rqa_tuple[k][i] = rqa_i[k]
175176
end
176177
end
177-
rqa_dict
178+
rqa_tuple
178179
end
179180
return esc(ret_ex)
180181
end

test/dynamicalsystems.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ rng = Random.seed!(194)
1111
# Dynamical Systems", in: Riley MA & Van Orden GC, Tutorials in Contemporary
1212
# Nonlinear Methods for the Behavioral Sciences, 2005, 26-94.
1313
# https://www.nsf.gov/pubs/2005/nsf05057/nmbs/nmbs.pdf
14-
#
14+
#
1515
trajectories = Dict(
1616
"Sine wave" => RA.Dataset(map(x->[sin.(x) cos.(x)], StepRangeLen(0.0,0.2,200))),
1717
"White noise" => RA.Dataset(randn!(zeros(200,2))),
1818
"Hénon (chaotic)" => trajectory(Systems.henon(a=1.4, b=0.3), 199, Ttr=1000),
1919
"Hénon (periodic)" => trajectory(Systems.henon(a=1.054, b=0.3), 199, Ttr=1000)
2020
)
21-
embed_params = Dict( #(m, τ)
21+
embed_params = Dict( #(m, τ)
2222
"Sine wave" => (9, 7),
2323
"White noise" => (1, 1),
2424
"Hénon (chaotic)" => (3, 1),
2525
"Hénon (periodic)" => (3, 1))
26-
rqa_threshold = Dict(
26+
rqa_threshold = Dict(
2727
"Sine wave" => 0.15,
2828
"White noise" => 0.15,
2929
"Hénon (chaotic)" => 0.15,
@@ -36,7 +36,7 @@ dict_keys = ["Sine wave","White noise","Hénon (chaotic)","Hénon (periodic)"]
3636
y = data[1:100,2]
3737
xe = embed(x, embed_params[k]...)
3838
ye = embed(y, embed_params[k]...)
39-
39+
4040
# Distance and recurrence matrices
4141
ε = rqa_threshold[k]
4242
dmat = distancematrix(xe, ye)
@@ -59,28 +59,27 @@ dict_keys = ["Sine wave","White noise","Hénon (chaotic)","Hénon (periodic)"]
5959
# Fixed rate for recurrence matrix
6060
crmat_fixed = CrossRecurrenceMatrix(xe, ye, 0.05; fixedrate=true)
6161
@test .04 < recurrencerate(crmat_fixed) < .06
62-
62+
6363
# Recurrence plot
6464
crp = recurrenceplot(crmat, width=125)
6565
szplot = size(crp)
6666
szmat = size(crmat)
6767
@test szplot[1] szplot[2]*szmat[2]/szmat[1] atol = 1
68-
68+
6969
# RQA
7070
rqapar = rqa(rmat, theiler=1, lmin=3, border=20)
7171
rqadiag = rqa(rmat, theiler=1, lmin=3, border=20, onlydiagonal=true)
7272
for p in keys(rqadiag)
7373
@test rqapar[p]==rqadiag[p]
7474
end
75-
75+
7676
# Windowed RQA
7777
rmatw = @windowed RecurrenceMatrix(xe, ε, metric=RecurrenceAnalysis.Chebyshev()) 50
7878
crmatw = @windowed(CrossRecurrenceMatrix(xe, ye, ε),30)
7979
@windowed jrmatw = JointRecurrenceMatrix(xe, ye, ε) 30
8080
@test jrmatw[3 .+ (1:30), 3 .+ (1:30)] == jrmat[3 .+ (1:30), 3 .+ (1:30)]
8181
@windowed(rrw = recurrencerate(rmatw), width=50, step=40)
8282
@windowed rqaw = rqa(rmatw) width=50 step=40
83-
@test rqaw["RR"] == rrw
83+
@test rqaw[:RR] == rrw
8484

8585
end
86-

test/smallmatrix.jl

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rmat = CrossRecurrenceMatrix(sparse(i,j,trues(length(i))))
2222
# 7 · x x · · o · · x · x
2323
# 8 · x · · · · o · · · ·
2424
# 9 · · · · x · · · o o ·
25-
# A · · x · x x · · · o ·
25+
# A · · x · x x · · · o ·
2626
#
2727
###
2828

@@ -36,19 +36,19 @@ rmat = CrossRecurrenceMatrix(sparse(i,j,trues(length(i))))
3636
@test histograms[k] == true_histograms[k]
3737
end
3838
rqa_params = rqa(rmat, theiler=0, lmin=1, border=1)
39-
@test rqa_params["RR"] == 33/110
40-
@test rqa_params["DET"] == 1.0
41-
@test rqa_params["L"] == 33/20
42-
@test rqa_params["Lmax"] == 5
43-
@test rqa_params["DIV"] == 0.2
44-
@test rqa_params["ENTR"] 0.996 atol=0.001
45-
@test rqa_params["TREND"] -0.0338 atol=0.0001
46-
@test rqa_params["LAM"] == 1.0
47-
@test rqa_params["TT"] == 33/23
48-
@test rqa_params["Vmax"] == 3
49-
@test rqa_params["MRT"] == 33/12
50-
@test rqa_params["RTE"] 1.286 atol=0.001
51-
@test rqa_params["NMPRT"] == 4
39+
@test rqa_params[:RR] == 33/110
40+
@test rqa_params[:DET] == 1.0
41+
@test rqa_params[:L] == 33/20
42+
@test rqa_params[:Lmax] == 5
43+
@test rqa_params[:DIV] == 0.2
44+
@test rqa_params[:ENTR] 0.996 atol=0.001
45+
@test rqa_params[:TREND] -0.0338 atol=0.0001
46+
@test rqa_params[:LAM] == 1.0
47+
@test rqa_params[:TT] == 33/23
48+
@test rqa_params[:Vmax] == 3
49+
@test rqa_params[:MRT] == 33/12
50+
@test rqa_params[:RTE] 1.286 atol=0.001
51+
@test rqa_params[:NMPRT] == 4
5252
end
5353
@testset "With Theiler window" begin
5454
histograms = recurrencestructures(rmat, theiler=2)
@@ -59,19 +59,19 @@ rmat = CrossRecurrenceMatrix(sparse(i,j,trues(length(i))))
5959
@test histograms[k] == true_histograms[k]
6060
end
6161
rqa_params = rqa(rmat, theiler=2, lmin=1, border=1)
62-
@test rqa_params["RR"] == 22/110
63-
@test rqa_params["DET"] == 1.0
64-
@test rqa_params["L"] == 22/14
65-
@test rqa_params["Lmax"] == 5
66-
@test rqa_params["DIV"] == 0.2
67-
@test rqa_params["ENTR"] 0.830 atol=0.001
68-
@test rqa_params["TREND"] -0.0122 atol=0.0001
69-
@test rqa_params["LAM"] == 1.0
70-
@test rqa_params["TT"] == 22/15
71-
@test rqa_params["Vmax"] == 3
72-
@test rqa_params["MRT"] == 24/6
73-
@test rqa_params["RTE"] 1.011 atol=0.001
74-
@test rqa_params["NMPRT"] == 3
62+
@test rqa_params[:RR] == 22/110
63+
@test rqa_params[:DET] == 1.0
64+
@test rqa_params[:L] == 22/14
65+
@test rqa_params[:Lmax] == 5
66+
@test rqa_params[:DIV] == 0.2
67+
@test rqa_params[:ENTR] 0.830 atol=0.001
68+
@test rqa_params[:TREND] -0.0122 atol=0.0001
69+
@test rqa_params[:LAM] == 1.0
70+
@test rqa_params[:TT] == 22/15
71+
@test rqa_params[:Vmax] == 3
72+
@test rqa_params[:MRT] == 24/6
73+
@test rqa_params[:RTE] 1.011 atol=0.001
74+
@test rqa_params[:NMPRT] == 3
7575
end
7676
@testset "With minimum line" begin
7777
histograms = recurrencestructures(rmat, lmin=2)
@@ -82,19 +82,19 @@ rmat = CrossRecurrenceMatrix(sparse(i,j,trues(length(i))))
8282
@test histograms[k] == true_histograms[k]
8383
end
8484
rqa_params = rqa(rmat, theiler=0, lmin=2, border=1)
85-
@test rqa_params["RR"] == 33/110
86-
@test rqa_params["DET"] == 22/33
87-
@test rqa_params["L"] == 22/9
88-
@test rqa_params["Lmax"] == 5
89-
@test rqa_params["DIV"] == 0.2
90-
@test rqa_params["ENTR"] 0.684 atol=0.001
91-
@test rqa_params["TREND"] -0.0338 atol=0.0001
92-
@test rqa_params["LAM"] == 18/33
93-
@test rqa_params["TT"] == 18/8
94-
@test rqa_params["Vmax"] == 3
95-
@test rqa_params["MRT"] == 33/12
96-
@test rqa_params["RTE"] 1.286 atol=0.001
97-
@test rqa_params["NMPRT"] == 4
85+
@test rqa_params[:RR] == 33/110
86+
@test rqa_params[:DET] == 22/33
87+
@test rqa_params[:L] == 22/9
88+
@test rqa_params[:Lmax] == 5
89+
@test rqa_params[:DIV] == 0.2
90+
@test rqa_params[:ENTR] 0.684 atol=0.001
91+
@test rqa_params[:TREND] -0.0338 atol=0.0001
92+
@test rqa_params[:LAM] == 18/33
93+
@test rqa_params[:TT] == 18/8
94+
@test rqa_params[:Vmax] == 3
95+
@test rqa_params[:MRT] == 33/12
96+
@test rqa_params[:RTE] 1.286 atol=0.001
97+
@test rqa_params[:NMPRT] == 4
9898
end
9999
@testset "Theiler and minimum line" begin
100100
histograms = recurrencestructures(rmat, theiler=2, lmin=2)
@@ -105,20 +105,18 @@ rmat = CrossRecurrenceMatrix(sparse(i,j,trues(length(i))))
105105
@test histograms[k] == true_histograms[k]
106106
end
107107
rqa_params = rqa(rmat, theiler=2, lmin=2, border=1)
108-
@test rqa_params["RR"] == 22/110
109-
@test rqa_params["DET"] == 13/22
110-
@test rqa_params["L"] == 13/5
111-
@test rqa_params["Lmax"] == 5
112-
@test rqa_params["DIV"] == 0.2
113-
@test rqa_params["ENTR"] 0.500 atol=0.001
114-
@test rqa_params["TREND"] -0.0122 atol=0.0001
115-
@test rqa_params["LAM"] == 12/22
116-
@test rqa_params["TT"] == 12/5
117-
@test rqa_params["Vmax"] == 3
118-
@test rqa_params["MRT"] == 24/6
119-
@test rqa_params["RTE"] 1.011 atol=0.001
120-
@test rqa_params["NMPRT"] == 3
108+
@test rqa_params[:RR] == 22/110
109+
@test rqa_params[:DET] == 13/22
110+
@test rqa_params[:L] == 13/5
111+
@test rqa_params[:Lmax] == 5
112+
@test rqa_params[:DIV] == 0.2
113+
@test rqa_params[:ENTR] 0.500 atol=0.001
114+
@test rqa_params[:TREND] -0.0122 atol=0.0001
115+
@test rqa_params[:LAM] == 12/22
116+
@test rqa_params[:TT] == 12/5
117+
@test rqa_params[:Vmax] == 3
118+
@test rqa_params[:MRT] == 24/6
119+
@test rqa_params[:RTE] 1.011 atol=0.001
120+
@test rqa_params[:NMPRT] == 3
121121
end
122122
end
123-
124-

0 commit comments

Comments
 (0)