Skip to content

Optical cross-talk

Cross-talk is measured on the Kelpie v2 sensor by reading, for each 2×2 macropixel, two on-chip counters over the same set of frames:

  • coinc — frames in which both micropixels of the pair fired (logical AND)
  • OR — frames in which at least one of the pair fired (logical union)

The per-macropixel cross-talk metric is the ratio of the accumulated counts:

CT = sum(coinc) / sum(OR)

which is dimensionless (a probability), so the frame time cancels and no exposure time is needed.

This slightly overestimates true optical cross-talk

The raw ratio includes accidental coincidences from independent dark counts landing in the same frame.

Macropixels where OR = 0 are undefined and come back as NaN. Everything in core.plots is NaN-aware, so those pixels are dropped from the statistics rather than poisoning them.

SPAD geometry — the indices run clockwise

This is the single most important fact in this module, and it is not the convention you would guess. Inside one 2×2 macropixel the four SPADs are indexed clockwise, not row-major:

S0 (0,0)   S1 (0,1)
S3 (1,0)   S2 (1,1)

So relative to S0:

pairing neighbour direction cell offset
S0S1 right horizontal (0, 1)
S0S3 below vertical (1, 0)
S0S2 corner diagonal (1, 1)

Note that S0S2 is the diagonal pairing and S0S3 the vertical one — the reverse of what a row-major reading gives. Vertical and diagonal cross-talk are physically different magnitudes (diagonal SPADs sit further apart), so swapping them silently mislabels two real measurements.

The layout is encoded once, in core.reduce.SPAD_LAYOUT (see core.reduce), which both the (64, 64) quadrant assembly of DCR and hitmap and the cross-talk directions read. tests/test_spad_geometry.py pins it, and crosstalk_analysis._DIRECTIONS, against an independent spelling of it, so the encodings cannot drift apart.

Data layout

One folder per micropixel pairing, e.g. S0S1, S0S2, S0S3. Each folder holds many repeated .bin acquisitions per counter, named like CT_coincS0S1<n>.bin / CT_ORS0S1<n>.bin; the counts are summed across every acquisition for statistics.

The coinc and OR files are told apart by their counter token, case sensitively: coinc files carry coinc, OR files OR. The pairing (S0S1...) is read from the file names.

The 01 preliminary batches use a CT01_ / CT02_ prefix and are excluded by the default tag="CT_", so the two acquisitions are never silently mixed.

Processing and plotting

Pointed at a folder holding one pairing's coinc/OR files, process_crosstalk saves its (32, 32) macropixel map as processed/<name>_ct_crosstalk.npy — the tag, not the pair, is in the file name; the pair is in the sidecar. plot_crosstalk loads it and saves a heatmap and a sorted distribution, in percent, to results/crosstalk/.

from dapkel.functions import crosstalk_analysis as ct

ct.process_crosstalk("path/to/S0S1", nframes=1000)
ct.plot_crosstalk("path/to/S0S1")

# a parent of S0S1 / S0S3 / S0S2: each pairing, then the combined map
ct.process_crosstalk("path/to/crosstalk", nframes=1000)
ct.plot_crosstalk("path/to/crosstalk")

The combined 64×64 map

Pointed at a parent folder holding one sub-folder per pairing instead, process_crosstalk processes and saves each sub-folder the same way, then lays the (32, 32) directional maps onto a single (64, 64) micropixel grid, saved in the parent. Sub-folders that are not S0-based pairings are skipped, and a missing direction is reported. Within each 2×2 block the neighbour cells carry S0's cross-talk in that direction, and the S0 cell carries the mean of the available directions:

mean   | horizontal
vertical | diagonal

plot_crosstalk on the parent draws that (64, 64) map and a bar chart of the median cross-talk per direction (..._direction_summary.png).

API

crosstalk_analysis

Optical cross-talk (CT) maps from the on-chip coinc/OR counters.

process_crosstalk   raw '.bin' -> processed/<name>_<tag>_crosstalk.npy
plot_crosstalk      that array -> results/crosstalk/ figures

CT = sum(coinc) / sum(OR) per macropixel - dimensionless, so the frame time cancels and no exposure time is needed. NaN where OR = 0.

A folder holds one micropixel pairing (S0S1...), read from the file names. Point 'process_crosstalk' at a parent folder of pairings instead and it also combines them: S0's cross-talk towards its horizontal, vertical and diagonal neighbour laid onto one (64, 64) micropixel map.

NOTE: the SPAD indices run CLOCKWISE inside a 2x2 macropixel ('core.reduce.SPAD_LAYOUT')::

S0 (0,0)   S1 (0,1)
S3 (1,0)   S2 (1,1)

