Skip to content

Commit

Permalink
Merge pull request #19 from invenia/rf/interp-fix
Browse files Browse the repository at this point in the history
Fix linear interp
  • Loading branch information
rofinn authored Jun 26, 2019
2 parents b7f371c + 4f21ce4 commit eb60a3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name = "Impute"
uuid = "f7bf1975-0170-51b9-8c5f-a992d46b9575"
authors = ["Invenia Technical Computing"]
version = "0.2.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[extras]
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["RDatasets", "Test"]
14 changes: 6 additions & 8 deletions src/imputors/interp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ function impute!(imp::Interpolate, ctx::Context, data::AbstractVector{<:Union{T,

diff = data[next_idx] - data[prev_idx]
incr = diff / T(gap_sz + 1)
start_val = data[prev_idx]
stop_val = data[next_idx]
val = data[prev_idx] + incr

values = Real(start_val):Real(incr):Real(stop_val)
# Iteratively fill in the values
for j in i:(next_idx - 1)
data[j] = val
val += incr
end

idx_range = prev_idx:(prev_idx + length(values) - 1)
# println(collect(idx_range))
# println(values)

data[idx_range] = values
i = next_idx
else
break
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ using Statistics
result = impute(a, :interp; limit=0.2)
@test result == collect(1.0:1.0:20)
@test result == interp(a)

# Test interpolation between identical points
b = ones(Union{Float64, Missing}, 20)
b[[2, 3, 7]] .= missing
@test interp(b) == ones(Union{Float64, Missing}, 20)

# Test interpolation at endpoints
b = ones(Union{Float64, Missing}, 20)
b[[1, 3, 20]] .= missing
result = interp(b)
@test ismissing(result[1])
@test ismissing(result[20])
end

@testset "Fill" begin
Expand Down

0 comments on commit eb60a3d

Please sign in to comment.