Skip to content

Commit

Permalink
Merge ed0dc69 into 57cccbe
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoleepp authored Feb 8, 2021
2 parents 57cccbe + ed0dc69 commit 67f1f63
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ authors = ["Invenia Technical Computing Corporation"]
version = "0.1.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
AxisArrays = "0.4"
AxisKeys = "0.1"
DataFrames = "0.22"
Tables = "1.3"
TimeZones = "1"
julia = "1.5"

[extras]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[targets]
test = ["AxisArrays", "AxisKeys", "DataFrames", "Test"]
test = ["AxisArrays", "AxisKeys", "DataFrames", "Test", "TimeZones"]
4 changes: 3 additions & 1 deletion src/Transforms.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module Transforms

using Dates
using Tables

export LinearCombination, Transform, Power
export HoD, LinearCombination, Transform, Power
export transform, transform!

include("utils.jl")
include("transformers.jl")
include("linear_combination.jl")
include("power.jl")
include("temporal.jl")

end
32 changes: 32 additions & 0 deletions src/temporal.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
HoD <: Transform
Get the hour of day corresponding to the data.
"""
struct HoD <: Transform end


_apply(x, ::HoD) = hour.(x)

function apply(A::AbstractArray, t::HoD; dims=:, inds=:, kwargs...)
if dims === Colon()
if inds === Colon()
return _apply(A, t; kwargs...)
else
return [_apply(A[ind], t; kwargs...) for ind in inds]
end
end

return [_apply(x[inds], t; kwargs...) for x in eachslice(A, dims=dims)]
end

function apply(table, t::HoD; cols=nothing, kwargs...)
Tables.istable(table) || throw(MethodError(apply!, (table, t)))

# Extract a columns iterator that we should be able to use to mutate the data.
# NOTE: Mutation is not guaranteed for all table types, but it avoid copying the data
columntable = Tables.columns(table)

cnames = cols === nothing ? propertynames(columntable) : cols
return [_apply(getproperty(columntable, cname), t; kwargs...) for cname in cnames]
end
6 changes: 2 additions & 4 deletions src/transformers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ Does not need to be extended unless a mutating [`Transform`](@ref) is not possib
function apply end

"""
apply!(A::AbstractArray{T}, ::Transform; dims=:, kwargs...) where T <: Real
apply!(A::AbstractArray, ::Transform; dims=:, kwargs...)
Applies the [`Transform`](@ref) to each element of `A`.
Optionally specify the `dims` to apply the [`Transform`](@ref) along certain dimensions.
"""
function apply!(
A::AbstractArray{T}, t::Transform; dims=:, kwargs...
) where T <: Real
function apply!(A::AbstractArray, t::Transform; dims=:, kwargs...)
dims == Colon() && return _apply!(A, t; kwargs...)

for x in eachslice(A; dims=dims)
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using AxisArrays
using AxisKeys
using DataFrames: DataFrame
using Dates
using Test
using TimeZones
using Transforms
using Transforms: _try_copy
using Test

@testset "Transforms.jl" begin
include("linear_combination.jl")
include("power.jl")
include("temporal.jl")
end
Loading

0 comments on commit 67f1f63

Please sign in to comment.