so S0S1 is horizontal, S0S3 VERTICAL and S0S2 DIAGONAL - the reverse of a row-major reading. See docs/guide/crosstalk.md.

process_crosstalk

process_crosstalk(path: str, nframes: int, tag: str = 'CT_') -> str

Unpack the coinc/OR files, compute the cross-talk map and save it.

On a folder holding one pairing's '.bin' files this saves its (32, 32) macropixel map. On a parent folder - one sub-folder per pairing, e.g. S0S1 / S0S3 / S0S2 - it processes and saves each sub-folder the same way, then saves the combined (64, 64) map in the parent: in every 2x2 block the neighbour cells hold S0's cross-talk in that direction and the S0 cell the mean over the directions measured.

Parameters:

Name Type Description Default
path str

Folder with the coinc/OR '.bin' files, or a parent of such folders.

required
nframes int

Frames the acquisition recorded per '.bin' file (one value for every pairing).

required
tag str

Filename prefix selecting the measurement batch. The default is 'CT_', the main batch, which excludes the preliminary 'CT01_'-style ones so two acquisitions are never mixed.

'CT_'

Returns:

Type Description
str

Path to the saved '.npy' (the combined map for a parent folder).

Raises:

Type Description
FileNotFoundError

Raised when neither path nor any sub-folder holds a pairing.

Source code in src/dapkel/functions/crosstalk_analysis.py
def process_crosstalk(path: str, nframes: int, tag: str = "CT_") -> str:
    """Unpack the coinc/OR files, compute the cross-talk map and save it.

    On a folder holding one pairing's '.bin' files this saves its (32, 32)
    macropixel map. On a *parent* folder - one sub-folder per pairing, e.g.
    S0S1 / S0S3 / S0S2 - it processes and saves each sub-folder the same way,
    then saves the combined (64, 64) map in the parent: in every 2x2 block the
    neighbour cells hold S0's cross-talk in that direction and the S0 cell the
    mean over the directions measured.

    Parameters
    ----------
    path : str
        Folder with the coinc/OR '.bin' files, or a parent of such folders.
    nframes : int
        Frames the acquisition recorded per '.bin' file (one value for every
        pairing).
    tag : str, optional
        Filename prefix selecting the measurement batch. The default is
        ``'CT_'``, the main batch, which excludes the preliminary
        ``'CT01_'``-style ones so two acquisitions are never mixed.

    Returns
    -------
    str
        Path to the saved '.npy' (the combined map for a parent folder).

    Raises
    ------
    FileNotFoundError
        Raised when neither ``path`` nor any sub-folder holds a pairing.
    """
    single = _ct_map(path, nframes, tag)
    if single is not None:
        ct, meta = single
        out_path = store.save_map(
            ct, path, kind=_KIND, tag=tag, meta={**meta, "unit": "ratio"}
        )
        print(
            f"  {meta['pair'] or '?'}: median CT {np.nanmedian(ct) * 100:.2f}%  "
            f"max {np.nanmax(ct) * 100:.2f}%  "
            f"({meta['undefined_macropixels']} macropixels with OR=0 -> NaN)"
        )
        return out_path

    print("\n> > > Combining per-direction S0 cross-talk into a 64x64 map < < <\n")
    maps: dict[str, np.ndarray] = {}
    combined = np.full((64, 64), np.nan)
    for entry in sorted(os.scandir(path), key=lambda e: e.name):
        if not entry.is_dir() or entry.name in (store.PROCESSED_DIR, store.RESULTS_DIR):
            continue
        found = _ct_map(entry.path, nframes, tag)
        if found is None:
            continue
        ct, meta = found
        idx = [int(d) for d in re.findall(r"S(\d)", meta["pair"])]
        partner = next((i for i in idx if i != 0), None)
        if len(idx) != 2 or 0 not in idx or partner not in _DIRECTIONS:
            print(f"  {entry.name}: {meta['pair'] or '?'} is not S0-based, skipped")
            continue
        store.save_map(
            ct, entry.path, kind=_KIND, tag=tag, meta={**meta, "unit": "ratio"}
        )
        direction = _DIRECTIONS[partner]
        maps[direction] = ct
        drow, dcol = reduce.SPAD_LAYOUT[partner]
        combined[drow::2, dcol::2] = ct
        print(
            f"  {entry.name}: {meta['pair']} ({direction}) median CT "
            f"{np.nanmedian(ct) * 100:.2f}%"
        )
    if not maps:
        raise FileNotFoundError(
            f"No '{tag}*' coinc/OR files in {path} or any sub-folder of it."
        )
    missing = [d for d in _DIRECTIONS.values() if d not in maps]
    if missing:
        print(f"  WARNING: missing direction(s): {', '.join(missing)}")

    # A macropixel with no measured direction stays NaN; its "Mean of empty
    # slice" warning is expected.
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        combined[0::2, 0::2] = np.nanmean(np.stack(list(maps.values())), axis=0)

    medians = {d: float(np.nanmedian(m) * 100.0) for d, m in maps.items()}
    medians["overall (mean)"] = float(np.nanmedian(combined[0::2, 0::2]) * 100.0)
    return store.save_map(
        combined,
        path,
        kind=_KIND,
        tag=tag,
        meta={"combined": True, "median_ct_percent": medians, "unit": "ratio"},
    )

