Skip to content

Commit

Permalink
Support dims Arrays using the HoD Tranform
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoleepp committed Feb 8, 2021
1 parent 8d74c29 commit 3d96dc7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 62 deletions.
22 changes: 7 additions & 15 deletions src/temporal.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
"""
CustomTransform <: Transform
HoD <: Transform
Apply the function passed in to CustomTransform as the transform.
Get the hour of day corresponding to the data.
"""
struct HoD <: Transform end

function _apply!(x, ::HoD; kwargs...)
x[:] = hour.(x)
return x
end

# struct HoW() <: Transform end
_apply(x, ::HoD) = hour.(x)

# function _apply!(x, ::HoD; kwargs...)
function apply(A::AbstractArray, t::HoD; dims=:, kwargs...)
dims == Colon() && return _apply(A, t; kwargs...)

# function hour_of_week(x)
# # TODO: this doesn't handle DST
# return dayofweek(x) * 24 + hour(x)
# end
# x[:] = hour_of_week.(x)
# return x
# end
return [_apply(x, t; kwargs...) for x in eachslice(A, dims=dims)]
end
171 changes: 124 additions & 47 deletions test/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,142 @@
x = collect(DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 5, 7, 9, 0))
# Expected result is an hour a day starting and ending on the 9th hour inclusive,
# with 126 full days in the middle
expected = [collect(9:23)..., repeat(collect(0:23), 126)..., collect(0:9)...]
expected = [9:23..., repeat(0:23, 126)..., 0:9...]

@test Transforms.apply!(x, hod) == expected
@test Transforms.apply(x, hod) == expected
@test hod(x) == expected

_x = copy(x)
Transforms.apply!(_x, hod)
@test _x == expected
# Test the tranform was not mutating
@test x != expected

@testset "StepRange" begin
x = DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 5, 7, 9, 0)
@test Transforms.apply(x, hod) == expected
@test hod(x) == expected
end

@testset "DST" begin
x = collect(
ZonedDateTime(2020, 3, 7, 9, 0, tz"America/New_York"):Hour(1):ZonedDateTime(2020, 3, 8, 9, 0, tz"America/New_York")
)
x = ZonedDateTime(2020, 3, 7, 9, 0, tz"America/New_York"):Hour(1):ZonedDateTime(2020, 3, 8, 9, 0, tz"America/New_York")

# expected result skips the DST transition hour of 2
expected = [collect(9:23)..., [0, 1]..., collect(3:9)...]
expected = [9:23..., 0, 1, 3:9...]

@test Transforms.apply!(x, hod) == expected
@test Transforms.apply(x, hod) == expected
@test hod(x) == expected
end
end

# @testset "Matrix" begin
# M = [1 2 3; 4 5 6]
# expected = [1 8 27; 64 125 216]
@testset "Matrix" begin
x = [
DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 3, 0);
DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 11, 0)
]
M = reshape(x, 3, 2)
expected = [1 9; 2 10; 3 11]

# @testset "dims = $d" for d in (Colon(), 1, 2)
# @test Transforms.apply(M, p; dims=d) == expected
# @test p(M; dims=d) == expected
@test Transforms.apply(M, hod) == expected
@test hod(M) == expected

# _M = copy(M)
# Transforms.apply!(_M, p; dims=d)
# @test _M == expected
# end
# end
# Test the tranform was not mutating
@test M != expected

@testset "dims = :" begin
d = Colon()
@test Transforms.apply(M, hod; dims=d) == expected
@test hod(M; dims=d) == expected

# Test the tranform was not mutating
@test M != expected
end

@testset "dims = 1" begin
d = 1
expected = [[1, 9], [2, 10], [3, 11]]
@test Transforms.apply(M, hod; dims=d) == expected
@test hod(M; dims=d) == expected

# Test the tranform was not mutating
@test M != expected
end

@testset "dims = 2" begin
d = 2
expected = [[1, 2, 3], [9, 10, 11]]
@test Transforms.apply(M, hod; dims=d) == expected
@test hod(M; dims=d) == expected

# Test the tranform was not mutating
@test M != expected
end
end

@testset "AxisArray" begin
x = [
DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 2, 0);
DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 10, 0);
DateTime(2020, 1, 1, 10, 0):Hour(1):DateTime(2020, 1, 1, 11, 0)
]
M = reshape(x, 2, 3)
A = AxisArray(M, foo=["a", "b"], bar=["x", "y", "z"])
expected = [1 9 10; 2 10 11]

@test Transforms.apply(A, hod) == expected
@test hod(A) == expected

@testset "dims = :" begin
d = Colon()
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end

@testset "dims = 1" begin
d = 1
expected = [[1, 9, 10], [2, 10, 11]]
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end

@testset "dims = 2" begin
d = 2
expected = [[1, 2], [9, 10], [10, 11]]
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end
end

@testset "AxisKey" begin
x = [
DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 2, 0);
DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 10, 0);
DateTime(2020, 1, 1, 10, 0):Hour(1):DateTime(2020, 1, 1, 11, 0)
]
M = reshape(x, 2, 3)
A = KeyedArray(M, foo=["a", "b"], bar=["x", "y", "z"])
expected = [1 9 10; 2 10 11]

@test Transforms.apply(A, hod) == expected
@test hod(A) == expected

@testset "dims = :" begin
d = Colon()
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end

@testset "dims = 1" begin
d = 1
expected = [[1, 9, 10], [2, 10, 11]]
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end

@testset "dims = 2" begin
d = 2
expected = [[1, 2], [9, 10], [10, 11]]
@test Transforms.apply(A, hod; dims=d) == expected
@test hod(A; dims=d) == expected
end
end

# @testset "NamedTuple" begin
# nt = (a = [1, 2, 3], b = [4, 5, 6])
Expand Down Expand Up @@ -69,32 +172,6 @@
# end
# end

# @testset "AxisArray" begin
# A = AxisArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"])
# expected = AxisArray([1 8 27; 64 125 216], foo=["a", "b"], bar=["x", "y", "z"])

# @testset "dims = $d" for d in (Colon(), 1, 2)
# transformed = Transforms.apply(A, p; dims=d)
# @test transformed isa AxisArray
# @test transformed == expected
# end

# end

# @testset "AxisKey" begin
# A = KeyedArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"])
# expected = KeyedArray([1 8 27; 64 125 216], foo=["a", "b"], bar=["x", "y", "z"])

# @testset "dims = $d" for d in (Colon(), :foo, :bar)
# transformed = Transforms.apply(A, p; dims=d)
# @test transformed isa KeyedArray
# @test transformed == expected
# end

# _A = copy(A)
# Transforms.apply!(_A, p)
# @test _A == expected
# end

# @testset "DataFrame" begin
# df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6])
Expand Down

0 comments on commit 3d96dc7

Please sign in to comment.