Skip to content

Commit

Permalink
fixed small bug for extracting categoricals from entries, added few t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
racinmat committed Jul 31, 2020
1 parent 457366b commit b33f39d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/extractors/extractcategorical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ExtractCategorical{V,I} <: AbstractExtractor
n::Int
end

ExtractCategorical(s::Entry) = ExtractCategorical(keys(s.counts))
ExtractCategorical(s::Entry) = ExtractCategorical(collect(keys(s.counts)))
ExtractCategorical(s::UnitRange) = ExtractCategorical(collect(s))
function ExtractCategorical(ks::Vector)
if isempty(ks)
Expand Down
22 changes: 22 additions & 0 deletions test/extractors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ end

@test e(["a", "b"]).data [1 0; 0 1; 0 0]
@test typeof(e(["a", "b"]).data) == Flux.OneHotMatrix{Array{Flux.OneHotVector,1}}

@test isnothing(ExtractCategorical([]))
e2 = ExtractCategorical(JsonGrinder.Entry(Dict("a"=>1,"c"=>1), 2))
@test e2("a").data[:] [1, 0, 0]
@test e2("c").data[:] [0, 1, 0]
@test e2("b").data[:] [0, 0, 1]
@test e2(nothing).data[:] [0, 0, 1]

@test catobs(e("a"), e("b")).data [1 0; 0 1; 0 0]
@test catobs(e("a").data, e("b").data) [1 0; 0 1; 0 0]
@test e(Dict(1=>2)).data[:] [0, 0, 1]
end

@testset "equals and hash test" begin
Expand Down Expand Up @@ -469,3 +480,14 @@ end
ext = JsonGrinder.extractscalar(AbstractString)
@test SparseMatrixCSC(ext(c).data) == SparseMatrixCSC(ext(e).data)
end

@testset "key as field" begin
j1 = JSON.parse("""{"a": 1}""")
j2 = JSON.parse("""{"b": 2.5}""")
j3 = JSON.parse("""{"c": 3.1}""")
j4 = JSON.parse("""{"d": 5}""")
j5 = JSON.parse("""{"e": 4.5}""")
j6 = JSON.parse("""{"f": 5}""")
sch = JsonGrinder.schema([j1, j2, j3, j4, j5, j6])
JsonGrinder.key_as_field(sch)
end

0 comments on commit b33f39d

Please sign in to comment.