Skip to content

Commit

Permalink
operator support in aes function
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed Apr 7, 2024
1 parent 8a8c882 commit 11cfafa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
12 changes: 8 additions & 4 deletions src/aes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ function aes(args...; kwargs...)

for arg in args
if arg isa Pair
push!(col_transforms, arg)
push!(aes_args, arg[1])
@error "Calculated columns currently do not support positional aes specification."
else
push!(aes_args, Symbol(arg))
end
Expand All @@ -16,8 +15,13 @@ function aes(args...; kwargs...)

for (k, v) in d
if v isa Pair
push!(col_transforms, v)
push!(aes_kwargs, String(k) => Symbol(v[1]))
replace_v = Symbol[]
for (source_index, source_colname) in enumerate(v[1])
push!(aes_kwargs, String(k) * string(source_index) => Symbol(source_colname))
push!(replace_v, Symbol(String(k) * string(source_index)))
end
push!(col_transforms, Symbol(k) => replace_v => v[2])
push!(aes_kwargs, String(k) => :Calculated)
else
push!(aes_kwargs, String(k) => Symbol(v))
end
Expand Down
2 changes: 1 addition & 1 deletion src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function Makie.SpecApi.Axis(plot::GGPlot)
axis_options[plottable_data[aes].label_target] = plottable_data[aes].label_function(plottable_data[aes].raw)
end

# add the transformed data to list to eventually be passed to the plots kwargs
# add the transformed data to list to eventually be passed to the plot args/kwargs
merge!(given_aes, plottable_data)
end

Expand Down
13 changes: 3 additions & 10 deletions src/extract_aes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function make_aes_extractor(required_aes)
return function extract_aes(args, kwargs)
aes_dict = Dict{String, Symbol}()
args_dict = Dict{String, Any}()
transforms = nothing
transforms = Dict{Symbol, Pair{Vector{Symbol}, AesTransform}}()

for arg in args
if arg isa DataFrame
Expand All @@ -21,18 +21,11 @@ function make_aes_extractor(required_aes)
push!(aes_dict, required_aes[i] => pos_arg)
end
end
aes_dict = merge(aes_dict, arg.named)
transforms = arg.column_transformations
merge!(aes_dict, arg.named)
merge!(transforms, arg.column_transformations)
end
end

if !isnothing(transforms)
rev_aes_dict = Dict([v => k for (k, v) in aes_dict])
transforms = Dict([Symbol(rev_aes_dict[k]) => [Symbol(rev_aes_dict[k])] => v for (k, v) in transforms])
else
transforms = Dict{Symbol, Pair{Vector{Symbol}, AesTransform}}()
end

d = Dict(kwargs)
args_dict = merge(args_dict, Dict([String(key) => d[key] for key in keys(d)]))

Expand Down
44 changes: 42 additions & 2 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ end
# When called with a string or symbol, as would happen in an aes() call
# returns a dict of the same type used in column_transformations

(at::AesTransform)(sym::Symbol) = sym => at
(at::AesTransform)(str::String) = Symbol(str) => at
(at::AesTransform)(sym::Symbol) = [sym] => at
(at::AesTransform)(str::String) = [Symbol(str)] => at

# simplest one is as_is, which just gets a column
# exactly as it is in the DataFrame
Expand Down Expand Up @@ -169,6 +169,46 @@ 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.:-
#import Base.:/
#import Base.:*




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

Expand Down

0 comments on commit 11cfafa

Please sign in to comment.