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

add Remainder, still need the tests #27

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
69 changes: 63 additions & 6 deletions src/transforms/remainder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,75 @@ and returns a new table with an additional column containing the
remainder value `xₙ₊₁ = total .- (x₁ + x₂ + ⋯ + xₙ)` If the `total`
value is not specified, then default to the maximum sum across rows.
"""
struct Remainder <: Transform end
struct Remainder <: Transform
total::Union{Float64,Nothing}
end

Remainder(total=nothing) = Remainder(total)

isrevertible(::Type{Remainder}) = true

function apply(::Remainder, table)
# TODO
assertions(::Type{Remainder}) = [TT.assert_continuous]

function apply(transform::Remainder, table)
# basic checks
for assertion in assertions(transform)
assertion(table)
end

# design matrix
X = Tables.matrix(table)

# find total across rows
total = if !isnothing(transform.total)
transform.total
else
maximum(sum(X, dims=2))
end

# original column names
names = Tables.columnnames(table)

# create a column with the remainder of each row
T = total .- S
Z = hcat(X, T)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

# table with the new column
names = (names..., :remainder)
𝒯 = (; zip(names, eachcol(Z))...)

newtable = 𝒯 |> Tables.materializer(table)
newtable, total
juliohm marked this conversation as resolved.
Show resolved Hide resolved
end

function revert(::Remainder, newtable, cache)
# TODO
Reject(:remainder)(newtable)
juliohm marked this conversation as resolved.
Show resolved Hide resolved
end

function reapply(::Remainder, table, cache)
# TODO
function reapply(transform::Remainder, table, cache)
# basic checks
for assertion in assertions(transform)
assertion(table)
end

# retrieve total from cache
total = cache

# original column names
names = Tables.columnnames(table)

# table as matrix and get the sum acros dims 2
X = Tables.matrix(table)
S = sum(X, dims=2)

# create a column with the remainder of each row
T = total .- S
Z = hcat(X, T)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

# table with the new column
names = (names..., :remainder)
𝒯 = (; zip(names, eachcol(Z))...)

newtable = 𝒯 |> Tables.materializer(table)
newtable, total
end
13 changes: 13 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@
tilr = revert(ILR(:b), n, c)
@test n |> Tables.columnnames == (:a, :c)
@test tilr |> Tables.columnnames == (:a, :b, :c)

# Tests for Remainder
t = (a=rand(100), b=rand(100), c=rand(100))
n, c = apply(Remainder(), t)
trem = revert(Remainder(), n, c)
@test Tables.matrix(n)[:, 1:end-1] == Tables.matrix(t)
@test all(x -> 0 ≤ x ≤ c, Tables.matrix(n)[:, end])
@test n |> Tables.columnnames == (:a, :b, :c, :remainder)
@test trem |> Tables.columnnames == (:a, :b, :c)

t = (a=rand(100), b=rand(100), c=rand(100))
n, c = reapply(Remainder(), t, c)
@test all(x -> 0 ≤ x ≤ c, Tables.matrix(n)[:, end])
end