plothist 1.3.2__py3-none-any.whl → 1.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
plothist/__init__.py CHANGED
@@ -1,115 +1,107 @@
1
+ from __future__ import annotations
2
+
3
+ from ._version import version as __version__
4
+ from .comparison import (
5
+ get_asymmetrical_uncertainties,
6
+ get_comparison,
7
+ get_difference,
8
+ get_pull,
9
+ get_ratio,
10
+ get_ratio_variances,
11
+ )
12
+ from .histogramming import (
13
+ create_axis,
14
+ flatten_2d_hist,
15
+ make_2d_hist,
16
+ make_hist,
17
+ )
18
+ from .plothist_style import (
19
+ add_luminosity,
20
+ add_text,
21
+ cubehelix_palette,
22
+ get_color_palette,
23
+ plot_reordered_legend,
24
+ set_fitting_ylabel_fontsize,
25
+ set_style,
26
+ )
1
27
  from .plotters import (
2
28
  create_comparison_figure,
3
- plot_hist,
4
29
  plot_2d_hist,
5
30
  plot_2d_hist_with_projections,
31
+ plot_comparison,
32
+ plot_data_model_comparison,
6
33
  plot_error_hist,
34
+ plot_function,
35
+ plot_hist,
7
36
  plot_hist_uncertainties,
37
+ plot_model,
8
38
  plot_two_hist_comparison,
9
- plot_comparison,
10
39
  savefig,
11
- plot_data_model_comparison,
12
- plot_model,
13
- plot_function,
14
40
  )
15
-
16
- from .histogramming import (
17
- create_axis,
18
- make_hist,
19
- make_2d_hist,
20
- flatten_2d_hist,
21
- )
22
-
23
41
  from .variable_registry import (
24
42
  create_variable_registry,
25
43
  get_variable_from_registry,
26
- update_variable_registry,
27
44
  remove_variable_registry_parameters,
45
+ update_variable_registry,
28
46
  update_variable_registry_ranges,
29
47
  )
30
48
 
31
- from .comparison import (
32
- get_asymmetrical_uncertainties,
33
- get_comparison,
34
- get_pull,
35
- get_difference,
36
- get_ratio,
37
- get_ratio_variances,
38
- )
39
-
40
- from .plothist_style import (
41
- set_style,
42
- cubehelix_palette,
43
- get_color_palette,
44
- set_fitting_ylabel_fontsize,
45
- add_text,
46
- add_luminosity,
47
- plot_reordered_legend,
48
- )
49
-
50
- from .get_dummy_data import get_dummy_data
51
-
52
49
  __all__ = [
53
50
  "__version__",
51
+ "add_luminosity",
52
+ "add_text",
53
+ "create_axis",
54
+ "create_comparison_figure",
54
55
  "create_variable_registry",
56
+ "cubehelix_palette",
57
+ "flatten_2d_hist",
58
+ "get_asymmetrical_uncertainties",
59
+ "get_color_palette",
60
+ "get_comparison",
61
+ "get_difference",
62
+ "get_pull",
63
+ "get_ratio",
64
+ "get_ratio_variances",
55
65
  "get_variable_from_registry",
56
- "update_variable_registry",
57
- "remove_variable_registry_parameters",
58
- "update_variable_registry_ranges",
59
- "create_comparison_figure",
60
- "create_axis",
61
- "make_hist",
62
66
  "make_2d_hist",
63
- "plot_hist",
67
+ "make_hist",
64
68
  "plot_2d_hist",
65
69
  "plot_2d_hist_with_projections",
70
+ "plot_comparison",
71
+ "plot_data_model_comparison",
66
72
  "plot_error_hist",
73
+ "plot_function",
74
+ "plot_hist",
67
75
  "plot_hist_uncertainties",
76
+ "plot_model",
77
+ "plot_reordered_legend",
68
78
  "plot_two_hist_comparison",
69
- "plot_comparison",
79
+ "remove_variable_registry_parameters",
70
80
  "savefig",
71
- "plot_data_model_comparison",
72
- "plot_model",
73
- "plot_function",
74
- "add_luminosity",
75
- "get_asymmetrical_uncertainties",
76
- "set_style",
77
- "cubehelix_palette",
78
- "get_color_palette",
79
81
  "set_fitting_ylabel_fontsize",
80
- "add_text",
81
- "get_asymmetrical_uncertainties",
82
- "get_comparison",
83
- "get_pull",
84
- "get_difference",
85
- "get_ratio",
86
- "get_ratio_variances",
87
- "flatten_2d_hist",
88
- "plot_reordered_legend",
89
- "get_dummy_data",
82
+ "set_style",
83
+ "update_variable_registry",
84
+ "update_variable_registry_ranges",
90
85
  ]
