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 samelo/hias 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 – sokeep="most_points"selects, within each setpoint group, the row with the largestn_measurements(the housekeeping-excluded count fromsummary();n_pointsis used when the frame has non_measurementscolumn). 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 bybuild_catalog(). Must containbyandrelative_path; bothprefer_spancolumns whenprefer_spanis given; and a point-count column (n_measurementsorn_points) whenkeep="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 – seetol.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 forkeep="widest", where the row with the largest span is kept. Optional forkeep="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 consecutivebyvalues (sorted ascending) differ by no more thantol; a gap larger thantolstarts a new group. Default0.0: only exactly equal setpoints group together. A physically meaningful choice is the instrument’s setpoint precision (e.g.0.5K for a temperature column recorded to the nearest half-kelvin).keep (
str) – Selection policy within a group."widest"(default) keeps the row with the largestprefer_spanrange;"most_points"keeps the row with the most measured points (see above for when each is the right choice). Any other value raisesValueError.
- Return type:
- Returns:
pandas.DataFrame– One row per setpoint group, inby-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 thekeeppolicy) are broken deterministically byrelative_path(lexicographically smallest wins), so the result is reproducible across runs regardless of input row order.- Raises:
ValueError – If
keepis not a supported policy, orkeep="widest"is requested withoutprefer_span.KeyError – If any column the chosen policy needs (see
dfabove) is missing fromdf.