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

XENONnT / straxen / 24708062285

21 Apr 2026 06:41AM UTC coverage: 88.965% (+0.01%) from 88.954%
24708062285

Pull #1650

github

web-flow
Merge 18f2df258 into 310bd239d
Pull Request #1650: Fix NumPy 2.4+ / pandas 3 compatibility

22 of 25 new or added lines in 7 files covered. (88.0%)

272 existing lines in 29 files now uncovered.

8917 of 10023 relevant lines covered (88.97%)

1.78 hits per line

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

89.08
/straxen/matplotlib_utils.py
1
import numpy as np
2✔
2
import matplotlib
2✔
3
import matplotlib.pyplot as plt
2✔
4

5
import strax
2✔
6
import straxen
2✔
7

8
export, __all__ = strax.exporter()
2✔
9

10

11
@export
2✔
12
def plot_pmts(
2✔
13
    c,
14
    label="",
15
    figsize=None,
16
    show_tpc=True,
17
    extend="neither",
18
    vmin=None,
19
    vmax=None,
20
    **kwargs,
21
):
22
    """Plot the PMT arrays side-by-side, coloring the PMTS with c.
23

24
    :param c: Array of colors to use. Must have len() n_tpc_pmts
25
    :param label: Label for the color bar
26
    :param figsize: Figure size to use.
27
    :param extend: same as plt.colorbar(extend=...)
28
    :param vmin: Minimum of color scale
29
    :param vmax: maximum of color scale
30
    :param show_axis_labels: if True it will show x and y labels
31
    Other arguments are passed to plot_on_single_pmt_array.
32

33
    """
34
    if vmin is None:
2✔
35
        vmin = np.nanmin(c)
2✔
36
    if vmax is None:
2✔
37
        vmax = np.nanmax(c)
2✔
38
    if vmin == vmax:
2✔
39
        # Single-valued array passed
40
        vmax += 1
2✔
41
    if figsize is None:
2✔
42
        figsize = (13.25, 5.75)
2✔
43

44
    f, axes = plt.subplots(1, 2, figsize=figsize)
2✔
45
    plot_result = None
2✔
46
    for array_i, array_name in enumerate(["top", "bottom"]):
2✔
47
        ax = axes[array_i]
2✔
48
        plt.sca(ax)
2✔
49
        plt.title(array_name.capitalize())
2✔
50

51
        plot_result = plot_on_single_pmt_array(
2✔
52
            c,
53
            array_name=array_name,
54
            show_tpc=show_tpc,
55
            vmin=vmin,
56
            vmax=vmax,
57
            **kwargs,
58
        )
59

60
    axes[0].set_xlabel("x [cm]")
2✔
61
    axes[0].xaxis.set_label_coords(1.035, -0.075)
2✔
62
    axes[0].set_ylabel("y [cm]")
2✔
63

64
    axes[1].yaxis.tick_right()
2✔
65
    axes[1].yaxis.set_label_position("right")
2✔
66

67
    plt.tight_layout()
2✔
68
    plt.subplots_adjust(wspace=0)
2✔
69
    plt.colorbar(mappable=plot_result, ax=axes, extend=extend, label=label)
2✔
70

71

72
@export
2✔
73
def plot_on_single_pmt_array(
2✔
74
    c,
75
    array_name="top",
76
    r=straxen.tpc_r * 1.03,
77
    pmt_label_size=8,
78
    pmt_label_color="white",
79
    show_tpc=True,
80
    log_scale=False,
81
    vmin=None,
82
    vmax=None,
83
    dead_pmts=None,
84
    dead_pmt_color="gray",
85
    **kwargs,
86
):
87
    """Plot one of the PMT arrays and color it by c.
88

89
    :param c: Array of colors to use. Must be len() of the number of TPC PMTs
90
    :param label: Label for the color bar
91
    :param pmt_label_size: Fontsize for the PMT number labels.
92
    Set to 0 to disable.
93
    :param pmt_label_color: Text color of the PMT number labels.
94
    :param log_scale: If True, use a logarithmic color scale
95
    :param extend: same as plt.colorbar(extend=...)
96
    :param vmin: Minimum of color scale
97
    :param vmax: maximum of color scale
98
    Other arguments are passed to plt.scatter.
99

100
    """
101
    if vmin is None:
2✔
UNCOV
102
        vmin = c.min()
×
103
    if vmax is None:
2✔
104
        vmax = c.max()
2✔
105

106
    pmt_positions = straxen.pmt_positions().to_records()
2✔
107

108
    ax = plt.gca()
2✔
109
    ax.set_aspect("equal")
2✔
110
    plt.xlim(-r, r)
2✔
111
    plt.ylim(-r, r)
2✔
112

113
    mask = pmt_positions["array"] == array_name
2✔
114
    pos = pmt_positions[mask]
2✔
115

116
    kwargs.setdefault("s", 280)
2✔
117
    if log_scale:
2✔
118
        kwargs.setdefault("norm", matplotlib.colors.LogNorm(vmin=vmin, vmax=vmax))
2✔
119
    else:
120
        kwargs.setdefault("vmin", vmin)
2✔
121
        kwargs.setdefault("vmax", vmax)
2✔
122
    result = plt.scatter(pos["x"], pos["y"], c=c[mask], **kwargs)
