calamari.catalog.select_representative#

calamari.catalog.select_representative(df, *, by, prefer_span=None, tol=0.0, keep='widest')#

Thin a catalog DataFrame to one representative row per nominal setpoint.

A catalog built over a real instrument tree routinely contains more than one sweep recorded at what is physically the same setpoint: a run aborted partway through and re-taken, an operator repeating a sweep, successive files that agree to the setpoint’s rounding precision but not bit-for-bit. Keeping every one of them double-counts boundary points derived downstream (a truncated re-run masquerading as an independent sweep, or the same transition counted twice) – this function reduces each cluster of same-setpoint rows to a single representative before that happens. The originating case is a per-temperature field-sweep catalog where a partial re-run needs to lose to the sweep that actually covers the full field range; this generalises that policy to any setpoint column and any pair of span-defining columns.

keep="widest" has a blind spot the summary columns cannot expose: a span is only the two endpoints. A superseded first attempt whose sweep reached both ends but skipped or lost a stretch in the middle – an interior hole – reports exactly the same lo/hi as the complete re-run, so on span it ties with the sweep that replaced it, and the lexicographic tie-break can keep the holey original. This has happened on a real campaign: temperature scans at fixed fields where abandoned first attempts matched the re-runs’ summary span while missing a large interior temperature interval. Point count sees what the span cannot – the hole is exactly the missing rows – so keep="most_points" selects, within each setpoint group, the row with the largest n_measurements (the housekeeping-excluded count from summary(); n_points is used when the frame has no n_measurements column). This assumes the candidates sample comparably – a densely-sampled narrow sweep out-counts a coarse complete one – so prefer it when the competing rows are attempts at the same sweep, and "widest" when they genuinely differ in range.

Parameters:
  • df (DataFrame) – Catalog rows, e.g. as produced by build_catalog(). Must contain by and relative_path; both prefer_span columns when prefer_span is given; and a point-count column (n_measurements or n_points) when keep="most_points".

  • by (str) – Name of the setpoint column to group rows by (e.g. a temperature or field column). Rows are grouped by proximity in this column, not by exact equality – see tol.

  • prefer_span (tuple[str, str] | None) – (lo_col, hi_col) column names whose difference (hi - lo) measures how much of a range each row’s measurement spans (e.g. ("H_min_Oe", "H_max_Oe")). Required for keep="widest", where the row with the largest span is kept. Optional for keep="most_points": if given there, span is the secondary criterion among rows with equal point counts.

  • tol (float) – Rows are chained into the same group when consecutive by values (sorted ascending) differ by no more than tol; a gap larger than tol starts a new group. Default 0.0: only exactly equal setpoints group together. A physically meaningful choice is the instrument’s setpoint precision (e.g. 0.5 K for a temperature column recorded to the nearest half-kelvin).

  • keep (str) – Selection policy within a group. "widest" (default) keeps the row with the largest prefer_span range; "most_points" keeps the row with the most measured points (see above for when each is the right choice). Any other value raises ValueError.

Return type:

DataFrame

Returns:

pandas.DataFrame – One row per setpoint group, in by-ascending order, with the original columns and column order preserved (no helper columns leak into the result) and the index reset. Ties within a group (after every criterion of the keep policy) are broken deterministically by relative_path (lexicographically smallest wins), so the result is reproducible across runs regardless of input row order.

Raises:
  • ValueError – If keep is not a supported policy, or keep="widest" is requested without prefer_span.

  • KeyError – If any column the chosen policy needs (see df above) is missing from df.