From dd280098aed334c4e08b2f441b6d94177ca6f571 Mon Sep 17 00:00:00 2001 From: Nicholas Smith Date: Tue, 6 Aug 2024 10:59:41 -0500 Subject: [PATCH] Add a validation option to ignore float infinities in bin edges (#257) Fixes #255 --- src/correctionlib/cli.py | 8 ++++++++ src/correctionlib/schemav2.py | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/correctionlib/cli.py b/src/correctionlib/cli.py index 2e96922..cf0364a 100644 --- a/src/correctionlib/cli.py +++ b/src/correctionlib/cli.py @@ -9,11 +9,14 @@ from rich.console import Console import correctionlib.version +from correctionlib import schemav2 from correctionlib.highlevel import model_auto, open_auto def validate(console: Console, args: argparse.Namespace) -> int: """Check if all files are valid""" + if args.ignore_float_inf: + schemav2.IGNORE_FLOAT_INF = True retcode = 0 for file in args.files: try: @@ -65,6 +68,11 @@ def setup_validate(subparsers: Any) -> None: default=None, help="Validate against specific schema version", ) + parser.add_argument( + "--ignore-float-inf", + action="store_true", + help="Disable errors for use of float infinities in bin edges (as required in v2.6 and later)", + ) parser.add_argument("files", nargs="+", metavar="FILE") diff --git a/src/correctionlib/schemav2.py b/src/correctionlib/schemav2.py index ddc1266..5f760b9 100644 --- a/src/correctionlib/schemav2.py +++ b/src/correctionlib/schemav2.py @@ -32,6 +32,9 @@ VERSION = 2 +# See https://github.com/cms-nanoAOD/correctionlib/issues/255 +IGNORE_FLOAT_INF = False + class Model(BaseModel): model_config = ConfigDict(extra="forbid") @@ -199,7 +202,7 @@ def validate_nonuniform_edges(edges: Edges) -> Edges: if edge in ("inf", "+inf", "-inf"): continue if isinstance(edge, float): - if not math.isfinite(edge): + if not IGNORE_FLOAT_INF and not math.isfinite(edge): raise ValueError( f"Edges array contains non-finite values: {edges}. Replace infinities with 'inf' or '-inf'. NaN is not allowed." )