calamari.sweeps.segment_by_constant#

calamari.sweeps.segment_by_constant(x, *, tol, min_points=1)#

Segment a 1-D signal into contiguous runs (plateaus) of ~constant value.

Generic primitive for a signal that steps between plateaus: a stacked multi-setpoint measurement (temperature held at 2 K for a whole field sweep, stepped to 3 K for the next, …), a staircase drive, or any trace that dwells at a level then jumps to the next. A new segment begins wherever the point-to-point change |x[i] - x[i-1]| exceeds tol, so tol must be chosen between the within-plateau jitter (kept together) and the inter-plateau step size (which splits). Because the test is on the consecutive difference, slow drift within one plateau – where each step stays under tol even though the plateau’s total span does not – is tolerated and stays one segment, which is the desired behaviour for real setpoint data that settles gradually.

Parameters:
  • x (ArrayLike) – The stepped signal (e.g. a temperature column in Kelvin). Converted to float. NaNs are kept in place and counted as ordinary points: a NaN neighbour makes |diff| NaN, which does not exceed tol, so a NaN never forces a split (callers who want NaN-free segments should mask first).

  • tol (float) – Maximum consecutive change tolerated within a plateau. Must be non-negative. A tol of exactly 0 splits on any change at all, which is the natural choice when x has already been quantised onto a grid (see split_isotherms()).

  • min_points (int) – Segments shorter than this many points are dropped from the result. Default 1 (keep every run, including singletons).

Return type:

list[ndarray]

Returns:

list of numpy.ndarray – One integer index array per kept segment, in order, each holding the positions into x belonging to that plateau (contiguous and ascending). Index arrays – rather than slices – are returned so a caller can build a boolean row-mask directly (mask[seg] = True) for restrict().

Raises:

SweepInputError – If tol is negative or min_points is less than 1.