Skip to content

Commit

Permalink
Merge pull request #25 from invenia/rf/vec-dates
Browse files Browse the repository at this point in the history
Allow passing vectors of dates
  • Loading branch information
rofinn authored Dec 7, 2022
2 parents 845b79f + 0dc394a commit f229f75
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DateSelectors"
uuid = "c900ad91-5c7c-41b8-bce1-46b0264fae1d"
authors = ["Invenia Technical Computing Corporation"]
version = "0.4.2"
version = "0.4.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
9 changes: 8 additions & 1 deletion src/NoneSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ Assign all dates to the validation set, select no holdout dates.
"""
struct NoneSelector <: DateSelector end

Iterators.partition(dates::StepRange{Date, Day}, ::NoneSelector) = _getdatesets(dates, Date[])
function Iterators.partition(dates::AbstractVector{Date}, ::NoneSelector)
# Just to maintain consistency between selectors
if dates isa StepRange && step(dates) != Day(1)
throw(ArgumentError("Expected step range over days, not ($(step(dates)))."))
end

return _getdatesets(dates, Date[])
end
7 changes: 5 additions & 2 deletions src/PeriodicSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ struct PeriodicSelector <: DateSelector
end
end

function Iterators.partition(dates::AbstractVector{Date}, s::PeriodicSelector)
if dates isa StepRange && step(dates) != Day(1)
throw(ArgumentError("Expected step range over days, not ($(step(dates)))."))
end

function Iterators.partition(dates::StepRange{Date, Day}, s::PeriodicSelector)
initial_time = _initial_date(s, dates)
sd, ed = extrema(dates)

Expand All @@ -43,7 +46,7 @@ function Iterators.partition(dates::StepRange{Date, Day}, s::PeriodicSelector)
# is still is well under 1/4 second. so keeping it simple

holdout_dates = Date[]
curr_window = initial_time:step(dates):(initial_time + s.stride - step(dates))
curr_window = initial_time:Day(1):(initial_time + s.stride - Day(1))
while first(curr_window) <= ed
# optimization: only creating holdout window if intersect not empty
if last(curr_window) >= sd
Expand Down
8 changes: 6 additions & 2 deletions src/RandomSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ struct RandomSelector <: DateSelector
end
end

function Iterators.partition(dates::StepRange{Date, Day}, s::RandomSelector)
function Iterators.partition(dates::AbstractVector{Date}, s::RandomSelector)
if dates isa StepRange && step(dates) != Day(1)
throw(ArgumentError("Expected step range over days, not ($(step(dates)))."))
end

sd, ed = extrema(dates)

rng = MersenneTwister(s.seed)

holdout_dates = Date[]
initial_time = _initial_date(s, dates)
curr_window = initial_time:step(dates):(initial_time + s.block_size - step(dates))
curr_window = initial_time:Day(1):(initial_time + s.block_size - Day(1))
while first(curr_window) <= ed
# Important: we must generate a random number for every block even before the start
# so that the `rng` state is updated constistently no matter when the start is
Expand Down
1 change: 1 addition & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract type DateSelector end
"""
partition(dates::AbstractInterval{Date}, s::DateSelector)
partition(dates::StepRange{Date, Day}, selector::DateSelector)
partition(dates::AbstractVector{Date}, s::DateSelector)
Partition the set of `dates` into disjoint `validation` and `holdout` sets according to the
`selector` and return a `NamedTuple({:validation, :holdout})` of iterators.
Expand Down
3 changes: 3 additions & 0 deletions test/NoneSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
validation, holdout = partition(date_range, selector)
@test validation == date_range
@test isempty(holdout)

# Test that we can also handle any abstract vector
@test first(partition(collect(date_range), selector)) == validation
end
3 changes: 3 additions & 0 deletions test/PeriodicSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
@test isempty(intersect(result...))

@test all(isequal(Week(1)), diff(result.holdout))

# Test that we can also handle any abstract vector
@test partition(collect(date_range), selector) == result
end

@testset "2 week period, 5 day stride" begin
Expand Down
3 changes: 3 additions & 0 deletions test/RandomSelector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
@test result.validation == result2.validation
@test result.holdout == result2.holdout

# Test that we can also handle any abstract vector
@test partition(collect(date_range), selector) == result

@testset "holdout fraction" begin
# Setting holdout_fraction 1 all days leaves the validation set empty
validation, holdout = partition(date_range, RandomSelector(42, 1))
Expand Down
6 changes: 3 additions & 3 deletions test/sensibility_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
end
end

@testset "Vector of days is not allowed" begin
@testset "Vector of days is allowed" begin
date_range = collect(Date(2019, 1, 1):Day(1):Date(2019, 2, 1))
@test_throws MethodError partition(date_range, NoneSelector())
@test first(partition(date_range, NoneSelector())) == date_range
end

@testset "Weekly intervals are not allowed" begin
Expand All @@ -46,7 +46,7 @@
PeriodicSelector(Week(2), Week(1)),
RandomSelector(42),
)
@test_throws MethodError partition(weekly_dates, selector)
@test_throws ArgumentError partition(weekly_dates, selector)
end
end
end

2 comments on commit f229f75

@rofinn
Copy link
Member Author

@rofinn rofinn commented on f229f75 Dec 7, 2022

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

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

git tag -a v0.4.3 -m "<description of version>" f229f7518cce27a17b3d8780090323790173c670
git push origin v0.4.3

Please sign in to comment.