Skip to content

Commit

Permalink
Fix linear interp behaviour when interpolating between identical poin…
Browse files Browse the repository at this point in the history
…ts and test end interpolation behaviour.
  • Loading branch information
rofinn committed Jun 25, 2019
1 parent b7f371c commit 4f21ce4
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

2 comments on commit 4f21ce4

@rofinn
Copy link
Member Author

@rofinn rofinn commented on 4f21ce4 Jul 4, 2019

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/1797

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 4f21ce4473be02def6cfee43763b83871bf97d5f
git push origin v0.2.0

Please sign in to comment.