-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Compare & Conditional node (#2866)
* Add Compare & Conditional node * Fix errors --------- Co-authored-by: Joey Ballentine <[email protected]>
- Loading branch information
1 parent
1562e1d
commit a3e33f4
Showing
6 changed files
with
193 additions
and
2 deletions.
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
96 changes: 96 additions & 0 deletions
96
backend/src/packages/chaiNNer_standard/utility/math/compare.py
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,96 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
|
||
from api import KeyInfo | ||
from nodes.properties.inputs import EnumInput, NumberInput | ||
from nodes.properties.outputs import BoolOutput | ||
|
||
from .. import math_group | ||
|
||
|
||
class Comparison(Enum): | ||
EQUAL = 0 | ||
NOT_EQUAL = 1 | ||
LESS = 3 | ||
LESS_EQUAL = 5 | ||
GREATER = 2 | ||
GREATER_EQUAL = 4 | ||
|
||
|
||
@math_group.register( | ||
schema_id="chainner:utility:compare", | ||
name="Compare", | ||
description="Compares the given numbers.", | ||
icon="MdCalculate", | ||
inputs=[ | ||
EnumInput( | ||
Comparison, | ||
label="Operation", | ||
option_labels={ | ||
Comparison.EQUAL: "L == R", | ||
Comparison.NOT_EQUAL: "L == R", | ||
Comparison.GREATER: "L > R", | ||
Comparison.LESS: "L < R", | ||
Comparison.GREATER_EQUAL: "L >= R", | ||
Comparison.LESS_EQUAL: "L <= R", | ||
}, | ||
icons={ | ||
Comparison.EQUAL: "=", | ||
Comparison.NOT_EQUAL: "≠", | ||
Comparison.GREATER: ">", | ||
Comparison.LESS: "<", | ||
Comparison.GREATER_EQUAL: "≥", | ||
Comparison.LESS_EQUAL: "≤", | ||
}, | ||
label_style="hidden", | ||
).with_id(0), | ||
NumberInput( | ||
label="Left", | ||
min=None, | ||
max=None, | ||
precision=100, | ||
step=1, | ||
).with_id(1), | ||
NumberInput( | ||
label="Right", | ||
min=None, | ||
max=None, | ||
precision=100, | ||
step=1, | ||
).with_id(2), | ||
], | ||
outputs=[ | ||
BoolOutput( | ||
label="Result", | ||
output_type=""" | ||
let op = Input0; | ||
let l = Input1; | ||
let r = Input2; | ||
match op { | ||
Comparison::Equal => l == r, | ||
Comparison::NotEqual => l != r, | ||
Comparison::Greater => l > r, | ||
Comparison::Less => l < r, | ||
Comparison::GreaterEqual => l >= r, | ||
Comparison::LessEqual => l <= r, | ||
} | ||
""", | ||
).suggest(), | ||
], | ||
key_info=KeyInfo.enum(0), | ||
) | ||
def compare_node(op: Comparison, left: float, right: float) -> bool: | ||
if op == Comparison.EQUAL: | ||
return left == right | ||
elif op == Comparison.NOT_EQUAL: | ||
return left != right | ||
elif op == Comparison.GREATER: | ||
return left > right | ||
elif op == Comparison.LESS: | ||
return left < right | ||
elif op == Comparison.GREATER_EQUAL: | ||
return left >= right | ||
elif op == Comparison.LESS_EQUAL: | ||
return left <= right |
33 changes: 33 additions & 0 deletions
33
backend/src/packages/chaiNNer_standard/utility/value/conditional.py
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,33 @@ | ||
from __future__ import annotations | ||
|
||
from api import Lazy | ||
from nodes.properties.inputs import AnyInput, BoolInput | ||
from nodes.properties.outputs import BaseOutput | ||
|
||
from .. import value_group | ||
|
||
|
||
@value_group.register( | ||
schema_id="chainner:utility:conditional", | ||
name="Conditional", | ||
description="Allows you to pass in multiple inputs and then change which one passes through to the output.", | ||
icon="BsShuffle", | ||
inputs=[ | ||
BoolInput("Condition", default=True, has_handle=True).with_id(0), | ||
AnyInput(label="If True").make_lazy(), | ||
AnyInput(label="If False").make_lazy(), | ||
], | ||
outputs=[ | ||
BaseOutput( | ||
output_type=""" | ||
if Input0 { Input1 } else { Input2 } | ||
""", | ||
label="Value", | ||
).as_passthrough_of(1), | ||
], | ||
see_also=["chainner:utility:switch"], | ||
) | ||
def conditional_node( | ||
cond: bool, if_true: Lazy[object], if_false: Lazy[object] | ||
) -> object: | ||
return if_true.value if cond else if_false.value |
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