Skip to content

Commit

Permalink
Merge pull request #83 from SymbolicML/parametric-expressions2
Browse files Browse the repository at this point in the history
Fix some method ambiguities in `Expression`
  • Loading branch information
MilesCranmer authored Jun 24, 2024
2 parents 139f99f + db17350 commit 905ebc0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <[email protected]>"]
version = "0.18.0"
version = "0.18.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
7 changes: 5 additions & 2 deletions ext/DynamicExpressionsSymbolicUtilsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ end
function Base.convert(
::typeof(SymbolicUtils.Symbolic),
tree::Union{AbstractExpression,AbstractExpressionNode},
operators::AbstractOperatorEnum;
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
variable_names::Union{Array{String,1},Nothing}=nothing,
index_functions::Bool=false,
# Deprecated:
varMap=nothing,
)
variable_names = deprecate_varmap(variable_names, varMap, :convert)
return node_to_symbolic(
tree, operators; variable_names=variable_names, index_functions=index_functions
tree,
get_operators(tree, operators);
variable_names=variable_names,
index_functions=index_functions,
)
end

Expand Down
57 changes: 46 additions & 11 deletions src/Expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ or `cur_operators` if it is not `nothing`. If left as default,
it requires `cur_operators` to not be `nothing`.
`cur_operators` would typically be an `OperatorEnum`.
"""
function get_operators(ex::AbstractExpression, operators)
function get_operators(
ex::AbstractExpression, operators::Union{AbstractOperatorEnum,Nothing}=nothing
)
return error("`get_operators` function must be implemented for $(typeof(ex)) types.")
end

Expand All @@ -110,7 +112,10 @@ end
The same as `operators`, but for variable names.
"""
function get_variable_names(ex::AbstractExpression, variable_names)
function get_variable_names(
ex::AbstractExpression,
variable_names::Union{Nothing,AbstractVector{<:AbstractString}}=nothing,
)
return error(
"`get_variable_names` function must be implemented for $(typeof(ex)) types."
)
Expand Down Expand Up @@ -179,10 +184,23 @@ function preserve_sharing(::Union{E,Type{E}}) where {T,N,E<:AbstractExpression{T
return preserve_sharing(N)
end

function get_operators(ex::Expression, operators=nothing)
function get_operators(
tree::AbstractExpressionNode, operators::Union{AbstractOperatorEnum,Nothing}=nothing
)
if operators === nothing
throw(ArgumentError("`operators` must be provided for $(typeof(tree)) types."))
else
return operators
end
end
function get_operators(
ex::Expression, operators::Union{AbstractOperatorEnum,Nothing}=nothing
)
return operators === nothing ? ex.metadata.operators : operators
end
function get_variable_names(ex::Expression, variable_names=nothing)
function get_variable_names(
ex::Expression, variable_names::Union{Nothing,AbstractVector{<:AbstractString}}=nothing
)
return variable_names === nothing ? ex.metadata.variable_names : variable_names
end
function get_tree(ex::Expression)
Expand Down Expand Up @@ -249,7 +267,10 @@ end
import ..StringsModule: string_tree, print_tree

function string_tree(
ex::AbstractExpression, operators=nothing; variable_names=nothing, kws...
ex::AbstractExpression,
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
variable_names=nothing,
kws...,
)
return string_tree(
get_tree(ex),
Expand All @@ -260,7 +281,11 @@ function string_tree(
end
for io in ((), (:(io::IO),))
@eval function print_tree(
$(io...), ex::AbstractExpression, operators=nothing; variable_names=nothing, kws...
$(io...),
ex::AbstractExpression,
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
variable_names=nothing,
kws...,
)
return println($(io...), string_tree(ex, operators; variable_names, kws...))
end
Expand All @@ -283,7 +308,9 @@ function max_feature(ex::AbstractExpression)
)
end

function _validate_input(ex::AbstractExpression, X, operators)
function _validate_input(
ex::AbstractExpression, X, operators::Union{AbstractOperatorEnum,Nothing}
)
if get_operators(ex, operators) isa OperatorEnum
@assert X isa AbstractMatrix
@assert max_feature(ex) <= size(X, 1)
Expand All @@ -292,7 +319,10 @@ function _validate_input(ex::AbstractExpression, X, operators)
end

function eval_tree_array(
ex::AbstractExpression, cX::AbstractMatrix, operators=nothing; kws...
ex::AbstractExpression,
cX::AbstractMatrix,
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
kws...,
)
_validate_input(ex, cX, operators)
return eval_tree_array(get_tree(ex), cX, get_operators(ex, operators); kws...)
Expand All @@ -305,7 +335,10 @@ import ..EvaluateDerivativeModule: eval_grad_tree_array
# - differentiable_eval_tree_array

function eval_grad_tree_array(
ex::AbstractExpression, cX::AbstractMatrix, operators=nothing; kws...
ex::AbstractExpression,
cX::AbstractMatrix,
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
kws...,
)
_validate_input(ex, cX, operators)
return eval_grad_tree_array(get_tree(ex), cX, get_operators(ex, operators); kws...)
Expand All @@ -319,14 +352,16 @@ end
function _grad_evaluator(
ex::AbstractExpression,
cX::AbstractMatrix,
operators=nothing;
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
variable=Val(true),
kws...,
)
_validate_input(ex, cX, operators)
return _grad_evaluator(get_tree(ex), cX, get_operators(ex, operators); variable, kws...)
end
function (ex::AbstractExpression)(X, operators=nothing; kws...)
function (ex::AbstractExpression)(
X, operators::Union{AbstractOperatorEnum,Nothing}=nothing; kws...
)
_validate_input(ex, X, operators)
return get_tree(ex)(X, get_operators(ex, operators); kws...)
end
Expand Down
25 changes: 17 additions & 8 deletions src/ParametricExpression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ParametricExpressionModule

using DispatchDoctor: @stable, @unstable

using ..OperatorEnumModule: AbstractOperatorEnum
using ..NodeModule: AbstractExpressionNode, Node, tree_mapreduce
using ..ExpressionModule: AbstractExpression, Metadata

Expand Down Expand Up @@ -70,7 +71,7 @@ struct ParametricExpression{
end
function ParametricExpression(
tree::ParametricNode{T1};
operators,
operators::Union{AbstractOperatorEnum,Nothing},
variable_names,
parameters::AbstractMatrix{T2},
parameter_names,
Expand Down Expand Up @@ -141,10 +142,15 @@ end
get_contents(ex::ParametricExpression) = ex.tree
get_metadata(ex::ParametricExpression) = ex.metadata
get_tree(ex::ParametricExpression) = ex.tree
function get_operators(ex::ParametricExpression, operators=nothing)
function get_operators(
ex::ParametricExpression, operators::Union{AbstractOperatorEnum,Nothing}=nothing
)
return operators === nothing ? ex.metadata.operators : operators
end
function get_variable_names(ex::ParametricExpression, variable_names=nothing)
function get_variable_names(
ex::ParametricExpression,
variable_names::Union{Nothing,AbstractVector{<:AbstractString}}=nothing,
)
return variable_names === nothing ? ex.metadata.variable_names : variable_names
end
@inline _copy_with_nothing(x) = copy(x)
Expand Down Expand Up @@ -232,15 +238,18 @@ function Base.convert(::Type{Node}, ex::ParametricExpression{T}) where {T}
)
end
#! format: off
function (ex::ParametricExpression)(X::AbstractMatrix, operators=nothing; kws...)
function (ex::ParametricExpression)(X::AbstractMatrix, operators::Union{AbstractOperatorEnum,Nothing}=nothing; kws...)
return eval_tree_array(ex, X, operators; kws...) # Will error
end
function eval_tree_array(::ParametricExpression{T}, ::AbstractMatrix{T}, operators=nothing; kws...) where {T}
function eval_tree_array(::ParametricExpression{T}, ::AbstractMatrix{T}, operators::Union{AbstractOperatorEnum,Nothing}=nothing; kws...) where {T}
return error("Incorrect call. You must pass the `classes::Vector` argument when calling `eval_tree_array`.")
end
#! format: on
function (ex::ParametricExpression)(
X::AbstractMatrix{T}, classes::AbstractVector{<:Integer}, operators=nothing; kws...
X::AbstractMatrix{T},
classes::AbstractVector{<:Integer},
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
kws...,
) where {T}
(output, flag) = eval_tree_array(ex, X, classes, operators; kws...) # Will error
if !flag
Expand All @@ -252,7 +261,7 @@ function eval_tree_array(
ex::ParametricExpression{T},
X::AbstractMatrix{T},
classes::AbstractVector{<:Integer},
operators=nothing;
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
kws...,
) where {T}
@assert length(classes) == size(X, 2)
Expand All @@ -270,7 +279,7 @@ function eval_tree_array(
end
function string_tree(
ex::ParametricExpression,
operators=nothing;
operators::Union{AbstractOperatorEnum,Nothing}=nothing;
variable_names=nothing,
display_variable_names=nothing,
X_sym_units=nothing,
Expand Down
4 changes: 4 additions & 0 deletions test/test_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ end

@testitem "Miscellaneous expression calls" begin
using DynamicExpressions
using DynamicExpressions: get_tree, get_operators

ex = @parse_expression(x1 + 1.5, binary_operators = [+], variable_names = ["x1"])
@test DynamicExpressions.ExpressionModule.node_type(ex) <: Node

@test !isempty(ex)

tree = get_tree(ex)
@test_throws ArgumentError get_operators(tree, nothing)
end
9 changes: 7 additions & 2 deletions test/test_multi_expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@
)::Expression{T,N}
return fused_expression.tree
end
function DE.get_operators(ex::MultiScalarExpression, operators=nothing)
function DE.get_operators(
ex::MultiScalarExpression, operators::Union{AbstractOperatorEnum,Nothing}=nothing
)
return operators === nothing ? ex.metadata.operators : operators
end
function DE.get_variable_names(ex::MultiScalarExpression, variable_names=nothing)
function DE.get_variable_names(
ex::MultiScalarExpression,
variable_names::Union{Nothing,AbstractVector{<:AbstractString}}=nothing,
)
return variable_names === nothing ? ex.metadata.variable_names : variable_names
end
function Base.copy(ex::MultiScalarExpression)
Expand Down

2 comments on commit 905ebc0

@MilesCranmer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109704

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.1 -m "<description of version>" 905ebc0e894c37ca12aa44b4a8473fddf4843035
git push origin v0.18.1

Please sign in to comment.