Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: signal.detrend on main no longer accepts a sequence for bp #18675

Closed
jakevdp opened this issue Jun 14, 2023 · 4 comments · Fixed by #18676
Closed

BUG: signal.detrend on main no longer accepts a sequence for bp #18675

jakevdp opened this issue Jun 14, 2023 · 4 comments · Fixed by #18676
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.signal
Milestone

Comments

@jakevdp
Copy link
Member

jakevdp commented Jun 14, 2023

Describe your issue.

Passing a sequence of integers to the bp argument of scipy.signal.detrend results in an error on the main branch. This appears to be due to #18455, which is not yet part of any release or pre-release (see #18455 (comment))

Reproducing Code Example

import scipy
import numpy as np
rng = np.random.RandomState(12345)

x = rng.rand(10)
bp = np.array([0, 2])

scipy.signal.detrend(x, bp=bp)

Error message

/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py:270: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  ar = np.asanyarray(ar)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-9-3498418e6fd9>](https://localhost:8080/#) in <cell line: 8>()
      7 
----> 8 print(scipy.signal.detrend(x, bp=bp)

[/usr/local/lib/python3.10/dist-packages/scipy/signal/_signaltools.py](https://localhost:8080/#) in detrend(data, axis, type, bp, overwrite_data)
   3561         dshape = data.shape
   3562         N = dshape[axis]
-> 3563         bp = np.sort(np.unique([0, bp, N]))
   3564         if np.any(bp > N):
   3565             raise ValueError("Breakpoints must be less than length "

/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in unique(*args, **kwargs)

[/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py](https://localhost:8080/#) in unique(ar, return_index, return_inverse, return_counts, axis)
    270     ar = np.asanyarray(ar)
    271     if axis is None:
--> 272         ret = _unique1d(ar, return_index, return_inverse, return_counts)
    273         return _unpack_tuple(ret)
    274 

[/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py](https://localhost:8080/#) in _unique1d(ar, return_index, return_inverse, return_counts)
    331         aux = ar[perm]
    332     else:
--> 333         ar.sort()
    334         aux = ar
    335     mask = np.empty(aux.shape, dtype=np.bool_)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

SciPy/NumPy/Python version and system information

1.11.0.dev0+2112.00f4e2e 1.22.4 sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT= DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= HASWELL MAX_THREADS=2
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.21.dev
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT= DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= HASWELL MAX_THREADS=2
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.21.dev
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.10.4
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 0.29.34
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  pythran:
    include directory: ../../tmp/pip-build-env-qaptfljs/overlay/lib/python3.10/site-packages/pythran
    version: 0.13.1
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp310-cp310/bin/python
  version: '3.10'
@jakevdp jakevdp added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Jun 14, 2023
@andyfaff
Copy link
Contributor

Fyi the location for nightly builds is now https://pypi.anaconda.org/scientific-python-nightly-wheels/simple

@ev-br
Copy link
Member

ev-br commented Jun 14, 2023

So it's the first glitch of the array-api compat movement: array API does not have np.r_ .

In [5]: np.unique(np.r_[0, bp, 10])
Out[5]: array([ 0,  2, 10])

In [6]: np.unique([0, bp, 10])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 np.unique([0, bp, 10])

File <__array_function__ internals>:200, in unique(*args, **kwargs)

File ~/mambaforge/envs/scipy-dev/lib/python3.10/site-packages/numpy/lib/arraysetops.py:272, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan)
    138 @array_function_dispatch(_unique_dispatcher)
    139 def unique(ar, return_index=False, return_inverse=False,
    140            return_counts=False, axis=None, *, equal_nan=True):
    141     """
    142     Find the unique elements of an array.
    143 
   (...)
    270 
    271     """
--> 272     ar = np.asanyarray(ar)
    273     if axis is None:
    274         ret = _unique1d(ar, return_index, return_inverse, return_counts, 
    275                         equal_nan=equal_nan)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

A fix is (untested) --- so np.r_ is ugly, huh :-).

In [27]: np.sort(np.unique(np.concatenate(([0], bp, [10]))))
Out[27]: array([ 0,  2, 10])

@ev-br ev-br added this to the 1.11.0 milestone Jun 14, 2023
@jakevdp
Copy link
Member Author

jakevdp commented Jun 14, 2023

It needs to handle scalar inputs for bp as well, so possibly normalize with bp = np.atleast_1d(bp) before doing that concatenation

@ev-br
Copy link
Member

ev-br commented Jun 14, 2023

Yes thanks, #18676 does it. Could you test that one Jake? The test suite for detrend is very minimal and was even more minimal until the 1.11 cycle. I wouldn't be surprised if there are more issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.signal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@andyfaff @jakevdp @ev-br @j-bowhay and others