Skip to content

Commit 0d9bc24

Browse files
authored
Bug fix in UniqueElementsEncoding (#418)
* Use `vec` so that we can use statespacesets too + outer constructor * Dot notation doesn't work? Construct vector directly * Up version * Use vec also in outcome space * Add tests * correct import * fix tests
1 parent 895ec61 commit 0d9bc24

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "ComplexityMeasures"
22
uuid = "ab4b797d-85ee-42ba-b621-05d793b346a2"
33
authors = "Kristian Agasøster Haaga <[email protected]>, George Datseries <[email protected]>"
44
repo = "https://github.com/juliadynamics/ComplexityMeasures.jl.git"
5-
version = "3.6.4"
5+
version = "3.6.5"
66

77
[deps]
88
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"

src/encoding_implementations/unique_elements_encoding.jl

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ encode.(Ref(e), x) == [1, 2, 3, 2, 3, 1] # true
2525
struct UniqueElementsEncoding{T, I <: Integer} <: Encoding
2626
encode_dict::Dict{T, I}
2727
decode_dict::Dict{I, T}
28-
function UniqueElementsEncoding(x)
29-
30-
# Ecode in order of first appearance, because `sort` doesn't work if we mix types,
31-
# e.g. `String` and `Int`.
32-
x_unique = unique(x)
33-
encode_dict = Dict{eltype(x), Int}()
34-
decode_dict = Dict{Int, eltype(x)}()
35-
for (i, xu) in enumerate(x_unique)
36-
encode_dict[xu] = i
37-
decode_dict[i] = xu
38-
end
39-
new{eltype(x), Int}(encode_dict, decode_dict)
28+
end
29+
function UniqueElementsEncoding(x)
30+
# Encode in order of first appearance, because `sort` doesn't work if we mix types,
31+
# e.g. `String` and `Int`.
32+
x_unique = unique(vec(x))
33+
T = eltype(x_unique)
34+
encode_dict = Dict{T, Int}()
35+
decode_dict = Dict{Int, T}()
36+
for (i, xu) in enumerate(x_unique)
37+
encode_dict[xu] = i
38+
decode_dict[i] = xu
4039
end
40+
return UniqueElementsEncoding(encode_dict, decode_dict)
4141
end
42+
4243
function UniqueElementsEncoding()
4344
throw(ArgumentError("`UniqueElementsEncoding` can't be initialized without input data."))
4445
end
@@ -47,6 +48,6 @@ function encode(encoding::UniqueElementsEncoding, x)
4748
return encoding.encode_dict[x]
4849
end
4950

50-
function decode(encoding::UniqueElementsEncoding, ω::Integer)
51+
function decode(encoding::UniqueElementsEncoding, ω::I) where I <: Integer
5152
return encoding.decode_dict[ω]
5253
end

src/outcome_spaces/unique_elements.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ probabilities(x) = probabilities(UniqueElements(), x)
3535
outcome_space(::UniqueElements, x) = sort!(unique(x))
3636

3737
function codify(o::UniqueElements, x)
38-
encoding = UniqueElementsEncoding(x)
39-
encode.(Ref(encoding), x)
38+
xv = vec(x)
39+
encoding = UniqueElementsEncoding(xv)
40+
encode.(Ref(encoding), xv)
4041
end
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
using Test
22
using ComplexityMeasures
3+
using StateSpaceSets
34

4-
x = ['a', 2, 5, 2, 5, 'a']
5-
e = UniqueElementsEncoding(x)
5+
x = ['a', 2, 5, 2, 5, 'a']; e = UniqueElementsEncoding(x)
66
@test encode.(Ref(e), x) == [1, 2, 3, 2, 3, 1]
7+
8+
y = ["a", "b", "c", "b", "a"]; ey = UniqueElementsEncoding(y)
9+
@test encode.(Ref(ey), y) == [1, 2, 3, 2, 1]
10+
11+
z = vec(StateSpaceSet(y)); ez = UniqueElementsEncoding(z)
12+
@test encode.(Ref(ez), z) == [1, 2, 3, 2, 1]
13+
14+
# TODO: this should really work (but broadcasting fails). The error is not in this package,
15+
# but is due to lacking broadcasting implementation in StateSpaceSets.jl
16+
# w = StateSpaceSet(y); ew = UniqueElementsEncoding(w)
17+
# @test encode.(Ref(ew), w) == [1, 2, 3, 2, 1]

test/outcome_spaces/implementations/unique_elements.jl

+3
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ end
3838

3939
# Codification of vector inputs (time series)
4040
x = [1, 3, 2, 1, 2, 2, 1, 3, 1]
41+
y = StateSpaceSet(["a", "b", "c", "b", "a"])
42+
4143
@test codify(UniqueElements(), x) isa Vector{Int}
44+
@test codify(UniqueElements(), y) isa Vector{Int}

0 commit comments

Comments
 (0)