Python:threading + curve_fit:内部例程的null参数 [英] Python: threading + curve_fit: null argument to internal routine
本文介绍了Python:threading + curve_fit:内部例程的null参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题,使用下面的代码,它应该使用线程做高斯拟合:
来自PIL import Image
import numpy as np
来自scipy.optimize import curve_fit
import threading
class myThread(threading.Thread):
def __init __(self,index):
threading.Thread .__ init __(self)
self.index = index
def run(self):
for i in np .arange(n_Bild.shape [1]):
curve_fit(self.gauss,x_x,Intensitaet [self.index,...],p0 =(Intensitaet [self.index,i],i, 0))
def gauss(self,x,a,b,c,d):
return a * np.exp( - (xb)** 2 /(2 * c ** 2) )+ d
Bild = Image.open(test.bmp)
n_Bild = np.asarray(Bild)
Intensitaet = np.zeros((n_Bild.shape [ 0],n_Bild.shape [1]),dtype = np.uint32)
Intensitaet + = n_Bild [...,...,0]
Intensitaet + = n_Bild [..., ..,1]
Intensitaet + = n_Bild [...,...,2]
x_x = np.arange(n_Bild.shape [1])#Pixel aufx-Achse
threads = []
#创建新线程
thread0 = myThread(0)
thread1 = myThread(1)
#将线程添加到线程列表
threads.append(thread0)
threads.append(thread1)
#开始新线程
thread0.start()
thread1.start()
#等待所有线程在线程中完成
t:
t.join()
printfinished
如果我运行我的程序,我得到一个错误:
SystemError:内部例程的空参数
线程中的异常Thread-2:
回溯(最近调用最后一次):
文件C:\Anaconda\ lib \threading.py,行808,在__bootstrap_inner
self.run()
文件G:/DropBox/Daten/Dropbox/Uni/Bachelorarbeit/Python/ThreadTest.py,第12行,in run
curve_fit(self.gauss,x_x,Intensitaet [self.index,...],p0 =(Intensitaet [self.index,i],i,1,0))
文件func,p0,args = args,full_output =C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py,line 533 in curve_fit
res = 1,** kw)
文件C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py,第378行,至少为ss
gtol,maxfev ,epsfcn,factor,diag)
错误:构造参数列表的内部错误#
我只运行一个线程而不是两个,程序工作正常,但我不知道我做错了什么。
感谢您的帮助。解决方案我相信leastsq()不是线程安全的,一个threading.Lock()围绕你的调用curve_fit()(这可能会打败你的目的)或使用多处理。
I have got some problems using the following code, which is supposed to do gaussian fits using threads:
from PIL import Image import numpy as np from scipy.optimize import curve_fit import threading class myThread (threading.Thread): def __init__(self, index): threading.Thread.__init__(self) self.index = index def run(self): for i in np.arange(n_Bild.shape[1]): curve_fit(self.gauss, x_x, Intensitaet[self.index, ...], p0=(Intensitaet[self.index, i], i, 1, 0)) def gauss(self, x, a, b, c, d): return a * np.exp(-(x-b) ** 2 / (2 * c ** 2)) + d Bild = Image.open("test.bmp") n_Bild = np.asarray(Bild) Intensitaet = np.zeros((n_Bild.shape[0], n_Bild.shape[1]), dtype=np.uint32) Intensitaet += n_Bild[..., ..., 0] Intensitaet += n_Bild[..., ..., 1] Intensitaet += n_Bild[..., ..., 2] x_x = np.arange(n_Bild.shape[1]) #Pixel auf "x"-Achse threads = [] # Create new threads thread0 = myThread(0) thread1 = myThread(1) # Add threads to thread list threads.append(thread0) threads.append(thread1) # Start new Threads thread0.start() thread1.start() # Wait for all threads to complete for t in threads: t.join() print "finished"
If I run my programm I get an error:
SystemError: null argument to internal routine Exception in thread Thread-2: Traceback (most recent call last): File "C:\Anaconda\lib\threading.py", line 808, in __bootstrap_inner self.run() File "G:/DropBox/Daten/Dropbox/Uni/Bachelorarbeit/Python/ThreadTest.py", line 12, in run curve_fit(self.gauss, x_x, Intensitaet[self.index, ...], p0=(Intensitaet[self.index, i], i, 1, 0)) File "C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py", line 533, in curve_fit res = leastsq(func, p0, args=args, full_output=1, **kw) File "C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py", line 378, in leastsq gtol, maxfev, epsfcn, factor, diag) error: Internal error constructing argument list.#
If I only run one thread instead of two, the programm works fine, but I have no idea what i'm doing wrong. Thanks for your help.
解决方案I believe that leastsq() is not threadsafe, and you need to either use a threading.Lock() around your calls to curve_fit() (which might defeat your purpose) or use multiprocessing.
这篇关于Python:threading + curve_fit:内部例程的null参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文