Possible Type inconsistency in an argument passed to a SciPy function
Created by: MiquelLluis
Hello!
I've recently come across a ValueError raised by SciPy when calling the method gwpy.timeseries.TimeSeries.whiten
using a custom window stored in a NumPy array:
Traceback (most recent call last):
File "/home/miquel/anaconda3/envs/p38/lib/python3.8/site-packages/scipy/signal/windows/windows.py", line 2132, in get_window
beta = float(window)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "example.py", line 31, in <module>
signal_w = signal.whiten(
File "/home/miquel/anaconda3/envs/p38/lib/python3.8/site-packages/gwpy/timeseries/timeseries.py", line 1747, in whiten
tdw = filter_design.fir_from_transfer(1/asd.value, ntaps=ntaps,
File "/home/miquel/anaconda3/envs/p38/lib/python3.8/site-packages/gwpy/signal/filter_design.py", line 254, in fir_from_transfer
impulse = truncate_impulse(impulse, ntaps=ntaps, window=window)
File "/home/miquel/anaconda3/envs/p38/lib/python3.8/site-packages/gwpy/signal/filter_design.py", line 208, in truncate_impulse
window = signal.get_window(window, ntaps)
File "/home/miquel/anaconda3/envs/p38/lib/python3.8/site-packages/scipy/signal/windows/windows.py", line 2146, in get_window
raise ValueError("%s as window type is not supported." %
ValueError: <class 'numpy.ndarray'> as window type is not supported.
I looked a bit into it following the traceback error and apparently there might be an inconsistency either at the code or at the docstring of (at least) the function truncate_impulse
(here). At the documentation says that the argument window accepts a numpy array, but then it calls the Scipy function scipy.signal.get_window
whose docstring states that it only accepts string, float, or tuple.
For now I managed to avoid this by telling the whiten
function to use its own window (in my casewindow=('tukey', 0.2)
) but I would like to be able to use my own pre-computed window. However since I'm not an actual developer (in fact this is my first time opening an issue) I was not sure how to proceed, so I apologize in advance if I made any mistakes.
Here is a script with a simplified example of what I'm doing, to reproduce the error:
import gwpy.timeseries
import numpy as np
import scipy as sp
import scipy.signal
# Parameters
SR = 4096 # Sample rate (Hz)
signal_duration = 50 # Seconds
fftlength = 1
overlap = 0.5
# Signal to whiten (some seconds of Gaussian noise)
signal = gwpy.timeseries.TimeSeries(
data=np.random.normal(scale=0.1, size=int(signal_duration*SR)),
t0=0,
sample_rate=SR
)
# Custom window
window = sp.signal.windows.tukey(int(fftlength*SR), alpha=0.2)
# Compute the ASD from the first seconds of the signal.
asd = signal.crop(0, 5).asd(
fftlength=fftlength,
overlap=overlap,
window=window
)
# Whitening, HERE should be raised the ValueError by SciPy.
signal_w = signal.whiten(
fftlength=fftlength,
overlap=overlap,
window=window,
asd=asd
)
print("All OK!")
I'm using Kubuntu 20.04.5 with a conda environment with the following version's:
- Python '3.8.13'
- GWpy '2.1.3'
- SciPy '1.7.3'
- NumPy '1.22.3'
Thanks in advance for your help and work!