Example usage for org.apache.commons.math3.linear RealVector getSubVector

List of usage examples for org.apache.commons.math3.linear RealVector getSubVector

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getSubVector.

Prototype

public abstract RealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException;

Source Link

Document

Get a subvector from consecutive elements.

Usage

From source file:edu.utexas.cs.tactex.utils.BrokerUtils.java

/**
 * Rotates a weekly record to start from the next timeslot
 * and append the last day to continue until the end of the day
 * /*from   w  w  w  . ja  va2 s  . c  om*/
 * @param energy
 * @param currentTimeslot
 * @return
 */
public static ArrayRealVector rotateWeeklyRecordAndAppendTillEndOfDay(RealVector record, int currentTimeslot) {
    // sanity check
    if (record.getDimension() != 7 * 24) {
        log.error("record dimension is not 7*24, not sure if code robust to that?");
    }
    int predictionStartWeeklyHour = (currentTimeslot + 1) % (record.getDimension());
    ArrayRealVector copy = new ArrayRealVector(record.getDimension());
    // rotate to start from current moment
    RealVector slice1 = record.getSubVector(predictionStartWeeklyHour,
            record.getDimension() - predictionStartWeeklyHour);
    RealVector slice2 = record.getSubVector(0, predictionStartWeeklyHour);
    copy.setSubVector(0, slice1);
    copy.setSubVector(slice1.getDimension(), slice2);
    return copy;
}

From source file:net.sf.dsp4j.octave_3_2_4.m.polynomial.Roots.java

