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

test: Use context managers to avoid <_io.FileIO [closed]> warnings #1784

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
17 changes: 9 additions & 8 deletions docs/examples/notebooks/ImpactPlot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@
"outputs": [],
"source": [
"def make_model(channel_list):\n",
" spec = json.load(\n",
" open(\"1Lbb-probability-models/RegionA/BkgOnly.json\", encoding=\"utf-8\")\n",
" )\n",
" patchset = pyhf.PatchSet(\n",
" json.load(\n",
" open(\"1Lbb-probability-models/RegionA/patchset.json\", encoding=\"utf-8\")\n",
" )\n",
" )\n",
" with open(\n",
" \"1Lbb-probability-models/RegionA/BkgOnly.json\", encoding=\"utf-8\"\n",
" ) as spec_file:\n",
" spec = json.load(spec_file)\n",
" with open(\n",
" \"1Lbb-probability-models/RegionA/patchset.json\", encoding=\"utf-8\"\n",
" ) as patchset_file:\n",
" patchset = pyhf.PatchSet(json.load(patchset_file))\n",
"\n",
" patch = patchset[\"sbottom_750_745_60\"]\n",
" spec = jsonpatch.apply_patch(spec, patch)\n",
" spec[\"channels\"] = [c for c in spec[\"channels\"] if c[\"name\"] in channel_list]\n",
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/notebooks/multiBinPois.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
}
],
"source": [
"source = json.load(open(validation_datadir + \"/1bin_example1.json\", encoding=\"utf-8\"))\n",
"with open(validation_datadir + \"/1bin_example1.json\", encoding=\"utf-8\") as source_file:\n",
" source = json.load(source_file)\n",
"model = uncorrelated_background(\n",
" source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']\n",
")\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/notebooks/multichannel-coupled-histo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
"source": [
"with open(\n",
" validation_datadir + \"/2bin_2channel_coupledhisto.json\", encoding=\"utf-8\"\n",
") as spec:\n",
" source = json.load(spec)\n",
") as source_file:\n",
" source = json.load(source_file)\n",
"\n",
"data, pdf = prep_data(source[\"channels\"])\n",
"\n",
Expand Down
1 change: 1 addition & 0 deletions docs/examples/notebooks/pullplot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
" \"1Lbb-probability-models/RegionA/BkgOnly.json\", encoding=\"utf-8\"\n",
" ) as spec_file:\n",
" spec = json.load(spec_file)\n",
"\n",
" spec[\"channels\"] = [c for c in spec[\"channels\"] if c[\"name\"] in channel_list]\n",
" spec[\"measurements\"][0][\"config\"][\"poi\"] = \"lumi\"\n",
"\n",
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ filterwarnings = [
'ignore:distutils Version classes are deprecated:DeprecationWarning', # tensorflow-probability
'ignore:the `interpolation=` argument to percentile was renamed to `method=`, which has additional options:DeprecationWarning', # Issue #1772
"ignore:The interpolation= argument to 'quantile' is deprecated. Use 'method=' instead:DeprecationWarning", # Issue #1772
'ignore: Exception ignored in:pytest.PytestUnraisableExceptionWarning', #FIXME: Exception ignored in: <_io.FileIO [closed]>
'ignore:invalid value encountered in (true_)?divide:RuntimeWarning', #FIXME
'ignore:invalid value encountered in add:RuntimeWarning', #FIXME
"ignore:In future, it will be an error for 'np.bool_' scalars to be interpreted as an index:DeprecationWarning", #FIXME: tests/test_tensor.py::test_pdf_eval[pytorch]
Expand Down
47 changes: 28 additions & 19 deletions tests/contrib/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@


def test_brazil_band_collection(datadir):
data = json.load(datadir.joinpath("hypotest_results.json").open(encoding="utf-8"))
with open(datadir.join("hypotest_results.json"), encoding="utf-8") as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -32,9 +33,10 @@ def test_brazil_band_collection(datadir):
assert brazil_band_collection.clb is None
assert brazil_band_collection.axes == ax

data = json.load(
datadir.joinpath("tail_probs_hypotest_results.json").open(encoding="utf-8")
)
with open(
datadir.join("tail_probs_hypotest_results.json"), encoding="utf-8"
) as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -55,7 +57,8 @@ def test_brazil_band_collection(datadir):

@pytest.mark.mpl_image_compare
def test_plot_results(datadir):
data = json.load(datadir.joinpath("hypotest_results.json").open(encoding="utf-8"))
with open(datadir.join("hypotest_results.json"), encoding="utf-8") as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -73,7 +76,8 @@ def test_plot_results(datadir):
reason="baseline image generated with matplotlib v3.6.0 which is Python 3.8+",
)
def test_plot_results_no_axis(datadir):
data = json.load(datadir.joinpath("hypotest_results.json").open(encoding="utf-8"))
with open(datadir.join("hypotest_results.json"), encoding="utf-8") as data_file:
data = json.load(data_file)

matplotlib.use("agg") # Use non-gui backend
fig, ax = plt.subplots()
Expand All @@ -85,9 +89,10 @@ def test_plot_results_no_axis(datadir):

@pytest.mark.mpl_image_compare
def test_plot_results_components(datadir):
data = json.load(
datadir.joinpath("tail_probs_hypotest_results.json").open(encoding="utf-8")
)
with open(
datadir.join("tail_probs_hypotest_results.json"), encoding="utf-8"
) as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -99,9 +104,10 @@ def test_plot_results_components(datadir):

@pytest.mark.mpl_image_compare
def test_plot_results_components_no_clb(datadir):
data = json.load(
datadir.joinpath("tail_probs_hypotest_results.json").open(encoding="utf-8")
)
with open(
datadir.join("tail_probs_hypotest_results.json"), encoding="utf-8"
) as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -121,9 +127,10 @@ def test_plot_results_components_no_clb(datadir):

@pytest.mark.mpl_image_compare
def test_plot_results_components_no_clsb(datadir):
data = json.load(
datadir.joinpath("tail_probs_hypotest_results.json").open(encoding="utf-8")
)
with open(
datadir.join("tail_probs_hypotest_results.json"), encoding="utf-8"
) as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand All @@ -143,9 +150,10 @@ def test_plot_results_components_no_clsb(datadir):

@pytest.mark.mpl_image_compare
def test_plot_results_components_no_cls(datadir):
data = json.load(
datadir.joinpath("tail_probs_hypotest_results.json").open(encoding="utf-8")
)
with open(
datadir.join("tail_probs_hypotest_results.json"), encoding="utf-8"
) as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand Down Expand Up @@ -173,7 +181,8 @@ def test_plot_results_components_data_structure(datadir):
"""
test results should have format of: [CLs_obs, [CLsb, CLb], [CLs_exp band]]
"""
data = json.load(datadir.joinpath("hypotest_results.json").open(encoding="utf-8"))
with open(datadir.join("hypotest_results.json"), encoding="utf-8") as data_file:
data = json.load(data_file)

fig = Figure()
ax = fig.subplots()
Expand Down
16 changes: 8 additions & 8 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def spec_staterror():
def spec_histosys():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -93,8 +93,8 @@ def spec_histosys():
def spec_normsys():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -128,8 +128,8 @@ def spec_normsys():
def spec_shapesys():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -159,8 +159,8 @@ def spec_shapesys():
def spec_shapefactor():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/test_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def test_invalid_bin_wise_modifier(datadir, patch_file):

assert pyhf.Model(spec)

with open(datadir.joinpath(patch_file), encoding="utf-8") as spec_file:
patch = JsonPatch.from_string(spec_file.read())
with open(datadir.joinpath(patch_file), encoding="utf-8") as read_file:
patch = JsonPatch.from_string(read_file.read())
bad_spec = patch.apply(spec)

with pytest.raises(pyhf.exceptions.InvalidModifier):
Expand Down
35 changes: 18 additions & 17 deletions tests/test_patchset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def patch():
],
)
def test_patchset_invalid_spec(datadir, patchset_file):
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patch_file:
patchsetspec = json.load(patch_file)
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patchset_spec_file:
patchsetspec = json.load(patchset_spec_file)
with pytest.raises(pyhf.exceptions.InvalidSpecification):
pyhf.PatchSet(patchsetspec)

