Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Hierarchical printing of graph-like expressions #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

MilesCranmer
Copy link
Member

@MilesCranmer MilesCranmer commented Dec 28, 2023

Say we create an expression:

julia> using DynamicExpressions

julia> x1, x2, x3 = (GraphNode{Float64}(feature=i) for i=1:3);

julia> operators = OperatorEnum(
           binary_operators=[+, -, *, /, ^],
           unary_operators=[cos, sin, exp],
       );

julia> base_tree = cos(x1 - 3.2) * x2 - x3 * x3
(cos(x1 - 3.2) * x2) - (x3 * {x3})

julia> base_tree2 = exp(x3 * x2) - base_tree
exp(x3 * x2) - ((cos(x1 - 3.2) * {x2}) - ({x3} * {x3}))

julia> tree = cos(base_tree * base_tree2) - base_tree * base_tree2 ^ 2
cos(((cos(x1 - 3.2) * x2) - (x3 * {x3})) * (exp({x3} * {x2}) - {((cos(x1 - 3.2) * x2) - (x3 * {x3}))})) - ({((cos(x1 - 3.2) * x2) - (x3 * {x3}))} * ({(exp({x3} * {x2}) - {((cos(x1 - 3.2) * x2) - (x3 * {x3}))})} ^ 2.0))

Note that the normal behavior is to encapsulate with {} when a node is shared.

However, we can print this with less redundancy with pretty_string_graph:

julia> print(pretty_string_graph(tree, operators))
┌─── z1 = (cos(x1 - 3.2) * x2) - (x3 * x3)
├─── z2 = exp(x3 * x2) - z1
│
= cos(z1 * z2) - (z1 * (z2 ^ 2.0))

which finds the shared nodes, assigns variable names based on their depth, and then prints the program in this hierarchical fashion.

This is only 2x slower than the regular string_tree method, so I'm considering have this as the default for GraphNode types. For consistency I might need to prepend the = to regular print statements though...

Curious to hear your thoughts @AlCap23 @Vaibhavdixit02 @ChrisRackauckas! I played around with a few different ideas for how to visualize this in the REPL and this one seemed like a good option. But interested in more.

@SymbolicML SymbolicML deleted a comment from sweep-ai bot Dec 28, 2023
Copy link
Contributor

Pull Request Test Coverage Report for Build 7344448443

  • 70 of 135 (51.85%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-4.2%) to 90.064%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Strings.jl 70 135 51.85%
Totals Coverage Status
Change from base Build 7337169551: -4.2%
Covered Lines: 1269
Relevant Lines: 1409

💛 - Coveralls

Copy link
Contributor

github-actions bot commented Dec 28, 2023

Benchmark Results

master 9cd35ff... t[master]/t[9cd35ff...]
eval/ComplexF32/evaluation 7.37 ± 0.52 ms 7.31 ± 0.46 ms 1.01
eval/ComplexF64/evaluation 9.58 ± 0.7 ms 9.5 ± 0.64 ms 1.01
eval/Float32/derivative 10.5 ± 1.3 ms 10.8 ± 1.3 ms 0.972
eval/Float32/derivative_turbo 12.1 ± 1.4 ms 12.3 ± 1.3 ms 0.986
eval/Float32/evaluation 2.65 ± 0.24 ms 2.62 ± 0.22 ms 1.01
eval/Float32/evaluation_turbo 0.618 ± 0.025 ms 0.603 ± 0.026 ms 1.02
eval/Float64/derivative 13.7 ± 0.54 ms 13.3 ± 0.55 ms 1.03
eval/Float64/derivative_turbo 14.5 ± 0.56 ms 14.6 ± 0.54 ms 0.992
eval/Float64/evaluation 2.82 ± 0.24 ms 2.82 ± 0.24 ms 0.999
eval/Float64/evaluation_turbo 1.12 ± 0.058 ms 1.11 ± 0.059 ms 1.01
utils/combine_operators/break_sharing 0.0415 ± 0.0028 ms 0.0409 ± 0.0026 ms 1.01
utils/convert/break_sharing 28.1 ± 0.56 μs 28.4 ± 0.72 μs 0.99
utils/convert/preserve_sharing 0.127 ± 0.0025 ms 0.128 ± 0.0026 ms 0.995
utils/copy/break_sharing 28.9 ± 0.58 μs 28.6 ± 0.63 μs 1.01
utils/copy/preserve_sharing 0.128 ± 0.0027 ms 0.127 ± 0.0025 ms 1
utils/count_constants/break_sharing 11.8 ± 0.22 μs 10.8 ± 0.17 μs 1.09
utils/count_constants/preserve_sharing 0.11 ± 0.0023 ms 0.111 ± 0.0023 ms 0.997
utils/count_depth/break_sharing 12.8 ± 0.22 μs 12.7 ± 0.21 μs 1
utils/count_nodes/break_sharing 10.5 ± 0.18 μs 10.1 ± 0.16 μs 1.03
utils/count_nodes/preserve_sharing 0.114 ± 0.0022 ms 0.114 ± 0.0022 ms 1
utils/get_set_constants!/break_sharing 0.0539 ± 0.00088 ms 0.0523 ± 0.00081 ms 1.03
utils/get_set_constants!/preserve_sharing 0.318 ± 0.0051 ms 0.32 ± 0.0048 ms 0.994
utils/has_constants/break_sharing 4.47 ± 0.22 μs 4.31 ± 0.21 μs 1.04
utils/has_operators/break_sharing 1.78 ± 0.017 μs 1.77 ± 0.016 μs 1
utils/hash/break_sharing 30.4 ± 0.46 μs 30 ± 0.48 μs 1.01
utils/hash/preserve_sharing 0.133 ± 0.0025 ms 0.133 ± 0.0025 ms 1
utils/index_constants/break_sharing 27.5 ± 0.68 μs 27.4 ± 0.76 μs 1
utils/index_constants/preserve_sharing 0.127 ± 0.0024 ms 0.127 ± 0.0024 ms 0.997
utils/is_constant/break_sharing 5.51 ± 0.21 μs 4.77 ± 0.22 μs 1.16
utils/simplify_tree/break_sharing 0.251 ± 0.02 ms 0.248 ± 0.02 ms 1.01
utils/simplify_tree/preserve_sharing 0.377 ± 0.022 ms 0.386 ± 0.022 ms 0.977
utils/string_tree/break_sharing 0.558 ± 0.011 ms 0.582 ± 0.015 ms 0.959
utils/string_tree/preserve_sharing 0.693 ± 0.013 ms 0.7 ± 0.022 ms 0.99
time_to_load 0.683 ± 0.0083 s 0.701 ± 0.0079 s 0.974

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant