def _gen_bead_pupil(X, Y, beadsize): r2 = X*X + Y*Y dx = X[1, 0] - X[0, 0] bead_proj = np.sqrt(np.maximum(beadsize*beadsize - (r2 - dx*dx), 0)) return abs(fftshift(fftn(bead_proj)))
def OnColoc(self, event): from PYME.Analysis import binAvg voxelsize = self.image.voxelsize #assume we have exactly 2 channels #FIXME - add a selector #grab image data imA = self.image.data[:,:,:,0].squeeze() imB = self.image.data[:,:,:,1].squeeze() X, Y = np.mgrid[0:float(imA.shape[0]), 0:float(imA.shape[1])] X = X/X.shape[0] Y = Y/X.shape[1] X = X - .5 Y = Y - .5 R = np.sqrt(X**2 + Y**2) H1 = pylab.fftn(imA) H2 = pylab.fftn(imB) rB = np.linspace(0,R.max()) bn, bm, bs = binAvg.binAvg(R, pylab.fftshift(abs(H1*H2)), rB) bn1, bm1, bs1 = binAvg.binAvg(R, pylab.fftshift(abs(H1*H1)), rB) bn2, bm2, bs2 = binAvg.binAvg(R, pylab.fftshift(abs(H2*H2)), rB) pylab.figure() ax = pylab.gca() ax.plot(rB[:-1], bm/np.sqrt(bm1*bm2)) ax.plot(rB[:-1], 2./np.sqrt(bn/2)) xt = np.array([10., 15, 20, 30, 50, 80, 100, 150]) rt = voxelsize[0]/xt pylab.xticks(rt[::-1],['%d' % xi for xi in xt[::-1]]) pylab.show()
def setIllumPattern(pattern, z0): global illPattern, illZOffset, illPCache sx, sy = pattern.shape psx, psy, sz = interpModel.shape il = np.zeros([sx,sy,sz], 'f') il[:,:,sz/2] = pattern ps = np.zeros_like(il) if sx > psx: ps[(sx/2-psx/2):(sx/2+psx/2), (sy/2-psy/2):(sy/2+psy/2), :] = interpModel else: ps[:,:,:] = interpModel[(psx/2-sx/2):(psx/2+sx/2), (psy/2-sy/2):(psy/2+sy/2), :] ps= ps/ps[:,:,sz/2].sum() illPattern = abs(ifftshift(ifftn(fftn(il)*fftn(ps)))).astype('f') illPCache = None
def OnColoc(self, event): from PYME.Analysis import binAvg voxelsize = self.image.voxelsize #assume we have exactly 2 channels #FIXME - add a selector #grab image data imA = self.image.data[:, :, :, 0].squeeze() imB = self.image.data[:, :, :, 1].squeeze() X, Y = np.mgrid[0:float(imA.shape[0]), 0:float(imA.shape[1])] X = X / X.shape[0] Y = Y / X.shape[1] X = X - .5 Y = Y - .5 R = np.sqrt(X**2 + Y**2) H1 = pylab.fftn(imA) H2 = pylab.fftn(imB) rB = np.linspace(0, R.max()) bn, bm, bs = binAvg.binAvg(R, pylab.fftshift(abs(H1 * H2)), rB) bn1, bm1, bs1 = binAvg.binAvg(R, pylab.fftshift(abs(H1 * H1)), rB) bn2, bm2, bs2 = binAvg.binAvg(R, pylab.fftshift(abs(H2 * H2)), rB) pylab.figure() ax = pylab.gca() ax.plot(rB[:-1], bm / np.sqrt(bm1 * bm2)) ax.plot(rB[:-1], 2. / np.sqrt(bn / 2)) xt = np.array([10., 15, 20, 30, 50, 80, 100, 150]) rt = voxelsize[0] / xt pylab.xticks(rt[::-1], ['%d' % xi for xi in xt[::-1]]) pylab.show()
def OnCheckSpacing(self, event): voxx = 0.07 voxy = 0.07 im = self.scope.frameWrangler.currentFrame F = (abs(pylab.fftshift(pylab.fftn(im - im.mean()))) + 1e-2).squeeze() currVoxelSizeID = self.scope.settingsDB.execute( "SELECT sizeID FROM VoxelSizeHistory ORDER BY time DESC").fetchone( ) if not currVoxelSizeID is None: voxx, voxy = self.scope.settingsDB.execute( "SELECT x,y FROM VoxelSizes WHERE ID=?", currVoxelSizeID).fetchone() pylab.figure(2) pylab.clf() xd = F.shape[0] / 2. yd = F.shape[1] / 2. cd = xd * 2 * voxx / 5 #kill central spike F[(xd - cd):(xd + cd), (yd - cd):(yd + cd)] = F.mean() pylab.imshow(F.T, interpolation='nearest', cmap=cm.hot) xd = F.shape[0] / 2. yd = F.shape[1] / 2. pylab.plot( xd + (xd * 2 * voxx / 1.8) * np.cos(np.arange(0, 2.1 * np.pi, .1)), yd + (xd * 2 * voxx / 1.8) * np.sin(np.arange(0, 2.1 * np.pi, .1)), lw=2, label='$1.8 {\mu}m$') pylab.plot( xd + (xd * 2 * voxx / 2.) * np.cos(np.arange(0, 2.1 * np.pi, .1)), yd + (xd * 2 * voxx / 2.) * np.sin(np.arange(0, 2.1 * np.pi, .1)), lw=1, label='$2 {\mu}m$') pylab.plot( xd + (xd * 2 * voxx / 1.6) * np.cos(np.arange(0, 2.1 * np.pi, .1)), yd + (xd * 2 * voxx / 1.6) * np.sin(np.arange(0, 2.1 * np.pi, .1)), lw=1, label='$1.6 {\mu}m$') pylab.xlim(xd - xd * 2 * voxx / 1, xd + xd * 2 * voxx / 1) pylab.ylim(yd - xd * 2 * voxx / 1, yd + xd * 2 * voxx / 1) pylab.legend() self.F = F
def Fraunhofer(i, z) : print "Propagation:Fraunhofer" ft = pl.fftshift(pl.fftn(pl.fftshift(i.i))) dx = i.wl*z/(i.nx*i.dx) dy = i.wl*z/(i.ny*i.dy) po = pl.exp(1j*2*pl.pi/i.wl*i.dx*i.dx)/(1j*i.wl*z) p = pl.arange(0,i.nx)-(i.nx+0.5)/2.0 q = pl.arange(0,i.ny)-(i.ny+0.5)/2.0 [pp,qq] = pl.meshgrid(p,q) pm = pl.exp(1j*pl.pi/(i.wl*z)*((pp*dx)**2+(qq*dy)**2)) i2 = Intensity.Intensity2D(i.nx,-i.nx*dx/2,i.nx*dy/2,i.ny,-i.ny*dy/2,i.ny*dy/2) i2.i = po*pm*ft return i2 print "Propagation:Fraunhofer>",dx,dy,i.nx*dx,i.ny*dy
def Fraunhofer(i, z): print "Propagation:Fraunhofer" ft = pl.fftshift(pl.fftn(pl.fftshift(i.i))) dx = i.wl * z / (i.nx * i.dx) dy = i.wl * z / (i.ny * i.dy) po = pl.exp(1j * 2 * pl.pi / i.wl * i.dx * i.dx) / (1j * i.wl * z) p = pl.arange(0, i.nx) - (i.nx + 0.5) / 2.0 q = pl.arange(0, i.ny) - (i.ny + 0.5) / 2.0 [pp, qq] = pl.meshgrid(p, q) pm = pl.exp(1j * pl.pi / (i.wl * z) * ((pp * dx)**2 + (qq * dy)**2)) i2 = Intensity.Intensity2D(i.nx, -i.nx * dx / 2, i.nx * dy / 2, i.ny, -i.ny * dy / 2, i.ny * dy / 2) i2.i = po * pm * ft return i2 print "Propagation:Fraunhofer>", dx, dy, i.nx * dx, i.ny * dy
def correlateFrames(A, B): A = A.squeeze() / A.mean() - 1 B = B.squeeze() / B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A) * ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() x0 = (X * Cp).sum() / Cpsum y0 = (Y * Cp).sum() / Cpsum return x0 - A.shape[0] / 2, y0 - A.shape[1] / 2, Cm, Cpsum
def correlateFrames(A, B): A = A.squeeze()/A.mean() - 1 B = B.squeeze()/B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A)*ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() x0 = (X*Cp).sum()/Cpsum y0 = (Y*Cp).sum()/Cpsum return x0 - A.shape[0]/2, y0 - A.shape[1]/2, Cm, Cpsum
def calcCorrShift(im1, im2): im1 = im1 - im1.mean() im2 = im2 - im2.mean() xc = np.abs(ifftshift(ifftn(fftn(im1) * ifftn(im2)))) xct = (xc - xc.max() / 1.1) * (xc > xc.max() / 1.1) print((xct.shape)) #figure(1) #imshow(xct) #dx, dy = ndimage.measurements.center_of_mass(xct) #print np.where(xct==xct.max()) dx, dy = np.where(xct == xct.max()) return dx[0] - im1.shape[0] / 2, dy[0] - im1.shape[1] / 2
def compare(self): d = 1.0 * self.scope.pa.dsa.squeeze() dm = d / d.mean() - 1 #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm) * self.FA))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() dx = (self.X * Cp).sum() / Cpsum dy = (self.Y * Cp).sum() / Cpsum ds = ndimage.shift(dm, [-dx, -dy]) * self.mask #print A.shape, As.shape ds_A = (ds - self.refA) return dx, dy, self.deltaZ * np.dot(ds_A.ravel(), self.dz) * self.dzn
def compare(self): d = 1.0*self.scope.pa.dsa.squeeze() dm = d/d.mean() - 1 #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm)*self.FA))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() dx = (self.X*Cp).sum()/Cpsum dy = (self.Y*Cp).sum()/Cpsum ds = ndimage.shift(dm, [-dx, -dy])*self.mask #print A.shape, As.shape ds_A = (ds - self.refA) return dx, dy, self.deltaZ*np.dot(ds_A.ravel(), self.dz)*self.dzn
def correlateAndCompareFrames(A, B): A = A.squeeze() / A.mean() - 1 B = B.squeeze() / B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A) * ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() x0 = (X * Cp).sum() / Cpsum y0 = (Y * Cp).sum() / Cpsum dx, dy = x0 - A.shape[0] / 2, y0 - A.shape[1] / 2 As = ndimage.shift(A, [-dx, -dy]) #print A.shape, As.shape return (As - B).mean(), dx, dy
def correlateAndCompareFrames(A, B): A = A.squeeze()/A.mean() - 1 B = B.squeeze()/B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A)*ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() x0 = (X*Cp).sum()/Cpsum y0 = (Y*Cp).sum()/Cpsum dx, dy = x0 - A.shape[0]/2, y0 - A.shape[1]/2 As = ndimage.shift(A, [-dx, -dy]) #print A.shape, As.shape return (As -B).mean(), dx, dy
def compare(self): d = 1.0 * self.scope.frameWrangler.currentFrame.squeeze() dm = d / d.mean() - 1 #where is the piezo suppposed to be #nomPos = self.piezo.GetPos(0) nomPos = self.piezo.GetTargetPos(0) #find closest calibration position posInd = np.argmin(np.abs(nomPos - self.calPositions)) #dz = float('inf') #count = 0 #while np.abs(dz) > 0.5*self.deltaZ and count < 1: # count += 1 #retrieve calibration information at this location calPos = self.calPositions[posInd] FA = self.calFTs[:, :, posInd] refA = self.calImages[:, :, posInd] ddz = self.dz[:, posInd] dzn = self.dzn[posInd] #what is the offset between our target position and the calibration position posDelta = nomPos - calPos print('%s' % [nomPos, posInd, calPos, posDelta]) #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm) * FA))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() dx = (self.X * Cp).sum() / Cpsum dy = (self.Y * Cp).sum() / Cpsum ds = ndimage.shift(dm, [-dx, -dy]) * self.mask #print A.shape, As.shape self.ds_A = (ds - refA) #calculate z offset between actual position and calibration position dz = self.deltaZ * np.dot(self.ds_A.ravel(), ddz) * dzn #posInd += np.round(dz / self.deltaZ) #posInd = int(np.clip(posInd, 0, self.NCalibStates)) # print count, dz #add the offset back to determine how far we are from the target position dz = dz - posDelta # if 1000*np.abs((dz + posDelta))>200 and self.WantRecord: #dz = np.median(self.buffer) # tif.imsave('C:\\Users\\Lab-test\\Desktop\\peakimage.tif', d) # np.savetxt('C:\\Users\\Lab-test\\Desktop\\parameter.txt', self.buffer[-1]) #np.savetxt('C:\\Users\\Lab-test\\Desktop\\posDelta.txt', posDelta) # self.WantRecord = False #return dx, dy, dz + posDelta, Cm, dz, nomPos, posInd, calPos, posDelta return dx, dy, dz, Cm, dz, nomPos, posInd, calPos, posDelta