Re: Calling CurveFitter from Java; only 4 fit functions work

Posted by Wayne Rasband-2 on
URL: http://imagej.273.s1.nabble.com/Calling-CurveFitter-from-Java-only-4-fit-functions-work-tp5022103p5022106.html

> On Apr 18, 2019, at 4:00 PM, Tihamer <[hidden email]> wrote:
>
> I've used ImageJ in the past purely as a graphic application and it's great.
>
> Today I needed to use some of its functionality in my own Java program, so I
> loaded up my project pom:

The following JavaScript version of your program should work better. It requires the latest ImageJ daily build (1.52o38), which fixes a CurveFitter.getResultString() bug that caused it to fail when there was a fitting error. Note that you have to construct a new CurveFitter for each fit.

-wayne

  xData = [0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0];
  yData = [2.0,5.0,8.0,11.0,14.0,17.0,20.0,23.0,26.0,29.0,32.0];
  zData = [0.0,2.0,16.0,54.0,128.0,250.0,432.0,686.0,1024.0,1458.0,2000.0];
  fit("a", xData, yData);
  fit("b", xData, zData);
   
  function fit(type, xData, yData) {
      for (func in CurveFitter.sortedTypes) {
         cf = new CurveFitter(xData, yData);
         IJ.log("\n"+func +type+": "+ CurveFitter.fitList[func]);
         cf.doFit(func);
         IJ.log(cf.getResultString());
      }
  }

-wayne

>
> <dependency>
>    <groupId>net.imagej</groupId>
>    <artifactId>ij</artifactId>
>    <version>1.52n</version>
>    <scope>test</scope>
> </dependency>
>
> I generated some test data and called CurveFitter from main:
>
> public static void main(String[] args) {
> double[] xData = {0.0, 1.0, 2.0,  3.0,  4.0,  5.0,  6.0,  7.0,  8.0,  9.0,
> 10.0};
> double[] yData = {2.0, 5.0, 8.0, 11.0, 14.0, 17.0, 20.0, 23.0, 26.0, 29.0,
> 32.0}; // linear
> double[] zData = {0.0, 2.0, 16.0, 54.0, 128.0, 250.0, 432.0, 686.0,
> 1024.0, 1458.0, 2000.0};
> System.out.println("\nLinear Test:");
> fitMyData(xData, yData);
> System.out.println("\nPower (linear regression) Test:");
> fitMyData(xData, zData);
> }
>
> public static void fitMyData(double[] xData, double[] yData) {
> CurveFitter curveFitter = new CurveFitter(xData, yData);
> for (int functionType : CurveFitter.sortedTypes) {
> if ( functionType != POLY3 && functionType != POLY4 && functionType !=
> POLY5 &&
> functionType != POLY6 && functionType != POLY7 && functionType != POLY8
> &&
> functionType != EXPONENTIAL && functionType != EXP_REGRESSION &&
> functionType != EXP_WITH_OFFSET && functionType != EXP_RECOVERY &&
> functionType != EXP_RECOVERY_NOOFFSET && functionType != LOG &&
> functionType != LOG2 &&
> functionType != GAUSSIAN && functionType != GAUSSIAN_NOOFFSET &&
> functionType != ERF &&
> functionType != RODBARD && functionType != RODBARD2 && functionType !=
> INV_RODBARD &&
> functionType != GAMMA_VARIATE && functionType != CHAPMAN) {
> System.out.print("\n\n " + functionType + " Function Type: " +
> CurveFitter.fitList[functionType] + " ");
> curveFitter.doFit(functionType);
> System.out.print(curveFitter.getResultString());
> }
> }
> }
>
> The results are very nice. E.g.:
>
> Linear Test:
>
> 0 Function Type: Straight Line
> Formula: y = a+bx
> Status: Success
> Number of iterations: 1
> Time: 0 ms
> Sum of residuals squared: 1.53560E-11
> Standard deviation: 0.00000E0
> R^2: 1.00000
> Parameters:
> a = 2.00000
> b = 3.00000
>
> <snip>
>
> Power (linear regression) Test:
> 16 Function Type: Power (linear regression)
> Formula: y = a*x^b
> Status: Success
> Number of iterations: 1
> Time: 0 ms
> Sum of residuals squared: 1.14026E-12
> Standard deviation: 2.46117E-13
> R^2: 1.00000
> Parameters:
> a = 2.00000
> b = 3.00000
>
> However, you can see my problem:  only four of the fit functions work; the
> rest of them throw a ava.lang.ArrayIndexOutOfBoundsException.  For example:
>
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
> at
> ij.measure.CurveFitter.modifyInitialParamsAndVariations(CurveFitter.java:891)
> at ij.measure.CurveFitter.doFit(CurveFitter.java:204)
> at ij.measure.CurveFitter.doFit(CurveFitter.java:169)
> at myproject.MyTest.fitMyData(MyTest.java:85)
>
> I looked at the source code, and I can't figure out what
> modifyInitialParamsAndVariations is trying to do.
>
> What am I doing wrong?

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html