jax-ml/jax · error · ValueError

Trend type must be 'linear' or 'constant'.

Error message

Trend type must be 'linear' or 'constant'.

What it means

detrend's 'type' argument selects the trend model and must be exactly 'linear' or 'constant'. Any other string (including case variants or scipy's alternate spellings) is rejected before any computation.

Source

Thrown at jax/_src/scipy/signal.py:530

    >>> with jnp.printoptions(precision=3, suppress=True):  # suppress float error
    ...   print("Detrended:", detrended)
    ...   print("Underlying trend:", data - detrended)
    Detrended: [-1. -0.  2. -0. -1.]
    Underlying trend: [ 2.  4.  6.  8. 10.]

    Removing a constant trend from the data:

    >>> detrended = jax.scipy.signal.detrend(data, type='constant')
    >>> with jnp.printoptions(precision=3):  # suppress float error
    ...   print("Detrended:", detrended)
    ...   print("Underlying trend:", data - detrended)
    Detrended: [-5. -2.  2.  2.  3.]
    Underlying trend: [6. 6. 6. 6. 6.]
  """
  if overwrite_data is not None:
    raise NotImplementedError("overwrite_data argument not implemented.")
  if type not in ['constant', 'linear']:
    raise ValueError("Trend type must be 'linear' or 'constant'.")
  data_arr, = promote_dtypes_inexact(jnp.asarray(data))
  if type == 'constant':
    return data_arr - data_arr.mean(axis, keepdims=True)
  else:
    N = data_arr.shape[axis]
    # bp is static, so we use np operations to avoid pushing to device.
    bp_arr = np.sort(np.unique(np.r_[0, bp, N]))
    if bp_arr[0] < 0 or bp_arr[-1] > N:
      raise ValueError("Breakpoints must be non-negative and less than length of data along given axis.")
    data_arr = jnp.moveaxis(data_arr, axis, 0)
    shape = data_arr.shape
    data_arr = data_arr.reshape(N, -1)
    for m in range(len(bp_arr) - 1):
      Npts = bp_arr[m + 1] - bp_arr[m]
      A = jnp.vstack([
        jnp.ones(Npts, dtype=data_arr.dtype),
        jnp.arange(1, Npts + 1, dtype=data_arr.dtype) / Npts.astype(data_arr.dtype)
      ]).T

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use exactly 'linear' or 'constant' (lowercase)
  2. Normalize external strings: type=type.strip().lower() and validate against the allowed pair

Example fix

// before
jax.scipy.signal.detrend(data, type=cfg['detrend'])
// after
t = cfg['detrend'].strip().lower()
assert t in ('linear', 'constant')
jax.scipy.signal.detrend(data, type=t)
Defensive patterns

Strategy: validation

Validate before calling

type_ = type_.strip().lower()
assert type_ in ('linear', 'constant')

Type guard

def is_valid_trend_type(t: str) -> bool:
    return t in ('linear', 'constant')

Prevention

When it happens

Trigger: detrend(x, type='Constant'), type='c', or a typo like 'liner'; feeding type from an unvalidated config string.

Common situations: Config-driven preprocessing; porting code that used scipy constants or abbreviations.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/c9c586333c62a696. Report an issue: GitHub.