-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiagnostics.py
More file actions
177 lines (150 loc) · 5.45 KB
/
diagnostics.py
File metadata and controls
177 lines (150 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""This module contains diagnostic output functions for the sed module
"""
from __future__ import annotations
from collections.abc import Sequence
import bokeh.plotting as pbk
import matplotlib.pyplot as plt
import numpy as np
from bokeh.io import output_notebook
from bokeh.layouts import gridplot
def plot_single_hist(
histvals: np.ndarray,
edges: np.ndarray,
legend: str = None,
**kwds,
) -> pbk.figure:
"""Bokeh-based plotting of a single histogram with legend and tooltips.
Args:
histvals (np.ndarray): Histogram counts (e.g. vertical axis).
edges (np.ndarray): Histogram edge values (e.g. horizontal axis).
legend (str, optional): Text for the plot legend. Defaults to None.
**kwds:
- *tooltip*: Tooltip formatting tuple. Defaults to [("(x, y)", "($x, $y)")]
Additional keyword arguments are passed to ``bokeh.plotting.figure().quad()``.
Returns:
pbk.figure: An instance of 'bokeh.plotting.figure' as a plot handle.
"""
ttp = kwds.pop("tooltip", [("(x, y)", "($x, $y)")])
fig = pbk.figure(background_fill_color="white")
fig.hover.tooltips = ttp
fig.quad(
top=histvals,
bottom=0,
left=edges[:-1],
right=edges[1:],
line_color="white",
alpha=0.8,
legend_label=legend,
**kwds,
)
fig.y_range.start = 0 # type: ignore
fig.legend.location = "top_right"
fig.grid.grid_line_color = "lightgrey"
return fig
def grid_histogram(
dct: dict,
ncol: int,
rvs: Sequence,
rvbins: Sequence,
rvranges: Sequence[tuple[float, float]],
backend: str = "matplotlib",
legend: bool = True,
histkwds: dict = None,
legkwds: dict = None,
**kwds,
):
"""Grid plot of multiple 1D histograms.
Args:
dct (dict): Dictionary containing the name and values of the random variables.
ncol (int): Number of columns in the plot grid.
rvs (Sequence): List of names for the random variables (rvs).
rvbins (Sequence): Bin values for all random variables.
rvranges (Sequence[tuple[float, float]]): Value ranges of all random variables.
backend (str, optional): Backend for making the plot ("matplotlib" or "bokeh").
Defaults to "matplotlib".
legend (bool, optional): Option to include a legend in each histogram plot.
Defaults to True.
histkwds (dict, optional): Keyword arguments for histogram plots.
Defaults to None.
legkwds (dict, optional): Keyword arguments for legends. Defaults to None.
**kwds:
- *figsize*: Figure size. Defaults to (6, 4)
"""
if histkwds is None:
histkwds = {}
if legkwds is None:
legkwds = {}
figsz = kwds.pop("figsize", (3, 2)) # figure size of each panel
if len(kwds) > 0:
raise TypeError(f"grid_histogram() got unexpected keyword arguments {kwds.keys()}.")
if backend == "matplotlib":
nrv = len(rvs)
nrow = int(np.ceil(nrv / ncol))
histtype = kwds.pop("histtype", "bar")
figsize = [figsz[0] * ncol, figsz[1] * nrow]
fig, ax = plt.subplots(nrow, ncol, figsize=figsize)
otherax = ax.copy()
for i, zipped in enumerate(zip(rvs, rvbins, rvranges)):
# Make each histogram plot
rvname, rvbin, rvrg = zipped
try:
axind = np.unravel_index(i, (nrow, ncol))
plt.setp(ax[axind].get_xticklabels(), fontsize=8)
plt.setp(ax[axind].get_yticklabels(), fontsize=8)
ax[axind].hist(
dct[rvname],
bins=rvbin,
range=rvrg,
label=rvname,
histtype=histtype,
**histkwds,
)
if legend:
ax[axind].legend(fontsize=10, **legkwds)
otherax[axind] = None
except IndexError:
plt.setp(ax[i].get_xticklabels(), fontsize=8)
plt.setp(ax[i].get_yticklabels(), fontsize=8)
ax[i].hist(
dct[rvname],
bins=rvbin,
range=rvrg,
label=rvname,
histtype=histtype,
**histkwds,
)
if legend:
ax[i].legend(fontsize=10, **legkwds)
otherax[i] = None
for oax in otherax.flatten():
if oax is not None:
fig.delaxes(oax)
plt.tight_layout()
elif backend == "bokeh":
output_notebook(hide_banner=True)
plots = []
for i, zipped in enumerate(zip(rvs, rvbins, rvranges)):
rvname, rvbin, rvrg = zipped
histvals, edges = np.histogram(dct[rvname], bins=rvbin, range=rvrg)
if legend:
plots.append(
plot_single_hist(
histvals,
edges,
legend=rvname,
**histkwds,
),
)
else:
plots.append(
plot_single_hist(histvals, edges, legend=None, **histkwds),
)
# Make grid plot
pbk.show(
gridplot(
plots, # type: ignore
ncols=ncol,
width=figsz[0] * 100,
height=figsz[1] * 100,
),
)