-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow dimensions to be used as AlgebraOfGraphics selectors (#823)
* Allow dimensions to be used as AoG selectors * Add a test from the issue's usecase * Update plotrecipes.jl * Fix tests * Update plotrecipes.jl * Allow types and constructed dims Co-authored-by: Rafael Schouten <[email protected]> * Don't use `DD.DimOrDimType` since that's a union with Symbol why? * complex logic to intercept dims that are just the name of the array * Add a demo to the docs * DD.name on DimArray is a tuple * Add AoG to docs Project * Update docs * Update plotrecipes.jl * Doh, forgot to remove `return` * Don't accept dims that aren't in the dimarray * Update tests for the last commit's changes --------- Co-authored-by: Rafael Schouten <[email protected]>
- Loading branch information
1 parent
2599b0e
commit 7a5c883
Showing
5 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module DimensionalDataAlgebraOfGraphicsExt | ||
|
||
import AlgebraOfGraphics as AoG | ||
import DimensionalData as DD | ||
|
||
# We can't use `DD.Dimensions.DimOrDimType` because that's a union with Symbol, | ||
# which causes an ambiguity and would otherwise override the dedicated Symbol | ||
# `select` method in AoG. | ||
const DimOrType = Union{DD.Dimensions.Dimension, Type{<: DD.Dimensions.Dimension}} | ||
|
||
#= | ||
This extension allows DimensionalData `Dimension` types to be used | ||
as column selectors for AlgebraOfGraphics.jl. | ||
Specifically, this implements the `AoG.select` method for `Columns` | ||
objects, which is the type that `AlgebraOfGraphics.data` returns. | ||
=# | ||
|
||
# The generic selector, to enable this to work even in `DataFrame(dimarray)` | ||
function AoG.select(data::AoG.Columns, dim::DimOrType) | ||
name = DD.name(dim) | ||
v = AoG.getcolumn(data.columns, Symbol(name)) | ||
return (v,) => identity => AoG.to_string(name) => nothing | ||
end | ||
|
||
# The specific selector for `DimTable`s. | ||
# This searches the dimensions of the dimtable for the appropriate dimension, | ||
# so that e.g. `X` also applies to any `XDim`, and so forth. | ||
function AoG.select(data::AoG.Columns{<: DD.AbstractDimTable}, dim::DimOrType) | ||
# Query the dimensions in the table for the dimension | ||
available_dimension = DD.dims(data.columns, dim) | ||
# If the dimension is not found, it might be the name of the | ||
# underlying array. | ||
name = if isnothing(available_dimension) | ||
if DD.name(dim) in DD.name(parent(data.columns)) | ||
error( | ||
"Dimension $dim not found in DimTable with dimensions $(DD.dims(data.columns)), **but** it is" * | ||
(length(DD.name(parent(data.columns))) > 1 ? "a layer of the parent array" : "the name of the parent array") * | ||
".\n\nYou can pass this directly as a `Symbol`, i.e. **`:$(DD.name(dim))`**." | ||
) | ||
else | ||
error("Dimension $dim not found in DimTable with dimensions $(DD.dims(data.columns)), and neither was it the name of the array ($(DD.name(parent(data.columns)))).") | ||
end | ||
else | ||
# The dimension was found, so use that name. | ||
DD.name(available_dimension) | ||
end | ||
# Get the column from the table | ||
v = AoG.getcolumn(data.columns, Symbol(name)) | ||
# Return the column, with the appropriate labels | ||
return (v,) => identity => AoG.to_string(name) => nothing | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters