def test_coverage(self):
     d = {}
     l = range(100)
     def f(x):
         d[x]=x**2
     handythread.foreach(f, l)
     for i in l:
         self.assertEqual(d[i],i**2)
 def __init__(self):
     rhicBpmTbt.__init__(self)
     if int(options.core)>1:
         Ax,Ay=self.svdBpmMat()
         self.removeBadBpms('x',Ax)
         self.removeBadBpms('y',Ay)
         foreach(self.svdClean,['x','y'],\
                 threads=2,return_=True)
     else:
         A = self.svdBpmMatrix('x')
         self.removeBadBpms('x',A)
         A = self.svdBpmMatrix('y')
         self.removeBadBpms('y',A)
         self.svdClean('x')
         self.svdClean('y')
     self.writeFile()
示例#3
0
def main():
    jobs = multiprocessing.cpu_count()-1
    a = range(10000)
    for method in ['normal','multi Pool','joblib delayed','handythread']:
        startTime = datetime.now()
        sprint=True
        if method=='normal':
            res = []
            for i in a:
                b = getsqrt(i)
                res.append(b)
        elif method=='multi Pool':
            pool = Pool(processes=jobs)
            res = pool.map(getsqrt, a)
        elif method=='joblib delayed':
            res = Parallel(n_jobs=jobs)(delayed(getsqrt)(i) for i in a)
        elif method=='handythread':
            res = foreach(getsqrt,a,threads=jobs,return_=True)
        else:
            sprint=False
        if sprint:
            print "Method was %s"%method
            print "Done :%s"%(datetime.now()-startTime)
            print res[-10:], type(res[-1])
    return(res)
示例#4
0
def svdClean():
    #-- read file
    t0=time.time(); a,tx,ty=red(opt.FILE)
    #print "File read in",round(time.time()-t0,1),'s'    
    ntx,nbx=npy.shape(tx);nty,nby=npy.shape(ty)
    print '[H, V] bpms: [',nbx, nby,']'
    print '[H, V] turns: [',ntx, nty,']'

    #--- peak-2-peak cut, for LHC convert to microns
    print "Peak to peak cut:",opt.PK2PK, "mm"        
    pkx=npy.nonzero(npy.ptp(tx,axis=0)>float(opt.PK2PK))[0]
    pky=npy.nonzero(npy.ptp(ty,axis=0)>float(opt.PK2PK))[0]
    tx=npy.take(tx,pkx,1);ty=npy.take(ty,pky,1)
    print '[H,V] BPMs after P2P cut:',len(pkx),len(pky)
    
    #--- svd cut
    #t0=time.time()
    #gdx,gdy=foreach(rBPM,[tx,ty],threads=2,return_=True)
    gdx=rBPM(tx);gdy=rBPM(ty); #-- gdx->rdx for corr index 
    rdx=[pkx[j] for j in gdx]; rdy=[pky[j] for j in gdy]
    tx=npy.take(tx,(gdx),1);ty=npy.take(ty,(gdy),1)
    #print "Applied SVD cut in",round(time.time()-t0,1),'s'

    #--- svd clean
    if int(opt.SVALS)<nbx and int(opt.SVALS)<nby:
        t0=time.time();
        tx,ty=foreach(clean,[tx,ty],threads=2,return_=True)
        print "Cleaned using SVD in",round(time.time()-t0,1),'s'
    else: print "All singulars values retained, no svd clean applied"

    #--- bad bpms to file
    f=open(opt.FILE+'.bad','w')
    print >> f, "@  FILE %s ",opt.FILE
    print >> f, "*  NAME    S    PLANE"
    print >> f, "$   %s     %le   %s "
    for j in range(len(a.H)):
        if j not in rdx:
            print >> f, a.H[j].name, a.H[j].location, "H"
    for j in range(len(a.V)):
        if j not in rdy:
            print >> f, a.V[j].name, a.V[j].location, "V"
    f.close()
    
    #--- good data to file #t0=time.time()
    f = open(opt.FILE+'.new','w')
    f.write('# '+opt.FILE+'\n')
    for j in range(len(gdx)):
        f.write('0 '+a.H[rdx[j]].name+' '+str(a.H[rdx[j]].location)+' ')
        #f.write('%s\n' % ' '.join(['%5.5f' % val for val in \
        #                           a.H[rdx[j]].data]))
        f.write('%s\n' % ' '.join(['%5.5f' % val for val in tx[:,j]]))
    for j in range(len(gdy)):
        f.write('1 '+a.V[rdy[j]].name+' '+str(a.V[rdy[j]].location)+' ')
        #f.write('%s\n' % ' '.join(['%5.5f' % val for val in \
        #                           a.V[rdy[j]].data]))
        f.write('%s\n' % ' '.join(['%5.5f' % val for val in ty[:,j]]))
    f.close();#print "File written in",round(time.time()-t0,1),'s'
    print "Total",round(time.time()-t0,1),'s'
 def svdBpmMat(self):
     global nturns, Ax, Ay
     bx = self.peak_peak('x')
     by = self.peak_peak('y')
     nbx = shape(bx)[1]; nby = shape(by)[1]
     print 'performing SVD'
     #----svd for matrix with bpms >10
     if nbx > 10 and nby>10:
         Ax, Ay = foreach(self.computeSVD,[bx,by],\
                          threads=2,return_=True)
     else: sys.exit('Exit, # of bpms < 10')
     return Ax, Ay
