使用多处理并行化scipy.optimize.leastsq

时间:2017-08-25 06:57:29

标签: python parallel-processing scipy python-multiprocessing

基于https://stackoverflow.com/a/10552563/8235309,我试图并行执行scipy.optimize.leastsq。 xx,yy,zz是3D云点的坐标,我计算了一个线点距离。

 try{
                // add a row to the RawContacts table
         ContentValues values = new ContentValues();
         values.put(RawContacts.ACCOUNT_TYPE, "com.android.contacts.sim");
         values.put(RawContacts.ACCOUNT_NAME, "SIM");
         Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);

        // get the ID of the newly-added line
 long rawContactId = ContentUris.parseId(rawContactUri);

        // add a "name" line to the Data table, linking it to the new RawContact
        // with the CONTACT_ID column
 values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
 values.put(StructuredName.DISPLAY_NAME, "Name");
 cr.insert(Data.CONTENT_URI, values);
  getContentResolver().notifyChange(Uri_Here,null); 
        // add a "phone" line to the Data table, linking it to the new RawContact
        // with the CONTACT_ID column
 values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
 values.put(Phone.NUMBER, "+12345678901");
 values.put(Phone.TYPE, Phone.TYPE_MOBILE);
 cr.insert(Data.CONTENT_URI, values);
getContentResolver().notifyChange(Uri_Here,null); 
//New Edit
}
catch(Exception e){
    //exception handling
}

---我得到的错误是:

TypeError Traceback(最近一次调用最后一次)  in()      37返回RunDistance      38 ---> 39结果,cov,infodict,mesg,ier = leastsq(fun,p,args =(xx,yy,zz),Dfun = None,full_output = True,col_deriv = 0,ftol = 1.49012e-08,xtol = 1.49012e -08,gtol = 0.0,epsfcn =无,因子= 100,diag =无)

/usr/local/lib/python3.6/site-packages/scipy/optimize/minpack.py in leastsq(func,x0,args,Dfun,full_output,col_deriv,ftol,xtol,gtol,maxfev,epsfcn,因子,诊断)     378米=形状[0]     379如果n> L: - > 380引发TypeError('输入错误:N =%s不得超过M =%s'%(n,m))     381如果epsfcn为None:     382 epsfcn = finfo(dtype).eps

TypeError:输入不正确:N = 4不得超过M = 1

没有多重处理的有趣功能

import multiprocessing
import numpy as np
from scipy.optimize import leastsq

p = [55, 0, 55, 0]

xx=np.array([ 54.696,  54.272,  54.272,  53.424,  53.424,  53.424,  53.848,
53.424,  53.848,  53.424,  53.424,  53.424,  53.848,  53.848,
53.848,  53.424,  53.424,  53.424,  53.424,  53.424,  53.848,
53.848,  53.424,  53.424,  53.848,  53.848,  53.424,  53.848,
53.424])
yy=np.array([ 53.848,  53.424,  53.848,  53.424,  53.848,  53.848,  53.848,
53.848,  53.848,  52.576,  53.424,  53.848,  52.576,  53.424,
53.848,  52.576,  53.424,  53.848,  52.576,  53.848,  52.576,
53.848,  52.576,  53.848,  52.576,  53.848,  52.576,  52.576,
52.576])
zz=np.array([ 4.936 ,  5.4296,  5.4296,  5.9232,  5.9232,  6.4168,  6.4168,
6.9104,  6.9104,  7.404 ,  7.404 ,  7.404 ,  7.404 ,  7.404 ,
7.404 ,  7.8976,  7.8976,  7.8976,  8.3912,  8.3912,  8.3912,
8.3912,  8.8848,  8.8848,  8.8848,  8.8848,  9.3784,  9.3784,
9.872 ])


def fun(p,xx,yy,zz):
    distance=[]    
    v0 = np.array([p[0], p[2], 0]); v1 = np.array([p[1],p[3], 1])
    def funA(v0,v1,xx,yy,zz):        
        for point in range(len(xx)):
            pp = np.array([xx[point], yy[point], zz[point]])
            yield pp,v0,v1
    pool = multiprocessing.Pool()
    distance.append(pool.starmap(funB, funA(v0,v1,xx,yy,zz)))
    return distance

def funB(pp, v0,v1):
        RunDistance=(np.linalg.norm(np.cross(pp-v0,v1))/np.linalg.norm(v1))
        return RunDistance

result,cov,infodict,mesg,ier = leastsq(fun,p, args=(xx,yy,zz), Dfun=None, full_output=True, col_deriv=0, ftol=1.49012e-08, xtol=1.49012e-08, gtol=0.0, epsfcn=None, factor=100, diag=None)

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,问题是该示例创建了一个嵌套列表,所以len(距离)= 1.通过放置修复:

return distance[0]

现在的错误是OSError:[Errno 24]打开的文件太多

也可以从这里更改/Library/LaunchDaemons/limit.maxfiles.plist来解决:https://github.com/ansible/ansible/issues/12259

当前错误:

---> 67         self.pid = os.fork()
     68         if self.pid == 0:
     69             try:

BlockingIOError: [Errno 35] Resource temporarily unavailable