Skip to content

Commit

Permalink
complience with scientific python guide (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjmeagher authored Jan 24, 2024
1 parent 8fa3831 commit 682989e
Show file tree
Hide file tree
Showing 33 changed files with 59 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
branches:
- main
pull_request:
workflow_dispatch:
jobs:
Docs:
runs-on: ubuntu-22.04
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
branches:
- main
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
Tests:
runs-on: ${{ matrix.os }}
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ repos:
rev: v4.0.0-alpha.8
hooks:
- id: prettier
files: \.ya?ml$
- repo: https://github.com/fsfe/reuse-tool
rev: v2.1.0
rev: v3.0.1
hooks:
- id: reuse
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
- repo: https://github.com/asottile/blacken-docs
- repo: https://github.com/adamchainz/blacken-docs
rev: "1.16.0"
hooks:
- id: blacken-docs
Expand All @@ -38,7 +39,7 @@ repos:
exclude: ^contrib/
additional_dependencies: [numpy, pandas]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
rev: v0.1.14
hooks:
- id: ruff
args: [--fix, --show-fixes]
Expand Down
1 change: 1 addition & 0 deletions contrib/print_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sys import argv

import numpy as np

from simweights import _fluxes

E = np.logspace(2, 10, 9)
Expand Down
2 changes: 1 addition & 1 deletion examples/additional_flux_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import pandas as pd
import pylab as plt
from numpy.typing import ArrayLike

# start-box1
import simweights
from numpy.typing import ArrayLike

# load the hdf5 file and make the weigher
hdffile = pd.HDFStore("Level2_IC86.2016_NuMu.021217.N100.hdf5", "r")
Expand Down
1 change: 1 addition & 0 deletions examples/basic_genie.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import nuflux
import pandas as pd

import simweights

simfile = pd.HDFStore("genie_reader_NuE_C_corr.hdf5")
Expand Down
3 changes: 2 additions & 1 deletion examples/combined_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import numpy as np
import pylab as plt
import simweights
import tables

import simweights

# start-box1
# load the Medium Energy file
ME_file = tables.File("Level2_IC86.2016_corsika.021746.hdf5", "r")
Expand Down
3 changes: 2 additions & 1 deletion examples/effective_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import numpy as np
import pylab as plt
import simweights
import tables

import simweights

# start-example1
# load hdf5 file
f = tables.open_file("Level2_IC86.2016_NuMu.021217.N100.hdf5", "r")
Expand Down
3 changes: 2 additions & 1 deletion examples/icetop_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import numpy as np
import pylab as plt
import simweights
import tables

import simweights

# —- Define which files you want to be part of the dataset ——
# (This particular example is all low-energy protons)
FILE_DIR = Path("/data/ana/CosmicRay/IceTop_level3/sim/IC86.2012/SIBYLL2.1/p/12360_v1s/h5files/")
Expand Down
3 changes: 2 additions & 1 deletion examples/livetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import numpy as np
import pylab as plt
import simweights
import tables

import simweights

# load hdf5 table
f = tables.open_file("Level2_IC86.2016_corsika.021682.N100.hdf5", "r")
wobj = simweights.CorsikaWeighter(f)
Expand Down
2 changes: 1 addition & 1 deletion examples/nugen_nuflux.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import nuflux
import pandas as pd
import pylab as plt
from numpy.typing import ArrayLike

# start-box1
import simweights
from numpy.typing import ArrayLike

# load the hdf5 file and make the weigher
hdffile = pd.HDFStore("Level2_IC86.2016_NuMu.021217.hdf5", "r")
Expand Down
3 changes: 2 additions & 1 deletion examples/nugen_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import pandas as pd
import pylab as plt
import simweights
from numpy.typing import ArrayLike

import simweights

# load the hdf5 file that we just created using pandas
hdffile = pd.HDFStore("Level2_IC86.2016_NuMu.021217.hdf5", "r")

Expand Down
1 change: 1 addition & 0 deletions examples/triggered_corsika_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pandas as pd
import pylab as plt

import simweights

# load the hdf5 file that we just created using pandas
Expand Down
3 changes: 2 additions & 1 deletion examples/tutorial_corsika.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import matplotlib.pyplot as plt
import numpy as np
import simweights
from icecube import dataio

import simweights

CORSIKA_DATASET_DIR = Path("/data/sim/IceCube/2016/filtered/level2/CORSIKA-in-ice/20789/")
corsika_filelist = sorted(
str(f) for f in CORSIKA_DATASET_DIR.glob("0000000-0000999/Level2_IC86.2016_corsika.020789.00000*.i3.zst")
Expand Down
3 changes: 2 additions & 1 deletion examples/tutorial_nugen.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import matplotlib.pyplot as plt
import nuflux
import numpy as np
import simweights
from icecube import dataclasses, dataio, simclasses

import simweights


def get_most_energetic_muon(mmclist: simclasses.I3MMCTrackList) -> float:
"Loop through the MMC track list and return the muon with the most energy."
Expand Down
3 changes: 2 additions & 1 deletion examples/without_tableio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from pathlib import Path

import pylab as plt
import simweights
from icecube import dataio, simclasses

import simweights

FILE_DIR = Path("/data/sim/IceCube/2016/filtered/level2/CORSIKA-in-ice/21889/0000000-0000999")
filelist = sorted(str(f) for f in FILE_DIR.glob("Level2_IC86.2016_corsika.021889.00000*.i3.zst"))

Expand Down
16 changes: 10 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ ignore-words-list = 'livetime'

[tool.mypy]
plugins = "numpy.typing.mypy_plugin"
show_error_codes = true
# show_error_codes = true
warn_unreachable = true
strict = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]

[tool.pylint.format]
Expand All @@ -77,6 +78,12 @@ max-line-length = "128"
disable = "C0114,R0902,R0913"

[tool.ruff]
src = ["src"]
line-length = 128
target-version = "py38"
namespace-packages = ["examples", "contrib", "docs"]

[tool.ruff.lint]
select = ["ALL"]
fixable = ["I"]
ignore = [
Expand All @@ -87,14 +94,11 @@ ignore = [
"ISC001", # confilcts with ruff formatter
"PLR0913",# Too many arguments in function definition
]
line-length = 128
target-version = "py38"
namespace-packages = ["examples", "contrib", "docs"]

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"contrib/*" = ["T201"]
"tests/*" = [
"D", # pydocstyle
Expand Down
4 changes: 2 additions & 2 deletions src/simweights/_generation_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING, NamedTuple, Sequence
from typing import TYPE_CHECKING, Any, NamedTuple, Sequence

import numpy as np

Expand All @@ -26,7 +26,7 @@ class SurfaceTuple(NamedTuple):

pdgid: int | PDGCode
nevents: float
dists: Sequence
dists: Sequence[Any]


class GenerationSurface:
Expand Down
9 changes: 3 additions & 6 deletions src/simweights/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import numbers
from typing import TYPE_CHECKING, Any, Union

import numpy as np
Expand All @@ -15,10 +14,8 @@
if TYPE_CHECKING: # pragma: no cover
from numpy.typing import ArrayLike, NDArray


IntNumber = Union[int, np.integer]
GeneratorType = Union[Generator, RandomState]
SeedType = Union[GeneratorType, IntNumber, None]
SeedType = Union[GeneratorType, int, None]


class Column:
Expand Down Expand Up @@ -167,9 +164,9 @@ def check_random_state(seed: SeedType = None) -> GeneratorType:
seed : {`numpy.random.Generator`, `numpy.random.RandomState`}
Random number generator.
"""
if seed is None or isinstance(seed, (numbers.Integral, np.integer)):
if seed is None or isinstance(seed, (int, np.integer)):
return np.random.default_rng(seed)
if isinstance(seed, (RandomState, Generator)):
return seed
mesg = f"{seed!r} cannot be used to seed a numpy.random.Generator instance"
mesg = f"{seed!r} cannot be used to seed a numpy.random.Generator instance" # type: ignore[unreachable]
raise ValueError(mesg)
1 change: 1 addition & 0 deletions tests/test_corsika_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas as pd
import tables
import uproot

from simweights import CorsikaWeighter, GaisserH4a
from simweights._utils import constcol

Expand Down
3 changes: 2 additions & 1 deletion tests/test_corsika_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import unittest

import numpy as np
import simweights
from scipy.interpolate import interp1d

import simweights
from simweights import CorsikaWeighter

info_dtype = [
Expand Down
1 change: 1 addition & 0 deletions tests/test_fluxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

import numpy as np

from simweights import _fluxes

E = np.logspace(2, 10, 9)
Expand Down
1 change: 1 addition & 0 deletions tests/test_generation_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import numpy as np
from numpy.testing import assert_allclose

from simweights import GenerationSurface, NaturalRateCylinder, PDGCode, PowerLaw, generation_surface
from simweights._generation_surface import SurfaceTuple # noqa: F401

Expand Down
1 change: 1 addition & 0 deletions tests/test_genie_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandas as pd
import tables
import uproot

from simweights import GenieWeighter
from simweights._utils import get_column, get_table

Expand Down
1 change: 1 addition & 0 deletions tests/test_genie_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest

import numpy as np

import simweights

info_dtype = [
Expand Down
1 change: 1 addition & 0 deletions tests/test_icetop_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandas as pd
import tables
import uproot

from simweights import IceTopWeighter


Expand Down
1 change: 1 addition & 0 deletions tests/test_icetop_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest

import numpy as np

import simweights

info_dtype = [
Expand Down
1 change: 1 addition & 0 deletions tests/test_nugen_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandas as pd
import tables
import uproot

from simweights import NuGenWeighter


Expand Down
1 change: 1 addition & 0 deletions tests/test_nugen_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
import pandas as pd

from simweights import CircleInjector, NaturalRateCylinder, NuGenWeighter, PowerLaw

base_keys = [
Expand Down
1 change: 1 addition & 0 deletions tests/test_powerlaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
from scipy import stats
from scipy.integrate import quad

from simweights import PowerLaw


Expand Down
1 change: 1 addition & 0 deletions tests/test_spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest

import numpy as np

from simweights import (
CircleInjector,
NaturalRateCylinder,
Expand Down
1 change: 1 addition & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
import uproot
from numpy.testing import assert_array_equal

from simweights import Hoerandel
from simweights._utils import (
Column,
Expand Down
1 change: 1 addition & 0 deletions tests/test_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from copy import copy

import numpy as np

from simweights import TIG1996, NaturalRateCylinder, PowerLaw, Weighter, generation_surface


Expand Down

0 comments on commit 682989e

Please sign in to comment.