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

Rename output columns in ProjectionPursuit and LogRatio transforms #248

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 12 additions & 19 deletions src/transforms/logratio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ abstract type LogRatio <: StatelessFeatureTransform end
# log-ratio transform interface
function refvar end
function newvars end
function oldvars end
function applymatrix end
function revertmatrix end

Expand All @@ -24,19 +23,20 @@ assertions(::LogRatio) = [SciTypeAssertion{Continuous}()]

function applyfeat(transform::LogRatio, feat, prep)
cols = Tables.columns(feat)
names = Tables.columnnames(cols) |> collect
onames = Tables.columnnames(cols)
varnames = collect(onames)

# reference variable
rvar = refvar(transform, names)
_assert(rvar ∈ names, "invalid reference variable")
rind = findfirst(==(rvar), names)
rvar = refvar(transform, varnames)
_assert(rvar ∈ varnames, "invalid reference variable")
rind = findfirst(==(rvar), varnames)

# permute columns if necessary
perm = rind ≠ lastindex(names)
perm = rind ≠ lastindex(varnames)
pfeat = if perm
popat!(names, rind)
push!(names, rvar)
feat |> Select(names)
popat!(varnames, rind)
push!(varnames, rvar)
feat |> Select(varnames)
else
feat
end
Expand All @@ -46,35 +46,28 @@ function applyfeat(transform::LogRatio, feat, prep)
Y = applymatrix(transform, X)

# new variable names
newnames = newvars(transform, names)
newnames = newvars(transform, varnames)

# return same table type
𝒯 = (; zip(newnames, eachcol(Y))...)
newfeat = 𝒯 |> Tables.materializer(feat)

newfeat, (rvar, rind, perm)
newfeat, (rind, perm, onames)
end

function revertfeat(transform::LogRatio, newfeat, fcache)
cols = Tables.columns(newfeat)
names = Tables.columnnames(cols)

# revert transform
Y = Tables.matrix(newfeat)
X = revertmatrix(transform, Y)

# retrieve cache
rvar, rind, perm = fcache

# original variable names
onames = oldvars(transform, names, rvar)
rind, perm, onames = fcache

# revert the permutation if necessary
if perm
n = length(onames)
inds = collect(1:(n - 1))
insert!(inds, rind, n)
onames = onames[inds]
X = X[:, inds]
end

Expand Down
4 changes: 1 addition & 3 deletions src/transforms/logratio/alr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ ALR() = ALR(nothing)

refvar(transform::ALR, names) = isnothing(transform.refvar) ? last(names) : transform.refvar

newvars(::ALR, names) = collect(names)[begin:(end - 1)]

oldvars(::ALR, names, rvar) = [collect(names); rvar]
newvars(::ALR, names) = Symbol.(:ARL, 1:(length(names) - 1))

applymatrix(::ALR, X) = mapslices(alr ∘ Composition, X, dims=2)

Expand Down
4 changes: 1 addition & 3 deletions src/transforms/logratio/clr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ struct CLR <: LogRatio end

refvar(::CLR, names) = last(names)

newvars(::CLR, names) = collect(names)

oldvars(::CLR, names, rvar) = collect(names)
newvars(::CLR, names) = Symbol.(:CLR, 1:length(names))

applymatrix(::CLR, X) = mapslices(clr ∘ Composition, X, dims=2)

Expand Down
4 changes: 1 addition & 3 deletions src/transforms/logratio/ilr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ ILR() = ILR(nothing)

refvar(transform::ILR, names) = isnothing(transform.refvar) ? last(names) : transform.refvar

newvars(::ILR, names) = collect(names)[begin:(end - 1)]

oldvars(::ILR, names, rvar) = [collect(names); rvar]
newvars(::ILR, names) = Symbol.(:ILR, 1:(length(names) - 1))

applymatrix(::ILR, X) = mapslices(ilr ∘ Composition, X, dims=2)

Expand Down
31 changes: 15 additions & 16 deletions src/transforms/projectionpursuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ end

sphering() = Quantile() → EigenAnalysis(:VDV)

function applyfeat(transform::ProjectionPursuit, table, prep)
# retrieve column names
cols = Tables.columns(table)
names = Tables.columnnames(cols)
function applyfeat(transform::ProjectionPursuit, feat, prep)
# original columns names
cols = Tables.columns(feat)
onames = Tables.columnnames(cols)

# preprocess the data to approximately spherical shape
ptable, pcache = apply(sphering(), table)
ptable, pcache = apply(sphering(), feat)

# initialize scores and Gaussian quantiles
Z = Tables.matrix(ptable)
Expand All @@ -183,20 +183,19 @@ function applyfeat(transform::ProjectionPursuit, table, prep)
iter += 1
end

# new column names
names = Symbol.(:PP, 1:size(Z, 2))

𝒯 = (; zip(names, eachcol(Z))...)
newtable = 𝒯 |> Tables.materializer(table)
newtable, (pcache, caches)
newtable = 𝒯 |> Tables.materializer(feat)
newtable, (pcache, caches, onames)
end

function revertfeat(::ProjectionPursuit, newtable, fcache)
# retrieve column names
cols = Tables.columns(newtable)
names = Tables.columnnames(cols)

function revertfeat(::ProjectionPursuit, newfeat, fcache)
# caches to retrieve transform steps
pcache, caches = fcache
pcache, caches, onames = fcache

Z = Tables.matrix(newtable)
Z = Tables.matrix(newfeat)
for (Q, qcache) in reverse(caches)
table = revert(Quantile(1), Tables.table(Z * Q), qcache)
Z = Tables.matrix(table) * Q'
Expand All @@ -205,6 +204,6 @@ function revertfeat(::ProjectionPursuit, newtable, fcache)
table = revert(sphering(), Tables.table(Z), pcache)
Z = Tables.matrix(table)

𝒯 = (; zip(names, eachcol(Z))...)
newtable = 𝒯 |> Tables.materializer(newtable)
𝒯 = (; zip(onames, eachcol(Z))...)
𝒯 |> Tables.materializer(newfeat)
end
Binary file modified test/data/projectionpursuit-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/projectionpursuit-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/transforms/logratio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

T = ALR()
n, c = apply(T, t)
@test Tables.schema(n).names == (:ARL1, :ARL2)
@test n == t |> ALR(:c)
talr = revert(T, n, c)
T = CLR()
n, c = apply(T, t)
@test Tables.schema(n).names == (:CLR1, :CLR2, :CLR3)
tclr = revert(T, n, c)
T = ILR()
n, c = apply(T, t)
@test Tables.schema(n).names == (:ILR1, :ILR2)
@test n == t |> ILR(:c)
tilr = revert(T, n, c)
@test Tables.matrix(talr) ≈ Tables.matrix(tclr)
Expand Down
2 changes: 1 addition & 1 deletion test/transforms/projectionpursuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
T = ProjectionPursuit(rng=MersenneTwister(2))
n, c = apply(T, t)

@test Tables.columnnames(n) == (:a, :b, :c, :d)
@test Tables.columnnames(n) == (:PP1, :PP2, :PP3, :PP4)

μ = mean(Tables.matrix(n), dims=1)
Σ = cov(Tables.matrix(n))
Expand Down
Loading