/**
  * The results of the {@link SR_EELS_CharacterisationPlugin} plugin are parsed.
  *
  * <p>This function extracts the values that describe the pathway of the borders of a spectrum.
  *
  * @return a polynomial that fits the given data points
  */
 private SR_EELS_Polynomial_2D getFunctionBorders() {
   final double[][] vals = new double[3 * importer.size()][3];
   int i = 0;
   for (final float[] point : importer) {
     for (int j = 0; j < 3; j++) {
       // y coordinate of the fit function at the centre of the
       // image/camera ->
       // z value
       vals[i + j][0] = point[2 + 2 * j] - CameraSetup.getFullWidth() / 2;
       // Coordinate on the energy dispersive axis -> x value
       // The same value for top, centre and bottom.
       vals[i + j][1] = point[0] - CameraSetup.getFullHeight() / 2;
       // coordinate on the lateral axis -> y value
       // The indices for centre, top and bottom are 2, 4 and 6.
       vals[i + j][2] =
           importer.getYInterceptPoint(i / 3)[11 + j] - CameraSetup.getFullHeight() / 2;
     }
     i += 3;
   }
   /*
    * Define the orders of the 2D polynomial.
    */
   final int m = 3;
   final int n = 2;
   final SR_EELS_Polynomial_2D func = new SR_EELS_Polynomial_2D(m, n);
   final double[] a_fit = new double[(m + 1) * (n + 1)];
   Arrays.fill(a_fit, 1.);
   final LMA lma = new LMA(func, a_fit, vals);
   lma.fit();
   /*
    * TODO: Output information about the fit using IJ.log
    */
   return new SR_EELS_Polynomial_2D(m, n, a_fit);
 }
 /**
  * The results of the {@link SR_EELS_CharacterisationPlugin} plugin are parsed.
  *
  * <p>This function extracts the values that describe the width of a spectrum depending on its
  * position on the camera.
  *
  * @return a polynomial that fits the given data points
  */
 private SR_EELS_Polynomial_2D getFunctionWidth() {
   final double[][] vals = new double[importer.size()][3];
   int i = 0;
   for (final float[] point : importer) {
     // The width of the spectrum -> z value
     vals[i][0] = point[8];
     // Coordinate on the energy dispersive axis -> x value
     // The same value for top, centre and bottom.
     vals[i][1] = point[0] - CameraSetup.getFullWidth() / 2;
     // coordinate on the lateral axis -> y value
     // The indices for centre, top and bottom are 2, 4 and 6.
     vals[i][2] = point[2] - CameraSetup.getFullHeight() / 2;
     i++;
   }
   /*
    * Define the orders of the 2D polynomial.
    */
   final int m = 2;
   final int n = 2;
   final SR_EELS_Polynomial_2D func = new SR_EELS_Polynomial_2D(m, n);
   final double[] b_fit = new double[(m + 1) * (n + 1)];
   Arrays.fill(b_fit, 1.);
   final LMA lma = new LMA(func, b_fit, vals);
   lma.fit();
   /*
    * TODO: Output information about the fit using IJ.log
    */
   return new SR_EELS_Polynomial_2D(m, n, b_fit);
 }
 /**
  * Use this method for batch processing. Values that are set up by the GUI have to be passed as
  * parameters.
  *
  * @param input_image is the image to correct.
  * @param path2REsults is the text file that contains the characterisation results for the width.
  * @return the corrected image.
  */
 public ImagePlus correctImage(final ImagePlus input_image, final String path2REsults) {
   this.inputProcessor =
       new SR_EELS_FloatProcessor(
           (FloatProcessor) input_image.getProcessor(),
           CameraSetup.getFullWidth() / input_image.getWidth(),
           CameraSetup.getFullHeight() / input_image.getHeight(),
           input_image.getWidth() / 2,
           input_image.getHeight() / 2);
   title = StringManipulator.removeExtensionFromTitle(input_image.getTitle());
   this.pathResults = path2REsults;
   run(null);
   return outputImage;
 }
  /*
   * (non-Javadoc)
   *
   * @see ij.plugin.filter.ExtendedPlugInFilter#showDialog(ij.ImagePlus,
   * java.lang.String, ij.plugin.filter.PlugInFilterRunner)
   */
  @Override
  public int showDialog(final ImagePlus imp, final String command, final PlugInFilterRunner pfr) {
    final String searchPath = IJ.getDirectory("image");
    final LinkedList<String> foundCharacterisationResults = new LinkedList<>();
    findDatasets(searchPath, foundCharacterisationResults, imp.getShortTitle());
    final String otherResult = "other...";
    foundCharacterisationResults.add(otherResult);
    if (foundCharacterisationResults.size() > 1) {
      /*
       * A dialog is presented to select one of the found files.
       */
      final GenericDialog gd =
          new GenericDialog(
              (command != "") ? command : "Debugging" + " - Select data set", IJ.getInstance());
      String[] arrayOfFoundResults = new String[foundCharacterisationResults.size()];
      arrayOfFoundResults = foundCharacterisationResults.toArray(arrayOfFoundResults);
      gd.addRadioButtonGroup(
          SR_EELS.FILENAME_RESULTS,
          arrayOfFoundResults,
          foundCharacterisationResults.size(),
          1,
          foundCharacterisationResults.get(0));
      gd.setResizable(false);
      gd.showDialog();
      if (gd.wasCanceled()) {
        canceled();
        return NO_CHANGES | DONE;
      }
      if (foundCharacterisationResults.size() > 1) {
        pathResults = gd.getNextRadioButton();
      }
    }
    /*
     * If only no file has been found, the parameters dialog is shown.
     */
    if (foundCharacterisationResults.size() == 0 | pathResults.equals(otherResult)) {

      do {
        if (showParameterDialog(command) == CANCEL) {
          canceled();
          return NO_CHANGES | DONE;
        }
      } while (!pathResults.contains(SR_EELS.FILENAME_RESULTS));
    } else {
      if (foundCharacterisationResults.size() == 1) {
        pathResults = foundCharacterisationResults.getFirst();
      } else {
        canceled();
        return NO_CHANGES | DONE;
      }
    }
    inputProcessor =
        new SR_EELS_FloatProcessor(
            (FloatProcessor) imp.getProcessor(),
            CameraSetup.getFullWidth() / imp.getWidth(),
            CameraSetup.getFullHeight() / imp.getHeight(),
            imp.getWidth() / 2,
            imp.getHeight() / 2);
    title = StringManipulator.removeExtensionFromTitle(imp.getTitle());
    return FLAGS;
  }