public static Complex[] roots(RealVector v) {

    if (v.isInfinite() || v.isNaN()) {
        throw new RuntimeException("roots: inputs must not contain Inf or NaN");
    }//from www  .  java2 s.  co m

    int n = v.getDimension();

    // ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the
    // ## leading k zeros and n - k - l roots of the polynomial are zero.

    int[] f = new int[v.getDimension()];
    if (v.getDimension() > 0) {
        int fI = 0;
        double max = v.getMaxValue();
        double min = FastMath.abs(v.getMinValue());
        if (min > max) {
            max = min;
        }
        RealVector v1 = v.mapDivide(max);
        f = OctaveBuildIn.find(v1);
    }

    Complex[] r = new Complex[0];
    if (f.length > 0 && n > 1) {
        v = v.getSubVector(f[0], f[f.length - 1] - f[0] + 1);
        if (v.getDimension() > 1) {
            double[] ones = new double[v.getDimension() - 2];
            Arrays.fill(ones, 1);
            RealMatrix A = OctaveBuildIn.diag(ones, -1);
            for (int i = 0; i < A.getRowDimension(); i++) {
                A.setEntry(i, 0, -v.getEntry(i + 1) / v.getEntry(0));
            }
            try {
                r = Eig.eig(A);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if (f[f.length - 1] < n) {
                int diffLength = n - 1 - f[f.length - 1];
                if (diffLength > 0) {
                    int rl = r.length;
                    r = Arrays.copyOf(r, r.length + diffLength);
                    Arrays.fill(r, rl, r.length, Complex.ZERO);
                }
            }
        } else {
            r = new Complex[n - f[f.length - 1]];
            Arrays.fill(r, Complex.ZERO);
        }
    } else {
        r = new Complex[0];
    }
    return r;

}

From source file:com.datumbox.framework.core.machinelearning.featureselection.PCA.java

/** {@inheritDoc} */
@Override/*from   w w w  .  ja  va  2s .  c o  m*/
protected void _fit(Dataframe trainingData) {
    ModelParameters modelParameters = knowledgeBase.getModelParameters();

    int n = trainingData.size();
    int d = trainingData.xColumnSize();

    //convert data into matrix
    Map<Object, Integer> featureIds = modelParameters.getFeatureIds();
    DataframeMatrix matrixDataset = DataframeMatrix.newInstance(trainingData, false, null, featureIds);
    RealMatrix X = matrixDataset.getX();

    //calculate means and subtract them from data
    RealVector meanValues = new OpenMapRealVector(d);
    for (Integer columnId : featureIds.values()) {
        double mean = 0.0;
        for (int row = 0; row < n; row++) {
            mean += X.getEntry(row, columnId);
        }
        mean /= n;

        for (int row = 0; row < n; row++) {
            X.addToEntry(row, columnId, -mean);
        }

        meanValues.setEntry(columnId, mean);
    }
    modelParameters.setMean(meanValues);

    //dxd matrix
    RealMatrix covarianceDD = (X.transpose().multiply(X)).scalarMultiply(1.0 / (n - 1.0));

    EigenDecomposition decomposition = new EigenDecomposition(covarianceDD);
    RealVector eigenValues = new ArrayRealVector(decomposition.getRealEigenvalues(), false);

    RealMatrix components = decomposition.getV();

    //Whiten Components W = U*L^0.5; To whiten them we multiply with L^0.5.
    if (knowledgeBase.getTrainingParameters().isWhitened()) {

        RealMatrix sqrtEigenValues = new DiagonalMatrix(d);
        for (int i = 0; i < d; i++) {
            sqrtEigenValues.setEntry(i, i, FastMath.sqrt(eigenValues.getEntry(i)));
        }

        components = components.multiply(sqrtEigenValues);
    }

    //the eigenvalues and their components are sorted by descending order no need to resort them
    Integer maxDimensions = knowledgeBase.getTrainingParameters().getMaxDimensions();
    Double variancePercentageThreshold = knowledgeBase.getTrainingParameters().getVariancePercentageThreshold();
    if (variancePercentageThreshold != null && variancePercentageThreshold <= 1) {
        double totalVariance = 0.0;
        for (int i = 0; i < d; i++) {
            totalVariance += eigenValues.getEntry(i);
        }

        double sum = 0.0;
        int varCounter = 0;
        for (int i = 0; i < d; i++) {
            sum += eigenValues.getEntry(i) / totalVariance;
            varCounter++;
            if (sum >= variancePercentageThreshold) {
                break;
            }
        }

        if (maxDimensions == null || maxDimensions > varCounter) {
            maxDimensions = varCounter;
        }
    }

    if (maxDimensions != null && maxDimensions < d) {
        //keep only the maximum selected eigenvalues
        eigenValues = eigenValues.getSubVector(0, maxDimensions);

        //keep only the maximum selected eigenvectors
        components = components.getSubMatrix(0, components.getRowDimension() - 1, 0, maxDimensions - 1);
    }

    modelParameters.setEigenValues(eigenValues);
    modelParameters.setComponents(components);
}

From source file:edu.stanford.cfuller.imageanalysistools.fitting.GaussianImageObject.java

/**
 * Fits this object to a 3-dimensional gaussian, and estimates error and goodness of fit.
 * @param p     The parameters for the current analysis.
 *///from ww  w .  jav  a2  s  .  c  om
public void fitPosition(ParameterDictionary p) {

    if (this.sizeInPixels == 0) {
        this.nullifyImages();
        return;
    }

    this.fitParametersByChannel = new java.util.ArrayList<FitParameters>();
    this.fitR2ByChannel = new java.util.ArrayList<Double>();
    this.fitErrorByChannel = new java.util.ArrayList<Double>();
    this.nPhotonsByChannel = new java.util.ArrayList<Double>();

    GaussianFitter3D gf = new GaussianFitter3D();

    //System.out.println(this.parent.getDimensionSizes().getZ());

    int numChannels = 0;

    if (p.hasKey(NUM_WAVELENGTHS_PARAM)) {
        numChannels = p.getIntValueForKey(NUM_WAVELENGTHS_PARAM);
    } else {
        numChannels = this.parent.getDimensionSizes().get(ImageCoordinate.C);
    }

    for (int channelIndex = 0; channelIndex < numChannels; channelIndex++) {

        RealVector fitParameters = new ArrayRealVector(7, 0.0);

        double ppg = p.getDoubleValueForKey(PHOTONS_PER_LEVEL_PARAM);

        this.parentBoxMin.set(ImageCoordinate.C, channelIndex);
        this.parentBoxMax.set(ImageCoordinate.C, channelIndex + 1);

        this.boxImages();

        List<Double> x = new java.util.ArrayList<Double>();
        List<Double> y = new java.util.ArrayList<Double>();
        List<Double> z = new java.util.ArrayList<Double>();
        List<Double> f = new java.util.ArrayList<Double>();

        for (ImageCoordinate ic : this.parent) {
            x.add((double) ic.get(ImageCoordinate.X));
            y.add((double) ic.get(ImageCoordinate.Y));
            z.add((double) ic.get(ImageCoordinate.Z));
            f.add((double) parent.getValue(ic));
        }

        xValues = new double[x.size()];
        yValues = new double[y.size()];
        zValues = new double[z.size()];
        functionValues = new double[f.size()];

        double xCentroid = 0;
        double yCentroid = 0;
        double zCentroid = 0;
        double totalCounts = 0;

        for (int i = 0; i < x.size(); i++) {

            xValues[i] = x.get(i);
            yValues[i] = y.get(i);
            zValues[i] = z.get(i);
            functionValues[i] = f.get(i) * ppg;
            xCentroid += xValues[i] * functionValues[i];
            yCentroid += yValues[i] * functionValues[i];
            zCentroid += zValues[i] * functionValues[i];
            totalCounts += functionValues[i];
        }

        xCentroid /= totalCounts;
        yCentroid /= totalCounts;
        zCentroid /= totalCounts;

        //z sometimes seems to be a bit off... trying (20110415) to go back to max value pixel at x,y centroid

        int xRound = (int) Math.round(xCentroid);
        int yRound = (int) Math.round(yCentroid);

        double maxVal = 0;
        int maxInd = 0;

        double minZ = Double.MAX_VALUE;
        double maxZ = 0;

        for (int i = 0; i < x.size(); i++) {

            if (zValues[i] < minZ)
                minZ = zValues[i];
            if (zValues[i] > maxZ)
                maxZ = zValues[i];

            if (xValues[i] == xRound && yValues[i] == yRound) {
                if (functionValues[i] > maxVal) {
                    maxVal = functionValues[i];
                    maxInd = (int) zValues[i];
                }
            }
        }

        zCentroid = maxInd;

        //parameter ordering: amplitude, var x-y, var z, x/y/z coords, background

        //amplitude: find the max value; background: find the min value

        double maxValue = 0;

        double minValue = Double.MAX_VALUE;

        for (ImageCoordinate ic : this.parent) {

            if (parent.getValue(ic) > maxValue)
                maxValue = parent.getValue(ic);
            if (parent.getValue(ic) < minValue)
                minValue = parent.getValue(ic);

        }

        fitParameters.setEntry(0, (maxValue - minValue) * 0.95);
        fitParameters.setEntry(6, minValue + 0.05 * (maxValue - minValue));

        //positions

        fitParameters.setEntry(3, xCentroid);
        fitParameters.setEntry(4, yCentroid);
        fitParameters.setEntry(5, zCentroid);

        //variances

        final double limitedWidthxy = 200;
        final double limitedWidthz = 500;

        double sizex = limitedWidthxy / p.getDoubleValueForKey(PIXELSIZE_PARAM);
        double sizez = limitedWidthz / p.getDoubleValueForKey(SECTIONSIZE_PARAM);

        if (p.hasKey(Z_WIDTH_PARAM)) {
            sizez = p.getDoubleValueForKey(Z_WIDTH_PARAM);
        }

        if (p.hasKey(XY_WIDTH_PARAM)) {
            sizex = p.getDoubleValueForKey(XY_WIDTH_PARAM);
        }

        fitParameters.setEntry(1, sizex / 2);
        fitParameters.setEntry(2, sizez / 2);

        //amplitude and background are in arbitrary intensity units; convert to photon counts

        fitParameters.setEntry(0, fitParameters.getEntry(0) * ppg);
        fitParameters.setEntry(6, fitParameters.getEntry(6) * ppg);

        //System.out.println("guess: " + fitParameters);

        //do the fit

        fitParameters = gf.fit(this, fitParameters, ppg);

        //System.out.println("fit: " + fitParameters);

        FitParameters fp = new FitParameters();

        fp.setPosition(ImageCoordinate.X, fitParameters.getEntry(3));
        fp.setPosition(ImageCoordinate.Y, fitParameters.getEntry(4));
        fp.setPosition(ImageCoordinate.Z, fitParameters.getEntry(5));

        fp.setSize(ImageCoordinate.X, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Y, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Z, fitParameters.getEntry(2));

        fp.setAmplitude(fitParameters.getEntry(0));
        fp.setBackground(fitParameters.getEntry(6));

        fitParametersByChannel.add(fp);

        //calculate R2

        double residualSumSquared = 0;
        double mean = 0;
        double variance = 0;
        double R2 = 0;

        double n_photons = 0;

        for (int i = 0; i < this.xValues.length; i++) {

            residualSumSquared += Math.pow(GaussianFitter3D.fitResidual(functionValues[i], xValues[i],
                    yValues[i], zValues[i], fitParameters), 2);

            mean += functionValues[i];

            n_photons += functionValues[i] - fitParameters.getEntry(6);

        }

        mean /= functionValues.length;

        for (int i = 0; i < this.xValues.length; i++) {
            variance += Math.pow(functionValues[i] - mean, 2);
        }

        R2 = 1 - (residualSumSquared / variance);

        this.fitR2ByChannel.add(R2);

        this.unboxImages();

        //calculate fit error

        double s_xy = fitParameters.getEntry(1) * fitParameters.getEntry(1)
                * Math.pow(p.getDoubleValueForKey(PIXELSIZE_PARAM), 2);
        double s_z = fitParameters.getEntry(2) * fitParameters.getEntry(2)
                * Math.pow(p.getDoubleValueForKey(SECTIONSIZE_PARAM), 2);

        //s_z = 0; //remove!!

        double error = (2 * s_xy + s_z) / (n_photons - 1);// + 4*Math.sqrt(Math.PI) * Math.pow(2*s_xy,1.5)*Math.pow(fitParameters.getEntry(6),2)/(p.getDoubleValueForKey("pixelsize_nm")*n_photons*n_photons);

        double b = fitParameters.getEntry(6);
        double a = p.getDoubleValueForKey(PIXELSIZE_PARAM);
        double alpha = p.getDoubleValueForKey(SECTIONSIZE_PARAM);
        double sa_x = s_xy + Math.pow(a, 2) / 12;
        double sa_z = s_z + Math.pow(alpha, 2) / 12;

        //System.out.printf("b = %f, a = %f, alpha = %f, s_xy = %f, s_z = %f, n= %f\n", b, a, alpha, s_xy, s_z, n_photons);

        double error_x = sa_x / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_x * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey(PIXELSIZE_PARAM), 2)));
        double error_z = sa_z / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_z * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey(SECTIONSIZE_PARAM), 2)));

        double A = 1.0 / (2 * Math.sqrt(2) * Math.pow(Math.PI, 1.5) * Math.sqrt(sa_z) * sa_x);

        ErrIntFunc eif = new ErrIntFunc();

        eif.setParams(b, n_photons, A, sa_z, sa_x, a, alpha);

        LegendreGaussIntegrator lgi = new LegendreGaussIntegrator(5, 10, 1000);

        //integrate over 10*width of PSF in z 

        double size = 10 * Math.sqrt(sa_z);

        double intpart = 0;
        try {

            if (b < 0)
                throw new ConvergenceException(new DummyLocalizable("negative background!")); // a negative value for b seems to cause the integration to hang, preventing the program from progressing

            intpart = lgi.integrate(10000, eif, -size, size);

            double fullIntPart = intpart + Math.pow(2 * Math.PI, 1.5) * sa_x * A / Math.sqrt(sa_z);

            error_x = Math.sqrt(2 / (n_photons * sa_z / (2 * sa_z + sa_x) * fullIntPart));
            error_z = Math.sqrt(2 / (n_photons * sa_x / (2 * sa_z + sa_x) * fullIntPart));

        } catch (ConvergenceException e) {
            LoggingUtilities.getLogger().severe("Integration error: " + e.getMessage());
            error_x = -1;
            error_z = -1;
        }

        if (error_x > 0 && error_z > 0) {

            error = Math.sqrt(2 * error_x * error_x + error_z * error_z);

        } else {
            error = Double.NaN;
        }

        this.fitErrorByChannel.add(error);

        this.positionsByChannel.add(fitParameters.getSubVector(3, 3));

        this.nPhotonsByChannel.add(n_photons);

    }

    this.hadFittingError = false;
}