91
86
 
92
87
 
93
88
  # Get style file and use it
94
- import matplotlib.pyplot as plt
95
89
  from importlib.resources import files
96
90
 
91
+ import matplotlib.pyplot as plt
92
+
97
93
  style_file = files("plothist").joinpath("default_style.mplstyle")
98
94
  plt.style.use(style_file)
99
95
 
100
- # Check the fonts
101
- from matplotlib.font_manager import findfont
102
- import warnings
96
+ # Install fonts
97
+ from importlib import resources
98
+
99
+ import matplotlib.font_manager as fm
103
100
 
104
- for font_type in ["Math", "Sans", "Roman"]:
105
- try:
106
- findfont(f"Latin Modern {font_type}", fallback_to_default=False)
107
- except:
108
- warnings.warn(
109
- "The recommended fonts to use plothist were not found. You can install them by typing 'install_latin_modern_fonts' in your terminal. If it still does not work, please check the documentation at https://plothist.readthedocs.io/en/latest/usage/font_installation.html",
110
- stacklevel=3,
111
- )
112
- break
101
+ with resources.as_file(resources.files("plothist_utils") / "fonts") as font_path:
102
+ font_files = fm.findSystemFonts(fontpaths=[str(font_path)])
103
+ for font in font_files:
104
+ fm.fontManager.addfont(font)
113
105
 
114
106
  # Check version of boost_histogram
115
107
  import boost_histogram as bh
plothist/_version.py ADDED
@@ -0,0 +1,21 @@
1
+ # file generated by setuptools-scm
2
+ # don't change, don't track in version control
3
+
4
+ __all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
5
+
6
+ TYPE_CHECKING = False
7
+ if TYPE_CHECKING:
8
+ from typing import Tuple
9
+ from typing import Union
10
+
11
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
12
+ else:
13
+ VERSION_TUPLE = object
14
+
15
+ version: str
16
+ __version__: str
17
+ __version_tuple__: VERSION_TUPLE
18
+ version_tuple: VERSION_TUPLE
19
+
20
+ __version__ = version = '1.4.0'
21
+ __version_tuple__ = version_tuple = (1, 4, 0)
plothist/_version.pyi ADDED
@@ -0,0 +1,2 @@
1
+ version: str
2
+ version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str]
plothist/comparison.py CHANGED
@@ -1,5 +1,8 @@
1
+ from __future__ import annotations
2
+
1
3
  import numpy as np
2
- import scipy.stats as stats
4
+ from scipy import stats
5
+
3
6
  from plothist.histogramming import _check_counting_histogram
4
7
 
5
8
 
plothist/histogramming.py CHANGED
@@ -1,6 +1,9 @@
1
+ from __future__ import annotations
2
+
3
+ import warnings
4
+
1
5
  import boost_histogram as bh
2
6
  import numpy as np
3
- import warnings
4
7
 
5
8
 
6
9
  # Define a custom warning for range issues
@@ -12,7 +15,7 @@ class RangeWarning(Warning):
12
15
  warnings.filterwarnings("always", category=RangeWarning)
13
16
 
