def deleteSimilarBeads(mm, maxDist, cc): #merges Beads that are too close cleanmm = [] for i in range(len(mm)): dist=1000 if len(cleanmm) == 0: cleanmm.append(mm[i]) else: for j in range(len(cleanmm)): currdist = (cleanmm[j][0]-mm[i][0])**2+(cleanmm[j][1]-mm[i][1])**2 if currdist < dist: dist = currdist index = j if dist < 10: x = (cleanmm[index][0] + mm[i][0])/2 y = (cleanmm[index][1] + mm[i][1])/2 roi = [x-2*maxDist, x+2*maxDist, y-2*maxDist, y+2*maxDist] candidate = coord2im.cropROI(cc, roi)[:,:4] mm2,stddev,intensitiy = beadVariance(candidate) cleanmm[index][0] = mm2[0] cleanmm[index][1] = mm2[1] else: cleanmm.append(mm[i]) return np.array(cleanmm)
def getCircledBead( self, layer ): # returns the bead (layer=0 for red, layer=1 for green) in the rectangular selected by single mouseclick listItem = self.ui.fileWidget.item(layer) filename = listItem.data(QtCore.Qt.DisplayRole).toString() dims, cc = coords.readfile(filename) rect = CursorGraphicsScene.getCursorPosition(self.scene) maxDist = CursorGraphicsScene.cursorRadius(self.scene) roi = [rect[0] - maxDist, rect[0] + maxDist, rect[1] - maxDist, rect[1] + maxDist] candidate = coords.cropROI(cc, roi)[:, :4] if len(candidate) < self.cutoff * dims[2]: print "Number of points below percentage of occurrency 2" bead = "false" else: mm, stddev, intensitiy = beadLocalisation.beadVariance(candidate) color = QtGui.QColor.fromRgbF(*self.m_colors[layer]) bead = BeadCircle(mm[0], mm[1], color, layer) if len(candidate) > dims[2]: print " WARNING: Number of points below percentage of occurrency because len(candidate) =%d > dims[2] = %d" % ( len(candidate), dims[2], ) # bead='false' return bead
def detectBeads(dims, cc, cutoff,maxDist=2, maxStdDev=1.3): numFrames = cc[-1,2]+1 beads = [] singleConsidered=0 skipped = 0 #counter scatterplotData = [] meanData = [] # sort beads in 'bins' intensities = cc[:,3] #~ allCandidates = cc[intensities > 2*np.mean(intensities)] # only high intensities print(dims[2]) initialCandidates = cc[cc[:,2]<50]#getCandidates(dims,cc, np.min([50, dims[2]])) #initialCandidates = allCandidates[allCandidates[:,2]<100] # from frame 0 to 49 counter=0 for x, y, frame, intensity in initialCandidates[:,0:4]: counter=counter+1 #print counter nearestDist, nearestIdx = nearestNeighbor((x,y,intensity), beads, maxDist) # print 'nearestDist: %1.2f, Bead: (%3.2f, %3.2f)' %(nearestDist, x,y) if nearestDist > 2*maxDist: beads.append([x,y,intensity]) else: #print "merging beads too close together: ", (x,y,intensity), beads[nearestIdx] beads.append([(x+beads[nearestIdx][0])/2., (y+beads[nearestIdx][1])/2.,(intensity+beads[nearestIdx][2])/2.]) beads.remove(beads[nearestIdx]) #print 'smaller', time.time()-start2 for beadCandidate in beads: # over all candidates x,y,intensity = beadCandidate roi = [x-maxDist, x+maxDist, y-maxDist, y+maxDist] candidate = coord2im.cropROI(cc, roi)[:,:4] singleConsidered+=len(candidate) #if len(candidate) > numFrames or len(candidate) < cutoff*numFrames: # allowing few percent not detected if len(candidate) < cutoff*numFrames: # allowing few percent not detected skipped += 1 continue mm,stddev,intensitiy = beadVariance(candidate) if stddev < maxStdDev: print("mean: ", mm, "variance of dist: ", stddev, "@intensity: ", intensity, "#", len(candidate)) scatterplotData.append( (intensity, stddev)) meanData.append(mm) else: print("IGNORED (variance too large): mean: ", mm, "variance of dist: ", stddev, "@intensity: ", intensity, "#", len(candidate)) print(singleConsidered, "single detections considered.") print(skipped, "initial candidates skipped.") meanData = deleteSimilarBeads(meanData, maxDist, cc) return meanData, scatterplotData
def countDetections(cc,x,y,radius): roi = x-radius, x+radius, y-radius, y+radius cc = coords.cropROI(cc, roi) number = 0 intensity = 0. center = np.array([x,y]) cc[:,4] = np.sqrt(np.sum((cc[:,0:2]-center)**2,axis=1)) idxs = cc[:,4]<radius if(idxs!=[]): cc = cc[idxs] return len(cc), np.sum(cc[:,3]) else: # empty set return 0, 0.
def doTransformationButton_clicked(self): self.heatmatrix = self.transformcontroller.calculateTransform([self.m_npimages[0].shape[0]/self.m_factor, self.m_npimages[0].shape[1]/self.m_factor]) self.emit(QtCore.SIGNAL("hideBeads()")) for i in range(1,len(self.m_npimages)): #Transforms the coordinates of the green according to the transformation points = self.m_coords[i] p_transformed = self.transformcontroller.doTransform(points[:,0:2], i) points[:,0:2] = p_transformed points = coords.cropROI(points, (0, self.m_dims[0][0]-1, 0, self.m_dims[0][1]-1)) img = coords.coords2Image(self.m_dims[i], points, self.m_factor) colorimg = np.zeros((img.shape[0],img.shape[1],4),dtype=np.uint8) mx = np.max(img) colorimg[:,:,1] = 1*img*(255./mx) # green self.m_npimages[i] = colorimg self.recalculateResult()
if __name__ == "__main__": path = r'./' files = [ # list of files to compare '20a_MT_Atto520-coords-HCI.txt'] roi = [77,91,72,82] #~ roi = [91,105,14,33 ] # Overview plot (whole image) overviewfile=files[0] dims, coords = cr.readfile(path+overviewfile) plt_overview(dims, coords, roi) for f in files: print "processing file " + f dims, coords = cr.readfile(path+f) coords = cr.cropROI(coords, roi) linewidth, inliers, fitparams = lw.robust_measure(coords) print np.sqrt(linewidth) #Plot data plt_fit(coords, inliers, roi, fitparams) plt.title("Data: " + f) plt.axis(roi) #plot residuals histogram model = lw.AffineLinearModel() resids = model.get_error(inliers, fitparams) plt.figure() plt.title("Residuals: " + f) plt.hist(resids,bins=50,range=[0,2])