Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DBA to work with Julia 1.x #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ uuid = "c3fb160f-4a10-5553-b683-e707b00e83ce"
[deps]
BinDeps = "9e28174c-4ba2-5203-b857-d8d62c4213ee"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Expand All @@ -13,4 +14,4 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "≥ 0.7.0"
julia = "≥ 0.7.0"
18 changes: 10 additions & 8 deletions src/dba.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using LinearAlgebra

"""
DBAResult(cost,converged,iterations,cost_trace)

Expand All @@ -18,14 +20,14 @@ and the current estimate of the average sequence.

Example usage:

x = [1,2,2,3,3,4]
y = [1,3,4]
z = [1,2,2,4]
x = Sequence([1., 2., 2., 3., 3., 4.])
y = Sequence([1., 3., 4.])
z = Sequence([1., 2., 2., 4.])
avg,result = dba([x,y,z])
"""
function dba(
sequences::AbstractVector{T},
method::DTWMethod,
method::DTWMethod = ClassicDTW(),
dist::SemiMetric = SqEuclidean();
init_center::T = rand(sequences),
iterations::Int = 1000,
Expand All @@ -44,7 +46,7 @@ function dba(
dbavg = deepcopy(init_center)

# storage for each iteration
newavg = Sequence(zeros(length(dbavg)))
newavg = Sequence(zeros(size(dbavg.val)))
counts = zeros(Int, length(dbavg))

# variables storing optimization progress
Expand Down Expand Up @@ -111,13 +113,13 @@ function dba_iteration!(
total_cost = 0.0

# store stats for barycenter averages
scale!(counts,0)
scale!(newavg,0)
rmul!(counts,0)
rmul!(newavg,0)

# main ploop
for seq in sequences
# time warp signal versus average
# if one of the two is empty, use unconstrained window. If both are nonempty, but not the same lenght, distpath will throw error
# if one of the two is empty, use unconstrained window. If both are nonempty, but not the same length, distpath will throw error
if isempty(i2min) && isempty(i2max)
cost, i1, i2 = distpath(d, oldavg, seq)
else
Expand Down
12 changes: 2 additions & 10 deletions src/sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ end
:( x.val[@ntuple($N, (n-> n==$N ? i : Colon()))...] = val )
end

# convert a sequence back into an array
# in case sequences have different lenght, throw error
function seq_to_array(seq::AbstractVector{T}) where T <: Sequence
len_seq = length(seq[1])
arr = zeros(typeof(seq[1][1]),len_seq,length(seq))
for i=1:length(seq)
length(seq[i]) != len_seq ? error("Sequences do not have the same length, cannot construct array") : nothing
arr[:,i] = seq[i][:]
end
return arr
function seq_to_array(seq::Sequence{N}) where N
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had updated this function to work as part of the debugging/dev process. It's not central to the operation of the dba() function now, but I think using the cat() function is clearer in terms of exposing how the array is constructed from the Sequence.

return cat(seq..., dims=N)
end
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,11 @@ end
@test px == qx
@test py == qy
end

@testset "DBA" begin
x = Sequence([1., 2., 2., 3., 3., 4.])
y = Sequence([1., 3., 4.])
z = Sequence([1., 2., 2., 4.])
avg, _ = dba([x, y, z], init_center=z)
@test avg == [1.0, 1.75, 2.75, 4.0]
end