-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtransforms.jl
66 lines (52 loc) · 1.71 KB
/
transforms.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENCE in the project root.
# ------------------------------------------------------------------
# transforms in functional form
include("transforms/alr.jl")
include("transforms/clr.jl")
include("transforms/ilr.jl")
"""
LogRatio(kind, [refvar])
A log-ratio transform of given `kind`.
Optionally, specify the reference variable
`refvar` for the the ratios. Default to the
last column of the input table.
## Examples
Additive log-ratio transform with denominator `O₂`:
```julia
julia> LogRatio(:ilr, :O₂)
```
"""
struct LogRatio <: Transform
kind::Symbol
refv::Union{Symbol,Nothing}
function LogRatio(kind, refv)
@assert kind ∈ (:alr,:clr,:ilr) "invalid log-ratio transform"
new(kind, refv)
end
end
LogRatio(kind) = LogRatio(kind, nothing)
cardinality(::LogRatio) = ManyToMany()
function apply(table, t::LogRatio; inv=false)
vars = Tables.columnnames(table)
# permute columns of table if necessary
𝒯 = if (t.kind ∈ (:alr, :ilr) && !inv &&
!isnothing(t.refv) && t.refv != last(vars))
# sanity check with reference variable
@assert t.refv ∈ vars "invalid reference variable"
# permute columns of table
ovars = setdiff(vars, (t.refv,))
pvars = [ovars; t.refv]
TableOperations.select(table, pvars...)
else
# forward input table
table
end
# return appropriate transform
t.kind == :alr && !inv && return alr(𝒯)
t.kind == :alr && inv && return alrinv(𝒯)
t.kind == :clr && !inv && return clr(𝒯)
t.kind == :clr && inv && return clrinv(𝒯)
t.kind == :ilr && !inv && return ilr(𝒯)
t.kind == :ilr && inv && return ilrinv(𝒯)
end