def fit_curve(x, y): """fits curve of a single gaussian, returns FWHM, R^2, and the fn args. """ cf = CurveFitter(x, y) cf.doFit(12) params = cf.getResultString() fwhm, r2 = getFWHM(params) return fwhm, r2, params
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
from array import array from ij import IJ from ij.measure.CurveFitter import * # Example from # http://rsbweb.nih.gov/ij/macros/examples/CurveFittingDemo.txt # x = array('d', [0, 1, 2, 3, 4, 5]) # y = array('d', [0, 0.9, 4.5, 8, 18, 24]) # # make one where I know approximate coefficients x = array("d", [0, 1, 2, 3, 4, 5]) y = array("d", [0, 1.1, 1.9, 2.95, 4.02, 4.99]) cf = CurveFitter(x, y) cf.doFit(STRAIGHT_LINE) res = cf.getParams() # N.B. cf.getParams() returns # [ intercept, slope, sum of square residuals] b = res[0] m = res[1] p3 = res[2] print "Linear fit example: b=" + IJ.d2s(b, 6) + ", m =" + IJ.d2s(m, 6) + ", par3=" + IJ.d2s(p3, 6) print cf.getResultString()
"unit=pixel pixel_width=1 pixel_height=1 voxel_depth=1.0000000") # remove any selection so that the mean intensity of the entire area is calculated. inputImp.killRoi() # generate a plot of the mean intensity of each image # this plot is never displayed. We just use the values generated # by the zAxisProfiler. zPlot = ZAxisProfiler.getPlot(inputImp, "time") # We do not need the X values. They are just the slice number. # exp = zPlot.getXValues() levels = zPlot.getYValues() # Find the best linear fit of mean gray level vs camera exposure 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")
def get_regression(xVals, yVals): """ Runs a linear regression on x and y values and returns slope """ cf = CurveFitter(xVals, yVals) cf.doFit(0) #linear return cf.getParams()[1]
# 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()
mean_In = mean_In / bleach_frame # Calculate normalized curve normalized_curve = [] for i in range(n_slices): normalized_curve.append( (If[i] - min_intensity) / (mean_If - min_intensity) * mean_In / In[i] ) x = [i * frame_interval for i in range( n_slices ) ] y = normalized_curve 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)
from ij import IJ from ij.measure import CurveFitter from ij.gui import Plot # create example data arrays xa = [1., 2., 3., 4.] ya = [3., 3.5, 4., 4.5]; # construct a CurveFitter instance cf = CurveFitter(xa, ya); # actual fitting # fit models: see http://rsb.info.nih.gov/ij/developer/api/constant-values.html#ij.measure.CurveFitter.STRAIGHT_LINE cf.doFit(CurveFitter.STRAIGHT_LINE); # print out fitted parameters. b = cf.getParams()[0] m = cf.getParams()[1] strOut = str(b) + " : " + str(m) IJ.log(strOut); xb = [0 ,5] yb = [b, 5*m+b] pl = Plot("Data", "x", "y") pl.setLimits(0,5,0,5)
# 'circle', # [a[0] for a in spshifts], # [vertical_strip_width - a[1] for a in spshifts]) # plot.show() # stack the scan correction measurements in one list to later # fit a single scan correction for all beams. # (even though a correction for each of the 61 beams is probably needed) x_to_fit = x_to_fit + [a[0] for a in spshifts] y_to_fit = y_to_fit + [8 - a[1] for a in spshifts] cv = CurveFitter([a[0] for a in spshifts], [8 - a[1] for a in spshifts]) # fit the distortion measurements cv.doFit(CurveFitter.EXP_WITH_OFFSET) # cv.doFit(CurveFitter.EXPONENTIAL) # cv.doFit(CurveFitter.POLY2) # cv.doFit(CurveFitter.POLY3) plot = cv.getPlot() plot.show() 8 / 0 plot_images.append(plot.getImagePlus()) IJ.log('sFOV ' + str(sFOV)) IJ.log('fitGoodness ' + str(cv.getFitGoodness())) IJ.log('formula ' + str(cv.getFormula())) fit_params = cv.getParams() IJ.log('fit_params' + str(fit_params)) # write the fit results to file