votes up 1

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

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

""" 
    >>> noise = rng.standard_normal(npoints)
    >>> x = 3 + 2*np.linspace(0, 1, npoints) + noise
    >>> (signal.detrend(x) - noise).max()
    0.06  # random

    """
    if type not in ['linear', 'l', 'constant', 'c']:
        raise ValueError("Trend type must be 'linear' or 'constant'.")
    data = np.asarray(data)
    dtype = data.dtype.char
    if dtype not in 'dfDF':
        dtype = 'd'
    if type in ['constant', 'c']:
        ret = data - np.mean(data, axis, keepdims=True)
        return ret
ūüė≤ Agile task management is now easier than calling a taxi. #Tracklify
ūüôŹ Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this ūüė†, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

A typo error in the value of type causes this value error :

How to recreate it:

Install scipy and numpy

pipenv install scipy numpy

Run the signal.detrend function with a wrong value of type parameter

from scipy import signal
from numpy.random import default_rng
rng = default_rng()
npoints = 1000
noise = rng.standard_normal(npoints)
x = 3 + 2*np.linspace(0, 1, npoints) + noise
(signal.detrend(x,type="linar") - noise).max()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-47a1cd8cc87c> in <module>()
      5 noise = rng.standard_normal(npoints)
      6 x = 3 + 2*np.linspace(0, 1, npoints) + noise
----> 7 (signal.detrend(x,type="linar") - noise).max()

/usr/local/lib/python3.7/dist-packages/scipy/signal/signaltools.py in detrend(data, axis, type, bp, overwrite_data)
   3448     """
   3449     if type not in ['linear', 'l', 'constant', 'c']:
-> 3450         raise ValueError("Trend type must be 'linear' or 'constant'.")
   3451     data = np.asarray(data)
   3452     dtype = data.dtype.char

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

Fixed version of the code:

Correct the typo in the name "linar" i.e it should be "linear"

from scipy import signal
from numpy.random import default_rng
rng = default_rng()
npoints = 1000
noise = rng.standard_normal(npoints)
x = 3 + 2*np.linspace(0, 1, npoints) + noise
(signal.detrend(x,type="linear") - noise).max()

0.02758934334176044

Aug 26, 2021 kellemnegasi answer
kellemnegasi 29.0k

Add a possible fix

Please authorize to post fix