Skip to content

Commit da22e1a

Browse files
authored
Merge pull request #39 from SymbolicML:deprecate-varmap
Deprecate varMap in favor of variable_names
2 parents abeb7fa + 2e9b45d commit da22e1a

File tree

7 files changed

+106
-53
lines changed

7 files changed

+106
-53
lines changed

.github/workflows/CI.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ jobs:
5555
julia --color=yes --project=. coverage.jl
5656
shell: bash
5757
- name: Coveralls
58-
uses: coverallsapp/github-action@master
58+
uses: coverallsapp/github-action@v2
5959
with:
60-
github-token: ${{ secrets.GITHUB_TOKEN }}
61-
path-to-lcov: coverage-lcov.info
60+
parallel: true
61+
path-to-lcov: lcov.info
62+
flag-name: julia-${{ matrix.julia-version }}-${{ matrix.os }}-${{ github.event_name }}
63+
64+
coveralls:
65+
name: Indicate completion to coveralls
66+
runs-on: ubuntu-latest
67+
needs: test
68+
steps:
69+
- name: Finish
70+
uses: coverallsapp/github-action@v2
71+
with:
72+
parallel-finished: true
73+

coverage.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using Coverage
22
# process '*.cov' files
33
coverage = process_folder() # defaults to src/; alternatively, supply the folder name as argument
4+
push!(coverage, process_folder("ext")...)
45

5-
LCOV.writefile("coverage-lcov.info", coverage)
6+
LCOV.writefile("lcov.info", coverage)
67

