Python:threading + curve_fit:内部例程的null参数

时间:2014-03-24 14:27:03

标签: python multithreading class curve-fitting python-multithreading

我使用下面的代码遇到了一些问题,它应该使用线程进行高斯拟合:



    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"

如果我运行我的程序,我会收到错误:


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.#

如果我只运行一个线程而不是两个,程序运行正常,但我不知道我做错了什么。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我相信leastsq()不是线程安全的,你需要在对curve_fit()的调用中使用threading.Lock()(这可能会破坏你的目的)或使用多处理。