import numpy as np
import math
def f(x):
    print x
    y = [1]*10000000
    [math.exp(i) for i in y]
def g(x):
    print x
    y = np.ones(10000000)
    np.exp(y)

from handythread import foreach
from processing import Pool
from timings import f,g
def fornorm(f,l):
    for i in l:
        f(i)
%timeit fornorm(f,range(10))
%timeit foreach(g,range(100),threads=2)
%timeit foreach(f,range(10),threads=2)
p = Pool(2)
%timeit p.map(g,range(100))
%timeit p.map(f,range(100))
示例#7
0
    #-- Input options & read data
    opt,args=parsse()
    file, NTURNS=rinput(opt.PATH)
    print "Input File:", file
    BLABEL=0;SLABEL=0;AMP01=0;PHASE01=0
    aD=rhicdata(file);bx,by,sx,sy=bMat(aD)

    #-- Tunes, CO, PTOP & Noise & write modes
    TUNEX=findTunes(bx,qx0,tol=0.1);
    TUNEY=findTunes(by,qy0,tol=0.1);
    Q1=average(TUNEX);Q2=average(TUNEY)
    print "Av. Measured {Qx, Qy}:", round(Q1,4), round(Q2,4)
    Q1RMS=sqrt(average(TUNEX)**2); Q2RMS=sqrt(average(TUNEY)**2)
    COX, PK2PKX=ptp(bx); COY, PK2PKY=ptp(by)
    #Ux,Sx,Vx,NOISEX=computeSVD(bx);Uy,Sy,Vy,NOISEY=computeSVD(by);
    [Ux,Sx,Vx,NOISEX],[Uy,Sy,Vy,NOISEY]=foreach(computeSVD,[bx,by],threads=2,return_=True)
    Fx=wrtUSV(file,Ux,Sx,Vx,sx,pl='x');
    Fy=wrtUSV(file,Uy,Sy,Vy,sy,pl='y');

    #--- find modes & compute phadv(x,y)
    if opt.MODE=="0": #--- ph/amp from uncoupled case
        x1,x2=findmodes(Fx,qx0); y1,y2=findmodes(Fy,qy0)
        PHX,AMPX=phBeta(Sx[x1]*Vx[:,x1],Sx[x2]*Vx[:,y2])
        PHY,AMPY=phBeta(Sy[y1]*Vy[:,y1],Sy[y2]*Vy[:,y2])
        C11=zeros(len(COX)+len(COY));C12=C11;C21=C11;C22=C11
    else:
        print "Coupled SVD not available yet"
        print "Only SVD modes written out, exiting.."
        sys.exit()

    #--- check phase for mode inversion
 def test_return_1(self):
     l = range(100)
     r = handythread.foreach(lambda x: x**2, l, return_=True, threads=1)
     for i in range(len(l)):
         self.assertEqual(l[i]**2,r[i])