Skip to content

Commit

Permalink
Move resources to top level directory (#29)
Browse files Browse the repository at this point in the history
* move resources directory to base

* auto save the output file of mu.py in appropriate path and test

* exclude resources

* set concurrency

* fix indent

* reorder mu.py test so that its output can be tested

* rename pytest.yml to build_and_test.yml

* install with optional resources flag

* fix paths

* Update pyproject.toml pythia8mc

* fix init
  • Loading branch information
tianluyuan authored Nov 12, 2024
1 parent d2eb6bb commit b607989
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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() }}
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')`.
```
Expand Down
2 changes: 1 addition & 1 deletion nuVeto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__all__ = ['mu', 'utils', 'nuveto',
'barr_uncertainties', 'external', 'examples']
'uncertainties', 'external', 'examples']
Empty file removed nuVeto/resources/__init__.py
Empty file.
Empty file removed nuVeto/resources/mu/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 21 additions & 10 deletions nuVeto/resources/mu/mu.py → resources/mu/mu.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand All @@ -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',
Expand All @@ -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()
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit b607989

Please sign in to comment.