سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top