|
| 1 | +import StatsBase: sample |
| 2 | + |
1 | 3 | # Model selection |
2 | 4 |
|
3 | 5 | # Taken from https://royalsocietypublishing.org/doi/pdf/10.1098/rspa.2017.0009 |
@@ -100,3 +102,72 @@ function optimal_shrinkage!(X::AbstractArray{T, 2}) where T <: Number |
100 | 102 | X .= U*Diagonal(S)*V' |
101 | 103 | return |
102 | 104 | end |
| 105 | + |
| 106 | + |
| 107 | +@inline function burst_sampling(x::AbstractArray, samplesize::Int64, bursts::Int64) |
| 108 | + @assert size(x)[end] >= samplesize*bursts "Length of data array too small for subsampling of size $size!" |
| 109 | + inds = sample(1:size(x)[end]-samplesize, bursts, replace = false) |
| 110 | + inds = sort(unique(vcat([collect(i:i+samplesize) for i in inds]...))) |
| 111 | + return resample(x, inds) |
| 112 | +end |
| 113 | + |
| 114 | + |
| 115 | +@inline function burst_sampling(x::AbstractArray, y::AbstractArray, samplesize::Int64, bursts::Int64) |
| 116 | + @assert size(x)[end] >= samplesize*bursts "Length of data array too small for subsampling of size $size!" |
| 117 | + @assert size(x)[end] == size(y)[end] |
| 118 | + inds = sample(1:size(x)[end]-samplesize, bursts, replace = false) |
| 119 | + inds = sort(unique(vcat([collect(i:i+samplesize) for i in inds]...))) |
| 120 | + return resample(x, inds), resample(y, inds) |
| 121 | +end |
| 122 | + |
| 123 | + |
| 124 | +@inline function burst_sampling(x::AbstractArray, t::AbstractVector, period::T, bursts::Int64) where T <: AbstractFloat |
| 125 | + @assert period > zero(typeof(period)) "Sampling period has to be positive." |
| 126 | + @assert size(x)[end] == size(t)[end] "Provide consistent data." |
| 127 | + @assert bursts >= 1 "Number of bursts has to be positive." |
| 128 | + @assert t[end]-t[1]>= period*bursts "Bursting impossible. Please provide more data or reduce bursts." |
| 129 | + t_ids = zero(eltype(t)) .<= t .- period .<= t[end] .- 2*period |
| 130 | + samplesize = Int64(floor(period/(t[end]-t[1])*length(t))) |
| 131 | + inds = sample(collect(1:length(t))[t_ids], bursts, replace = false) |
| 132 | + inds = sort(unique(vcat([collect(i:i+samplesize) for i in inds]...))) |
| 133 | + return resample(x, inds), resample(t, inds) |
| 134 | +end |
| 135 | + |
| 136 | + |
| 137 | +@inline function subsample(x::AbstractVector, frequency::Int64) |
| 138 | + @assert frequency > 1 |
| 139 | + return x[1:frequency:end] |
| 140 | +end |
| 141 | + |
| 142 | + |
| 143 | +@inline function subsample(x::AbstractArray, frequency::Int64) |
| 144 | + @assert frequency > 1 |
| 145 | + return x[:, 1:frequency:end] |
| 146 | +end |
| 147 | + |
| 148 | +@inline function subsample(x::AbstractArray, t::AbstractVector, period::T) where T <: AbstractFloat |
| 149 | + @assert period > zero(typeof(period)) "Sampling period has to be positive." |
| 150 | + @assert size(x)[end] == size(t)[end] "Provide consistent data." |
| 151 | + @assert t[end]-t[1]>= period "Subsampling impossible. Sampling period exceeds time window." |
| 152 | + idx = Int64[1] |
| 153 | + t_now = t[1] |
| 154 | + @inbounds for (i, t_current) in enumerate(t) |
| 155 | + if t_current - t_now >= period |
| 156 | + push!(idx, i) |
| 157 | + t_now = t_current |
| 158 | + end |
| 159 | + end |
| 160 | + return resample(x, idx), resample(t, idx) |
| 161 | +end |
| 162 | + |
| 163 | +@inline function resample(x::AbstractArray{T,1}, indx::AbstractArray{Int64}) where T <: Number |
| 164 | + @assert maximum(indx) <= length(x) |
| 165 | + @assert minimum(indx) >= 1 |
| 166 | + return x[indx] |
| 167 | +end |
| 168 | + |
| 169 | +@inline function resample(x::AbstractArray{T,2}, indx::AbstractArray{Int64}) where T <: Number |
| 170 | + @assert maximum(indx) <= size(x, 2) |
| 171 | + @assert minimum(indx) >= 1 |
| 172 | + return x[:, indx] |
| 173 | +end |
0 commit comments