在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:baggepinnen/DynamicAxisWarping.jl开源软件地址:https://github.com/baggepinnen/DynamicAxisWarping.jl开源编程语言:Julia 100.0%开源软件介绍:DynamicAxisWarping.jlDynamic Time Warping (DTW), matrix profile and related algorithms in Julia. This package supports arbitrary metrics and arbitrary "spaces", i.e., as long as you are passing a vector or higher dimensional array of something that your distance can operate on, you're good to go. Time is always considered to be the last dimension. This package is registered and can be installed with: using Pkg
pkg"add DynamicAxisWarping" Simple usageInputs of dimension larger than 1 will be treated as sequences where time is in the last dimension. When using higher-dimensional series, make sure the provided distance accepts them. Any distance implementing the Distances.jl interface works, as well as functions on the form using DynamicAxisWarping, Distances, Plots
cost, i1, i2 = dtw(a,b, [dist=SqEuclidean()]; transportcost = 1)
cost, i1, i2 = fastdtw(a,b, dist, radius) # note https://arxiv.org/abs/2003.11246
cost = dtw_cost(a, b, dist, radius) # Optimized method that only returns cost. Supports early stopping, see docstring. Can be made completely allocation free.
# dtw supports arbitrary upper and lower bound vectors constraining the warping path.
imin,imax = radiuslimits(5,20,20), plot([imin imax])
dtw(a, b, dist, imin, imax) # Cost equivalent to dtw_cost(a, b, dist, 5)
# The Distances.jl interface is supported
d = DTW(radius=5)
d(a,b) Plottingdtwplot(a, b, [dist=SqEuclidean()]; transportcost = 1)
matchplot(a, b, [dist=SqEuclidean()]) Example: using DynamicAxisWarping, Plots
fs = 70
t = range(0,stop=1,step=1/fs)
y0 = sin.(2pi .*t)
y1 = sin.(3pi .*t)
y = [y0;y1[2:end]] .+ 0.01 .* randn.()
q = [y0;y0[2:end]] .+ 0.01 .* randn.()
y[10:15] .+= 0.5
q[13:25] .+= 0.5
f1 = plot([q y])
f2 = dtwplot(q,y,lc=:green, lw=1)
f3 = matchplot(q,y,ds=3,separation=1)
plot(f1,f2,f3, legend=false, layout=3, grid=false) Find a short pattern in a long time seriesThe function using DynamicAxisWarping, Distances
radius = 5
a = sin.(0.1 .* (1:100)) .+ 0.1 .* randn.()
b = sin.(0.1 .* (1:100_000)) .+ 0.1 .* randn.()
res = dtwnn(a, b, SqEuclidean(), radius, saveall=false, bsf_multiplier=1) # takes < 0.1s # DynamicAxisWarping.DTWSearchResult(0.4625287975222824, 73452, (prune_end = 79108, prune_env = 0))
plot([a b[eachindex(a) .+ (res.loc-1)]])
Multi-threaded searchBelow is an example of how several long series using ThreadTools
const workspaces = map(1:Threads.nthreads()) do i
DTWWorkspace(q, dist, radius)
end
@time results = tmap(Y) do y
dtwnn(workspaces[Threads.threadid()], y, showprogress = false)
end
mincost, minind = findmin(results) # special method for Vector{DTWSearchResult} OptimizationsThe following optimizations are implemented.
a = sin.(0.1f0 .* (1:100)) .+ 0.1f0 .* randn.(Float32)
b = sin.(0.1f0 .* (1:1000_000)) .+ 0.1f0 .* randn.(Float32)
@btime dtwnn($a, $b, SqEuclidean(), 5, ZNormalizer, prune_endpoints = true, prune_envelope = true)
# 853.336 ms (25519 allocations: 5.00 MiB) Differentiable Soft-DTWThe Soft-DTW algorithm is provided through the function soft_dtw_cost(a, b, [SqEuclidean()]; γ = 1, transportcost = 1)
To differentiate w.r.t. the first argument, try using ReverseDiff
da = ReverseDiff.gradient(a->soft_dtw_cost(a,b; γ=1), a) Zygote.jl will not work due to the array-mutation limitation.
See also function The following example illustrates how to calculate a barycenter (generalized average) using Soft DTW and Optim.jl, the result is shown below, together with three instances of the input series Generalized DTWThe using LinearAlgebra
ts = range(0, stop=4π, length=128)
x = LinearInterpolation(sin.(ts) .+ 0.1 .* randn.())
y = LinearInterpolation(sin.(1.1 .* ts))
norm(x.(ts) - y.(ts)) # 1.7184237220575787
cost, ϕ, ψ = gdtw(x,y)
norm(x.(ϕ.(ts)) - y.(ψ.(ts))) # 0.9266090849096682
Clustering and barycenter averagingbarycenter = dba(vector_of_arrays)
result = dbaclust(data, nclust, DTW()) Note that Sparse distance matrixThe early termination and some of the stopping heuristics can be used to efficiently calculate a sparse distance matrix where only the k nearest neighbors are fully caluclated and stored. To this end, we have the function dists, inds = sparse_distmat(y::Vector{Vector}, k, dist, radius) Matrix ProfileThis package defines specialized methods for profile = matrix_profile(y, m, DTW(radius, [transportcost]))
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论