From 96da09ea7ef41ce1a8a0ffe4b268a9b85c87f9f7 Mon Sep 17 00:00:00 2001 From: rafaqz Date: Mon, 4 Sep 2023 23:34:53 +0200 Subject: [PATCH 1/3] fix intervalbounds for datetime --- src/Lookups/lookup_arrays.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Lookups/lookup_arrays.jl b/src/Lookups/lookup_arrays.jl index 25340b132..83689b267 100644 --- a/src/Lookups/lookup_arrays.jl +++ b/src/Lookups/lookup_arrays.jl @@ -651,6 +651,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] From 6e2bd3de7b4d6a1544ad6dd816a3a12ae6a15187 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Tue, 5 Nov 2024 17:15:17 +0100 Subject: [PATCH 2/3] fix and test Center DateTime --- src/Lookups/lookup_arrays.jl | 21 +++++++++++++++------ test/lookup.jl | 13 +++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Lookups/lookup_arrays.jl b/src/Lookups/lookup_arrays.jl index 83689b267..f84def2b6 100644 --- a/src/Lookups/lookup_arrays.jl +++ b/src/Lookups/lookup_arrays.jl @@ -223,14 +223,23 @@ _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) = +_bounds(::Start, ::ForwardOrdered, span::Regular, lookup) = first(lookup), last(lookup) + step(span) +_bounds(::Start, ::ReverseOrdered, span::Regular, lookup) = last(lookup), first(lookup) - step(span) +_bounds(::Center, ::ForwardOrdered, span::Regular, lookup) = first(lookup) - step(span) / 2, last(lookup) + step(span) / 2 -_bounds(::Center, ::ReverseOrdered, span, lookup) = +_bounds(::Center, ::ReverseOrdered, span::Regular, 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) +# DateTime handling +function _bounds(::Center, ::ForwardOrdered, span::Regular, lookup::Lookup{<:Dates.AbstractTime}) + f, l, s = first(lookup), last(lookup), step(span) + (f - (f - (f - s)) / 2, l - (l - (l + s)) / 2) +end +function _bounds(::Center, ::ReverseOrdered, span::Regular, lookup::Lookup{<:Dates.AbstractTime}) + f, l, s = first(lookup), last(lookup), step(span) + (l - (l - (l + s)) / 2, f - (f - (f - s)) / 2) +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 = """ diff --git a/test/lookup.jl b/test/lookup.jl index 3fcde1d2c..18eb502e0 100644 --- a/test/lookup.jl +++ b/test/lookup.jl @@ -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 @@ -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))) From b879041defc7f685f8174cfb197842865ce63c65 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Tue, 5 Nov 2024 17:24:58 +0100 Subject: [PATCH 3/3] cleanup --- src/Lookups/lookup_arrays.jl | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Lookups/lookup_arrays.jl b/src/Lookups/lookup_arrays.jl index f84def2b6..2d7a0fa18 100644 --- a/src/Lookups/lookup_arrays.jl +++ b/src/Lookups/lookup_arrays.jl @@ -225,18 +225,15 @@ _bounds(::Intervals, span::Regular, lookup::AbstractSampled) = _bounds(locus(lookup), order(lookup), span, 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) -_bounds(::Center, ::ForwardOrdered, span::Regular, lookup) = - first(lookup) - step(span) / 2, last(lookup) + step(span) / 2 -_bounds(::Center, ::ReverseOrdered, span::Regular, lookup) = - last(lookup) + step(span) / 2, first(lookup) - step(span) / 2 -# DateTime handling -function _bounds(::Center, ::ForwardOrdered, span::Regular, lookup::Lookup{<:Dates.AbstractTime}) - f, l, s = first(lookup), last(lookup), step(span) - (f - (f - (f - s)) / 2, l - (l - (l + s)) / 2) +function _bounds(::Center, order::Ordered, span::Regular, lookup) + bounds = first(lookup) - step(span) / 2, last(lookup) + step(span) / 2 + return _maybeflipbounds(order, bounds) end -function _bounds(::Center, ::ReverseOrdered, span::Regular, lookup::Lookup{<:Dates.AbstractTime}) +# DateTime handling +function _bounds(::Center, order::Ordered, span::Regular, lookup::Lookup{<:Dates.AbstractTime}) f, l, s = first(lookup), last(lookup), step(span) - (l - (l - (l + s)) / 2, f - (f - (f - s)) / 2) + 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)