plot_crosstalk

plot_crosstalk(path: str, tag: str = 'CT_', *, cmap: str | None = None) -> ndarray

Load a saved cross-talk map and save its figures.

A single pairing gets a heatmap and a sorted distribution; a combined parent-folder map gets the (64, 64) directional heatmap and a bar chart of the median cross-talk per direction. All values are drawn in percent.

Parameters:

Name Type Description Default
path str

Folder that 'process_crosstalk' was run on.

required
tag str

The prefix it was run with. The default is 'CT_'.

'CT_'
cmap str | None

Matplotlib colormap name for the heatmap. The default is None - the active style's.

None

Returns:

Type Description
ndarray

The cross-talk map (dimensionless ratio, NaN where undefined).

Raises:

Type Description
FileNotFoundError

Raised when no saved map exists for tag.

Source code in src/dapkel/functions/crosstalk_analysis.py
def plot_crosstalk(
    path: str, tag: str = "CT_", *, cmap: str | None = None
) -> np.ndarray:
    """Load a saved cross-talk map and save its figures.

    A single pairing gets a heatmap and a sorted distribution; a combined
    parent-folder map gets the (64, 64) directional heatmap and a bar chart of
    the median cross-talk per direction. All values are drawn in percent.

    Parameters
    ----------
    path : str
        Folder that 'process_crosstalk' was run on.
    tag : str, optional
        The prefix it was run with. The default is ``'CT_'``.
    cmap : str | None, optional
        Matplotlib colormap name for the heatmap. The default is None - the
        active style's.

    Returns
    -------
    np.ndarray
        The cross-talk map (dimensionless ratio, NaN where undefined).

    Raises
    ------
    FileNotFoundError
        Raised when no saved map exists for ``tag``.
    """
    ct, meta = store.load_map(path, kind=_KIND, tag=tag)
    name = store.map_file_name(
        os.path.basename(os.path.normpath(path)), _KIND, tag
    ).removesuffix(".npy")
    results_dir = store.results_dir(path, _KIND)
    median, maximum, _ = plots.map_stats(ct, scale=100.0)

    if meta.get("combined"):
        fig = plots.sensor_map(
            ct,
            scale=100.0,
            title=(
                "S0 directional cross-talk\n2x2 block: mean | horiz / vert | "
                f"diag  (median {median:.1f} %)"
            ),
            clabel="CT (%)",
            cmap=cmap,
            xlabel="Micropixel column",
            ylabel="Micropixel row",
        )
        store.save_figure(fig, results_dir, f"{name}_heatmap.png")
        plt.close(fig)

        medians = meta["median_ct_percent"]
        fig, ax = plt.subplots()
        bars = ax.bar(list(medians), list(medians.values()))
        ax.bar_label(bars, fmt="%.2f")
        ax.set_ylabel("Median CT  (%)")
        ax.set_title("Cross-talk by direction")
        ax.grid(True, axis="y")
        fig.tight_layout()
        store.save_figure(fig, results_dir, f"{name}_direction_summary.png")
        plt.close(fig)
        return ct

    pair = meta.get("pair", "")
    fig = plots.sensor_map(
        ct,
        scale=100.0,
        title=f"{pair}  cross-talk\nmedian {median:.1f}  max {maximum:.1f}  %",
        clabel="CT (%)",
        cmap=cmap,
        xlabel="Macropixel column",
        ylabel="Macropixel row",
    )
    store.save_figure(fig, results_dir, f"{name}_heatmap.png")
    plt.close(fig)

    fig = plots.sorted_distribution(
        ct,
        scale=100.0,  # ratio -> percent; NaN (OR=0) macropixels are dropped
        ylabel="Cross-talk  (%)",
        xlabel="Macropixel percentile  (%)",
        title=f"Sorted {pair} cross-talk distribution",
        fmt=".1f",
        unit="%",
    )
    store.save_figure(fig, results_dir, f"{name}_distribution.png")
    plt.close(fig)
    return ct