From source file:edu.stanford.cfuller.imageanalysistools.fitting.GaussianImageObjectWithCovariance.java

/**
 * Fits this object to a 3-dimensional gaussian, and estimates error and goodness of fit.
 * @param p     The parameters for the current analysis.
 *//*from  w w  w . j  a  v a  2  s  .c  o  m*/
public void fitPosition(ParameterDictionary p) {

    if (this.sizeInPixels == 0) {
        this.nullifyImages();
        return;
    }

    this.fitParametersByChannel = new java.util.ArrayList<FitParameters>();
    this.fitR2ByChannel = new java.util.ArrayList<Double>();
    this.fitErrorByChannel = new java.util.ArrayList<Double>();
    this.nPhotonsByChannel = new java.util.ArrayList<Double>();

    GaussianFitter3DWithCovariance gf = new GaussianFitter3DWithCovariance();

    //System.out.println(this.parent.getDimensionSizes().getZ());

    int numChannels = 0;

    if (p.hasKey("num_wavelengths")) {
        numChannels = p.getIntValueForKey("num_wavelengths");
    } else {
        numChannels = this.parent.getDimensionSizes().get(ImageCoordinate.C);
    }

    //for (int channelIndex = 0; channelIndex < this.parent.getDimensionSizes().getC(); channelIndex++) {
    for (int channelIndex = 0; channelIndex < numChannels; channelIndex++) {

        RealVector fitParameters = new ArrayRealVector(11, 0.0);

        double ppg = p.getDoubleValueForKey("photons_per_greylevel");

        this.parentBoxMin.set(ImageCoordinate.C, channelIndex);
        this.parentBoxMax.set(ImageCoordinate.C, channelIndex + 1);

        this.boxImages();

        List<Double> x = new java.util.ArrayList<Double>();
        List<Double> y = new java.util.ArrayList<Double>();
        List<Double> z = new java.util.ArrayList<Double>();
        List<Double> f = new java.util.ArrayList<Double>();

        for (ImageCoordinate ic : this.parent) {
            x.add((double) ic.get(ImageCoordinate.X));
            y.add((double) ic.get(ImageCoordinate.Y));
            z.add((double) ic.get(ImageCoordinate.Z));
            f.add((double) parent.getValue(ic));
        }

        xValues = new double[x.size()];
        yValues = new double[y.size()];
        zValues = new double[z.size()];
        functionValues = new double[f.size()];

        double xCentroid = 0;
        double yCentroid = 0;
        double zCentroid = 0;
        double totalCounts = 0;

        for (int i = 0; i < x.size(); i++) {

            xValues[i] = x.get(i);
            yValues[i] = y.get(i);
            zValues[i] = z.get(i);
            functionValues[i] = f.get(i) * ppg;
            xCentroid += xValues[i] * functionValues[i];
            yCentroid += yValues[i] * functionValues[i];
            zCentroid += zValues[i] * functionValues[i];
            totalCounts += functionValues[i];
        }

        xCentroid /= totalCounts;
        yCentroid /= totalCounts;
        zCentroid /= totalCounts;

        //z sometimes seems to be a bit off... trying (20110415) to go back to max value pixel at x,y centroid

        int xRound = (int) Math.round(xCentroid);
        int yRound = (int) Math.round(yCentroid);

        double maxVal = 0;
        int maxInd = 0;

        double minZ = Double.MAX_VALUE;
        double maxZ = 0;

        for (int i = 0; i < x.size(); i++) {

            if (zValues[i] < minZ)
                minZ = zValues[i];
            if (zValues[i] > maxZ)
                maxZ = zValues[i];

            if (xValues[i] == xRound && yValues[i] == yRound) {
                if (functionValues[i] > maxVal) {
                    maxVal = functionValues[i];
                    maxInd = (int) zValues[i];
                }
            }
        }

        zCentroid = maxInd;

        //parameter ordering: amplitude, var x-y, var z, x/y/z coords, background

        //amplitude: find the max value; background: find the min value

        double maxValue = 0;

        double minValue = Double.MAX_VALUE;

        for (ImageCoordinate ic : this.parent) {

            if (parent.getValue(ic) > maxValue)
                maxValue = parent.getValue(ic);
            if (parent.getValue(ic) < minValue)
                minValue = parent.getValue(ic);

        }

        fitParameters.setEntry(0, (maxValue - minValue) * 0.95);
        fitParameters.setEntry(10, minValue + 0.05 * (maxValue - minValue));

        //positions

        fitParameters.setEntry(7, xCentroid);
        fitParameters.setEntry(8, yCentroid);
        fitParameters.setEntry(9, zCentroid);

        //variances

        final double limitedWidthxy = 200;
        final double limitedWidthz = 500;

        double sizex = limitedWidthxy / p.getDoubleValueForKey("pixelsize_nm");
        double sizey = limitedWidthxy / p.getDoubleValueForKey("pixelsize_nm");
        double sizez = limitedWidthz / p.getDoubleValueForKey("z_sectionsize_nm");

        if (p.hasKey(Z_WIDTH_PARAM)) {
            sizez = p.getDoubleValueForKey(Z_WIDTH_PARAM);
        }

        if (p.hasKey(XY_WIDTH_PARAM)) {
            sizex = p.getDoubleValueForKey(XY_WIDTH_PARAM);
            sizey = p.getDoubleValueForKey(XY_WIDTH_PARAM);
        }

        fitParameters.setEntry(1, sizex / 2);
        fitParameters.setEntry(2, sizey / 2);
        fitParameters.setEntry(3, sizez / 2);

        //covariances -- guess zero to start

        fitParameters.setEntry(4, 0.0);
        fitParameters.setEntry(5, 0.0);
        fitParameters.setEntry(6, 0.0);

        //amplitude and background are in arbitrary intensity units; convert to photon counts

        fitParameters.setEntry(0, fitParameters.getEntry(0) * ppg);
        fitParameters.setEntry(10, fitParameters.getEntry(10) * ppg);

        //System.out.println("guess: " + fitParameters);

        //do the fit

        //System.out.println("Initial for object " + this.label + ": " + fitParameters.toString());

        fitParameters = gf.fit(this, fitParameters, ppg);

        //System.out.println("Parameters for object " + this.label + ": " + fitParameters.toString());

        //System.out.println("fit: " + fitParameters);

        FitParameters fp = new FitParameters();

        fp.setPosition(ImageCoordinate.X, fitParameters.getEntry(7));
        fp.setPosition(ImageCoordinate.Y, fitParameters.getEntry(8));
        fp.setPosition(ImageCoordinate.Z, fitParameters.getEntry(9));

        fp.setSize(ImageCoordinate.X, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Y, fitParameters.getEntry(2));
        fp.setSize(ImageCoordinate.Z, fitParameters.getEntry(3));

        fp.setAmplitude(fitParameters.getEntry(0));
        fp.setBackground(fitParameters.getEntry(10));

        fp.setOtherParameter("corr_xy", fitParameters.getEntry(4));
        fp.setOtherParameter("corr_xz", fitParameters.getEntry(5));
        fp.setOtherParameter("corr_yz", fitParameters.getEntry(6));

        fitParametersByChannel.add(fp);

        //            System.out.println(fitParameters);

        //calculate R2

        double residualSumSquared = 0;
        double mean = 0;
        double variance = 0;
        double R2 = 0;

        double n_photons = 0;

        for (int i = 0; i < this.xValues.length; i++) {

            residualSumSquared += Math.pow(GaussianFitter3DWithCovariance.fitResidual(functionValues[i],
                    xValues[i], yValues[i], zValues[i], fitParameters), 2);

            mean += functionValues[i];

            n_photons += functionValues[i] - fitParameters.getEntry(10);

        }

        mean /= functionValues.length;

        for (int i = 0; i < this.xValues.length; i++) {
            variance += Math.pow(functionValues[i] - mean, 2);
        }

        R2 = 1 - (residualSumSquared / variance);

        this.fitR2ByChannel.add(R2);

        this.unboxImages();

        //calculate fit error -- these are wrong for the case with unequal x-y variances and covariance allowed, but leave them for now.

        double s_xy = fitParameters.getEntry(1) * fitParameters.getEntry(1)
                * Math.pow(p.getDoubleValueForKey("pixelsize_nm"), 2);
        double s_z = fitParameters.getEntry(3) * fitParameters.getEntry(3)
                * Math.pow(p.getDoubleValueForKey("z_sectionsize_nm"), 2);

        //s_z = 0; //remove!!

        double error = (2 * s_xy + s_z) / (n_photons - 1);// + 4*Math.sqrt(Math.PI) * Math.pow(2*s_xy,1.5)*Math.pow(fitParameters.getEntry(6),2)/(p.getDoubleValueForKey("pixelsize_nm")*n_photons*n_photons);

        double b = fitParameters.getEntry(10);
        double a = p.getDoubleValueForKey("pixelsize_nm");
        double alpha = p.getDoubleValueForKey("z_sectionsize_nm");
        double sa_x = s_xy + Math.pow(a, 2) / 12;
        double sa_z = s_z + Math.pow(alpha, 2) / 12;
        double error_x = sa_x / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_x * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey("pixelsize_nm"), 2)));
        double error_z = sa_z / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_z * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey("z_sectionsize_nm"), 2)));

        double A = 1.0 / (2 * Math.sqrt(2) * Math.pow(Math.PI, 1.5) * Math.sqrt(sa_z) * sa_x);

        ErrIntFunc eif = new ErrIntFunc();

        eif.setParams(b, n_photons, A, sa_z, sa_x, a, alpha);

        //LegendreGaussIntegrator lgi = new LegendreGaussIntegrator(5, 10, 1000);

        //integrate over 10*width of PSF in z 

        //double size = 10*Math.sqrt(sa_z);

        //double intpart = 0;
        //            try {
        //               
        //               if (b < 0) throw new ConvergenceException(new DummyLocalizable("negative background!")); // a negative value for b seems to cause the integration to hang, preventing the program from progressing
        //               
        //               intpart = lgi.integrate(10000, eif, -size, size);
        //               
        //               double fullIntPart = intpart + Math.pow(2*Math.PI, 1.5)*sa_x*A/Math.sqrt(sa_z);
        //               
        //               error_x = Math.sqrt(2/(n_photons*sa_z/(2*sa_z + sa_x)*fullIntPart));
        //               error_z = Math.sqrt(2/(n_photons*sa_x/(2*sa_z + sa_x)*fullIntPart));
        //               
        //            } catch (ConvergenceException e) {
        //               LoggingUtilities.getLogger().severe("Integration error: " + e.getMessage());
        //               error_x = -1;
        //               error_z = -1;
        //            }
        //            

        if (error_x > 0 && error_z > 0) {

            error = Math.sqrt(2 * error_x * error_x + error_z * error_z);

        } else {
            error = Double.NaN;
        }

        error = 0; // until a better error calculation

        this.fitErrorByChannel.add(error);

        this.positionsByChannel.add(fitParameters.getSubVector(7, 3));

        this.nPhotonsByChannel.add(n_photons);

    }

    this.hadFittingError = false;
    this.nullifyImages();
}

From source file:org.grouplens.samantha.modeler.svdfeature.SVDFeature.java

public void dump(File modelFile) {
    try {//from   w ww.j  av a  2  s . c  o m
        BufferedWriter writer = new BufferedWriter(new FileWriter(modelFile));
        RealVector biases = variableSpace.getScalarVarByName(SVDFeatureKey.BIASES.get());
        writer.write(Double.valueOf(biases.getEntry(0)).toString() + "\n");
        String biasLine = realVectorToString(biases.getSubVector(1, biases.getDimension() - 1));
        writer.write(biasLine + "\n");
        List<RealVector> factors = variableSpace.getVectorVarByName(SVDFeatureKey.FACTORS.get());
        for (int i = 0; i < factors.size(); i++) {
            String factLine = realVectorToString(factors.get(i));
            writer.write(factLine + "\n");
        }
        writer.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new BadRequestException(e);
    }
}