Skip to content

Commit

Permalink
Add support for strings in compose function (#44)
Browse files Browse the repository at this point in the history
* Add support for strings in 'compose' function

* Apply suggestions

* Update README
  • Loading branch information
eliascarv authored Nov 29, 2023
1 parent b5ec0a4 commit af3ecb2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
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

0 comments on commit af3ecb2

Please sign in to comment.