// ------------------------------------------- // Constructor // ------------------------------------------- public MultiFit( double[] image, ArrayList<Peak> params, double tolerance, int imageSizeX, int imageSizeY, boolean zFitting) { assert (tolerance < 1.0e-1); assert (image != null && image.length == (imageSizeX * imageSizeY)); this.tolerance = tolerance; this.imgData = image; this.imgSizeX = imageSizeX; this.imgSizeY = imageSizeY; this.fgData = new double[image.length]; this.bgData = new double[image.length]; this.bgCounts = new int[image.length]; this.fits = new ArrayList<FitPeak>(params.size()); for (Peak p : params) { FitPeak newFit = new FitPeak(p, 10); if (newFit.peak.hasStatus(PeakStatus.RUNNING)) { newFit.error = 0.0; newFit.errorOld = 0.0; } else { newFit.error = newFit.peak.getIError(); newFit.errorOld = newFit.error; } if (zFitting) { calcWidthsFromZ(newFit); } else { newFit.peak.setXWidth(1.0 / (2.0 * Math.pow(newFit.peak.getXWidth(), 2))); newFit.peak.setYWidth(1.0 / (2.0 * Math.pow(newFit.peak.getYWidth(), 2))); } newFit.xc = (int) newFit.peak.getXCenter(); newFit.yc = (int) newFit.peak.getYCenter(); // possible bug casting fromm double to int in original version newFit.wx = calcWidth(newFit.peak.getXWidth(), -10); newFit.wy = calcWidth(newFit.peak.getYWidth(), -10); // todo these are annoying constants newFit.setClampHeight(1000.0); newFit.setClampBackground(100.0); newFit.setClampXCenter(1.0); newFit.setClampYCenter(1.0); newFit.setClampXWidth(0.3); newFit.setClampYWidth(0.3); newFit.setClampZCenter(0.1); this.fits.add(newFit); } calcFit(); calcError(); }
private void calcError() { for (FitPeak fit : this.fits) { if (fit.peak.hasStatus(PeakStatus.RUNNING)) { final int offset = fit.offset; final int wx = fit.wx; final int wy = fit.wy; double error = 0.0; for (int i = -wy; i <= wy; i++) { for (int j = -wx; j <= wx; j++) { final int idx = (i * this.imgSizeX) + (j + offset); final double fi = this.fgData[idx] + (this.bgData[idx] / ((double) this.bgCounts[idx])); final double xi = this.imgData[idx]; error += (2 * (fi - xi)) - (2 * xi * Math.log(fi / xi)); } } fit.errorOld = fit.error; fit.error = error; if ((Math.abs(error - fit.errorOld) / error) < this.tolerance) { fit.peak.setStatus(PeakStatus.CONVERGED); } } } }