Expand All @@ -48,8 +48,8 @@ def test_patchset_invalid_spec(datadir, patchset_file):
],
)
def test_patchset_bad(datadir, patchset_file):
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patch_file:
patchsetspec = json.load(patch_file)
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patchset_spec_file:
patchsetspec = json.load(patchset_spec_file)
with pytest.raises(pyhf.exceptions.InvalidPatchSet):
pyhf.PatchSet(patchsetspec)

Expand Down Expand Up @@ -102,29 +102,29 @@ def test_patchset_repr(patchset):
def test_patchset_verify(datadir):
with open(
datadir.joinpath("example_patchset.json"), encoding="utf-8"
) as patch_file:
patchset = pyhf.PatchSet(json.load(patch_file))
with open(datadir.joinpath("example_bkgonly.json"), encoding="utf-8") as ws_file:
ws = pyhf.Workspace(json.load(ws_file))
) as patchset_file:
patchset = pyhf.PatchSet(json.load(patchset_file))
with open(datadir.joinpath("example_bkgonly.json"), encoding="utf-8") as bkg_file:
ws = pyhf.Workspace(json.load(bkg_file))
assert patchset.verify(ws) is None


def test_patchset_verify_failure(datadir):
with open(
datadir.joinpath("example_patchset.json"), encoding="utf-8"
) as patch_file:
patchset = pyhf.PatchSet(json.load(patch_file))
) as patchset_file:
patchset = pyhf.PatchSet(json.load(patchset_file))
with pytest.raises(pyhf.exceptions.PatchSetVerificationError):
assert patchset.verify({})


