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

Support for conditional expressions on workflows. #666

Merged
merged 2 commits into from
Jan 4, 2024

Conversation

pfebrer
Copy link
Contributor

@pfebrer pfebrer commented Jan 1, 2024

With this PR one can use conditional expressions and comparisons inside the workflow code, which will be parsed to ConditionalExpressionSyntaxNode and CompareSyntaxNode respectively.

This removes the need for having to use/define a function just to write an if/else expression, so it makes workflows simpler to explain.

Also, by parsing the conditional expressions into a node, we can safely compute only the branch of the conditional that is needed, which is nice.

I have rewritten some plots to use this.

As an example, now this is valid:

from sisl.nodes import Workflow

@Workflow.from_func
def f(a, b, c, d):
    return c if a == b else d

and the diagram of the workflow created looks like this:

f.network.visualize()

Screenshot from 2024-01-01 20-24-02

Things like "a = function_1() if test() else function_2()" are now valid
as workflow code. This removes the need for having to call/define a function
that just does the branching.

Furthermore, since the conditional expression is now understood and parsed
into a custom "ConditionalExpressionSyntaxNode", only the branch needed (as
decided by `test()`) is executed.

Comparison syntax (e.g. "a == b") is also parsed into a custom node, therefore
it is now allowed in the syntax of workflows.
self._outdate_due_to_inputs = False
raise

def _receive_outdated(self):

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
Copy link

codecov bot commented Jan 1, 2024

Codecov Report

Attention: 25 lines in your changes are missing coverage. Please review.

Comparison is base (c667b7e) 87.62% compared to head (117ca49) 87.61%.

Files Patch % Lines
src/sisl/nodes/syntax_nodes.py 70.58% 15 Missing ⚠️
src/sisl/nodes/workflow.py 81.25% 3 Missing ⚠️
src/sisl/viz/plots/bands.py 40.00% 3 Missing ⚠️
src/sisl/nodes/tests/test_syntax_nodes.py 93.10% 2 Missing ⚠️
src/sisl/nodes/node.py 83.33% 1 Missing ⚠️
src/sisl/viz/plots/geometry.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #666      +/-   ##
==========================================
- Coverage   87.62%   87.61%   -0.02%     
==========================================
  Files         364      364              
  Lines       48865    48944      +79     
==========================================
+ Hits        42819    42880      +61     
- Misses       6046     6064      +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zerothi zerothi merged commit 25c1b3a into zerothi:main Jan 4, 2024
6 of 8 checks passed
@pfebrer pfebrer deleted the conditional_expressions branch January 5, 2024 10:33
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.

2 participants