IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 227bc6ea authored by Remy Prechelt's avatar Remy Prechelt
Browse files

Add `read_hist` method with coverage info.

parent dfcf323b
No related branches found
No related tags found
No related merge requests found
......@@ -622,8 +622,8 @@ sanity:
- cd ${CI_PROJECT_DIR}/Python # change into the Python directory
- pip install --user -e '.[test]' # install the package + test deps
- make all 2&>1 | tee python-test.log # this runs all of the Python tests
- echo "finished" >> python-test.log # create even an empty file...
- cd ${CI_PROJECT_DIR} # reset the directory
- cd ${CI_PROJECT_DIR} # reset the directory
coverage: '/^TOTAL\s*\d+\s*\d+\s*(.*\%)/'
artifacts:
when: always
expire_in: 1 year
......
......@@ -16,7 +16,7 @@ tests:
${PYTHON} -m pytest --cov=corsika tests
flake:
${PYTHON} -m flake8 corsika
${PYTHON} -m flake8 corsika tests
black:
${PYTHON} -m black -t py37 corsika tests
......
"""
A Python interface to CORSIKA 8.
(c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
This software is distributed under the terms of the GNU General Public
......@@ -6,4 +8,9 @@
the license.
"""
from . import io
# all imported objects
__all__ = ["io"]
__version__: str = "8.0.0-alpha"
"""
The 'io' module provides for reading CORSIKA8 output files.
(c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
This software is distributed under the terms of the GNU General Public
Licence version 3 (GPL Version 3). See file LICENSE for a full version of
the license.
"""
from .hist import read_hist
# all exported objects
__all__ = ["read_hist"]
"""
This file supports reading boost_histograms from CORSIKA8.
(c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
This software is distributed under the terms of the GNU General Public
Licence version 3 (GPL Version 3). See file LICENSE for a full version of
the license.
"""
import boost_histogram as bh
import numpy as np
def read_hist(filename: str) -> bh.Histogram:
"""
Read a histogram produced with CORSIKA8's `save_hist()` function.
Parameters
----------
filename: str
The filename of the .npy file containing the histogram.
Returns
-------
hist: bh.Histogram
An initialized bh.Histogram instance.
Throws
------
ValueError:
If the histogram type is not supported.
"""
# load the filenames
d = np.load(filename)
# extract the axis and overflows information
axistypes = d["axistypes"].view("c")
overflow = d["overflow"]
underflow = d["underflow"]
# this is where we store the axes that we extract from the file.
axes = []
# we now loop over the axes
for i, (at, has_overflow, has_underflow) in enumerate(
zip(axistypes, overflow, underflow)
):
# continuous histogram
if at == b"c":
axes.append(
bh.axis.Variable(
d[f"binedges_{i}"], overflow=has_overflow, underflow=has_underflow
)
)
# discrete histogram
elif at == b"d":
axes.append(bh.axis.IntCategory(d[f"bins_{i}"], growth=(not has_overflow)))
# and unknown histogram type
else:
raise ValueError(f"'{at}' is not a valid C8 histogram axistype.")
# create the histogram and fill it in
h = bh.Histogram(*axes)
h.view(flow=True)[:] = d["data"]
return h
......@@ -59,3 +59,7 @@ ignore_missing_imports = True
# ignore missing types for matplotlib
[mypy-matplotlib.*]
ignore_missing_imports = True
# ignore missing types for boost_histogram
[mypy-boost_histogram.*]
ignore_missing_imports = True
......@@ -32,7 +32,7 @@ setup(
keywords=["cosmic ray", "physics", "astronomy", "simulation"],
packages=["corsika"],
python_requires=">=3.6*, <4",
install_requires=["numpy", "pyyaml",],
install_requires=["numpy", "pyyaml", "boost_histogram"],
extras_require={
"test": [
"pytest",
......
"""
Tests for `corsika.io.hist`
(c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
This software is distributed under the terms of the GNU General Public
Licence version 3 (GPL Version 3). See file LICENSE for a full version of
the license.
"""
import pytest
import corsika
def test_corsika_io() -> None:
"""
Test I can corsika.io without a further import.
"""
corsika.io.read_hist
def test_corsika_read_hist() -> None:
"""
Check that I can read in the test histograms with `read_hist`.
"""
# try and read in a continuous histogram
# try and read in a discrete histogram
def test_corsika_read_hist_fail() -> None:
"""
Check that an exception is thrown when reading
an incorrectly formatted histogram.
"""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment