I’ve got a problem with Fit in a macro that I can’t figure out - I’m wondering if it’s me, or an IJ bug. I’ve been trying to figure this out for 2 hours and I’m at loss…
I’m try to find the centre of small circle in an image by getting two profiles in x and y directions across it, then by fitting a gaussian to the two profile arrays (ie in x then y directions) to get the mid-points in x and y. The data sets are almost identical.
…should be pretty easy.
However, when I run Fit.doFit in my macro to do the fitting it works fine for the first array, but then is completely wrong for the 2nd. Basically:
a) the first fit works fine
b) the second fit is completely wrong - even when passing in almost correct guess.
c) the 2nd incorrect fit reports an R^2 value that is completely incorrect
d) adding some logging info into my code to print out values - and also do a Fit.logResults - results in the output coming out of order, and the 2nd call to logResults (for the 2nd Fit.doFit) results not showing up at all in the log
The two files below are the output from Fit.plot in my script. X is the first (good) one, Y is the second (crazy nuts) one.
I don’t think it’s my code: …I’ve tried fitting the output manually in a normal plot window, and get the same error. …I’ve tried copying the Y data values (only) from the plot window to a new plot window, and still the same result. …I’ve tried making a completely different macro and copying the data values into new arrays and fitting - still fails.
So I’m at a loss. doFit and manual fit seems to work fine on one set of data and completely fail for a 2nd - almost equivalent - data set even when I’m supplying guess to doFit that is almost the exact correct parameter values.
I’ve included the two data sets from the plots below. The first doFit Gaussian fits without a problem to (even with no guess), the 2nd completely fails - even with almost perfect guess parameters passed!
Second (bad - gaussian just won’t fit it):
My code - for what it’s worth - is below:
c = newArray(boxSP); for (i=0;i<boxSP;i++) c[i] = i+x0; Plot.create("X prof "+angle,"Y", "val", c, profX); Plot.show(); Array.getStatistics(profX, min, max, mean, stddev); // give fit some initial guesses guess = newArray( max, min, (x0+x0+boxSP)/2, 2 ); Fit.doFit("Gaussian", c, profX, guess); midX = Fit.p(2); print("fitting X"); print("guesses: ", guess,guess,guess,guess); Fit.logResults; Fit.plot; d = newArray(boxSP); for (i=0;i<boxSP;i++) d[i] = i+y0; Plot.create("Y prof "+angle,"X", "val", d, profY); Plot.show(); Array.getStatistics(profY, min, max, mean, stddev); // give fit some initial guesses guess = newArray( max, min, (y0+y0+boxSP)/2, 2 ); Fit.doFit("Gaussian", d, profY, guess); midY = Fit.p(2); print("fitting Y"); print("guesses: ", guess,guess,guess,guess); Fit.logResults; Fit.plot;