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 support for strings in compose function #44

Merged
merged 3 commits into from
Nov 29, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ julia> table = (a=[1,2,3], b=[4,5,6], c=[7,8,9])
(a = [1, 2, 3], b = [4, 5, 6], c = [7, 8, 9])

julia> ctable = compose(table, (:a,:b))
(c = [7, 8, 9], coda = Composition{2, (:a, :b)}["1.000 : 4.000", "2.000 : 5.000", "3.000 : 6.000"])
(c = [7, 8, 9], CODA = Composition{2, (:a, :b)}[1.000 : 4.000, 2.000 : 5.000, 3.000 : 6.000])

julia> ctable.coda[1]
julia> ctable.CODA[1]
2-part composition
┌ ┐
a ┤■■■■■■■■■ 1.0
Expand Down
11 changes: 6 additions & 5 deletions src/codaarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,27 @@ Parts in compositional `array`.
parts(::CoDaArray{D,PARTS}) where {D,PARTS} = PARTS

"""
compose(table, colnames; keepcols=true, as=:coda)
compose(table, colnames; keepcols=true, as=:CODA)

Convert columns `colnames` of `table` into parts of a
composition and save the result in a [`CoDaArray`](@ref).
If `keepcols` is set to `true`, then save the result `as`
a column in a new table with all other columns preserved.
"""
function compose(table, colnames=Tables.columnnames(Tables.columns(table)); keepcols=true, as=:coda)
function compose(table, colnames=nothing; keepcols=true, as=:CODA)
cols = Tables.columns(table)
names = Tables.columnnames(cols)
scols = (nm => Tables.getcolumn(cols, nm) for nm in colnames)
snames = isnothing(colnames) ? names : Symbol.(colnames)
scols = (nm => Tables.getcolumn(cols, nm) for nm in snames)
# construct compositional array from selected columns
coda = (; scols...) |> CoDaArray

# different types of return
if keepcols
other = setdiff(names, colnames)
other = setdiff(names, snames)
ocols = (nm => Tables.getcolumn(cols, nm) for nm in other)
# preserve input table type
(; ocols..., as => coda) |> Tables.materializer(table)
(; ocols..., Symbol(as) => coda) |> Tables.materializer(table)
else
coda
end
Expand Down
8 changes: 6 additions & 2 deletions test/codaarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
@test_throws KeyError array.INVALID

table = compose(jura, (:Cd, :Cu, :Pb, :Co, :Cr, :Ni, :Zn))
@test Tables.columnnames(table) == [:X, :Y, :Rock, :Land, :coda]
@test Tables.getcolumn(table, :coda) == array
@test Tables.columnnames(table) == [:X, :Y, :Rock, :Land, :CODA]
@test Tables.getcolumn(table, :CODA) == array

table = compose(jura, (:Cd, :Cu, :Pb, :Co, :Cr, :Ni, :Zn), as=:comps)
@test Tables.columnnames(table) == [:X, :Y, :Rock, :Land, :comps]
@test Tables.getcolumn(table, :comps) == array

table = compose(jura, ("Cd", "Cu", "Pb", "Co", "Cr", "Ni", "Zn"), as="comps")
@test Tables.columnnames(table) == [:X, :Y, :Rock, :Land, :comps]
@test Tables.getcolumn(table, :comps) == array

# performance test
rng = MersenneTwister(2)
N = 100_000
Expand Down