78
# process '*.info' files
89
coverage = merge_coverage_counts(
910
coverage,
1011
filter!(
11-
let prefixes = (joinpath(pwd(), "src", ""),)
12+
let prefixes = (joinpath(pwd(), "src", ""), joinpath(pwd(), "ext", ""))
1213
c -> any(p -> startswith(c.filename, p), prefixes)
1314
end,
1415
LCOV.readfolder("test"),

ext/DynamicExpressionsSymbolicUtilsExt.jl

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
module DynamicExpressionsSymbolicUtilsExt
22

33
import Base: convert
4+
#! format: off
45
if isdefined(Base, :get_extension)
56
using SymbolicUtils
67
import DynamicExpressions.EquationModule: Node, DEFAULT_NODE_TYPE
78
import DynamicExpressions.OperatorEnumModule: AbstractOperatorEnum
8-
import DynamicExpressions.UtilsModule: isgood, isbad, @return_on_false
9+
import DynamicExpressions.UtilsModule: isgood, isbad, @return_on_false, deprecate_varmap
910
import DynamicExpressions.ExtensionInterfaceModule: node_to_symbolic, symbolic_to_node
1011
else
1112
using ..SymbolicUtils
1213
import ..DynamicExpressions.EquationModule: Node, DEFAULT_NODE_TYPE
1314
import ..DynamicExpressions.OperatorEnumModule: AbstractOperatorEnum
14-
import ..DynamicExpressions.UtilsModule: isgood, isbad, @return_on_false
15+
import ..DynamicExpressions.UtilsModule: isgood, isbad, @return_on_false, deprecate_varmap
1516
import ..DynamicExpressions.ExtensionInterfaceModule: node_to_symbolic, symbolic_to_node
1617
end
18+
#! format: on
1719

1820
const SYMBOLIC_UTILS_TYPES = Union{<:Number,SymbolicUtils.Symbolic{<:Number}}
1921

@@ -77,8 +79,11 @@ function split_eq(
7779
op,
7880
args,
7981
operators::AbstractOperatorEnum;
80-
varMap::Union{Array{String,1},Nothing}=nothing,
82+
variable_names::Union{Array{String,1},Nothing}=nothing,
83+
# Deprecated:
84+
varMap=nothing,
8185
)
86+
variable_names = deprecate_varmap(variable_names, varMap, :split_eq)
8287
!(op (sum, prod, +, *)) && throw(error("Unsupported operation $op in expression!"))
8388
if Symbol(op) == Symbol(sum)
8489
ind = findoperation(+, operators.binops)
@@ -89,8 +94,8 @@ function split_eq(
8994
end
9095
return Node(
9196
ind,
92-
convert(Node, args[1], operators; varMap=varMap),
93-
convert(Node, op(args[2:end]...), operators; varMap=varMap),
97+
convert(Node, args[1], operators; variable_names=variable_names),
98+
convert(Node, op(args[2:end]...), operators; variable_names=variable_names),
9499
)
95100
end
96101

@@ -105,30 +110,31 @@ function convert(
105110
::typeof(SymbolicUtils.Symbolic),
106111
tree::Node,
107112
operators::AbstractOperatorEnum;
108-
varMap::Union{Array{String,1},Nothing}=nothing,
113+
variable_names::Union{Array{String,1},Nothing}=nothing,
109114
index_functions::Bool=false,
115+
# Deprecated:
116+
varMap=nothing,
110117
)
111-
return node_to_symbolic(tree, operators; varMap=varMap, index_functions=index_functions)
118+
variable_names = deprecate_varmap(variable_names, varMap, :convert)
119+
return node_to_symbolic(
120+
tree, operators; variable_names=variable_names, index_functions=index_functions
121+
)
112122
end
113123

114-
function convert(
115-
::typeof(Node),
116-
x::Number,
117-
operators::AbstractOperatorEnum;
118-
varMap::Union{Array{String,1},Nothing}=nothing,
119-
)
124+
function convert(::typeof(Node), x::Number, operators::AbstractOperatorEnum; kws...)
120125
return Node(; val=DEFAULT_NODE_TYPE(x))
121126
end
122127

123128
function convert(
124129
::typeof(Node),
125130
expr::SymbolicUtils.Symbolic,
126131
operators::AbstractOperatorEnum;
127-
varMap::Union{Array{String,1},Nothing}=nothing,
132+
variable_names::Union{Array{String,1},Nothing}=nothing,
128133
)
134+
variable_names = deprecate_varmap(variable_names, nothing, :convert)
129135
if !SymbolicUtils.istree(expr)
130-
varMap === nothing && return Node(String(expr.name))
131-
return Node(String(expr.name), varMap)
136+
variable_names === nothing && return Node(String(expr.name))
137+
return Node(String(expr.name), variable_names)
132138
end
133139

134140
# First, we remove integer powers:
@@ -140,19 +146,21 @@ function convert(
140146
op = convert_to_function(SymbolicUtils.operation(expr), operators)
141147
args = SymbolicUtils.arguments(expr)
142148

143-
length(args) > 2 && return split_eq(op, args, operators; varMap=varMap)
149+
length(args) > 2 && return split_eq(op, args, operators; variable_names=variable_names)
144150
ind = if length(args) == 2
145151
findoperation(op, operators.binops)
146152
else
147153
findoperation(op, operators.unaops)
148154
end
149155

150-
return Node(ind, map(x -> convert(Node, x, operators; varMap=varMap), args)...)
156+
return Node(
157+
ind, map(x -> convert(Node, x, operators; variable_names=variable_names), args)...
158+
)
151159
end
152160

153161
"""
154162
node_to_symbolic(tree::Node, operators::AbstractOperatorEnum;
155-
varMap::Union{Array{String, 1}, Nothing}=nothing,
163+
variable_names::Union{Array{String, 1}, Nothing}=nothing,
156164
index_functions::Bool=false)
157165
158166
The interface to SymbolicUtils.jl. Passing a tree to this function
@@ -162,7 +170,7 @@ will generate a symbolic equation in SymbolicUtils.jl format.
162170
163171
- `tree::Node`: The equation to convert.
164172
- `operators::AbstractOperatorEnum`: OperatorEnum, which contains the operators used in the equation.
165-
- `varMap::Union{Array{String, 1}, Nothing}=nothing`: What variable names to use for
173+
- `variable_names::Union{Array{String, 1}, Nothing}=nothing`: What variable names to use for
166174
each feature. Default is [x1, x2, x3, ...].
167175
- `index_functions::Bool=false`: Whether to generate special names for the
168176
operators, which then allows one to convert back to a `Node` format
@@ -172,19 +180,23 @@ will generate a symbolic equation in SymbolicUtils.jl format.
172180
function node_to_symbolic(
173181
tree::Node,
174182
operators::AbstractOperatorEnum;
175-
varMap::Union{Array{String,1},Nothing}=nothing,
183+
variable_names::Union{Array{String,1},Nothing}=nothing,
176184
index_functions::Bool=false,
185+
# Deprecated:
186+
varMap=nothing,
177187
)
188+
variable_names = deprecate_varmap(variable_names, varMap, :node_to_symbolic)
178189
expr = subs_bad(parse_tree_to_eqs(tree, operators, index_functions))
179190
# Check for NaN and Inf
180191
@assert isgood(expr) "The recovered equation contains NaN or Inf."
181-
# Return if no varMap is given
182-
varMap === nothing && return expr
192+
# Return if no variable_names is given
193+
variable_names === nothing && return expr
183194
# Create a substitution tuple
184195
subs = Dict(
185196
[
186197
SymbolicUtils.Sym{LiteralReal}(Symbol("x$(i)")) =>
187-
SymbolicUtils.Sym{LiteralReal}(Symbol(varMap[i])) for i in 1:length(varMap)
198+
SymbolicUtils.Sym{LiteralReal}(Symbol(variable_names[i])) for
199+
i in 1:length(variable_names)
188200
]...,
189201
)
190202
return substitute(expr, subs)
@@ -193,9 +205,12 @@ end
193205
function symbolic_to_node(
194206
eqn::SymbolicUtils.Symbolic,
195207
operators::AbstractOperatorEnum;
196-
varMap::Union{Array{String,1},Nothing}=nothing,
208+
variable_names::Union{Array{String,1},Nothing}=nothing,
209+
# Deprecated:
210+
varMap=nothing,
197211
)::Node
198-
return convert(Node, eqn, operators; varMap=varMap)
212+
variable_names = deprecate_varmap(variable_names, varMap, :symbolic_to_node)
213+
return convert(Node, eqn, operators; variable_names=variable_names)
199214
end
200215

201216
function multiply_powers(eqn::Number)::Tuple{SYMBOLIC_UTILS_TYPES,Bool}

src/Equation.jl

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module EquationModule
22

33
import ..OperatorEnumModule: AbstractOperatorEnum
4-
import ..UtilsModule: @memoize_on, @with_memoize
4+
import ..UtilsModule: @memoize_on, @with_memoize, deprecate_varmap
55

66
const DEFAULT_NODE_TYPE = Float32
77

@@ -144,14 +144,14 @@ Create a variable node, using the format `"x1"` to mean feature 1
144144
Node(var_string::String) = Node(; feature=parse(Int, var_string[2:end]))
145145

146146
"""
147-
Node(var_string::String, varMap::Array{String, 1})
147+
Node(var_string::String, variable_names::Array{String, 1})
148148
149149
Create a variable node, using a user-passed format
150150
"""
151-
function Node(var_string::String, varMap::Array{String,1})
151+
function Node(var_string::String, variable_names::Array{String,1})
152152
return Node(;
153153
feature=[
154-
i for (i, _variable) in enumerate(varMap) if _variable == var_string
154+
i for (i, _variable) in enumerate(variable_names) if _variable == var_string
155155
][1]::Int,
156156
)
157157
end
@@ -199,20 +199,23 @@ function string_op(
199199
tree::Node,
200200
operators::AbstractOperatorEnum;
201201
bracketed::Bool=false,
202-
varMap::Union{Array{String,1},Nothing}=nothing,
202+
variable_names::Union{Array{String,1},Nothing}=nothing,
203+
# Deprecated
204+
varMap=nothing,
203205
)::String where {F}
206+
variable_names = deprecate_varmap(variable_names, varMap, :string_op)
204207
op_name = get_op_name(string(op))
205208
if op_name in ["+", "-", "*", "/", "^"]
206-
l = string_tree(tree.l, operators; bracketed=false, varMap=varMap)
207-
r = string_tree(tree.r, operators; bracketed=false, varMap=varMap)
209+
l = string_tree(tree.l, operators; bracketed=false, variable_names=variable_names)
210+
r = string_tree(tree.r, operators; bracketed=false, variable_names=variable_names)
208211
if bracketed
209212
return "$l $op_name $r"
210213
else
211214
return "($l $op_name $r)"
212215
end
213216
else
214-
l = string_tree(tree.l, operators; bracketed=true, varMap=varMap)
215-
r = string_tree(tree.r, operators; bracketed=true, varMap=varMap)
217+
l = string_tree(tree.l, operators; bracketed=true, variable_names=variable_names)
218+
r = string_tree(tree.r, operators; bracketed=true, variable_names=variable_names)
216219
return "$op_name($l, $r)"
217220
end
218221
end
@@ -224,31 +227,38 @@ Convert an equation to a string.
224227
225228
# Arguments
226229
227-
- `varMap::Union{Array{String, 1}, Nothing}=nothing`: what variables
230+
- `variable_names::Union{Array{String, 1}, Nothing}=nothing`: what variables
228231
to print for each feature.
229232
"""
230233
function string_tree(
231234
tree::Node{T},
232235
operators::AbstractOperatorEnum;
233236
bracketed::Bool=false,
234-
varMap::Union{Array{String,1},Nothing}=nothing,
237+
variable_names::Union{Array{String,1},Nothing}=nothing,
238+
# Deprecated
239+
varMap=nothing,
235240
)::String where {T}
241+
variable_names = deprecate_varmap(variable_names, varMap, :string_tree)
236242
if tree.degree == 0
237243
if tree.constant
238244
return string_constant(tree.val::T; bracketed=bracketed)
239245
else
240-
if varMap === nothing
246+
if variable_names === nothing
241247
return "x$(tree.feature)"
242248
else
243-
return varMap[tree.feature]
249+
return variable_names[tree.feature]
244250
end
245251
end
246252
elseif tree.degree == 1
247253
op_name = get_op_name(string(operators.unaops[tree.op]))
248-
return "$(op_name)($(string_tree(tree.l, operators, bracketed=true, varMap=varMap)))"
254+
return "$(op_name)($(string_tree(tree.l, operators, bracketed=true, variable_names=variable_names)))"
249255
else
250256
return string_op(
251-
operators.binops[tree.op], tree, operators; bracketed=bracketed, varMap=varMap
257+
operators.binops[tree.op],
258+
tree,
259+
operators;
260+
bracketed=bracketed,
261+
variable_names=variable_names,
252262
)
253263
end
254264
end
@@ -267,17 +277,23 @@ function print_tree(
267277
io::IO,
268278
tree::Node,
269279
operators::AbstractOperatorEnum;
270-
varMap::Union{Array{String,1},Nothing}=nothing,
280+
variable_names::Union{Array{String,1},Nothing}=nothing,
281+
# Deprecated
282+
varMap=nothing,
271283
)
272-
return println(io, string_tree(tree, operators; varMap=varMap))
284+
variable_names = deprecate_varmap(variable_names, varMap, :print_tree)
285+
return println(io, string_tree(tree, operators; variable_names=variable_names))
273286
end
274287

275288
function print_tree(
276289
tree::Node,
277290
operators::AbstractOperatorEnum;
278-
varMap::Union{Array{String,1},Nothing}=nothing,
291+
variable_names::Union{Array{String,1},Nothing}=nothing,
292+
# Deprecated
293+
varMap=nothing,
279294
)
280-
return println(string_tree(tree, operators; varMap=varMap))
295+
variable_names = deprecate_varmap(variable_names, varMap, :print_tree)
296+
return println(string_tree(tree, operators; variable_names=variable_names))
281297
end
282298

283299
end

src/Utils.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,13 @@ end
157157

158158
@inline fill_similar(value, array, args...) = fill!(similar(array, args...), value)
159159

160+
function deprecate_varmap(variable_names, varMap, func_name)
161+
if varMap !== nothing
162+
Base.depwarn("`varMap` is deprecated; use `variable_names` instead", func_name)
163+
@assert variable_names === nothing "Cannot pass both `varMap` and `variable_names`"
164+
variable_names = varMap
165+
end
166+
return variable_names
167+
end
168+
160169
end

test/test_print.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ true_s = "((sin(cos(sin(cos(x1) * x3) * 3.0) * -0.5) + 2.0) * 5.0)"
1919

2020
# TODO: Next, we test that custom varMaps work:
2121

22-
s = string_tree(tree, operators; varMap=["v1", "v2", "v3"])
22+
s = string_tree(tree, operators; variable_names=["v1", "v2", "v3"])
2323
true_s = "((sin(cos(sin(cos(v1) * v3) * 3.0) * -0.5) + 2.0) * 5.0)"
2424
@test s == true_s
2525

test/test_symbolic_utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ operators = OperatorEnum(;
1313
)
1414
tree = Node(5, (Node(; val=3.0) * Node(1, Node("x1")))^2.0, Node(; val=-1.2))
1515

16-
eqn = node_to_symbolic(tree, operators; varMap=["energy"], index_functions=true)
16+
eqn = node_to_symbolic(tree, operators; variable_names=["energy"], index_functions=true)
1717
@test string(eqn) == "greater(safe_pow(3.0_inv(energy), 2.0), -1.2)"
1818

19-
tree2 = symbolic_to_node(eqn, operators; varMap=["energy"])
19+
tree2 = symbolic_to_node(eqn, operators; variable_names=["energy"])
2020
@test string_tree(tree, operators) == string_tree(tree2, operators)

0 commit comments

Comments
 (0)