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

Fix PyROOT binding registration #224

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
- python-version: pypy-3.7
runs-on: ubuntu-latest


steps:
- uses: actions/checkout@v4
with:
Expand All @@ -52,3 +51,33 @@ jobs:

- name: Test package
run: python -m pytest -ra

checkroot:
name: Check ROOT bindings
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up Conda
uses: conda-incubator/[email protected]
with:
python-version: 3.11
miniforge-variant: Mambaforge
channels: conda-forge,defaults
channel-priority: true

- name: Install ROOT
shell: bash -l {0}
run: |
mamba install root

- name: Install package
shell: bash -l {0}
run: python -m pip install .[test]

- name: Test package
shell: bash -l {0}
run: python -m pytest -ra
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
args: ["--py36-plus"]

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
rev: 7.0.0
hooks:
- id: flake8
exclude: docs/conf.py
Expand Down
2 changes: 1 addition & 1 deletion src/correctionlib/JSONEncoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import gzip
import json
import math
from typing import Any, List, Type
from typing import Any, List, Type # noqa: F401

import pydantic

Expand Down
4 changes: 2 additions & 2 deletions src/correctionlib/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def register_pyroot_binding() -> None:
lib = lib / "libcorrectionlib.dylib"
else:
lib = lib / "libcorrectionlib.so"
gbl.gSystem.Load(lib)
gbl.gInterpreter.AddIncludePath(base_path / "include")
gbl.gSystem.Load(str(lib))
gbl.gInterpreter.AddIncludePath(str(base_path / "include"))
gbl.gROOT.ProcessLine('#include "correction.h"')
36 changes: 36 additions & 0 deletions tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

import correctionlib
import correctionlib.schemav2 as cs


def test_pyroot_binding():
ROOT = pytest.importorskip("ROOT")
correctionlib.register_pyroot_binding()
assert ROOT.correction.CorrectionSet

ptweight = cs.Correction(
name="ptweight",
version=1,
inputs=[
cs.Variable(name="pt", type="real", description="Muon transverse momentum")
],
output=cs.Variable(
name="weight", type="real", description="Multiplicative event weight"
),
data=cs.Binning(
nodetype="binning",
input="pt",
edges=[10, 20, 30, 40, 50, 80, 120],
content=[1.1, 1.08, 1.06, 1.04, 1.02, 1.0],
flow="clamp",
),
)
cset = cs.CorrectionSet(schema_version=2, corrections=[ptweight])
csetstr = cset.model_dump_json().replace('"', r"\"")

ROOT.gInterpreter.Declare(
f'auto cset = correction::CorrectionSet::from_string("{csetstr}");' # noqa: B907
)
ROOT.gInterpreter.Declare('auto corr = cset->at("ptweight");')
assert ROOT.corr.evaluate([1.2]) == 1.1
2 changes: 1 addition & 1 deletion tests/test_core_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_evaluator_validation():
]

for json in bad_json:
with pytest.raises(ValidationError):
with pytest.raises(ValidationError): # noqa: B908
schema.CorrectionSet.model_validate_json(json)
pytest.fail(f"{json} did not fail validation")
with pytest.raises(RuntimeError):
Expand Down
Loading