Skip to content

Commit

Permalink
Add a validation option to ignore float infinities in bin edges (#257)
Browse files Browse the repository at this point in the history
Fixes #255
  • Loading branch information
nsmith- authored Aug 6, 2024
1 parent 58e4112 commit dd28009
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/correctionlib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")


Expand Down
5 changes: 4 additions & 1 deletion src/correctionlib/schemav2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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."
)
Expand Down

0 comments on commit dd28009

Please sign in to comment.