Skip to content

Commit b66be5f

Browse files
authored
Rename output columns in ProjectionPursuit and LogRatio transforms (#248)
1 parent efa1e29 commit b66be5f

File tree

9 files changed

+34
-45
lines changed

9 files changed

+34
-45
lines changed

src/transforms/logratio.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ abstract type LogRatio <: StatelessFeatureTransform end
1414
# log-ratio transform interface
1515
function refvar end
1616
function newvars end
17-
function oldvars end
1817
function applymatrix end
1918
function revertmatrix end
2019

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

2524
function applyfeat(transform::LogRatio, feat, prep)
2625
cols = Tables.columns(feat)
27-
names = Tables.columnnames(cols) |> collect
26+
onames = Tables.columnnames(cols)
27+
varnames = collect(onames)
2828

2929
# reference variable
30-
rvar = refvar(transform, names)
31-
_assert(rvar names, "invalid reference variable")
32-
rind = findfirst(==(rvar), names)
30+
rvar = refvar(transform, varnames)
31+
_assert(rvar varnames, "invalid reference variable")
32+
rind = findfirst(==(rvar), varnames)
3333

3434
# permute columns if necessary
35-
perm = rind lastindex(names)
35+
perm = rind lastindex(varnames)
3636
pfeat = if perm
37-
popat!(names, rind)
38-
push!(names, rvar)
39-
feat |> Select(names)
37+
popat!(varnames, rind)
38+
push!(varnames, rvar)
39+
feat |> Select(varnames)
4040
else
4141
feat
4242
end
@@ -46,35 +46,28 @@ function applyfeat(transform::LogRatio, feat, prep)
4646
Y = applymatrix(transform, X)
4747

4848
# new variable names
49-
newnames = newvars(transform, names)
49+
newnames = newvars(transform, varnames)
5050

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

55-
newfeat, (rvar, rind, perm)
55+
newfeat, (rind, perm, onames)
5656
end
5757

5858
function revertfeat(transform::LogRatio, newfeat, fcache)
59-
cols = Tables.columns(newfeat)
60-
names = Tables.columnnames(cols)
61-
6259
# revert transform
6360
Y = Tables.matrix(newfeat)
6461
X = revertmatrix(transform, Y)
6562

6663
# retrieve cache
67-
rvar, rind, perm = fcache
68-
69-
# original variable names
70-
onames = oldvars(transform, names, rvar)
64+
rind, perm, onames = fcache
7165

7266
# revert the permutation if necessary
7367
if perm
7468
n = length(onames)
7569
inds = collect(1:(n - 1))
7670
insert!(inds, rind, n)
77-
onames = onames[inds]
7871
X = X[:, inds]
7972
end
8073

src/transforms/logratio/alr.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ ALR() = ALR(nothing)
1818

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

21-
newvars(::ALR, names) = collect(names)[begin:(end - 1)]
22-
23-
oldvars(::ALR, names, rvar) = [collect(names); rvar]
21+
newvars(::ALR, names) = Symbol.(:ARL, 1:(length(names) - 1))
2422

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

src/transforms/logratio/clr.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ struct CLR <: LogRatio end
1111

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

14-
newvars(::CLR, names) = collect(names)
15-
16-
oldvars(::CLR, names, rvar) = collect(names)
14+
newvars(::CLR, names) = Symbol.(:CLR, 1:length(names))
1715

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

src/transforms/logratio/ilr.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ ILR() = ILR(nothing)
1818

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

21-
newvars(::ILR, names) = collect(names)[begin:(end - 1)]
22-
23-
oldvars(::ILR, names, rvar) = [collect(names); rvar]
21+
newvars(::ILR, names) = Symbol.(:ILR, 1:(length(names) - 1))
2422

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

src/transforms/projectionpursuit.jl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ end
153153

154154
sphering() = Quantile() EigenAnalysis(:VDV)
155155

156-
function applyfeat(transform::ProjectionPursuit, table, prep)
157-
# retrieve column names
158-
cols = Tables.columns(table)
159-
names = Tables.columnnames(cols)
156+
function applyfeat(transform::ProjectionPursuit, feat, prep)
157+
# original columns names
158+
cols = Tables.columns(feat)
159+
onames = Tables.columnnames(cols)
160160

161161
# preprocess the data to approximately spherical shape
162-
ptable, pcache = apply(sphering(), table)
162+
ptable, pcache = apply(sphering(), feat)
163163

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

186+
# new column names
187+
names = Symbol.(:PP, 1:size(Z, 2))
188+
186189
𝒯 = (; zip(names, eachcol(Z))...)
187-
newtable = 𝒯 |> Tables.materializer(table)
188-
newtable, (pcache, caches)
190+
newtable = 𝒯 |> Tables.materializer(feat)
191+
newtable, (pcache, caches, onames)
189192
end
190193

191-
function revertfeat(::ProjectionPursuit, newtable, fcache)
192-
# retrieve column names
193-
cols = Tables.columns(newtable)
194-
names = Tables.columnnames(cols)
195-
194+
function revertfeat(::ProjectionPursuit, newfeat, fcache)
196195
# caches to retrieve transform steps
197-
pcache, caches = fcache
196+
pcache, caches, onames = fcache
198197

199-
Z = Tables.matrix(newtable)
198+
Z = Tables.matrix(newfeat)
200199
for (Q, qcache) in reverse(caches)
201200
table = revert(Quantile(1), Tables.table(Z * Q), qcache)
202201
Z = Tables.matrix(table) * Q'
@@ -205,6 +204,6 @@ function revertfeat(::ProjectionPursuit, newtable, fcache)
205204
table = revert(sphering(), Tables.table(Z), pcache)
206205
Z = Tables.matrix(table)
207206

208-
𝒯 = (; zip(names, eachcol(Z))...)
209-
newtable = 𝒯 |> Tables.materializer(newtable)
207+
𝒯 = (; zip(onames, eachcol(Z))...)
208+
𝒯 |> Tables.materializer(newfeat)
210209
end

test/data/projectionpursuit-1.png

1.16 KB
Loading

test/data/projectionpursuit-3.png

1013 Bytes
Loading

test/transforms/logratio.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
T = ALR()
1212
n, c = apply(T, t)
13+
@test Tables.schema(n).names == (:ARL1, :ARL2)
1314
@test n == t |> ALR(:c)
1415
talr = revert(T, n, c)
1516
T = CLR()
1617
n, c = apply(T, t)
18+
@test Tables.schema(n).names == (:CLR1, :CLR2, :CLR3)
1719
tclr = revert(T, n, c)
1820
T = ILR()
1921
n, c = apply(T, t)
22+
@test Tables.schema(n).names == (:ILR1, :ILR2)
2023
@test n == t |> ILR(:c)
2124
tilr = revert(T, n, c)
2225
@test Tables.matrix(talr) Tables.matrix(tclr)

test/transforms/projectionpursuit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
T = ProjectionPursuit(rng=MersenneTwister(2))
1111
n, c = apply(T, t)
1212

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

1515
μ = mean(Tables.matrix(n), dims=1)
1616
Σ = cov(Tables.matrix(n))

0 commit comments

Comments
 (0)