• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenCOMPES / sed / 9799510297

04 Jul 2024 08:17PM UTC coverage: 92.349% (-0.2%) from 92.511%
9799510297

Pull #466

github

rettigl
add tests for illegal keyword errors
Pull Request #466: Catch illegal kwds

106 of 123 new or added lines in 17 files covered. (86.18%)

3 existing lines in 1 file now uncovered.

6940 of 7515 relevant lines covered (92.35%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/sed/diagnostics.py
1
"""This module contains diagnostic output functions for the sed module
2

3
"""
4
from __future__ import annotations
1✔
5

6
from collections.abc import Sequence
1✔
7

8
import bokeh.plotting as pbk
1✔
9
import matplotlib.pyplot as plt
1✔
10
import numpy as np
1✔
11
from bokeh.io import output_notebook
1✔
12
from bokeh.layouts import gridplot
1✔
13

14

15
def plot_single_hist(
1✔
16
    histvals: np.ndarray,
17
    edges: np.ndarray,
18
    legend: str = None,
19
    **kwds,
20
) -> pbk.figure:
21
    """Bokeh-based plotting of a single histogram with legend and tooltips.
22

23
    Args:
24
        histvals (np.ndarray): Histogram counts (e.g. vertical axis).
25
        edges (np.ndarray): Histogram edge values (e.g. horizontal axis).
26
        legend (str, optional): Text for the plot legend. Defaults to None.
27
        **kwds:
28
            - *tooltip*: Tooltip formatting tuple. Defaults to [("(x, y)", "($x, $y)")]
29

30
            Additional keyword arguments are passed to ``bokeh.plotting.figure().quad()``.
31

32
    Returns:
33
        pbk.figure: An instance of 'bokeh.plotting.figure' as a plot handle.
34
    """
35
    ttp = kwds.pop("tooltip", [("(x, y)", "($x, $y)")])
1✔
36

37
    fig = pbk.figure(background_fill_color="white", tooltips=ttp)
1✔
38
    fig.quad(
1✔
39
        top=histvals,
40
        bottom=0,
41
        left=edges[:-1],
42
        right=edges[1:],
43
        line_color="white",
44
        alpha=0.8,
45
        legend_label=legend,
46
        **kwds,
47
    )
48

49
    fig.y_range.start = 0  # type: ignore
1✔
50
    fig.legend.location = "top_right"
1✔
51
    fig.grid.grid_line_color = "lightgrey"
1✔
52

53
    return fig
1✔
54

55

56
def grid_histogram(
1✔
57
    dct: dict,
58
    ncol: int,
59
    rvs: Sequence,
60
    rvbins: Sequence,
61
    rvranges: Sequence[tuple[float, float]],
62
    backend: str = "bokeh",
63
    legend: bool = True,
64
    histkwds: dict = None,
65
    legkwds: dict = None,
66
    **kwds,
67
):
68
    """Grid plot of multiple 1D histograms.
69

70
    Args:
71
        dct (dict): Dictionary containing the name and values of the random variables.
72
        ncol (int): Number of columns in the plot grid.
73
        rvs (Sequence): List of names for the random variables (rvs).
74
        rvbins (Sequence): Bin values for all random variables.
75
        rvranges (Sequence[tuple[float, float]]): Value ranges of all random variables.
76
        backend (str, optional): Backend for making the plot ('matplotlib' or 'bokeh').
77
            Defaults to "bokeh".
78
        legend (bool, optional): Option to include a legend in each histogram plot.
79
            Defaults to True.
80
        histkwds (dict, optional): Keyword arguments for histogram plots.
81
            Defaults to None.
82
        legkwds (dict, optional): Keyword arguments for legends. Defaults to None.
83
        **kwds:
84
            - *figsize*: Figure size. Defaults to (14, 8)
85
    """
86
    if histkwds is None:
1✔
87
        histkwds = {}
1✔
88
    if legkwds is None:
1✔
89
        legkwds = {}
1✔
90

91
    figsz = kwds.pop("figsize", (14, 8))
1✔
92

93
    if len(kwds) > 0:
1✔
NEW
94
        raise TypeError(f"grid_histogram() got unexpected keyword arguments {kwds.keys()}.")
×
95

96
    if backend == "matplotlib":
1✔
97
        nrv = len(rvs)
1✔
98
        nrow = int(np.ceil(nrv / ncol))
1✔
99
        histtype = kwds.pop("histtype", "step")
1✔
100

101
        fig, ax = plt.subplots(nrow, ncol, figsize=figsz)
1✔
102
        otherax = ax.copy()
1✔
103
        for i, zipped in enumerate(zip(rvs, rvbins, rvranges)):
1✔
104
            # Make each histogram plot
105
            rvname, rvbin, rvrg = zipped
1✔
106
            try:
1✔
107
                axind = np.unravel_index(i, (nrow, ncol))
1✔
108
                ax[axind].hist(
1✔
109
                    dct[rvname],
110
                    bins=rvbin,
111
                    range=rvrg,
112
                    label=rvname,
113
                    histtype=histtype,
114
                    **histkwds,
115
                )
116
                if legend:
1✔
117
                    ax[axind].legend(fontsize=15, **legkwds)
1✔
118

119
                otherax[axind] = None
1✔
120

121
            except IndexError:
1✔
122
                ax[i].hist(
1✔
123
                    dct[rvname],
124
                    bins=rvbin,
125
                    range=rvrg,
126
                    label=rvname,
127
                    histtype=histtype,
128
                    **histkwds,
129
                )
130
                if legend:
1✔
131
                    ax[i].legend(fontsize=15, **legkwds)
1✔
132

133
                otherax[i] = None
1✔
134

135
        for oax in otherax.flatten():
1✔
136
            if oax is not None:
1✔
137
                fig.delaxes(oax)
1✔
138

139
    elif backend == "bokeh":
1✔
140
        output_notebook(hide_banner=True)
1✔
141

142
        plots = []
1✔
143
        for i, zipped in enumerate(zip(rvs, rvbins, rvranges)):
1✔
144
            rvname, rvbin, rvrg = zipped
1✔
145
            histvals, edges = np.histogram(dct[rvname], bins=rvbin, range=rvrg)
1✔
146

147
            if legend:
1✔
148
                plots.append(
1✔
149
                    plot_single_hist(
150
                        histvals,
151
                        edges,
152
                        legend=rvname,
153
                        **histkwds,
154
                    ),
155
                )
156
            else:
157
                plots.append(
×
158
                    plot_single_hist(histvals, edges, legend=None, **histkwds),
159
                )
160

161
        # Make grid plot
162
        pbk.show(
1✔
163
            gridplot(
164
                plots,  # type: ignore
165
                ncols=ncol,
166
                width=figsz[0] * 30,
167
                height=figsz[1] * 28,
168
            ),
169
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc