Skip to content

Data quality — check before you analyse

Nothing in this module contributes to a physics result. These are the checks you run first, on a fresh acquisition, to answer one question: did the TDC actually record timing?

The failure mode is silent

When the ring-oscillator TDC never stops — or the readout is misconfigured — unpack still returns a full (32, 32, nframes) array of plausible-looking integers. Every downstream analysis still runs. The delta-t histogram still has a shape. It is simply meaningless.

There is no exception, no warning, and no obviously wrong number. The only way to catch it is to look at the raw code distribution.

What healthy data looks like

A working pixel's TDC codes spread over the full range for the reference clock in use — ~279 codes at 50 MHz, ~1397 at 10 MHz, ~2794 at 5 MHz, ~6985 at 2 MHz, since the ring sets the ~71.6 ps code width and the clock sets the full scale. A broken run collapses onto a handful of values — often a single code repeated across every frame.

process_time_codes histograms every pixel's codes over all frames and saves the (32, 32, n_codes) per-pixel code histogram to processed/<name>_<tag>_time_codes.npy — a few tens of MB at the slowest clock, so any pixel can then be inspected without re-reading the run. plot_time_codes plots one pixel from it to results/data_quality/ and returns that pixel's counts per code:

import numpy as np
from dapkel.functions import data_quality

# a handful of files is enough to see whether the timing is populated
data_quality.process_time_codes(path, nframes=10_000, max_files=5)

hist = data_quality.plot_time_codes(path, pixel=(16, 16))
print(hist.sum(), "valid codes,", np.count_nonzero(hist), "unique")

The title gives the span of the codes as a full scale in ns. A few unique values out of thousands of frames means the run is unusable. This mirrors the timestamp inspection in Kelpie_run.m.

Run it on a pixel or two before spending time on coincidences.

API

data_quality

Data-quality check to run on a fresh Kelpie acquisition.

process_time_codes   raw '.bin' -> processed/<name>_<tag>_time_codes.npy
plot_time_codes      that array -> results/data_quality/ one pixel's codes

Nothing here contributes to a physics result - it answers one question before you spend time on analysis: did the TDC actually record timing?

The failure mode is silent. When the TDC never stops, 'unpack' still returns a full array of plausible integers and every downstream analysis still runs on meaningless data. Healthy codes spread over the full code range of the clock in use (~279 codes at 50 MHz, ~1397 at 10 MHz, ~6985 at 2 MHz); a broken run collapses onto a handful of values.

See docs/guide/data_quality.md.

process_time_codes

process_time_codes(path: str, nframes: int, tag: str = 'ORT', *, max_files: int | None = None) -> str

Histogram every pixel's TDC codes over all frames and save it.

Saves the (32, 32, n_codes) per-pixel code histogram, which is a few tens of MB at the slowest clock - so any pixel can then be inspected without re-reading the run.

Parameters:

Name Type Description Default
path str

Folder with the '.bin' data files.

required
nframes int

Frames the acquisition recorded per '.bin' file.

required
tag str

File tag of a timestamping program. The default is 'ORT'.

'ORT'
max_files int | None

Use at most this many files - a handful is enough to see whether the timing is populated. The default is None (all).

None

Returns:

Type Description
str

Path to the saved '.npy'.

Source code in src/dapkel/functions/data_quality.py
def process_time_codes(
    path: str,
    nframes: int,
    tag: str = "ORT",
    *,
    max_files: int | None = None,
) -> str:
    """Histogram every pixel's TDC codes over all frames and save it.

    Saves the (32, 32, n_codes) per-pixel code histogram, which is a few tens
    of MB at the slowest clock - so any pixel can then be inspected without
    re-reading the run.

    Parameters
    ----------
    path : str
        Folder with the '.bin' data files.
    nframes : int
        Frames the acquisition recorded per '.bin' file.
    tag : str, optional
        File tag of a timestamping program. The default is ``'ORT'``.
    max_files : int | None, optional
        Use at most this many files - a handful is enough to see whether the
        timing is populated. The default is None (all).

    Returns
    -------
    str
        Path to the saved '.npy'.
    """
    files = io.find_bin_files(path, tag)[:max_files]
    counts = reduce.code_histogram(files, nframes=nframes, label=tag)
    return store.save_map(
        counts,
        path,
        kind=_KIND,
        tag=tag,
        meta={"n_files": len(files), "nframes": int(nframes)},
    )

plot_time_codes

plot_time_codes(path: str, pixel: Pixel = (16, 16), tag: str = 'ORT') -> ndarray

Plot one pixel's TDC-code histogram from the saved per-pixel histogram.

Mirrors the Kelpie_run.m timestamp inspection: the codes should span the whole code range of the reference clock and not collapse onto a few values. The title gives the span as a full scale in ns, the same measurement every other analysis takes off the data ('core.timing.full_scale_from_max_code').

Parameters:

Name Type Description Default
path str

Data folder that 'process_time_codes' was run on.

required
pixel tuple[int, int]

The (row, col) pixel to inspect. The default is (16, 16).

(16, 16)
tag str

The tag it was run with. The default is 'ORT'.

'ORT'

Returns:

Type Description
ndarray

The pixel's counts per code.

Source code in src/dapkel/functions/data_quality.py
def plot_time_codes(
    path: str, pixel: Pixel = (16, 16), tag: str = "ORT"
) -> np.ndarray:
    """Plot one pixel's TDC-code histogram from the saved per-pixel histogram.

    Mirrors the ``Kelpie_run.m`` timestamp inspection: the codes should span
    the whole code range of the reference clock and not collapse onto a few
    values. The title gives the span as a full scale in ns, the same
    measurement every other analysis takes off the data
    ('core.timing.full_scale_from_max_code').

    Parameters
    ----------
    path : str
        Data folder that 'process_time_codes' was run on.
    pixel : tuple[int, int], optional
        The ``(row, col)`` pixel to inspect. The default is ``(16, 16)``.
    tag : str, optional
        The tag it was run with. The default is ``'ORT'``.

    Returns
    -------
    np.ndarray
        The pixel's counts per code.
    """
    counts, _ = store.load_map(path, kind=_KIND, tag=tag)
    r, c = int(pixel[0]), int(pixel[1])
    hist = counts[r, c]
    used = np.nonzero(hist)[0]

    # The x axis stays in raw codes: whether the codes span the TDC's range is
    # the question, and the code range is the evidence.
    fig, ax = plt.subplots()
    ax.step(np.arange(hist.size), hist, where="mid")
    ax.set_xlabel("Timestamp  (TDC code)")
    ax.set_ylabel("Counts")
    span = (
        f"{timing.full_scale_from_max_code(int(used[-1])) * 1e9:.1f} ns"
        if used.size
        else "nothing"
    )
    ax.set_title(
        f"Pixel ({r},{c})  time codes  (n={hist.sum()}, {used.size} unique, "
        f"spanning {span})"
    )
    ax.grid(True)
    fig.tight_layout()

    name = os.path.basename(os.path.normpath(path))
    store.save_figure(
        fig,
        store.results_dir(path, "data_quality"),
        f"{store.map_file_name(name, _KIND, tag).removesuffix('.npy')}_{r}_{c}.png",
    )
    return hist