2✔
123

124
    if show_tpc:
2✔
125
        ax.set_facecolor("lightgrey")
2✔
126
        ax.add_artist(
2✔
127
            plt.Circle((0, 0), straxen.tpc_r, edgecolor="k", facecolor="w", zorder=-5, linewidth=1)
128
        )
129
    else:
UNCOV
130
        ax.set_axis_off()
×
131
    if dead_pmts is not None:
2✔
132
        _dead_mask = [pi in dead_pmts for pi in pos["i"]]
2✔
133
        plt.scatter(pos[_dead_mask]["x"], pos[_dead_mask]["y"], c=dead_pmt_color, **kwargs)
2✔
134

135
    if pmt_label_size:
2✔
136
        for p in pos:
2✔
137
            plt.text(
2✔
138
                p["x"],
139
                p["y"],
140
                str(p["i"]),
141
                horizontalalignment="center",
142
                verticalalignment="center",
143
                fontsize=pmt_label_size,
144
                color=pmt_label_color,
145
            )
146
    return result
2✔
147

148

149
@export
2✔
150
def log_y(a=None, b=None, scalar_ticks=True, tick_at=None):
2✔
151
    """Make the y axis use a log scale from a to b."""
152
    plt.yscale("log")
×
153
    if a is not None:
×
154
        if b is None:
×
155
            a, b = a[0], a[-1]
×
156
        ax = plt.gca()
×
157
        plt.ylim(a, b)
×
158
        if scalar_ticks:
×
UNCOV
159
            ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%g"))
×
UNCOV
160
            ax.set_yticks(logticks(a, b, tick_at))
×
161

162

163
@export
2✔
164
def log_x(a=None, b=None, scalar_ticks=True, tick_at=None):
2✔
165
    """Make the x axis use a log scale from a to b."""
166
    plt.xscale("log")
2✔
167
    if a is not None:
2✔
168
        if b is None:
2✔
UNCOV
169
            a, b = a[0], a[-1]
×
170
        plt.xlim(a, b)
2✔
171
        ax = plt.gca()
2✔
172
        if scalar_ticks:
2✔
173
            ax.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%g"))
2✔
174
            ax.set_xticks(logticks(a, b, tick_at))
2✔
175

176

177
def logticks(tmin, tmax=None, tick_at=None):
2✔
178
    if tick_at is None:
2✔
179
        tick_at = (1, 2, 5, 10)
2✔
180
    a, b = np.log10([tmin, tmax])
2✔
181
    a = np.floor(a)
2✔
182
    b = np.ceil(b)
2✔
183
    ticks = strax.stable_sort(
2✔
184
        np.unique(np.outer(np.array(tick_at), 10.0 ** np.arange(a, b)).ravel())
185
    )
186
    ticks = ticks[(tmin <= ticks) & (ticks <= tmax)]
2✔
187
    return ticks
2✔
188

189

190
@export
2✔
191
def draw_box(x, y, **kwargs):
2✔
192
    """Draw rectangle, given x-y boundary tuples."""
UNCOV
193
    plt.gca().add_patch(
×
194
        matplotlib.patches.Rectangle(
195
            (x[0], y[0]), x[1] - x[0], y[1] - y[0], facecolor="none", **kwargs
196
        )
197
    )
198

199

200
@export
2✔
201
def plot_single_pulse(records, run_id, pulse_i=""):
2✔
202
    """Function which plots a single pulse.
203

204
    :param records: Records which belong to the pulse.
205
    :param run_id: Id of the run.
206
    :param pulse_i: Index of the pulse to be plotted.
207
    :return: fig, axes objects.
208

209
    """
210
    pulse = _make_pulse(records)
2✔
211

212
    fig, axes = plt.subplots()
2✔
213
    sec, ns = _split_off_ns(records[0]["time"])
2✔
214
    date = np.datetime_as_string(sec.astype("<M8[ns]"), unit="s")
2✔
215
    plt.title(f"Pulse {pulse_i} from {run_id}\nRecorded at {date[:10]}, {date[10:]} UTC {ns} ns")
2✔
216

217
    plt.step(np.arange(len(pulse)), pulse, where="post", label=f'Ch: {records[0]["channel"]}')
2✔
218
    plt.legend()
2✔
219
    plt.xlabel(f'Sample [{records[0]["dt"]} ns]')
2✔
220
    plt.ylabel("Height [ADC counts]")
2✔
221
    plt.grid()
2✔
222
    return fig, axes
2✔
223

224

225
def _make_pulse(records):
2✔
226
    """Helper to make a pulse based on fragements."""
227
    pulse = np.zeros(records[0]["pulse_length"], dtype=np.float32)
2✔
228

229
    offset = 0
2✔
230
    for r in records:
2✔
231
        pulse[offset : offset + r["length"]] = r["data"][: r["length"]]
2✔
232
        offset += r["length"]
2✔
233

234
    return pulse
2✔
235

236

237
def _split_off_ns(time):
2✔
238
    """Mini helper to divide time into seconds and ns."""
239
    sec = (time // 10**9) * 10**9
2✔
240
    ns = time - sec
2✔
241
    return sec, ns
2✔
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

© 2026 Coveralls, Inc