code: def detrend(data, axis=-1, type="linear", bp=0, overwrite_data=False):
      """
      Remove linear trend along axis from data.
  
      Parameters
      ----------
      data : array_like
          The input data.
      axis : int, optional
          The axis along which to detrend the data. By default this is the
          last axis (-1).
      type : {'linear', 'constant'}, optional
          The type of detrending. If ``type == 'linear'`` (default),
          the result of a linear least-squares fit to `data` is subtracted
          from `data`.
          If ``type == 'constant'``, only the mean of `data` is subtracted.
      bp : array_like of ints, optional
          A sequence of break points. If given, an individual linear fit is
          performed for each part of `data` between two break points.
          Break points are specified as indices into `data`. This parameter
          only has an effect when ``type == 'linear'``.
      overwrite_data : bool, optional
          If True, perform in place detrending and avoid a copy. Default is False
  
      Returns
      -------
      ret : ndarray
          The detrended input data.
  
      See Also
      --------
      scipy.signal.detrend
  
  
      """
      if type not in ["linear", "l", "constant", "c"]:
          raise ValueError("Trend type must be 'linear' or 'constant'.")
      data = cupy.asarray(data)
      dtype = data.dtype.char
      if dtype not in "dfDF":
          dtype = "d"
      if type in ["constant", "c"]:
          ret = data - cupy.mean(data, axis, keepdims=True)
          return ret
      else:
          dshape = data.shape
          N = dshape[axis]
          bp = cupy.sort(cupy.unique(cupy.r_[0, bp, N]))
          if cupy.any(bp > N):
              raise ValueError(
                  "Breakpoints must be less than length "
                  "of data along given axis."
              )
          bp = bp.tolist()
          # Restructure data so that axis is along first dimension and
          #  all other dimensions are collapsed into second dimension
          rnk = len(dshape)
          if axis < 0:
              axis = axis + rnk
          newdata = cupy.moveaxis(data, axis, 0)
          newdata_shape = newdata.shape
          newdata = newdata.reshape(N, -1)
  
          if not overwrite_data:
              newdata = newdata.copy()  # make sure we have a copy
          if newdata.dtype.char not in "dfDF":
              newdata = newdata.astype(dtype)
  
          # Find leastsq fit and remove it for each piece
          for m in range(len(bp) - 1):
              Npts = bp[m + 1] - bp[m]
              A = cupy.ones((Npts, 2), dtype)
              A[:, 0] = cupy.arange(1, Npts + 1, dtype=dtype) / Npts
              sl = slice(bp[m], bp[m + 1])
              coef, resids, rank, s = lstsq(A, newdata[sl], rcond=None)
              newdata[sl] = newdata[sl] - A @ coef
  
          # Put data back in original shape.
          newdata = newdata.reshape(newdata_shape)
          ret = cupy.moveaxis(newdata, 0, axis)
          return ret
  
imports: import warnings
  import cupy
  from cupy._core import internal
  from cupy.linalg import lstsq
  import cupyx.scipy.fft
  from cupyx.scipy.ndimage import _util
  from cupyx.scipy.ndimage import _filters
  from cupyx.scipy.signal import _signaltools_core
  from cupyx.scipy.signal._arraytools import const_ext, even_ext, odd_ext, axis_reverse, axis_slice, axis_assign
  from cupyx.scipy.signal._iir_utils import apply_iir, apply_iir_sos, compute_correction_factors, compute_correction_factors_sos
label_desc: Use image node_id 18 for calling a global function with example usage: detrend(data, axis, type, bp, overwrite_data) and returns: ret, ret