def lnupq(v0,v1,nt,ni,randomi,moreps): umax = nt-1 p = zerofloat(nt) q = zerofloat(nt) u = zerofloat(nt) si = SincInterpolator.fromErrorAndFrequency(0.01,0.40) print "max length = "+str(si.getMaximumLength()) if (v0==v1): print "v0==v1" minDepth = 0.0 dz = 5.0 for n in range(nt): u[n] = ((v0*n/2.0)-minDepth)/dz dq = (umax-50)/float(ni+1) ts = rampfloat(dq+31,dq,ni) ran = Random(55) #rj=1.0 for ji in range(ni): rj = 1.0 if randomi: rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji]#time u tp = 2.0*(dz*tq+minDepth)/v0#time t(u) #print "tq = "+str(tq) #print "tp = "+str(tp) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int(2.0*(dz*ts[ni-1]+minDepth)/v0+0.5) return u,p,q,itmin,itmax
def randomBoolean(chance, case=None): if case==None: r = Random() else: r = Random(case) b = r.nextFloat()<chance return b
def sqrtupq(r0,r1,v,nt,ni): umax = nt-1 p = zerofloat(nt) q = zerofloat(nt) u = zerofloat(nt) si = SincInterpolator.fromErrorAndFrequency(0.01,0.45) if (r0==r1): for n in range(nt): u[n] = r0*n+v dq = umax/float(ni+1) ts = rampfloat(dq,dq,ni) ran = Random(55) for ji in range(ni): rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji]#time u tp = (tq-v)/r0#time t(u) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int((tq-v)/r0+0.5) return u,p,q,itmin,itmax else: a = 2.0*(pow(r1,3.0)-pow(r0,3.0))/(3.0*(umax-v)) b = r0*r0 for n in range(nt): u[n] = (2.0/(3.0*a))*(pow((a*n+b),(3.0/2.0))-pow(b,(3.0/2.0)))+v dq = umax/float(ni+1) ts = rampfloat(dq,dq,ni) si = SincInterpolator.fromErrorAndFrequency(0.01,0.45) ran = Random(55) for ji in range(ni): rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji]#time u tp = (1.0/a)*(pow(3.0*a*(tq-v)/2.0+pow(b,3.0/2.0),(2.0/3.0))-b)#time t(u) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int(((1.0/a)*(pow(3.0*a*(umax-v)/2.0+pow(b,3.0/2.0),(2.0/3.0))-b))+0.5) return u,p,q,itmin,itmax
def cosupq(r0,r1,c,nt,ni): umax = nt-1 p = zerofloat(nt) q = zerofloat(nt) u = zerofloat(nt) si = SincInterpolator.fromErrorAndFrequency(0.01,0.45) if (r0==r1): for n in range(nt): u[n] = r0*n+c dq = umax/float(ni+1)#float cast ensures float division. ts = rampfloat(dq,dq,ni) ran = Random(55) for ji in range(ni): rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji] #time u tp = (tq-c)/r0#time t(u) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int((umax-c)/r0+0.5) return u,p,q,itmin,itmax else: b = r0 a = b*sqrt(1-(r1/r0)*(r1/r0))/(umax-c) for n in range(nt): u[n] = b*sin(a*n)/a+c dq = umax/float(ni+1)#float cast ensures float division. ts = rampfloat(dq,dq,ni) ran = Random(55) for ji in range(ni): rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji] #time u tp = asin(a*(tq-c)/b)/a si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int((asin(a*(umax-c)/b)/a)+0.5) return u,p,q,itmin,itmax
def lnupq(r0,r1,v,nt,ni,randomi,moreps): umax = nt-1 p = zerofloat(nt) q = zerofloat(nt) u = zerofloat(nt) si = SincInterpolator.fromErrorAndFrequency(0.01,0.40) print "max length = "+str(si.getMaximumLength()) if (r0==r1): print "r0==r1" for n in range(nt): u[n] = r0*n+v dq = (umax-v)/float(ni+1) ts = rampfloat(dq+v+20,dq,ni) ran = Random(55) #rj=1.0 for ji in range(ni): rj = 1.0 if randomi: rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji]#time u tp = (tq-v)/r0#time t(u) #print "tq = "+str(tq) #print "tp = "+str(tp) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) #for impulses seen in p, but not in q if moreps: tsni = ts[ni-1] ulastp = r0*nt+v#the u that corresponds to the last impulse in p nexi = int((ulastp-tsni)/dq)#extra i to fill in the rest of p exts = rampfloat(tsni+dq,dq,nexi) for ji in range(nexi): rj = 1.0 if randomi: rj = 2.0*ran.nextFloat()-1.0 tq = exts[ji]#time u tp = (tq-v)/r0#time t(u) #print "tq = "+str(tq) #print "tp = "+str(tp) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int((umax-v)/r0+0.5) return u,p,q,itmin,itmax else: print "r0>r1" a = (umax-v)/log(r0/r1) b = r0*log(r0/r1)/(umax-v) for n in range(nt): u[n] = a*log(1.0+b*n)+v dq = (umax)/float(ni+1) ts = rampfloat(dq+v+20,dq,ni) ran = Random(55) #rj = 1.0 for ji in range(ni): rj = 1.0 if randomi: rj = 2.0*ran.nextFloat()-1.0 tq = ts[ji]#time u tp = (exp((tq-v)/a)-1.0)/b#time t(u) #print "tq = "+str(tq) #print "tp = "+str(tp) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) #for impulses seen in p, but not in q if moreps: tsni = ts[ni-1] ulastp = a*log(b*nt+1)+v#the u that corresponds to the last impulse in p nexi = int((ulastp-tsni)/dq)#extra i to fill in the rest of p exts = rampfloat(tsni+dq,dq,nexi) for ji in range(nexi): rj = 1.0 if randomi: rj = 2.0*ran.nextFloat()-1.0 tq = exts[ji]#time u tp = (exp((tq-v)/a)-1.0)/b#time t(u) #print "tq = "+str(tq) #print "tp = "+str(tp) si.accumulate(tp,rj,nt,1.0,0.0,p) si.accumulate(tq,rj,nt,1.0,0.0,q) itmin = 0 itmax = int(((exp((umax-v)/a)-1.0)/b)+0.5) return u,p,q,itmin,itmax
fp.setLineWidth(3) for roi in rois: fp.draw(roi) fp.setColor(90) # oblique membrane fp.setLineWidth(5) roi_oblique = OvalRoi(w / 2 + w / 8, h / 2 + h / 8, w / 4, h / 4) fp.draw(roi_oblique) # Add noise # 1. Vesicles fp.setLineWidth(1) random = Random(67779) for i in xrange(150): x = random.nextFloat() * (w - 1) y = random.nextFloat() * (h - 1) fp.draw(OvalRoi(x, y, 4, 4)) fp.setRoi(None) # 2. blur sigma = 1.0 GaussianBlur().blurFloat(fp, sigma, sigma, 0.02) # 3. shot noise fp.noise(25.0) fp.setMinAndMax(0, 255) imp.show() # Try training for slightly off-center pixels from the drawed lines
def randomInt(n, case=None): if case == None: r = Random() else: r = Random(case) i = r.nextInt(n) return i def randomBoolean(chance, case=None): if case == None: r = Random() else: r = Random(case) b = r.nextFloat() < chance return b def randomFirstName(case=None): if case == None: r = Random() else: r = Random(case) firstNames = [ "Alan", "Betty", "Carlos", "Denise", "Edward", "Felicity", "Graham", "Harriet", "Ivan", "Judith", "Keith", "Lucy", "Michael", "Nicola", "Owen", "Patsy", "Quentin", "Rosemary", "Stephen", "Tracey", "Ulrich", "Viola", "William" ] return firstNames[r.nextInt(len(firstNames))]