Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix atol in At for Dates #834

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Lookups/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function at(
end
end
function at(
::Ordered, ::Span, lookup::Lookup{<:Union{Number,Dates.TimeType,AbstractString}}, selval, atol, rtol::Nothing;
::Ordered, ::Span, lookup::Lookup{<:Union{Number,Dates.AbstractTime,AbstractString}}, selval, atol, rtol::Nothing;
err=_True()
)
x = unwrap(selval)
Expand Down Expand Up @@ -244,6 +244,8 @@ function at(::Order, ::Span, lookup::Lookup, selval, atol, rtol::Nothing; err=_T
end

@inline _is_at(x, y, atol) = x == y
@inline _is_at(x::Dates.AbstractTime, y::Dates.AbstractTime, atol::Dates.Period) =
x >= y - atol && x <= y + atol
@inline _is_at(x::Real, y::Real, atol::Real) = abs(x - y) <= atol
@inline _is_at(x::Real, ys::AbstractArray, atol) = any(y -> _is_at(x, y, atol), ys)
@inline _is_at(xs::AbstractArray, y::Real, atol) = any(x -> _is_at(x, y, atol), xs)
Expand Down Expand Up @@ -349,8 +351,8 @@ end
_adjust_locus(locus::Center, v, lookup) = v
_adjust_locus(locus::Start, v, lookup) = v - abs(step(lookup)) / 2
_adjust_locus(locus::End, v, lookup) = v + abs(step(lookup)) / 2
_adjust_locus(locus::Start, v::Dates.TimeType, lookup) = v - (v - (v - abs(step(lookup)))) / 2
_adjust_locus(locus::End, v::Dates.TimeType, lookup) = v + (v + abs(step(lookup)) - v) / 2
_adjust_locus(locus::Start, v::Dates.AbstractTime, lookup) = v - (v - (v - abs(step(lookup)))) / 2
_adjust_locus(locus::End, v::Dates.AbstractTime, lookup) = v + (v + abs(step(lookup)) - v) / 2
_adjust_locus(locus::Start, v::Dates.Date, lookup) = v - (v - (v - abs(step(lookup)))) ÷ 2
_adjust_locus(locus::End, v::Dates.Date, lookup) = v + (v + abs(step(lookup)) - v) ÷ 2

Expand Down Expand Up @@ -1172,7 +1174,7 @@ _by(x::AbstractRange) = first(x)
_by(x::IntervalSets.Interval) = x.left
_by(x) = x

_in(needle::Dates.TimeType, haystack::Dates.TimeType) = needle == haystack
_in(needle::Dates.AbstractTime, haystack::Dates.AbstractTime) = needle == haystack
_in(needle, haystack) = needle in haystack
_in(needles::Tuple, haystacks::Tuple) = all(map(_in, needles, haystacks))
_in(needle::Interval, haystack::ClosedInterval) = needle.left in haystack && needle.right in haystack
Expand Down
17 changes: 5 additions & 12 deletions test/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ end
@test view(da, Between(40, 45), At((2:3)u"s")) isa DimArray{Int64,2}
end

@testset "with DateTime index" begin
@testset "with DateTime lookup" begin
@testset "Start locus" begin
timedim = Ti(Sampled(DateTime(2001):Month(1):DateTime(2001, 12);
span=Regular(Month(1)), sampling=Intervals(Start())
Expand All @@ -1190,16 +1190,9 @@ end
@test @inferred da[DateTime(2001, 4, 7) .. DateTime(2001, 8, 30)] == [5, 6, 7]
@test @inferred da[Date(2001, 4, 7) .. Date(2001, 8, 30)] == [5, 6, 7]

timedim = Ti(Sampled(Date(2001):Month(1):Date(2001, 12);
span=Regular(Month(1)), sampling=Intervals(Start())
))
da = DimArray(1:12, timedim)
@test @inferred da[Ti(At(DateTime(2001, 3)))] == 3
@test @inferred da[Ti(At(Date(2001, 3)))] == 3
@test @inferred da[Near(DateTime(2001, 4, 7))] == 4
@test @inferred da[Near(Date(2001, 4, 7))] == 4
@test @inferred da[DateTime(2001, 4, 7) .. DateTime(2001, 8, 30)] == [5, 6, 7]
@test @inferred da[Date(2001, 4, 7) .. Date(2001, 8, 30)] == [5, 6, 7]
@test_throws SelectorError da[Ti(At(Date(2001, 3, 4); atol=Day(2)))]
@test @inferred da[Ti(At(Date(2001, 3, 4); atol=Day(3)))] == 3
@test @inferred da[Ti(At(DateTime(2001, 3, 4); atol=Day(3)))] == 3
end
@testset "End locus" begin
timedim = Ti(Sampled(DateTime(2001):Month(1):DateTime(2001, 12);
Expand Down Expand Up @@ -1264,7 +1257,7 @@ end
@test @inferred view(da, Between((11, 26)), Between((1.4u"s", 4u"s"))) == [6 7]
end

@testset "with DateTime index" begin
@testset "with DateTime lookup" begin
span_ti = Explicit(vcat(
reshape((DateTime(2001, 1):Month(1):DateTime(2001, 12)), 1, 12),
reshape((DateTime(2001, 2):Month(1):DateTime(2002, 1)), 1, 12)
Expand Down
Loading