def test_patchset_apply(datadir):
with open(
datadir.joinpath("example_patchset.json"), encoding="utf-8"
) as patch_file:
patchset = pyhf.PatchSet(json.load(patch_file))
with open(datadir.joinpath("example_bkgonly.json"), encoding="utf-8") as ws_file:
ws = pyhf.Workspace(json.load(ws_file))
) as patchset_file:
patchset = pyhf.PatchSet(json.load(patchset_file))
with open(datadir.joinpath("example_bkgonly.json"), encoding="utf-8") as bkg_file:
ws = pyhf.Workspace(json.load(bkg_file))
with mock.patch('pyhf.patchset.PatchSet.verify') as m:
assert m.call_count == 0
assert patchset.apply(ws, 'patch_channel1_signal_syst1')
Expand All @@ -149,9 +149,10 @@ def test_patch_equality(patch):

def test_patchset_get_string_values(datadir):
with open(
datadir.joinpath('patchset_good_stringvalues.json'), encoding="utf-8"
) as patch_file:
patchset = pyhf.PatchSet(json.load(patch_file))
datadir.joinpath("patchset_good_stringvalues.json"), encoding="utf-8"
) as patchset_file:
patchset = pyhf.PatchSet(json.load(patchset_file))

assert patchset["Gtt_2100_5000_800"]
assert patchset["Gbb_2200_5000_800"]
assert patchset[[2100, 800, "Gtt"]]
Expand Down
20 changes: 10 additions & 10 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ def test_pdf_integration_shapesys_zeros(backend):
def test_pdf_integration_histosys(backend):
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -438,8 +438,8 @@ def test_pdf_integration_histosys(backend):
def test_pdf_integration_normsys(backend):
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -502,8 +502,8 @@ def test_pdf_integration_normsys(backend):
def test_pdf_integration_shapesys(backend):
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -628,8 +628,8 @@ def test_invalid_modifier_name_resuse():
def test_override_paramset_defaults():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down Expand Up @@ -664,8 +664,8 @@ def test_override_paramset_defaults():
def test_override_paramsets_incorrect_num_parameters():
with open(
"validation/data/2bin_histosys_example2.json", encoding="utf-8"
) as spec_file:
source = json.load(spec_file)
) as source_file:
source = json.load(source_file)
spec = {
'channels': [
{
Expand Down
12 changes: 6 additions & 6 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ def test_jsonpatch_fail(patch):

@pytest.mark.parametrize('patchset_file', ['patchset_good.json'])
def test_patchset(datadir, patchset_file):
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patch_file:
patchset = json.load(patch_file)
pyhf.schema.validate(patchset, 'patchset.json')
with open(datadir.joinpath(patchset_file), encoding="utf-8") as read_file:
patchset = json.load(read_file)
pyhf.schema.validate(patchset, "patchset.json")


@pytest.mark.parametrize(
Expand All @@ -596,10 +596,10 @@ def test_patchset(datadir, patchset_file):
],
)
def test_patchset_fail(datadir, patchset_file):
with open(datadir.joinpath(patchset_file), encoding="utf-8") as patch_file:
patchset = json.load(patch_file)
with open(datadir.joinpath(patchset_file), encoding="utf-8") as read_file:
patchset = json.load(read_file)
with pytest.raises(pyhf.exceptions.InvalidSpecification):
pyhf.schema.validate(patchset, 'patchset.json')
pyhf.schema.validate(patchset, "patchset.json")


def test_defs_always_cached(
Expand Down