-
Notifications
You must be signed in to change notification settings - Fork 30
Add Switch node to correctionlib for flexible logic #318
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,6 +130,7 @@ namespace { | |
| else if ( type == "formularef" ) { return FormulaRef(obj, context); } | ||
| else if ( type == "transform" ) { return Transform(obj, context); } | ||
| else if ( type == "hashprng" ) { return HashPRNG(obj, context); } | ||
| else if ( type == "switch" ) { return Switch(obj, context); } | ||
| else { throw std::runtime_error("Unrecognized Content object nodetype"); } | ||
| } | ||
| throw std::runtime_error("Invalid Content node type"); | ||
|
|
@@ -670,6 +671,62 @@ double Category::evaluate(const std::vector<Variable::Type>& values) const { | |
| return std::visit(node_evaluate{values}, *child); | ||
| } | ||
|
|
||
| Switch::Switch(const JSONObject& json, const Correction& context) { | ||
| json.getRequired<rapidjson::Value::ConstArray>("inputs"); | ||
|
|
||
| for (const auto& item : json.getRequired<rapidjson::Value::ConstArray>("selections")) { | ||
| if ( ! item.IsObject() ) { throw std::runtime_error("Expected Comparison object in Switch selections"); } | ||
| JSONObject comp(item.GetObject()); | ||
|
|
||
| Selection sel; | ||
| sel.variableIdx = find_input_index(comp.getRequired<const char*>("variable"), context.inputs()); | ||
|
|
||
| if ( context.inputs()[sel.variableIdx].type() == Variable::VarType::string ) { | ||
| throw std::runtime_error("Switch node currently only supports numeric comparisons"); | ||
| } | ||
|
|
||
| sel.op = comp.getRequired<std::string_view>("op"); | ||
| sel.value = comp.getRequired<double>("value"); | ||
| sel.content = std::make_unique<Content>(resolve_content(comp.getRequiredValue("content"), context)); | ||
|
|
||
| selections_.push_back(std::move(sel)); | ||
| } | ||
|
|
||
| default_ = std::make_unique<Content>(resolve_content(json.getRequiredValue("default"), context)); | ||
| } | ||
|
|
||
| double Switch::evaluate(const std::vector<Variable::Type>& values) const { | ||
| for (const auto& sel : selections_) { | ||
| double val = 0.0; | ||
| const auto& input = values[sel.variableIdx]; | ||
|
|
||
| if ( auto v = std::get_if<double>(&input) ) { | ||
| val = *v; | ||
| } else if ( auto v = std::get_if<int64_t>(&input) ) { | ||
| val = static_cast<double>(*v); | ||
| } else { | ||
| throw std::runtime_error("Switch node encountered non-numeric input value during evaluation"); | ||
| } | ||
|
|
||
| bool pass = false; | ||
|
|
||
| if (sel.op == "<") pass = (val < sel.value); | ||
| else if (sel.op == ">") pass = (val > sel.value); | ||
| else if (sel.op == "<=") pass = (val <= sel.value); | ||
| else if (sel.op == ">=") pass = (val >= sel.value); | ||
| else if (sel.op == "==") pass = (val == sel.value); | ||
| else if (sel.op == "!=") pass = (val != sel.value); | ||
| else { | ||
| throw std::runtime_error("Unknown operator in Switch node: " + sel.op); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once |
||
| if (pass) { | ||
| return std::visit(node_evaluate{values}, *sel.content); | ||
| } | ||
| } | ||
| return std::visit(node_evaluate{values}, *default_); | ||
| } | ||
|
|
||
| Correction::Correction(const JSONObject& json) : | ||
| name_(json.getRequired<const char *>("name")), | ||
| description_(json.getOptional<const char*>("description").value_or("")), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import json | ||
|
|
||
| import correctionlib | ||
| import correctionlib.schemav2 as cs | ||
|
|
||
| corr = cs.Correction( | ||
| name="boundary_test", | ||
| version=1, | ||
| inputs=[cs.Variable(name="eta", type="real")], | ||
| output=cs.Variable(name="weight", type="real"), | ||
| data=cs.Switch( | ||
| nodetype="switch", | ||
| inputs=["eta"], | ||
| selections=[ | ||
| cs.Comparison( | ||
| variable="eta", | ||
| op="<=", | ||
| value=3.0, | ||
| content=1.0, | ||
| ) | ||
| ], | ||
| default=0.0, | ||
| ), | ||
| ) | ||
|
|
||
| json_str = corr.model_dump_json(exclude_unset=True) | ||
| print(f"Generated JSON with Switch node:\n{json_str}\n") | ||
|
|
||
| cset_json = json.dumps({"schema_version": 2, "corrections": [json.loads(json_str)]}) | ||
| cset = correctionlib.CorrectionSet.from_string(cset_json) | ||
| evaluator = cset["boundary_test"] | ||
|
|
||
| print("-" * 40) | ||
| print("TESTING C++ EVALUATOR") | ||
| print("-" * 40) | ||
|
|
||
| val_inclusive = 3.0 | ||
| res_inclusive = evaluator.evaluate([val_inclusive]) | ||
| print(f"Input: {val_inclusive:<10} | Expected: 1.0 | Got: {res_inclusive}") | ||
|
|
||
| val_exclusive = 3.00001 | ||
| res_exclusive = evaluator.evaluate([val_exclusive]) | ||
| print(f"Input: {val_exclusive:<10} | Expected: 0.0 | Got: {res_exclusive}") | ||
|
|
||
| if res_inclusive == 1.0 and res_exclusive == 0.0: | ||
| print("\n[SUCCESS] The Switch node correctly handles inclusive boundaries!") | ||
| else: | ||
| print("\n[FAIL] Logic error in Switch node implementation.") | ||
|
Comment on lines
+26
to
+48
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the pytest framework to pick up this test you need to wrap this whole block into a |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert the string to an operation Enum here so that unknown operators raise on construction rather than evaluation. This also makes the comparison on evaluation much faster by not doing a string comparison for every call.