14
17
 
15
- def create_axis(bins, range=None, data=np.array([]), overflow=False, underflow=False):
18
+ def create_axis(bins, range=None, data=None, overflow=False, underflow=False):
16
19
  """
17
20
  Create an axis object for histogram binning based on the input data and parameters.
18
21
 
@@ -23,7 +26,7 @@ def create_axis(bins, range=None, data=np.array([]), overflow=False, underflow=F
23
26
  range : None or tuple, optional
24
27
  The range of the axis. If None, it will be determined based on the data.
25
28
  data : array-like, optional
26
- The input data for determining the axis range. Default is an empty array.
29
+ The input data for determining the axis range. Default is None.
27
30
  overflow : bool, optional
28
31
  Whether to include an overflow bin. If False, the upper edge of the last bin is inclusive. Default is False.
29
32
  underflow : bool, optional
@@ -46,6 +49,8 @@ def create_axis(bins, range=None, data=np.array([]), overflow=False, underflow=F
46
49
  ValueError
47
50
  If the range parameter contains "min" or "max" but the data is empty.
48
51
  """
52
+ if data is None:
53
+ data = np.array([])
49
54
 
50
55
  try:
51
56
  N = len(bins)
@@ -94,15 +99,15 @@ def create_axis(bins, range=None, data=np.array([]), overflow=False, underflow=F
94
99
  return bh.axis.Regular(bins, x_min, x_max, underflow=underflow, overflow=overflow)
95
100
 
96
101
 
97
- def make_hist(data=np.array([]), bins=50, range=None, weights=1):
102
+ def make_hist(data=None, bins=50, range=None, weights=1):
98
103
  """
99
104
  Create a histogram object and fill it with the provided data.
100
105
 
101
106
  Parameters
102
107
  ----------
103
108
  data : array-like, optional
104
- 1D array-like data used to fill the histogram (default is an empty array).
105
- If an empty array, an empty histogram is returned.
109
+ 1D array-like data used to fill the histogram (default is None).
110
+ If None is provided, an empty histogram is returned.
106
111
  bins : int or tuple, optional
107
112
  Binning specification for the histogram (default is 50).
108
113
  If an integer, it represents the number of bins.
@@ -125,6 +130,8 @@ def make_hist(data=np.array([]), bins=50, range=None, weights=1):
125
130
  RangeWarning
126
131
  If more than 1% of the data is outside of the binning range.
127
132
  """
133
+ if data is None:
134
+ data = np.array([])
128
135
 
129
136
  axis = create_axis(bins, range, data)
130
137
 
@@ -145,7 +152,7 @@ def make_hist(data=np.array([]), bins=50, range=None, weights=1):
145
152
  # Issue a warning if more than 1% of the data is outside of the binning range
146
153
  if range_coverage < 0.99:
147
154
  warnings.warn(
148
- f"Only {100*range_coverage:.2f}% of data contained in the binning range [{axis.edges[0]}, {axis.edges[-1]}].",
155
+ f"Only {100 * range_coverage:.2f}% of data contained in the binning range [{axis.edges[0]}, {axis.edges[-1]}].",
149
156
  category=RangeWarning,
150
157
  stacklevel=2,
151
158
  )
@@ -153,15 +160,15 @@ def make_hist(data=np.array([]), bins=50, range=None, weights=1):
153
160
  return h
154
161
 
155
162
 
156
- def make_2d_hist(data=np.array([[], []]), bins=(10, 10), range=(None, None), weights=1):
163
+ def make_2d_hist(data=None, bins=(10, 10), range=(None, None), weights=1):
157
164
  """
158
165
  Create a 2D histogram object and fill it with the provided data.
159
166
 
160
167
  Parameters
161
168
  ----------
162
169
  data : array-like, optional
163
- 2D array-like data used to fill the histogram (default is an empty array).
164
- If an empty array, an empty histogram is returned.
170
+ 2D array-like data used to fill the histogram (default is None).
171
+ If None is provided, an empty histogram is returned.
165
172
  bins : tuple, optional
166
173
  Binning specification for each dimension of the histogram (default is (10, 10)).
167
174
  Each element of the tuple represents the number of bins for the corresponding dimension.
@@ -190,6 +197,8 @@ def make_2d_hist(data=np.array([[], []]), bins=(10, 10), range=(None, None), wei
190
197
  RangeWarning
191
198
  If more than 1% of the data is outside of the binning range.
192
199
  """
200
+ if data is None:
201
+ data = np.array([[], []])
193
202
  if len(data) != 2:
194
203
  raise ValueError("data should have two components, x and y")
195
204
  if len(data[0]) != len(data[1]):
@@ -219,7 +228,7 @@ def make_2d_hist(data=np.array([[], []]), bins=(10, 10), range=(None, None), wei
219
228
  # Issue a warning if more than 1% of the data is outside of the binning range
220
229
  if range_coverage < 0.99:
221
230
  warnings.warn(
222
- f"Only {100*range_coverage:.2f}% of data contained in the binning range ([{x_axis.edges[0]}, {x_axis.edges[-1]}], [{y_axis.edges[0]}, {y_axis.edges[-1]}]).",
231
+ f"Only {100 * range_coverage:.2f}% of data contained in the binning range ([{x_axis.edges[0]}, {x_axis.edges[-1]}], [{y_axis.edges[0]}, {y_axis.edges[-1]}]).",
223
232
  category=RangeWarning,
224
233
  stacklevel=2,
225
234
  )
@@ -246,7 +255,6 @@ def _check_counting_histogram(hist):
246
255
  raise ValueError(
247
256
  f"The histogram must be a counting histogram, but the input histogram has kind {hist.kind}."
248
257
  )
249
- return
250
258
 
251
259
 
252
260
  def _make_hist_from_function(func, ref_hist):
@@ -1,7 +1,10 @@
1
+ from __future__ import annotations
2
+
3
+ from importlib.resources import files
4
+
5
+ import matplotlib as mpl
1
6
  import matplotlib.pyplot as plt
2
7
  import numpy as np
3
- import matplotlib as mpl
4
- from importlib.resources import files
5
8
 
6
9
 
7
10
  def set_style(style="default"):
@@ -87,7 +90,7 @@ def cubehelix_palette(
87
90
  # Adapted from matplotlib
88
91
  def color(lambda_):
89
92
  # emphasise either low intensity values (gamma < 1),
90
- # or high intensity values (γ > 1)
93
+ # or high intensity values (gamma > 1)
91
94
  lambda_gamma = lambda_**gamma
92
95
 
93
96
  # Angle and amplitude for the deviation
@@ -159,7 +162,7 @@ def get_color_palette(cmap, N):
159
162
  "#FFB5B8",
160
163
  ][0:N]
161
164
 
162
- elif cmap == "cubehelix":
165
+ if cmap == "cubehelix":
163
166
  return cubehelix_palette(N)
164
167
 
165
168
  if N < 2:
@@ -296,7 +299,7 @@ def add_text(
296
299
 
297
300
  # Add background
298
301
  if white_background:
299
- t.set_bbox(dict(facecolor="white", edgecolor="white"))
302
+ t.set_bbox({"facecolor": "white", "edgecolor": "white"})
300
303
 
301
304
 
302
305
  def add_luminosity(
@@ -424,8 +427,7 @@ def plot_reordered_legend(ax, order, **kwargs):
424
427
  # Check if order is valid
425
428
  if not all(i in range(len(labels)) for i in order) or len(set(order)) < len(order):
426
429
  raise ValueError(
427
- "The order list should contain all integers from 0 to "
428
- f"{len(labels) - 1}."
430
+ f"The order list should contain all integers from 0 to {len(labels) - 1}."
429
431
  )
430
432
 
431
433
  # Reorder handlers and labels