@@ -23,56 +23,66 @@ assertions(::LogRatio) = [scitypeassert(Continuous)]
23
23
24
24
function applyfeat (transform:: LogRatio , feat, prep)
25
25
cols = Tables. columns (feat)
26
- onames = Tables. columnnames (cols)
27
- varnames = collect (onames)
26
+ names = Tables. columnnames (cols)
27
+ vars = collect (names)
28
+
29
+ # perform closure for full revertibility
30
+ cfeat, ccache = apply (Closure (), feat)
28
31
29
32
# reference variable
30
- rvar = refvar (transform, varnames)
31
- _assert (rvar ∈ varnames, " invalid reference variable" )
32
- rind = findfirst (== (rvar), varnames)
33
+ rvar = refvar (transform, vars)
34
+ _assert (rvar ∈ vars, " invalid reference variable" )
35
+
36
+ # reference index
37
+ rind = findfirst (== (rvar), vars)
33
38
34
39
# permute columns if necessary
35
- perm = rind ≠ lastindex (varnames )
40
+ perm = rind ≠ lastindex (vars )
36
41
pfeat = if perm
37
- popat! (varnames , rind)
38
- push! (varnames , rvar)
39
- feat |> Select (varnames )
42
+ popat! (vars , rind)
43
+ push! (vars , rvar)
44
+ cfeat |> Select (vars )
40
45
else
41
- feat
46
+ cfeat
42
47
end
43
48
44
49
# apply transform
45
50
X = Tables. matrix (pfeat)
46
51
Y = applymatrix (transform, X)
47
52
48
53
# new variable names
49
- newnames = newvars (transform, varnames )
54
+ newnames = newvars (transform, vars )
50
55
51
56
# return same table type
52
57
𝒯 = (; zip (newnames, eachcol (Y))... )
53
58
newfeat = 𝒯 |> Tables. materializer (feat)
54
59
55
- newfeat, (rind , perm, onames )
60
+ newfeat, (ccache , perm, rind, vars )
56
61
end
57
62
58
63
function revertfeat (transform:: LogRatio , newfeat, fcache)
64
+ # retrieve cache
65
+ ccache, perm, rind, vars = fcache
66
+
59
67
# revert transform
60
68
Y = Tables. matrix (newfeat)
61
69
X = revertmatrix (transform, Y)
62
-
63
- # retrieve cache
64
- rind, perm, onames = fcache
70
+ pfeat = (; zip (vars, eachcol (X))... )
65
71
66
72
# revert the permutation if necessary
67
- if perm
68
- n = length (onames )
73
+ cfeat = if perm
74
+ n = length (vars )
69
75
inds = collect (1 : (n - 1 ))
70
76
insert! (inds, rind, n)
71
- X = X[:, inds]
77
+ pfeat |> Select (inds)
78
+ else
79
+ pfeat
72
80
end
73
81
82
+ # revert closure for full revertibility
83
+ 𝒯 = revert (Closure (), cfeat, ccache)
84
+
74
85
# return same table type
75
- 𝒯 = (; zip (onames, eachcol (X))... )
76
86
𝒯 |> Tables. materializer (newfeat)
77
87
end
78
88
0 commit comments