调用scipy.savgol_filter时触发DLASCLS参数4非法值错误的原因与解决方法
问题触发特征
- 原有未做修改的批量CSV处理Python脚本,在Windows 10 19041版本重大更新完成后,首次运行无异常、第二次循环处理时直接崩溃
- 复现验证可稳定触发问题的条件:
- 保留
savgol_filter滤波逻辑、完整绘制流程、原长度1001个数据点时,稳定在第二次循环触发报错 - 移除滤波步骤、跳过绘图逻辑、把数据点缩减到原长度的1/3时,脚本可全程正常运行无报错
- 保留
完整报错堆栈
** On entry to DLASCLS parameter number 4 had an illegal value
** On entry to DLASCLS parameter number 4 had an illegal value
Traceback (most recent call last):
File "simplified_problem.py", line 29, in
ysmooth = savgol_filter(y,51,5)
File "C:\Programs\Python\Python37\lib\site-packages\scipy\signal_savitzky_golay.py",
line 337, in savgol_filter
coeffs = savgol_coeffs(window_length, polyorder, deriv=deriv, delta=delta)
File "C:\Programs\Python\Python37\lib\site-packages\scipy\signal_savitzky_golay.py",
line 139, in savgol_coeffs
coeffs, _, _, _ = lstsq(A, y)
File "C:\Programs\Python\Python37\lib\site-packages\scipy\linalg\basic.py", line 1218,
in lstsq
raise LinAlgError("SVD did not converge in Linear Least Squares")
numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares
简化复现代码
import numpy as np import matplotlib.pyplot as plt from scipy.signal import savgol_filter import pandas as pd from pathlib import Path def curve(x, E, A, xmin): '''Function describing a curve x -- independent variable E -- trap energy. expected values between 0-1 eV A -- pre-exponential factor. positive for peaks with positive current and negative for peaks with negative current xmin -- position of the peak extremum. value in Kelvin k -- Boltzmannfactor in eV/K ''' k = 8.617333262145E-5 #eV/K return A*np.exp(-(E/(k*x))-((np.exp((E/(k*xmin)))*(E/(k*xmin))**(9/2))/((E/(k*xmin))+3.5))*np.exp(-(E/(k*x)))*(E/(k*x))**(-7/2)) for i in range(3): print(i) x = np.arange(50,1051) noise = np.random.normal(0,1,len(x))*3e-10 y = curve(x,0.35,-1e-4,500)+noise #smooth data ysmooth = savgol_filter(y,51,5) #create (sub)plots fig = plt.figure(constrained_layout=True,figsize=(8,4)) gs = fig.add_gridspec(2, 4) ax11 = fig.add_subplot(gs[0:2, 0:2]) ax11.plot(x,y) ax11.plot(x,ysmooth) plt.show()
根因说明
该故障和用户脚本本身的逻辑无关,核心诱因是Windows 10 19041版本重大更新引发的原生线性代数计算库接口不兼容:
- 旧版SciPy在Windows平台运行时,会绑定预编译好的OpenBLAS/MKL线性代数动态库,系统更新过程中替换了系统级浮点计算关联的核心组件,导致LAPACK标准库的
DLASCLS缩放函数在连续第二次调用时,传入的缩放参数被异常改写为非法值(NaN、超出浮点取值范围的极值),最终触发最小二乘求解的SVD迭代流程无法收敛 - 故障触发存在数据量级敏感性:当输入数据的数值跨度超过6个数量级时(本案例中信号值为1e-4量级、叠加噪声为3e-10量级),更容易触发底层参数校验失败,这也是为什么缩减数据量、跳过滤波步骤时脚本可以正常运行的原因
可落地的解决方式
- 优先升级SciPy、NumPy到适配当前Windows版本的最新稳定版本,新版预编译包会专门针对Windows 19041及以上版本的系统组件做接口适配,消除动态库兼容冲突
- 临时规避方案:调用
savgol_filter前对输入序列做统一数量级缩放,比如把原始y值整体放大1e4倍完成滤波后,再将结果缩放回原量级,降低底层LAPACK缩放流程的参数异常触发概率 - 针对批量处理场景,可在脚本启动时显式指定SciPy使用纯Python实现的SVD计算后端,绕开系统原生动态库的兼容问题
内容的提问来源于stack exchange,提问作者Hextia

