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

使用curve_fit拟合NMR弛豫速率时遭数据类型错误,求解决方案

使用curve_fit分析NMR弛豫速率时遭遇"unhashable type: 'numpy.ndarray'"错误

我正在编写脚本,用curve_fit分析NMR弛豫速率,输入输出数据均来自外部文件。运行curve_fit时,先后遇到过多种数据类型相关错误,比如数据为不可哈希类型、数组被错误转换为序列、数据形状不匹配等。我已经修改脚本,确保数据类型统一为对象字符串、形状均为(36,),但脚本仍无法正常运行,当前收到的错误提示为:"unhashable type: 'numpy.ndarray'"。

当前完整脚本

import math 
import numpy as np
from numpy import *
import scipy
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.cm as cm
import nmrglue as ng
import sys
from mpl_toolkits.mplot3d import Axes3D
from pathlib import Path
import csv
import pandas as pd

def exp(x, a, b, c):
    d=1/b 
    return a+c*np.exp(-b*x) 

out=open((f'/Users/haileyrude/Dropbox/OkunoNMR/1/output.txt'), 'w')

List_area=[]
List_inten=[]

with open(str(f'Okuno Lab Python Work/Okuno Test 1/tau.list'), 'r') as d:
    reader = csv.reader(d)
    prepretauList = list(reader)
    pretauList = np.array(prepretauList)
    tauList = pretauList.astype(str)

dic,data = ng.pipe.read(str(Path(f'/Users/haileyrude/Dropbox/OkunoNMR/1/ft/test036.ft1')))#ZY

udic = ng.pipe.guess_udic(dic,data)
uc = ng.fileiobase.uc_from_udic(udic)
ppm_1H = uc.ppm_scale()
uc0 = ng.pipe.make_uc(dic,data,dim=0)
ub=uc0("6.5ppm")
lb=uc0("9.5ppm")

ppm = ppm_1H[lb:ub]
inten = data[lb:ub]
maxvalue=max(inten.real)
pt=np.where(data.real==maxvalue)
intensity=data[pt]
maxpt=uc0.ppm(pt[0])
    
display(uc0.ppm(pt[0]))
off=0
count=0
for i in range(1, len(tauList)+1, 1):
    
    off=off+500
    count=count+1
    try:
        dic,data = ng.pipe.read(str(Path(f'/Users/haileyrude/Dropbox/OkunoNMR/1/ft/test00{str(i)}.ft1')))#ZY
    except IOError:
        dic,data = ng.pipe.read(str(Path(f'/Users/haileyrude/Dropbox/OkunoNMR/1/ft/test0{str(i)}.ft1')))#ZY

    #print(dic,data)
    #define intergration area for the methyl, include some baseline
    ppm = ppm_1H[lb:ub]
    inten = data[lb:ub]
    intensity=data[pt]
    
    #intergrate
    area=inten.sum()
    List_area.append(float(area.real)) 
    List_inten.append(intensity.real)

    colors = cm.gist_heat(np.linspace(0, 1, len(tauList)+1))
    plt.xlim([6.5, 9.5])
    plt.plot(ppm, inten.real, color=colors[count]) 


prex=array(tauList, dtype="object")
arx=prex.astype(str)
x=prex.flatten()

prey=array(List_inten, dtype="object")
ary=prey.astype(str)
y=ary.flatten()

popt, pcov = curve_fit(exp, x, y)
#get the errors
errors=np.sqrt(pcov.diagonal())
print ('rate', popt[1], '+/-', errors[1] )   
print(out,'rate', popt[1], '+/-', errors[1]) 
    

#print 'Relax rate:', popt[1], '+/-', float(errors[1])


rate_fig = plt.figure(1)
plt.plot(tauList, List_inten, marker='o', markersize=10, linestyle='None')
plt.plot(xaxis, exp(xaxis, *popt), marker='None', linestyle='-', linewidth=2, color='r')
plt.show()

排查用代码及输出

为排查问题,我运行了以下代码:

print(x)
print(len(x))
print(x.shape)
print(x.dtype)

print(y)
print(len(y))
print(y.shape)
print(y.dtype)

得到的输出如下:

x = ['0.001000' '0.001500' '0.002000' '0.002500' '0.003000' '0.004000'
 '0.005000' '0.007500' '0.010000' '0.012500' '0.015000' '0.017500'
 '0.020000' '0.022500' '0.025000' '0.027500' '0.030000' '0.035000'
 '0.050000' '0.075000' '0.100000' '0.125000' '0.150000' '0.175000'
 '0.250000' '0.500000' '0.750000' '1.000000' '1.250000' '1.500000'
 '1.750000' '2.000000' '2.500000' '3.000000' '3.500000' '5.000000']

len(x) = 36

x.shape = (36,)

x dtype = object

y = ['-157341216.0' '-156514192.0' '-156109792.0' '-155292736.0'
 '-155541808.0' '-154359088.0' '-153406112.0' '-152017536.0'
 '-150163360.0' '-148821312.0' '-147121408.0' '-145200544.0'
 '-143513360.0' '-142319280.0' '-140684704.0' '-139887536.0'
 '-138132720.0' '-135468448.0' '-127346128.0' '-114155104.0'
 '-101980152.0' '-90303376.0' '-78995040.0' '-68271560.0' '-38386176.0'
 '38851676.0' '89095032.0' '121053536.0' '142517744.0' '155447904.0'
 '163979280.0' '168583824.0' '174455648.0' '176974160.0' '177651056.0'
 '178662960.0']

len(y) = 36

y.shape = (36,)

y dtype = <U12

我认为问题存在简单的解决方法,但暂时未能发现,恳请各位提供帮助!

内容的提问来源于stack exchange,提问作者HAILEY RUDE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 13:35:55