def fit_gauss(lroi, imp, p, peak_id, id_, type_, rm): lroi.setName("{}_{}_{}".format(str(id_), peak_id, type_)) imp.setRoi(lroi) rm.addRoi(lroi) prof = ProfilePlot(imp) y = prof.getProfile() x = xrange(len(y)) fitter = CurveFitter(x, y) fitter.doFit(CurveFitter.GAUSSIAN) param_values = fitter.getParams() std = param_values[3] fwhm = 2.3548 * std r2 = fitter.getFitGoodness() y_ = [fitter.f(x_) for x_ in x] area_profile = sum(y) - len(y) *min(y) area_gauss = sum(y_) - len(y_)*min(y_) output = {} output["x_pos"] = p.x output["y_pos"] = p.y output["fwhm"] = fwhm output["fwhm_nm"] = pixel_size_nm * fwhm output["r2_GoF"] = r2 output["id"] = id_ output["peak_id"] = peak_id output["type"] = type_ # yai, excel maagic :-) output["avg_fwhm"] = '=AVERAGEIFS(F:F,B:B,B{},F:F,"<>"&"")'.format(id_+2) output["area_profile"] = area_profile output["area_gauss"] = area_gauss if peak_id == DEBUG: plot = Plot("ROI peak {} type {}".format(peak_id, type_), "X (gray)", "Y (fit window)") plot.setLineWidth(2) plot.setColor(Color.RED) plot.addPoints(x, y, Plot.LINE) plot.setColor(Color.BLUE) plot.addPoints(x, y_, Plot.LINE) plot.show() return output
cf = CurveFitter(exposures, list(levels)) cf.doFit(CurveFitter.STRAIGHT_LINE) fitParams = cf.getParams() slope = fitParams[1] intercept = fitParams[0] rSqr = cf.getRSquared() print("slope=", slope, " ; intercept=", intercept, " ; rSquared=", rSqr) # Plot the data and the regression line newPlotFlags = Plot.TRIANGLE + Plot.X_GRID + Plot.X_NUMBERS + Plot.Y_GRID + Plot.Y_NUMBERS newPlot = Plot("DARK NOISE", "EXPOSURE, ms", "MEAN GRAY LEVEL", newPlotFlags) newPlot.setLineWidth(2) newPlot.setColor("red") newPlot.add("triangle", exposures, list(levels)) newPlot.setLineWidth(1) newPlot.setColor("black") newPlot.drawLine(exposures[0], cf.f(exposures[0]), exposures[-1], cf.f(exposures[-1])) newPlot.setColor("blue") newPlot.setFontSize(20) newPlot.addText("y = a+bx", 100.0, 13000.0) newPlot.addText("a = " + str(round(intercept, 2)), 100.0, 12250.0) newPlot.addText("b = " + str(round(slope, 2)), 100.0, 11500.0) newPlot.addText("R squared = " + str(round(rSqr, 3)), 100.0, 10750.0) newPlot.show() # Place the plot data into a ResultsTable rt = newPlot.getResultsTable() rt.show("Dark Noise Results")
# http://hyperphysics.phy-astr.gsu.edu/hbase/math/gaufcn2.html import ij.measure.CurveFitter imp = IJ.getImage() prof = ProfilePlot(imp) curprofile = prof.getProfile() print(len(curprofile)); #for i in curprofile: # print(i) pixx = range(len(curprofile)) fitA = range(len(curprofile)) cf = CurveFitter(pixx, curprofile) cf.doFit(cf.GAUSSIAN) #print(cf.getFormula()) print(cf.getResultString()) cfp = cf.getParams() EstimatedDiameter = "Diameter = " + str(2.355*cfp[3]) IJ.log(EstimatedDiameter) for i in range(len(curprofile)): fitA[i] = cf.f(cfp, i) fitplot = Plot("fitted", "pixels", "intensity", pixx, fitA) fitplot.addPoints(pixx, curprofile, fitplot.CIRCLE) fitplot.addLabel(0.1, 0.1, EstimatedDiameter) fitplot.show()
xtofit = [ i * frame_interval for i in range( n_slices - bleach_frame ) ] ytofit = normalized_curve[ bleach_frame : n_slices ] # Fitter fitter = CurveFitter(xtofit, ytofit) fitter.doFit(CurveFitter.EXP_RECOVERY_NOOFFSET) IJ.log("Fit FRAP curve by " + fitter.getFormula() ) param_values = fitter.getParams() IJ.log( fitter.getResultString() ) # Overlay fit curve, with oversampling (for plot) xfit = [ (t / 10.0 + bleach_frame) * frame_interval for t in range(10 * len(xtofit) ) ] yfit = [] for xt in xfit: yfit.append( fitter.f( fitter.getParams(), xt - xfit[0]) ) plot = Plot("Normalized FRAP curve for " + current_imp.getTitle(), "Time ("+time_units+')', "NU", [], []) plot.setLimits(0, max(x), 0, 1.5 ); plot.setLineWidth(2) plot.setColor(Color.BLACK) plot.addPoints(x, y, Plot.LINE) plot.addPoints(x,y,PlotWindow.X); plot.setColor(Color.RED) plot.addPoints(xfit, yfit, Plot.LINE)