Skip to content

Commit

Permalink
break out aes operators into separate file, fix mistakes in subtract …
Browse files Browse the repository at this point in the history
…and divide implementations
  • Loading branch information
rdboyes committed Apr 8, 2024
1 parent 828db2b commit 60aeb97
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TidierPlots"
uuid = "337ecbd1-5042-4e2a-ae6f-ca776f97570a"
authors = ["Randall Boyes <[email protected]> and contributors"]
version = "0.6.3"
version = "0.6.4"

[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Expand Down
1 change: 1 addition & 0 deletions src/TidierPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ include("structs.jl")

include("addplots.jl")
include("aes.jl")
include("aes_ops.jl")
include("draw.jl")
include("extract_aes.jl")
#include("facets.jl")
Expand Down
5 changes: 4 additions & 1 deletion src/aes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ macro aes(exprs...)
positional = Symbol[]
for aes_ex in exprs
if aes_ex isa Expr
if aes_ex.args[2] isa QuoteNode
if aes_ex.args[2] isa Expr


elseif aes_ex.args[2] isa QuoteNode
aes_dict[String(aes_ex.args[1])] = aes_ex.args[2].value
elseif aes_ex.args[2] isa String
aes_dict[String(aes_ex.args[1])] = Symbol(aes_ex.args[2])
Expand Down
134 changes: 134 additions & 0 deletions src/aes_ops.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import Base.:>>

Base.:>>(sym::Symbol, fn::Function) = aesthetics_function(fn)(sym)

# 'rithm'tic (dubious implementation)

import Base.:+

function add_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .+ data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_add_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .+ constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:+(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(add_cols_fn)
Base.:+(s1::Symbol, s2::Real) = [s1] => AesTransform(make_add_const_function(s2))
Base.:+(s2::Real, s1::Symbol) = [s1] => AesTransform(make_add_const_function(s2))

import Base.:-

function subtract_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .- data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_subtract_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = constant .- data[!, source[1]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:-(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(subtract_cols_fn)
Base.:-(s1::Symbol, s2::Real) = [s1] => AesTransform(make_add_const_function(-s2))
Base.:-(s2::Real, s1::Symbol) = [s1] => AesTransform(make_subtract_const_function(s2))

import Base.:*

function multiply_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_multiply_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:*(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(multiply_cols_fn)
Base.:*(s1::Symbol, s2::Real) = [s1] => AesTransform(make_multiply_const_function(s2))
Base.:*(s2::Real, s1::Symbol) = [s1] => AesTransform(make_multiply_const_function(s2))


import Base.:/

function divide_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] ./ data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_divide_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = constant ./ data[!, source[1]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:/(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(divide_cols_fn)
Base.:/(s1::Symbol, s2::Real) = [s1] => AesTransform(make_multiply_const_function(1/s2))
Base.:/(s2::Real, s1::Symbol) = [s1] => AesTransform(make_divide_const_function(s2))
134 changes: 0 additions & 134 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,140 +165,6 @@ function aesthetics_function(generic_fn::Function)
return AesTransform(aes_fn)
end

import Base.:>>

Base.:>>(sym::Symbol, fn::Function) = aesthetics_function(fn)(sym)

# 'rithm'tic (dubious implementation)

import Base.:+

function add_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .+ data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_add_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .+ constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:+(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(add_cols_fn)
Base.:+(s1::Symbol, s2::Real) = [s1] => AesTransform(make_add_const_function(s2))
Base.:+(s2::Real, s1::Symbol) = [s1] => AesTransform(make_add_const_function(s2))

import Base.:-

function subtract_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .- data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_subtract_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .- constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:-(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(subtract_cols_fn)
Base.:-(s1::Symbol, s2::Real) = [s1] => AesTransform(make_subtract_const_function(s2))
Base.:-(s2::Real, s1::Symbol) = [s1] => AesTransform(make_subtract_const_function(s2))

import Base.:/

function divide_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] ./ data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_divide_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] ./ constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:/(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(divide_cols_fn)
Base.:/(s1::Symbol, s2::Real) = [s1] => AesTransform(make_divide_const_function(s2))
Base.:/(s2::Real, s1::Symbol) = [s1] => AesTransform(make_divide_const_function(s2))

import Base.:*

function multiply_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_multiply_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:*(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(multiply_cols_fn)
Base.:*(s1::Symbol, s2::Real) = [s1] => AesTransform(make_multiply_const_function(s2))
Base.:*(s2::Real, s1::Symbol) = [s1] => AesTransform(make_multiply_const_function(s2))


# tweaks
# takes an existing PlottableData object and modifies the makie_function
Expand Down

0 comments on commit 60aeb97

Please sign in to comment.