package gdsc.smlm.function.gaussian; import org.apache.commons.math3.util.FastMath; /*----------------------------------------------------------------------------- * GDSC SMLM Software * * Copyright (C) 2013 Alex Herbert * Genome Damage and Stability Centre * University of Sussex, UK * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. *---------------------------------------------------------------------------*/ /** * Evaluates an 2-dimensional Gaussian function for a single peak. * <p> * The single parameter x in the {@link #eval(int, double[])} function is assumed to be a linear index into * 2-dimensional * data. The dimensions of the data must be specified to allow unpacking to coordinates. * <p> * Data should be packed in descending dimension order, e.g. Y,X : Index for [x,y] = MaxX*y + x. */ public class SingleNSNBFixedGaussian2DFunction extends Gaussian2DFunction { private static final int[] gradientIndices; static { gradientIndices = createGradientIndices(1, new SingleNSNBFixedGaussian2DFunction(1, 1)); } protected double width; protected double background; protected double x0pos; protected double x1pos; protected double n; protected double height; protected double aa; protected double aa2; /** * Constructor * * @param maxx * The maximum x value of the 2-dimensional data (used to unpack a linear index into coordinates) * @param maxy * The maximum y value of the 2-dimensional data (used to unpack a linear index into coordinates) */ public SingleNSNBFixedGaussian2DFunction(int maxx, int maxy) { super(maxx, maxy); } /* * (non-Javadoc) * * @see gdsc.smlm.function.gaussian.Gaussian2DFunction#copy() */ @Override public Gaussian2DFunction copy() { return new SingleNSNBFixedGaussian2DFunction(maxx, maxy); } /* * (non-Javadoc) * * @see gdsc.fitting.function.NonLinearFunction#initialise(double[]) */ public void initialise(double[] a) { background = a[BACKGROUND]; x0pos = a[X_POSITION]; x1pos = a[Y_POSITION]; width = a[X_SD]; final double sx = a[X_SD]; final double sx2 = sx * sx; n = ONE_OVER_TWO_PI / sx2; height = a[SIGNAL] * n; // All prefactors are negated since the Gaussian uses the exponential to the negative: // A * exp( -( a(x-x0)^2 + 2b(x-x0)(y-y0) + c(y-y0)^2 ) ) aa = -0.5 / sx2; aa2 = -2.0 * aa; } /** * Produce an output predicted value for a given set of input * predictors (x) and coefficients (a). * <p> * Evaluates an 2-dimensional elliptical Gaussian function for a single peak. * <p> * The first coefficient is the Gaussian background level (B). The coefficients are then packed for each peak: * Amplitude; Angle; position[N]; sd[N]. Amplitude (A) is the volume of the Gaussian. Angle (r) is the rotation * angle of the ellipse. Position (x,y) is the position of the Gaussian in each of the N-dimensions. SD (sx,sy) is * the standard deviation in each of the N-dimensions. * <p> * The equation per peak is:<br/> * y_peak = A/(2*pi*sx*sy) * exp( -( a(x-x0)^2 + c(y-y0)^2 ) )<br/> * Where: <br/> * a = 1/(2*sx^2) <br/> * c = 1/(2*sy^2) * * @param x * Input predictor * @param dyda * Partial gradient of function with respect to each coefficient * @return The predicted value * * @see gdsc.smlm.function.NonLinearFunction#eval(int, double[]) */ public double eval(final int x, final double[] dyda) { // Unpack the predictor into the dimensions final int x1 = x / maxx; final int x0 = x % maxx; return background + gaussian(x0, x1, dyda); } private double gaussian(final int x0, final int x1, final double[] dy_da) { final double dx = x0 - x0pos; final double dy = x1 - x1pos; // Calculate gradients final double y = height * FastMath.exp(aa * (dx * dx + dy * dy)); final double yaa2 = y * aa2; dy_da[0] = yaa2 * dx; dy_da[1] = yaa2 * dy; return y; } /* * (non-Javadoc) * * @see gdsc.fitting.function.NonLinearFunction#eval(int) */ public double eval(final int x) { // Unpack the predictor into the dimensions final int x1 = x / maxx; final int x0 = x % maxx; final double dx = x0 - x0pos; final double dy = x1 - x1pos; return background + height * FastMath.exp(aa * (dx * dx + dy * dy)); } @Override public int getNPeaks() { return 1; } @Override public boolean evaluatesBackground() { return false; } @Override public boolean evaluatesSignal() { return false; } @Override public boolean evaluatesShape() { return false; } @Override public boolean evaluatesPosition() { return true; } @Override public boolean evaluatesSD0() { return false; } @Override public boolean evaluatesSD1() { return false; } @Override public int getParametersPerPeak() { return 2; } /* * (non-Javadoc) * * @see gdsc.fitting.function.NonLinearFunction#gradientIndices() */ public int[] gradientIndices() { return gradientIndices; } }