diff --git a/.github/workflows/pytest.yml b/.github/workflows/build_and_test.yml similarity index 83% rename from .github/workflows/pytest.yml rename to .github/workflows/build_and_test.yml index 25ba370..87fa5fc 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/build_and_test.yml @@ -19,6 +19,10 @@ on: # * is a special character in YAML so you have to quote this string - cron: '25 9 * * 2' +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + jobs: build: @@ -44,8 +48,12 @@ jobs: # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - name: Run pytest + run: pytest + - name: Test mu.py run: | - pytest + pip install -e .[resources] + ./resources/mu/mu.py -o fake/path/mymudet.pkl --plight pl_step_1000 resources/mu/mmc/ice_allm97.pklz + pytest -k test_pdet results: if: ${{ always() }} diff --git a/README.md b/README.md index e06979e..0069140 100644 --- a/README.md +++ b/README.md @@ -57,14 +57,16 @@ See `examples/plots.py` for more detailed examples. ![Pdet](/paper/figs_for_readme/prpl_step1000.png?raw=true) -To calculate the passing fraction requires knowing the muon detection pdf as a function of the overburden and energy of the muon at the surface. This is constructed from a convolution of the muon reaching probability and the detector response. The muon reaching probability is constructed from MMC simulations and is provided for propagation in ice in `resources/mu/mmc/ice.pklz`. The detector response probability must be defined in `resources/mu/pl.py` as a function of the muon energy (at detector). Then, construct the overall muon detection pdf and place it into the correct location. +To calculate the passing fraction requires knowing the muon detection probability as a function of the overburden and energy of the muon at the surface. This is constructed from a convolution of the muon reaching probability and the detector response. The scripts for generating the necessary files are not packaged but provided in the `resources/` directory, which can be obtained with a clone of this repository. They also require some extra dependencies, which can be installed with `pip install nuVeto[resources]`. + +The muon reaching probability is constructed from MMC simulations and is provided for propagation in ice in `resources/mu/mmc/ice_(allm97|bb).pklz` for two different cross section parameterizations. The detector response probability must be defined in `resources/mu/pl.py` as a function of the muon energy (at detector). Then, construct the overall muon detection probability. ```bash -cd nuVeto/resources/mu -./mu.py -o ../../prpl/mymudet.pkl --plight pl_step_1000 mmc/ice_allm97.pklz +cd resources/mu +./mu.py -o mymudet.pkl --plight pl_step_1000 mmc/ice_allm97.pklz ``` -To use the newly generated file, pass it as a string to the `prpl` argument. +To use the newly generated file, pass the stem without file extension as a string to the `prpl` argument. ```bash passing(enu, cos_theta, prpl='mymudet')`. ``` diff --git a/nuVeto/__init__.py b/nuVeto/__init__.py index 3c456d7..70a816f 100644 --- a/nuVeto/__init__.py +++ b/nuVeto/__init__.py @@ -1,2 +1,2 @@ __all__ = ['mu', 'utils', 'nuveto', - 'barr_uncertainties', 'external', 'examples'] + 'uncertainties', 'external', 'examples'] diff --git a/nuVeto/resources/__init__.py b/nuVeto/resources/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/nuVeto/resources/mu/__init__.py b/nuVeto/resources/mu/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/pyproject.toml b/pyproject.toml index 782ba44..6affac2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,13 +29,13 @@ classifiers = [ [project.optional-dependencies] plotting = ["matplotlib"] -resources = ["pythia8", "matplotlib", "pandas"] +resources = ["pythia8mc", "matplotlib", "pandas"] testing = ["pytest"] [project.urls] Repository = "https://github.com/tianluyuan/nuVeto.git" [tool.setuptools.packages.find] -exclude = ["paper*"] +exclude = ["paper*", "resources*"] [tool.setuptools_scm] diff --git a/nuVeto/resources/mu/mmc/ice_allm97.pklz b/resources/mu/mmc/ice_allm97.pklz similarity index 100% rename from nuVeto/resources/mu/mmc/ice_allm97.pklz rename to resources/mu/mmc/ice_allm97.pklz diff --git a/nuVeto/resources/mu/mmc/ice_bb.pklz b/resources/mu/mmc/ice_bb.pklz similarity index 100% rename from nuVeto/resources/mu/mmc/ice_bb.pklz rename to resources/mu/mmc/ice_bb.pklz diff --git a/nuVeto/resources/mu/mmc/water_allm97.pklz b/resources/mu/mmc/water_allm97.pklz similarity index 100% rename from nuVeto/resources/mu/mmc/water_allm97.pklz rename to resources/mu/mmc/water_allm97.pklz diff --git a/nuVeto/resources/mu/mmc/water_bb.pklz b/resources/mu/mmc/water_bb.pklz similarity index 100% rename from nuVeto/resources/mu/mmc/water_bb.pklz rename to resources/mu/mmc/water_bb.pklz diff --git a/nuVeto/resources/mu/mu.py b/resources/mu/mu.py similarity index 74% rename from nuVeto/resources/mu/mu.py rename to resources/mu/mu.py index 40bc726..93ed282 100755 --- a/nuVeto/resources/mu/mu.py +++ b/resources/mu/mu.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 - -import os +from pathlib import Path from collections import namedtuple import pickle import gzip import argparse import numpy as np import pandas as pd +import logging from scipy import interpolate -from nuVeto.resources.mu import pl +from importlib import resources from nuVeto.utils import calc_bins, centers +import pl def hist_preach(infile): @@ -35,7 +36,7 @@ def hist_preach(infile): def int_ef(preach, plight): """ integate p_reach*p_light over e_f to reduce dimensionality for interpolator """ - if isinstance(preach, str) and os.path.isfile(preach): + if isinstance(preach, str) and Path(preach).is_file(): try: preach = pickle.load(gzip.open(preach, 'rb'), encoding='latin1') except IOError: @@ -55,7 +56,9 @@ def interp(preach, plight): return interpolate.RegularGridInterpolator((df.index, df.columns), df.values, bounds_error=False, fill_value=None) -if __name__ == '__main__': +def main(): + logger = logging.getLogger('mu.py') + logging.basicConfig(encoding='utf-8', level=logging.INFO) parser = argparse.ArgumentParser( description='Generate muon detection probability') parser.add_argument('mmc', metavar='MMC', @@ -65,14 +68,22 @@ def interp(preach, plight): help='choice of a plight function to apply as defined in pl.py') parser.add_argument('--noconvolution', default=False, action='store_true', help='Generate pdfs of preach from raw MMC output and save to pklz') - parser.add_argument('-o', dest='output', default='mu.pkl', - help='output file. To be read in this needs to be in "nuVeto/data/prpl/"') + parser.add_argument('-o', dest='output', default='mu.pkl', type=Path, + help='output file. If --noconvolution is False this will be saved into the necessary package directory.') args = parser.parse_args() if args.noconvolution: + output = args.output hpr = hist_preach(args.mmc) - pickle.dump(hpr, gzip.open(args.output, 'wb'), protocol=-1) + pickle.dump(hpr, gzip.open(output, 'wb'), protocol=-1) else: + output = resources.files('nuVeto') / 'data' / 'prpl' / args.output.name + if args.output.name != str(args.output): + logger.warning(f'Overriding {args.output} to {output}. To suppress this warning pass the filename only.') intp = interp(args.mmc, getattr(pl, args.plight)) - pickle.dump(intp, open(args.output, 'wb'), protocol=-1) - print(f'Output pickled into {args.output}') + pickle.dump(intp, open(output, 'wb'), protocol=-1) + logger.info(f'Output pickled into {output}') + + +if __name__ == '__main__': + main() diff --git a/nuVeto/resources/mu/pl.py b/resources/mu/pl.py similarity index 100% rename from nuVeto/resources/mu/pl.py rename to resources/mu/pl.py diff --git a/nuVeto/resources/mu/reaching.py b/resources/mu/reaching.py similarity index 100% rename from nuVeto/resources/mu/reaching.py rename to resources/mu/reaching.py diff --git a/nuVeto/resources/pythia/pythia_decay.py b/resources/pythia/pythia_decay.py similarity index 100% rename from nuVeto/resources/pythia/pythia_decay.py rename to resources/pythia/pythia_decay.py