Skip to content

Commit

Permalink
Reducing the memory allocation of the significance testing (#54)
Browse files Browse the repository at this point in the history
* reducing mem alloc when computing pvals

* replace if-elseif by 2 if in accumulate_pvals!

* remove printing command

* remove init_pvals
  • Loading branch information
JanJereczek authored Oct 19, 2023
1 parent 6554dab commit 6832606
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
34 changes: 34 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,37 @@ Output on Jan's machine:

# Check 0 allocations
@btime inplace_map!(ar1_whitenoise, $var_x, $wv)


#################################################
# surrogates_loop!
#################################################

using TransitionsInTimeseries

n = 1001
t = collect(1.0:n)
x = copy(t)

indicators = (mean, var)
change_metrics = RidgeRegressionSlope()

config = SlidingWindowConfig(indicators, change_metrics;
width_ind = 100, stride_ind = 1,
width_cha = 100, stride_cha = 1, whichtime = last,
)

res = estimate_indicator_changes(config, x, t)
signif = SurrogatesSignificance(n = 100, tail = :both, p = 0.1)
flags = significant_transitions(res, signif)

@btime estimate_indicator_changes($config, $x, $t)
# 141.557 μs (4 allocations: 40.56 KiB)
@btime significant_transitions($res, $signif)
#=
Former version of the code: 5.178 ms (1954 allocations: 3.03 MiB)
Current version of the code: 4.863 ms (738 allocations: 2.96 MiB)
Although there is an improvement in the number of allocations, the performance
is not significantly improved.
=#
55 changes: 25 additions & 30 deletions src/significance/surrogates_significance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ function sliding_surrogates_loop!(
indicator_dummys, change_dummys,
width_ind, stride_ind, width_cha, stride_cha, tail
)
pval_right = zeros(length(pval))
pval_left = copy(pval_right)

if tail == :both
pval_right = zeros(length(pval))
pval_left = copy(pval_right)
end
# parallelized surrogate loop
Threads.@threads for _ in 1:n_surrogates
id = Threads.threadid()
Expand All @@ -159,30 +157,17 @@ function sliding_surrogates_loop!(
width = width_ind, stride = stride_ind)
windowmap!(chametric, change_dummy, indicator_dummys[id];
width = width_cha, stride = stride_cha)
if tail == :right
pval .+= c .< change_dummy
elseif tail == :left
pval .+= c .> change_dummy
elseif tail == :both
pval_right .+= c .< change_dummy
pval_left .+= c .> change_dummy
end
end
if tail == :both
pval .= 2min.(pval_right, pval_left)
accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy)
end
choose_pval!(pval, pval_right, pval_left, tail)
end

function segmented_surrogates_loop!(
indicator, chametric, c, pval, n_surrogates, sgens,
indicator_dummys, change_dummys, width_ind, stride_ind, tail
)

if tail == :both
pval_right = zeros(length(pval))
pval_left = copy(pval_right)
end

pval_right = zeros(length(pval))
pval_left = copy(pval_right)
# parallelized surrogate loop
Threads.@threads for _ in 1:n_surrogates
id = Threads.threadid()
Expand All @@ -191,16 +176,26 @@ function segmented_surrogates_loop!(
windowmap!(indicator, indicator_dummys[id], s;
width = width_ind, stride = stride_ind)
change_dummy = chametric(indicator_dummys[id])
if tail == :right
pval .+= c .< change_dummy
elseif tail == :left
pval .+= c .> change_dummy
elseif tail == :both
pval_right .+= c .< change_dummy
pval_left .+= c .> change_dummy
end
accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy)
end
choose_pval!(pval, pval_right, pval_left, tail)
end

function accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy)
if tail == :both || tail == :right
pval_right .+= c .< change_dummy
end
if tail == :both || tail == :left
pval_left .+= c .> change_dummy
end
end

function choose_pval!(pval, pval_right, pval_left, tail)
if tail == :both
pval .= 2min.(pval_right, pval_left)
elseif tail == :right
pval .= pval_right
elseif tail == :left
pval .= pval_left
end
end
end

0 comments on commit 6832606

Please sign in to comment.