Skip to content

Commit

Permalink
Merge pull request #63 from invenia/rf/axiskey-vectors
Browse files Browse the repository at this point in the history
Get impute working with AxisKeys.KeyedArrays
  • Loading branch information
rofinn authored Sep 22, 2020
2 parents 4a1df35 + a9da1df commit 76e9d8b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
matrix:
- julia_version: 1.0
- julia_version: 1.3
- julia_version: nightly

platform:
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ os:
- linux
- osx
julia:
- 1.0
# 1.0 should also work, but Pkg.test hit some chmod issues on 1.0 in docker containers
- 1.3
- nightly
notifications:
email: false
Expand Down
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ TableOperations = "ab02a1b2-a7df-11e8-156e-fb1833f50b87"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
Distances = "0.9"
AxisKeys = "0.1.5"
Distances = "0.8, 0.9"
IterTools = "1.2, 1.3"
Missings = "0.4"
NearestNeighbors = "0.4"
Expand All @@ -27,6 +28,7 @@ julia = "1"

[extras]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -35,4 +37,4 @@ RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AxisArrays", "Combinatorics", "DataFrames", "Dates", "Distances", "RDatasets", "Test"]
test = ["AxisArrays", "AxisKeys", "Combinatorics", "DataFrames", "Dates", "Distances", "RDatasets", "Test"]
17 changes: 9 additions & 8 deletions src/imputors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,8 @@ function impute(data, imp::Imputor; kwargs...)
return impute!(deepcopy(data), imp; kwargs...)
end

# Wrapper method intended to handle ambiguities between vector and row tables.
function impute!(data::AbstractVector, imp::Imputor)
if istable(data)
return materializer(data)(impute!(Tables.columns(data), imp))
else
return _impute!(data, imp)
end
end
# Generic fallback for methods that have only defined _impute(v, imp; kwargs...)
impute!(data::AbstractVector, imp::Imputor; kwargs...) = _impute!(data, imp; kwargs...)

"""
impute!(data::AbstractMatrix, imp::Imputor; kwargs...)
Expand Down Expand Up @@ -175,6 +169,13 @@ function impute!(table, imp::Imputor)
return table
end

# Special case row tables
# NOTE: This may introduce ambiguities for specific imputors that have defined a
# `impute!(data, imp)`` method
function impute!(data::Vector{<:NamedTuple}, imp::Imputor)
return materializer(data)(impute!(Tables.columns(data), imp))
end

for file in ("drop.jl", "locf.jl", "nocb.jl", "interp.jl", "fill.jl", "chain.jl", "srs.jl", "svd.jl", "knn.jl")
include(joinpath("imputors", file))
end
15 changes: 8 additions & 7 deletions src/imputors/drop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function impute!(data::Vector, imp::DropObs)
imp.context(c -> filter!(x -> !ismissing!(c, x), data))
end

function impute!(data::Vector{<:NamedTuple}, imp::DropObs)
return materializer(data)(impute(Tables.columns(data), imp))
end

function impute(data::AbstractVector, imp::DropObs)
imp.context(c -> filter(x -> !ismissing!(c, x), data))
end
Expand Down Expand Up @@ -95,6 +99,10 @@ end
# TODO: Switch to using Base.@kwdef on 1.1
DropVars(; context=Context()) = DropVars(context)

function impute!(data::Vector{<:NamedTuple}, imp::DropVars)
return materializer(data)(impute(Tables.columns(data), imp))
end

function impute(data::AbstractMatrix, imp::DropVars; dims=1)
imp.context() do c
return filtervars(data; dims=dims) do vars
Expand All @@ -121,10 +129,3 @@ end
# Add impute! methods to override the default behaviour in imputors.jl
impute!(data::AbstractMatrix, imp::Union{DropObs, DropVars}) = impute(data, imp)
impute!(data, imp::Union{DropObs, DropVars}) = impute(data, imp)
function impute!(data::AbstractVector, imp::Union{DropObs, DropVars})
if istable(data)
return materializer(data)(impute(Tables.columns(data), imp))
else
throw(MethodError(impute!, (data, imp)))
end
end
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AxisArrays
using AxisKeys
using Combinatorics
using DataFrames
using Dates
Expand Down Expand Up @@ -488,6 +489,15 @@ end
# Confirm that we don't have any more missing values
@test all(!ismissing, result)
end

@testset "KeyedArray" begin
data = KeyedArray(Matrix(orig); row=1:size(orig, 1), V=names(orig))
result = Impute.interp(data; context=ctx) |> Impute.locf!() |> Impute.nocb!()

@test size(result) == size(data)
# Confirm that we don't have any more missing values
@test all(!ismissing, result)
end
end

@testset "Alternate missing functions" begin
Expand Down

2 comments on commit 76e9d8b

@rofinn
Copy link
Member Author

@rofinn rofinn commented on 76e9d8b Sep 22, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/21793

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 76e9d8b4e067c03d6f765cc3cdb01c4d634da07d
git push origin v0.5.1

Please sign in to comment.