code: def detrend(
      data: ArrayLike,
      axis: int = -1,
      type: str = "linear",
      bp: int = 0,
      overwrite_data: None = None,
  ) -> Array:
      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
              sl = slice(bp_arr[m], bp_arr[m + 1])
              coef, *_ = linalg.lstsq(A, data_arr[sl])
              data_arr = data_arr.at[sl].add(
                  -jnp.matmul(A, coef, precision=lax.Precision.HIGHEST)
              )
          return jnp.moveaxis(data_arr.reshape(shape), 0, axis)
  
imports: from __future__ import annotations
  from collections.abc import Sequence
  from functools import partial
  import math
  import operator
  from typing import Callable
  import warnings
  import numpy
  import scipy.signal
  from scipy.fft import next_fast_len
  import jax
  import jax.numpy.fft
  import jax.numpy
  from jax import lax
  from jax._src.api_util import _ensure_index_tuple
  from jax._src import dtypes
  from jax._src.lax.lax import PrecisionLike
  from jax._src.numpy import linalg
  from jax._src.numpy.util import check_arraylike, _wraps, promote_dtypes_inexact, promote_dtypes_complex
  from jax._src.third_party.scipy import signal_helper
  from jax._src.typing import Array, ArrayLike
  from jax._src.util import canonicalize_axis, tuple_delete, tuple_insert
label_desc: Use image node_id 8 for calling a global function with example usage: detrend(data, axis, type, bp, overwrite_data) and returns: unknown, jnp