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

Implement bounds and intervalbounds for Center locus with DateTime #846

Merged
merged 3 commits into from
Nov 5, 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
27 changes: 19 additions & 8 deletions src/Lookups/lookup_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,20 @@ _bounds(::ReverseOrdered, ::Intervals, span::Explicit, ::AbstractSampled) =
(val(span)[1, end], val(span)[2, 1])
_bounds(::Intervals, span::Regular, lookup::AbstractSampled) =
_bounds(locus(lookup), order(lookup), span, lookup)
_bounds(::Start, ::ForwardOrdered, span, lookup) = first(lookup), last(lookup) + step(span)
_bounds(::Start, ::ReverseOrdered, span, lookup) = last(lookup), first(lookup) - step(span)
_bounds(::Center, ::ForwardOrdered, span, lookup) =
first(lookup) - step(span) / 2, last(lookup) + step(span) / 2
_bounds(::Center, ::ReverseOrdered, span, lookup) =
last(lookup) + step(span) / 2, first(lookup) - step(span) / 2
_bounds(::End, ::ForwardOrdered, span, lookup) = first(lookup) - step(span), last(lookup)
_bounds(::End, ::ReverseOrdered, span, lookup) = last(lookup) + step(span), first(lookup)
_bounds(::Start, ::ForwardOrdered, span::Regular, lookup) = first(lookup), last(lookup) + step(span)
_bounds(::Start, ::ReverseOrdered, span::Regular, lookup) = last(lookup), first(lookup) - step(span)
function _bounds(::Center, order::Ordered, span::Regular, lookup)
bounds = first(lookup) - step(span) / 2, last(lookup) + step(span) / 2
return _maybeflipbounds(order, bounds)
end
# DateTime handling
function _bounds(::Center, order::Ordered, span::Regular, lookup::Lookup{<:Dates.AbstractTime})
f, l, s = first(lookup), last(lookup), step(span)
bounds = (f - (f - (f - s)) / 2, l - (l - (l + s)) / 2)
_maybeflipbounds(order, bounds)
end
_bounds(::End, ::ForwardOrdered, span::Regular, lookup) = first(lookup) - step(span), last(lookup)
_bounds(::End, ::ReverseOrdered, span::Regular, lookup) = last(lookup) + step(span), first(lookup)


const SAMPLED_ARGUMENTS_DOC = """
Expand Down Expand Up @@ -651,6 +657,11 @@ function intervalbounds(order::Ordered, locus::Center, span::Regular, l::Lookup,
bounds = (x - halfstep, x + halfstep)
return _maybeflipbounds(order, bounds)
end
function intervalbounds(order::Ordered, locus::Center, span::Regular, l::LookupArray{<:Dates.AbstractTime}, i::Int)
x = l[i]
bounds = (x - (x - step(span))) / 2 + x, (x - (x + step(span))) / 2 + x
return _maybeflipbounds(order, bounds)
end
# Irregular Center
function intervalbounds(order::ForwardOrdered, locus::Center, span::Irregular, l::Lookup, i::Int)
x = l[i]
Expand Down
13 changes: 13 additions & 0 deletions test/lookup.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DimensionalData, Test, Unitful
using Dates
using DimensionalData.Lookups, DimensionalData.Dimensions
using DimensionalData.Lookups: _slicespan, isrev, _bounds
using DimensionalData.Dimensions: _slicedims
Expand Down Expand Up @@ -112,6 +113,18 @@ end
@testset "bounds and intervalbounds" begin
@testset "Intervals" begin
@testset "Regular bounds are calculated from interval type and span value" begin
@testset "Forward Center DateTime" begin
ind = DateTime(2000):Month(1):DateTime(2000, 11)
dim = format(X(ind; sampling=Intervals(Center())))
@test bounds(dim) == (DateTime(1999, 12, 16, 12), DateTime(2000, 11, 16))
@test intervalbounds(dim, 3) == (DateTime(2000, 03, 15, 12), DateTime(2000, 02, 14, 12))
end
@testset "Reverse Center DateTime" begin
ind = DateTime(2000, 11):Month(-1):DateTime(2000, 1)
dim = format(X(ind; sampling=Intervals(Center())))
@test bounds(dim) == (DateTime(1999, 12, 16, 12), DateTime(2000, 11, 16))
@test intervalbounds(dim, 3) == (DateTime(2000, 09, 16, 12), DateTime(2000, 08, 17))
end
@testset "forward ind" begin
ind = 10.0:10.0:50.0
dim = X(Sampled(ind, order=ForwardOrdered(), sampling=Intervals(Start()), span=Regular(10.0)))
Expand Down
Loading