Skip to content

Commit

Permalink
Update pre-commit, store tagger scores as structured array (#177)
Browse files Browse the repository at this point in the history
* update pre-commit

* run ruff

* update tagger to use structured array

* update tagger

* bug fixes

* update changelog

* revert line length

* Update puma/histogram.py

Co-authored-by: Dmitrii Kobylianskii <[email protected]>

---------

Co-authored-by: Dmitrii Kobylianskii <[email protected]>
  • Loading branch information
samvanstroud and dkobylianskii authored Apr 24, 2023
1 parent b233f47 commit c9549ad
Show file tree
Hide file tree
Showing 47 changed files with 355 additions and 476 deletions.
1 change: 0 additions & 1 deletion .github/workflows/github_update_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def pylint_fixmes():
list
pylint_msgs with Todos
"""

(pylint_stdout, _) = lint.py_run("puma/ --disable=all --enable=fixme ", True)
pylint_stdout = pylint_stdout.read()
pylint_files, pylint_msgs = [], []
Expand Down
43 changes: 16 additions & 27 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.254"
hooks:
- id: isort
args: ["--profile", "black"]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/psf/black
rev: 23.1.0
rev: "23.1.0"
hooks:
- id: black
args: ["--preview"]

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8

- repo: https://github.com/terrencepreilly/darglint
rev: v1.8.1
hooks:
- id: darglint
#- repo: https://github.com/pre-commit/mirrors-mypy
# rev: "v1.1.1"
# hooks:
# - id: mypy
# args: [--ignore-missing-imports]
# additional_dependencies: ["types-requests", "types-PyYAML"]

- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args: [
"-rn", # Only display messages
"-sn", # Don't display the score
"--ignore-paths=.github/*,docs/*",
]
#- repo: https://github.com/adrienverge/yamllint.git
# rev: v1.17.0
# hooks:
# - id: yamllint
# args: ["-d", "{extends: relaxed, rules: {line-length: disable}}"]
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### [Latest]

- Update pre-commit (using ruff) and store tagger scores as structured array [!177](https://github.com/umami-hep/puma/pull/177)
- Remove dev image [!176](https://github.com/umami-hep/puma/pull/176)
- Fix bug in ratio axis limits [!175](https://github.com/umami-hep/puma/pull/175)
- Add `VarVsVar` plot [!172](https://github.com/umami-hep/puma/pull/172)
Expand Down
5 changes: 1 addition & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@
commits = requests.get("https://api.github.com/repos/umami-hep/puma/commits/main")
latest_commit_hash = commits.json()["sha"]

if current_hash == latest_commit_hash:
version_match = "latest"
else:
version_match = f"v{release}"
version_match = "latest" if current_hash == latest_commit_hash else f"v{release}"

html_theme = "pydata_sphinx_theme"
html_theme_options = {
Expand Down
15 changes: 6 additions & 9 deletions docs/source/dev_guidelines/development_guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ automatic formatting when you save a file.
## Linters

In addition to the pure style-component of checking the code with `black`, we use
`flake8` and `pylint` to check the code for bad coding practices. Make sure to run them before
you commit your code.

In addition to that, we require docstrings in the `numpy`-style, which are checked in
the pipeline by `darglint`.
`ruff` to check the code for bad coding practices and docstrings. Make sure to run
`ruff` before you commit your code.

## Pre-commit hook

To check staged files for style and `flake8` conformity, you can use the `pre-commit`
hook, which then won't allow you to commit your staged changes if `isort`, `black` or
`flake8` fails.
You might have to set it up by executing the following in the root of the repo:
To check staged files for style conformity, you can use the `pre-commit`
hook, which then won't allow you to commit your staged changes if `ruff`
or `black fails.
You can set it up by executing the following in the root of the repo:

```bash
pre-commit install
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx_build_multiversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def build_docs_version(version):

def main():
"""main function that is executed when the script is called."""
with open("docs/source/_static/switcher.json", "r") as f: # pylint: disable=W1514
with open("docs/source/_static/switcher.json") as f:
version_switcher = json.load(f)

# get currently active branch
Expand Down
4 changes: 2 additions & 2 deletions examples/plot_fraction_scan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example of fraction scan plot"""
"""Example of fraction scan plot."""

import numpy as np

Expand All @@ -24,7 +24,7 @@


def calc_effs(fc_value: float):
"""Tagger efficiency for fixed working point
"""Tagger efficiency for fixed working point.
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_histogram_underoverflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example script that demonstrates under/overflow bins"""
"""Example script that demonstrates under/overflow bins."""
import numpy as np

from puma import Histogram, HistogramPlot
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_pie.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example plotting script for the puma.PiePlot class"""
"""Example plotting script for the puma.PiePlot class."""
from ftag import Flavours

from puma.pie import PiePlot
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_pt_vs_eff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# define a small function to calculate discriminant
def disc_fct(arr: np.ndarray, f_c: float = 0.018) -> np.ndarray:
"""Tagger discriminant
"""Tagger discriminant.
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_rocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# define a small function to calculate discriminant
def disc_fct(arr: np.ndarray, f_c: float = 0.018) -> np.ndarray:
"""Tagger discriminant
"""Tagger discriminant.
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_weighted_histograms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example script for plotting weighted histograms"""
"""Example script for plotting weighted histograms."""
import numpy as np

from puma import Histogram, HistogramPlot
Expand Down
1 change: 0 additions & 1 deletion puma/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""puma framework - Plotting UMami Api."""

# flake8: noqa
# pylint: skip-file

__version__ = "0.2.4"

Expand Down
47 changes: 20 additions & 27 deletions puma/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
from puma.utils.histogram import hist_ratio, hist_w_unc


class Histogram(
PlotLineObject
): # pylint: disable=too-few-public-methods,too-many-instance-attributes
class Histogram(PlotLineObject):
"""
Histogram class storing info about histogram and allows to calculate ratio w.r.t
other histograms.
"""

def __init__( # pylint: disable=too-many-arguments
def __init__(
self,
values: np.ndarray,
weights: np.ndarray = None,
Expand Down Expand Up @@ -79,9 +77,8 @@ def __init__( # pylint: disable=too-many-arguments
"Invalid type of histogram input data. Allowed values are "
"numpy.ndarray, list, pandas.core.series.Series"
)
if weights is not None:
if len(values) != len(weights):
raise ValueError("`values` and `weights` are not of same length.")
if weights is not None and len(values) != len(weights):
raise ValueError("`values` and `weights` are not of same length.")

self.values = values
self.weights = weights
Expand Down Expand Up @@ -173,10 +170,10 @@ def divide(self, other):
return (ratio, ratio_unc)


class HistogramPlot(PlotBase): # pylint: disable=too-many-instance-attributes
"""Histogram plot class"""
class HistogramPlot(PlotBase):
"""Histogram plot class."""

def __init__( # pylint: disable=too-many-arguments
def __init__(
self,
bins=40,
bins_range: tuple = None,
Expand All @@ -188,7 +185,7 @@ def __init__( # pylint: disable=too-many-arguments
grid: bool = False,
**kwargs,
) -> None:
"""histogram plot properties
"""histogram plot properties.
Parameters
----------
Expand Down Expand Up @@ -292,7 +289,7 @@ def add(self, histogram: object, key: str = None, reference: bool = False):
self.set_reference(key)

def set_reference(self, key: str):
"""Setting the reference histogram curves used in the ratios
"""Setting the reference histogram curves used in the ratios.
Parameters
----------
Expand Down Expand Up @@ -325,12 +322,11 @@ def plot(self, **kwargs):
ValueError
If specified bins type is not supported.
"""
if self.ylabel is not None:
if self.norm and "norm" not in self.ylabel.lower():
logger.warning(
"You are plotting normalised distributions but 'norm' is not "
"included in your y-label."
)
if self.ylabel is not None and self.norm and "norm" not in self.ylabel.lower():
logger.warning(
"You are plotting normalised distributions but 'norm' is not "
"included in your y-label."
)
plt_handles = []

# Calculate bins of stacked histograms to ensure all histograms fit in plot
Expand Down Expand Up @@ -422,7 +418,7 @@ def plot(self, **kwargs):

def get_discrete_values(self, elem: object):
"""Get discrete values of a variable and adjust the
bins accordingly
bins accordingly.
Parameters
----------
Expand All @@ -443,7 +439,6 @@ def get_discrete_values(self, elem: object):
If the number of bins is set to 1 such that no values can be
distinguished
"""

if len(elem.bin_edges) > 1:
if abs(elem.bin_edges[1] - elem.bin_edges[0]) <= 1:
indice = []
Expand Down Expand Up @@ -476,14 +471,14 @@ def get_discrete_values(self, elem: object):
)
else:
raise ValueError(
"Choose a binning with more than one bin in order to plot"
"only discrete values."
"Choose a binning with more than one bin in order to plot only discrete"
" values."
)

return bins

def get_reference_histo(self, histo):
"""Get reference histogram from list of references
"""Get reference histogram from list of references.
Parameters
----------
Expand All @@ -500,7 +495,6 @@ def get_reference_histo(self, histo):
ValueError
If no reference histo was found or multiple matches.
"""

matches = 0
reference_histo = None

Expand All @@ -516,8 +510,8 @@ def get_reference_histo(self, histo):

if matches != 1:
raise ValueError(
f"Found {matches} matching reference candidates, but only one match "
"is allowed."
f"Found {matches} matching reference candidates, but only one match is"
" allowed."
)

logger.debug(
Expand Down Expand Up @@ -583,7 +577,6 @@ def add_bin_width_to_ylabel(self):
ValueError
If plotting_done is False (therefore `bins` is not yet calculated)
"""

if self.plotting_done is False:
raise ValueError(
"`add_bin_width_to_ylabel` should be called after plotting, since bins "
Expand Down
4 changes: 2 additions & 2 deletions puma/hlplots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""High level plotting API within puma, to avoid code duplication."""
# flake8: noqa
# pylint: skip-file

from puma.hlplots.results import Results
from puma.hlplots.tagger import Tagger

__all__ = ["Results", "Tagger"]
Loading

0 comments on commit c9549ad

Please sign in to comment.