Skip to content

Commit

Permalink
Clean up tests and typos.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlarson committed Nov 3, 2024
1 parent 135697f commit 066bdca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
24 changes: 19 additions & 5 deletions src/simweights/_genie_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def GenieWeighter(file_obj: Any, nfiles: float | None = None) -> Weighter: # no
Reads ``I3GenieInfo`` from S-Frames and ``I3GenieResult`` from Q-Frames for genie-reader files
and ``I3MCWeightDict`` and "I3GENIEResultDict" from Q-Frames for older legacy genie-icetray files.
"""
if has_table(file_obj, "I3GenieInfo"):
if not any(has_table(file_obj, colname) for colname in ["I3GenieInfo", "I3GenieResult", "I3GENIEResultDict"]):
msg = (
f"The file `{getattr(file_obj, 'filename', '<NONE>')}` does not contain at least one of I3GenieInfo, "
"I3GenieResult, or I3GENIEResultDict, so this is unlikely to be a GENIE file."
)
raise TypeError(msg)
if has_table(file_obj, "I3GenieInfo") and has_table(file_obj, "I3GenieResult"):
# Branch for newer genie-reader files
if nfiles is not None:
msg = (
Expand Down Expand Up @@ -123,14 +129,13 @@ def GenieWeighter(file_obj: Any, nfiles: float | None = None) -> Weighter: # no
volscale = np.ones_like(get_column(result_table, "wght"))
weighter.add_weight_column("volscale", volscale)

else:
elif has_table(file_obj, "I3MCWeightDict") and has_table(file_obj, "I3GENIEResultDict"):
# Branch for older genie-icetray files
if nfiles is None:
msg = (

Check warning on line 135 in src/simweights/_genie_weighter.py

View check run for this annotation

Codecov / codecov/patch

src/simweights/_genie_weighter.py#L135

Added line #L135 was not covered by tests
f"GenieWeighter received an nfiles={nfiles}, but `{getattr(file_obj, 'filename', '<NONE>')}` "
"was produced with genie-reader instead of genie-icetray. We expect to read the number of "
"files from the number of observed S-frames in the file, so this is unnecessary. Do not pass "
"in a value for nfiles for genie-reader files."
"was produced with genie-icetray instead of genie-reader. We require the number of files to be "
"passed in for genie-icetray files since we can't simply count S-frames."
)
raise RuntimeError(msg)

Check warning on line 140 in src/simweights/_genie_weighter.py

View check run for this annotation

Codecov / codecov/patch

src/simweights/_genie_weighter.py#L140

Added line #L140 was not covered by tests

Expand All @@ -149,4 +154,13 @@ def GenieWeighter(file_obj: Any, nfiles: float | None = None) -> Weighter: # no
weighter.add_weight_column("energy", get_column(result_table, "Ev"))
weighter.add_weight_column("cos_zen", cos_zen)
weighter.add_weight_column("wght", get_column(result_table, "wght") * get_column(result_table, "_glbprbscale"))

else:
msg = (
"Missing at least one necessary object for GENIE event weighting. If your file is produced by "
"genie-icetray, be sure to include both the I3MCWeightDict and I3GENIEResultDict in your input "
"file. If the file is produced by genie-reader, include both I3GenieInfo and I3GenieResult."
)
raise KeyError(msg)

return weighter
10 changes: 5 additions & 5 deletions tests/test_genie_icetray_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from simweights import GenieWeighter

datasets = [
"genie-icetray.140000A_000000.hdf",
"genie-icetray.140000B_000000.hdf",
"genie-icetray.140000C_000000.hdf",
"genie-icetray.140000D_000000.hdf",
"level2_genie-icetray.140000_000000.hdf",
"genie-icetray.140000A_000000.hdf5",
"genie-icetray.140000B_000000.hdf5",
"genie-icetray.140000C_000000.hdf5",
"genie-icetray.140000D_000000.hdf5",
"level2_genie-icetray.140000_000000.hdf5",
]
approx = pytest.approx
datadir = os.environ.get("SIMWEIGHTS_TESTDATA", None)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_genie_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ def test_genie(self):
Ewidth = np.ediff1d(x)
np.testing.assert_allclose(y, flux * event_weight * Ewidth * c1.etendue / nfiles, 5e-3)

with self.assertRaises(TypeError):
with self.assertRaises(RuntimeError):
simweights.GenieWeighter(d, nfiles=10)

with self.assertRaises(KeyError):
with self.assertRaises(TypeError):
simweights.GenieWeighter({"I3CorsikaWeight": weight})

with self.assertRaises(KeyError):
simweights.GenieWeighter({"I3GenieResult": weight})


if __name__ == "__main__":
unittest.main()

0 comments on commit 066bdca

Please sign in to comment.