You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python报错:'numpy.ndarray' object is not callable 排查求助

问题分析与解决

问题描述

代码此前运行正常,今日首次全量运行无问题,但未做任何修改二次运行时,所有调用Phase_Extractor函数的单元格均触发TypeError: 'numpy.ndarray' object is not callable错误,怀疑与[]和()误用有关但无法定位。

代码片段

def Phase_Extractor(fpath):

    n = 1 #air refractive index
    wvl = 632E-9

    displacement = []
    phase = []

    tdms_file1 = TdmsFile.read(fpath)
    for group in tdms_file1.groups():
        groupname = group.name
        for channel in group.channels():
            channel_name = channel.name
            # Access dictionary of properties:
            properties = channel.properties
            # Access numpy array of data for channel:
            velocity_volts1 = channel[:]
            # Access a subset of data
            data_subset = channel[0:100]
            
    velocity = velocity_volts1/1000 ########## Convert from volts to m/s ################################

    time = np.arange(0,5,0.000005) 

    displacement = scipy.integrate.cumtrapz(velocity, time) ################ convert to displacement from velocity #########

    for i in range(0, len(displacement)):
        phase.append(((2*np.pi*n*displacement[i]))/wvl) ############# Convert to phase ################################
        
    phase = np.append(phase, phase[0-1]) ################### make phase list same size as time step list ###################  
        

    return phase

报错栈

TypeError                                 Traceback (most recent call last)
Input In [38], in <cell line: 1>()
----> 1 MDP500Full = MinPhaseFinder(r"\Users\seanl\Documents\Master Thesis\500HzFullvol Converted.tdms", 500)
  2 MDP500Half = MinPhaseFinder(r"\Users\seanl\Documents\Master Thesis\500HzHalfvol Converted.tdms", 500)
  3 MDP500Quarter = MinPhaseFinder(r"\Users\seanl\Documents\Master Thesis\500HzQuartervol Converted.tdms", 500)

Input In [26], in MinPhaseFinder(fpath, sf)
      1 def MinPhaseFinder(fpath, sf):
----> 3     filtered_phase = butter_highpass_filter(Phase_Extractor(fpath), 7 , 20000)
      5     X = fft(filtered_phase)
      6     sr = 200E3

Input In [11], in Phase_Extractor(fpath)
     26 time = np.arange(0,5,0.000005) 
     28 displacement = scipy.integrate.cumtrapz(velocity, time) 
---> 30 for i in range(0, len(displacement)):
     31     phase.append(((2*np.pi*n*displacement[i]))/wvl) 
     33 phase = np.append(phase, phase[0-1]) 

TypeError: 'numpy.ndarray' object is not callable

原因与解决方法

核心原因

报错出现在len(displacement)这一步,说明内置函数len被意外覆盖成了numpy数组(比如之前运行过len = np.array(...)这类代码)。Jupyter Notebook的全局变量会跨单元格保留,首次全量运行时未污染len,二次运行时因之前的全局变量残留,导致调用len()时把数组当成函数执行,触发错误。

解决步骤

  1. 重启Jupyter内核:清除所有被污染的全局变量,重新运行全部代码即可恢复正常。
  2. 检查变量命名:排查所有代码单元格,确保没有使用len、range、append等内置函数名作为变量名,避免再次污染。

代码优化建议

  • 将函数内的常量(n、wvl)放在函数内部,避免全局变量意外修改。
  • 遍历TDMS文件的channel时,当前代码会覆盖velocity_volts1,仅保留最后一个channel的数据,若需处理多channel需调整逻辑。
  • 用向量化操作替代for循环提升效率:
    # 替换原for循环
    phase = (2 * np.pi * n * displacement) / wvl
    
  • 简化末尾的数组追加操作:
    phase = np.append(phase, phase[-1])  # 替代phase[0-1],更易读
    

内容的提问来源于stack exchange,提问作者Sean Lane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 13:40:34