Example usage for org.apache.commons.math.stat.regression SimpleRegression SimpleRegression

List of usage examples for org.apache.commons.math.stat.regression SimpleRegression SimpleRegression

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.regression SimpleRegression SimpleRegression.

Prototype

public SimpleRegression() 

Source Link

Document

Create an empty SimpleRegression instance

Usage

From source file:guineu.modules.filter.Alignment.RANSAC.RansacAlignerTask.java

private List<RTs> smooth(List<RTs> list, Range RTrange) {

    // Add one point at the begining and another at the end of the list to
    // ampliate the RT limits to cover the RT range completly
    try {//from  www . j  a  va 2  s.  c  om
        Collections.sort(list, new RTs());

        RTs firstPoint = list.get(0);
        RTs lastPoint = list.get(list.size() - 1);

        double min = Math.abs(firstPoint.RT - RTrange.getMin());

        double RTx = firstPoint.RT - min;
        double RTy = firstPoint.RT2 - min;

        RTs newPoint = new RTs(RTx, RTy);
        list.add(newPoint);

        double max = Math.abs(RTrange.getMin() - lastPoint.RT);
        RTx = lastPoint.RT + max;
        RTy = lastPoint.RT2 + max;

        newPoint = new RTs(RTx, RTy);
        list.add(newPoint);
    } catch (Exception exception) {
    }

    // Add points to the model in between of the real points to smooth the regression model
    Collections.sort(list, new RTs());

    for (int i = 0; i < list.size() - 1; i++) {
        RTs point1 = list.get(i);
        RTs point2 = list.get(i + 1);
        if (point1.RT < point2.RT - 2) {
            SimpleRegression regression = new SimpleRegression();
            regression.addData(point1.RT, point1.RT2);
            regression.addData(point2.RT, point2.RT2);
            double rt = point1.RT + 1;
            while (rt < point2.RT) {
                RTs newPoint = new RTs(rt, regression.predict(rt));
                list.add(newPoint);
                rt++;
            }

        }
    }

    return list;
}

From source file:guineu.modules.filter.Alignment.RANSACGCGC.RansacGCGCAlignerTask.java

private List<GCGCRTs> smooth(List<GCGCRTs> list, Range RTrange) {

    // Add one point at the begining and another at the end of the list to
    // ampliate the RT limits to cover the RT range completly
    try {/*from  ww w .j ava 2 s  .c om*/
        Collections.sort(list, new GCGCRTs());

        GCGCRTs firstPoint = list.get(0);
        GCGCRTs lastPoint = list.get(list.size() - 1);

        double min = Math.abs(firstPoint.RT - RTrange.getMin());

        double RTx = firstPoint.RT - min;
        double RTy = firstPoint.RT2 - min;

        GCGCRTs newPoint = new GCGCRTs(RTx, RTy);
        list.add(newPoint);

        double max = Math.abs(RTrange.getMin() - lastPoint.RT);
        RTx = lastPoint.RT + max;
        RTy = lastPoint.RT2 + max;

        newPoint = new GCGCRTs(RTx, RTy);
        list.add(newPoint);
    } catch (Exception exception) {
    }

    // Add points to the model in between of the real points to smooth the regression model
    Collections.sort(list, new GCGCRTs());

    for (int i = 0; i < list.size() - 1; i++) {
        GCGCRTs point1 = list.get(i);
        GCGCRTs point2 = list.get(i + 1);
        if (point1.RT < point2.RT - 2) {
            SimpleRegression regression = new SimpleRegression();
            regression.addData(point1.RT, point1.RT2);
            regression.addData(point2.RT, point2.RT2);
            double rt = point1.RT + 1;
            while (rt < point2.RT) {
                GCGCRTs newPoint = new GCGCRTs(rt, regression.predict(rt));
                list.add(newPoint);
                rt++;
            }

        }
    }

    return list;
}

From source file:io.github.msdk.features.ransacaligner.RansacAlignerMethod.java

private List<RTs> smooth(List<RTs> list) {
    // Add points to the model in between of the real points to smooth the
    // regression model
    Collections.sort(list, new RTs());

    for (int i = 0; i < list.size() - 1; i++) {
        RTs point1 = list.get(i);//  ww w  .  j  a v  a 2 s . c o m
        RTs point2 = list.get(i + 1);
        if (point1.RT < point2.RT - 2) {
            SimpleRegression regression = new SimpleRegression();
            regression.addData(point1.RT, point1.RT2);
            regression.addData(point2.RT, point2.RT2);
            double rt = point1.RT + 1;
            while (rt < point2.RT) {
                RTs newPoint = new RTs(rt, regression.predict(rt));
                list.add(newPoint);
                rt++;
            }

        }
    }

    return list;
}

From source file:com.joliciel.jochre.graphics.SourceImageImpl.java

/**
 * Returns the slope of the current image's horizontal inclination.
 * Assumes an initial stab has already been made at group shapes into rows,
 * and that rows are grouped from top to bottom.
 * @return//w  w  w  .  j a va 2  s. c o m
 */
public double getInclination() {
    LOG.debug("#### getInclination ####");
    // It may well be that rows have been grouped together
    // wrongly if the image has several columns.
    // The only rows that are fairly reliable are very long horizontal bars
    // and the first long row, in which all letters are aligned to the same baseline,
    // regardless of their size.

    // let's get the medium width first
    Mean widthMean = new Mean();
    for (RowOfShapes row : this.getRows()) {
        widthMean.increment(row.getRight() - row.getLeft());
    }
    double meanWidth = widthMean.getResult();
    LOG.debug("meanWidth: " + meanWidth);

    double minWidth = meanWidth * 0.75;

    // find the first row with a pretty wide width
    RowOfShapes theRow = null;
    for (RowOfShapes row : this.getRows()) {
        int width = row.getRight() - row.getLeft();
        if (width > minWidth) {
            theRow = row;
            break;
        }
    }

    // calculate a regression for average shapes on this row
    double minHeight = theRow.getAverageShapeHeight() - theRow.getAverageShapeHeightMargin();
    double maxHeight = theRow.getAverageShapeHeight() + theRow.getAverageShapeHeightMargin();
    SimpleRegression regression = new SimpleRegression();
    for (Shape shape : theRow.getShapes()) {
        if (shape.getHeight() >= minHeight && shape.getHeight() <= maxHeight) {
            for (int x = 0; x < shape.getWidth(); x++) {
                for (int y = 0; y < shape.getHeight(); y++) {
                    if (shape.isPixelBlack(x, y, this.getBlackThreshold())) {
                        regression.addData(shape.getLeft() + x, shape.getTop() + y);
                    }
                }
            }
        }
    }

    return regression.getSlope();
}

From source file:com.joliciel.jochre.graphics.RowOfShapesImpl.java

/**
 * The regression passes through the bottom of average shapes on this line.
 * It gives the line's slope, and a starting point for finding the baseline and meanline.
 *//*from w ww  .  j a  v a2  s. co m*/
public SimpleRegression getRegression() {
    if (this.regression == null) {
        // begin by calculating some sort of average line crossing the whole row, so that we can see if the row is
        // rising or falling to start with?
        // Calculate the line crossing the mid-point of all "average" shapes on this row
        // get the "smoothed" linear approximation of the mid-points
        regression = new SimpleRegression();

        int numShapes = 0;
        int minShapes = 10;
        DescriptiveStatistics shapeWidthStats = new DescriptiveStatistics();
        DescriptiveStatistics shapeHeightStats = new DescriptiveStatistics();

        for (Shape shape : this.getShapes()) {
            shapeWidthStats.addValue(shape.getWidth());
            shapeHeightStats.addValue(shape.getHeight());
        }

        double minWidth = shapeWidthStats.getPercentile(25);
        double maxWidth = shapeWidthStats.getPercentile(75);
        double minHeight = shapeHeightStats.getPercentile(25);
        double maxHeight = shapeHeightStats.getPercentile(75);

        for (Shape shape : this.getShapes()) {
            // only add points whose shape is of "average" width and height (to leave out commas, etc.)
            if (shape.getWidth() >= minWidth && shape.getWidth() <= maxWidth && shape.getHeight() >= minHeight
                    && shape.getHeight() <= maxHeight) {

                // using bottom only, since rows with different font sizes tend to align bottom
                regression.addData((((double) shape.getLeft() + (double) shape.getRight()) / 2.0),
                        ((double) shape.getBottom()));
                numShapes++;
            }
        }

        // special case where row contains very few shapes (generally letter or number + period)
        boolean horizontalLine = false;
        if (numShapes < minShapes) {
            LOG.debug("Too few shapes: " + numShapes + ", assuming straight horizontal line");
            horizontalLine = true;
        } else if ((this.getRight() - this.getLeft()) < (this.getContainer().getWidth() / 6.0)) {
            LOG.debug("Too narrow: " + (this.getRight() - this.getLeft())
                    + ", assuming straight horizontal line");
            horizontalLine = true;
        }
        if (horizontalLine) {
            // assume a straight horizontal line
            Mean midPointMean = new Mean();
            for (Shape shape : this.getShapes()) {
                // only add points whose shape is of "average" height (to leave out commas, etc.)
                if (shape.getWidth() >= minWidth && shape.getWidth() <= maxWidth
                        && shape.getHeight() >= minHeight && shape.getHeight() <= maxHeight) {
                    midPointMean.increment((double) shape.getBottom());
                }
            }
            if (midPointMean.getN() == 0) {
                for (Shape shape : this.getShapes()) {
                    midPointMean.increment((double) shape.getBottom());
                }
            }
            double meanMidPoint = midPointMean.getResult();
            regression = new SimpleRegression();
            regression.addData(this.getLeft(), meanMidPoint);
            regression.addData(this.getRight(), meanMidPoint);
        }

        // displays intercept of regression line
        LOG.debug("intercept: " + regression.getIntercept());

        // displays slope of regression line
        LOG.debug("slope: " + regression.getSlope());

        // displays slope standard error
        LOG.debug("std err: " + regression.getSlopeStdErr());

        LOG.debug("x = 0, y = " + regression.predict(0));
        LOG.debug("x = " + this.getContainer().getWidth() + ", y = "
                + regression.predict(this.getContainer().getWidth()));
    }
    return regression;
}

From source file:net.sf.mzmine.modules.peaklistmethods.identification.metamsecorrelate.MetaMSEcorrelateTask.java

/**
 * correlates one row to another/*from  www. j a va2  s  .  c  o  m*/
 * peak shape correlation (Pearson) (if negative or zero direct escape)
 *       otherwise avg(corr)>= minCorrPeakShape
 * intensity profile correlation (Pearson) >= minCorrIProfile
 *       TODO: maybe impute low values instead of 0 for not detected!
 * @param row
 * @param g
 * @return average correlation over both factors ranging from minR to 1 (or 0 if minimum correlation was not met)
 * @throws Exception 
 */
private double corrRowToRow(final PeakList peakList, final RawDataFile raw[], PeakListRow row, PeakListRow row2)
        throws Exception {
    double corr = 0;
    SimpleRegression reg = new SimpleRegression();
    // count
    int c = 0;
    // go through all raw files 
    for (int r = 0; r < raw.length; r++) {
        Feature f1 = row.getPeak(raw[r]);
        Feature f2 = row2.getPeak(raw[r]);
        if (f1 != null && f2 != null) {
            // peak shape correlation
            FeatureShapeCorrelationData cFS = corrFeatureShape(f1, f2, true);
            if (cFS != null) {
                double tmpcorr = cFS.getR();
                // escape if peak shapes are showing a negative correlation
                if (tmpcorr <= 0)
                    return tmpcorr;
                corr += tmpcorr;
                c++;
            } else {
                // correlation was not possible
                // maybe due to a small peak in this raw file
                // escape if features would be high enough for a correlation
                // this means the features are not intercepting
                if (countDPHigherThanNoise(f1) >= minCorrelatedDataPoints
                        && countDPHigherThanNoise(f2) >= minCorrelatedDataPoints)
                    return 0;
            }
        }
        // I profile correlation
        // TODO: low value imputation?
        double I1 = f1 != null ? f1.getHeight() : 0;
        double I2 = f2 != null ? f2.getHeight() : 0;
        reg.addData(I1, I2);
    }
    // First search for isotopes TODO later fill in isotopes from raw
    int absCharge = AlignedIsotopeGrouperTask.find13CIsotope(peakList, row, row2, maxCharge, mzTolerance);
    boolean isIsotope = absCharge != -1;
    // TODO search for adducts and add correlation: IProfile doesnt have to be the same for adducts
    boolean isAdduct = false;
    if (!isIsotope)
        findAdducts(peakList, row, row2, row.getRowCharge(), row2.getRowCharge());
    double adductBonus = (isIsotope || isAdduct) && useAdductBonusR ? adductBonusR : 0;
    // TODO weighting of intensity corr and feature shape corr
    // there was no correlation possible due to small peaks
    if (c == 0) {
        // 
        return isAdduct || isIsotope ? 1 : 0;
    } else {
        corr = (corr / c);
        double corrIprofile = reg.getR();
        if (corr + adductBonus < minShapeCorrR)
            return 0;
        else if (corrIprofile < minIntensityProfileR)
            return 0;
        else
            return (corr + corrIprofile) / 2;
    }
}

From source file:net.sf.mzmine.modules.peaklistmethods.identification.metamsecorrelate.MetaMSEcorrelateTask.java

/**
 * correlates the height profile of one row to another
 * NO escape routine/*w  w  w . j  a  v a 2s . c  o m*/
 * @param raw
 * @param row
 * @param g
 * @return Pearson r of height correlation
 */
public static double corrRowToRowIProfile(final RawDataFile raw[], PeakListRow row, PeakListRow g) {
    SimpleRegression reg = new SimpleRegression();
    // go through all raw files 
    for (int r = 0; r < raw.length; r++) {
        Feature f1 = row.getPeak(raw[r]);
        Feature f2 = g.getPeak(raw[r]);
        // I profile correlation
        // TODO: low value imputation?
        double I1 = f1 != null ? f1.getHeight() : 0;
        double I2 = f2 != null ? f2.getHeight() : 0;
        reg.addData(I1, I2);
    }
    // TODO weighting of intensity corr
    double corrIprofile = reg.getR();
    return corrIprofile;
}

From source file:net.sf.mzmine.modules.peaklistmethods.identification.metamsecorrelate.MetaMSEcorrelateTask.java

/**
 * feature shape correlation/*from   w  w  w. j ava2 s  .c om*/
 * @param f1
 * @param f2
 * @return feature shape correlation 
 * or null if not possible
 * not enough data points for a correlation
 * @throws Exception 
 */
public static FeatureShapeCorrelationData corrFeatureShape(Feature f1, Feature f2, boolean sameRawFile)
        throws Exception {
    //Range<Double> rt1 = f1.getRawDataPointsRTRange();
    //Range<Double> rt2 = f2.getRawDataPointsRTRange();
    if (sameRawFile) {
        // scan numbers (not necessary 1,2,3...)
        int[] sn1 = f1.getScanNumbers();
        int[] sn2 = f2.getScanNumbers();
        int offsetI1 = 0;
        int offsetI2 = 0;
        // find corresponding value
        if (sn2[0] > sn1[0]) {
            for (int i = 1; i < sn1.length; i++) {
                if (sn1[i] == sn2[0]) {
                    offsetI1 = i;
                    break;
                }
            }
            // peaks are not overlapping
            if (offsetI1 == 0)
                return null;
        }
        if (sn2[0] < sn1[0]) {
            for (int i = 1; i < sn2.length; i++) {
                if (sn1[0] == sn2[i]) {
                    offsetI2 = i;
                    break;
                }
            }
            // peaks are not overlapping
            if (offsetI2 == 0)
                return null;
        }
        // only correlate intercepting areas 0-max
        int max = 0;
        if (sn1.length - offsetI1 <= sn2.length - offsetI2)
            max = sn1.length - offsetI1;
        if (sn1.length - offsetI1 > sn2.length - offsetI2)
            max = sn2.length - offsetI2;
        if (max - offsetI1 > minCorrelatedDataPoints && max - offsetI2 > minCorrelatedDataPoints) {
            RawDataFile raw = f1.getDataFile();
            SimpleRegression reg = new SimpleRegression();
            // save max and min of intensity of val1(x)
            double maxX = 0;
            double minX = Double.POSITIVE_INFINITY;
            Vector<Double> I1 = new Vector<Double>();
            Vector<Double> I2 = new Vector<Double>();
            // add all data points over a given threshold
            // raw data (not smoothed)
            for (int i = 0; i < max; i++) {
                if (sn1[i + offsetI1] != sn2[i + offsetI2])
                    throw new Exception("Scans are not the same for peak shape corr");
                double val1 = f1.getDataPoint(sn1[i + offsetI1]).getIntensity();
                double val2 = f2.getDataPoint(sn2[i + offsetI2]).getIntensity();
                if (val1 >= noiseLevelShapeCorr && val2 >= noiseLevelShapeCorr) {
                    reg.addData(val1, val2);
                    if (val1 < minX)
                        minX = val1;
                    if (val1 > maxX)
                        maxX = val1;
                    I1.add(val1);
                    I2.add(val2);
                }
            }
            // return pearson r
            if (reg.getN() >= minCorrelatedDataPoints) {
                Double[][] data = new Double[][] { I1.toArray(new Double[I1.size()]),
                        I2.toArray(new Double[I2.size()]) };
                return new FeatureShapeCorrelationData(reg, data, minX, maxX);
            }
        }
    } else {
        // TODO if different raw file search for same rt
        // impute rt/I values if between 2 data points
    }
    return null;
}

From source file:org.noisemap.core.ProgressionOrbisGisManager.java

@Override
public void run() {
    while (enabled) {
        double progression = (getMainProgression() * 100);
        if (monitor != null) {
            monitor.progressTo((long) progression);
            if (monitor.isCancelled()) {
                break;
            }//www .  ja v a  2  s  .com
        }
        // Evaluate computation time and print it in console.
        if (progressionHistoryTime.isEmpty()) {
            if (progression > 0) {
                progressionHistoryTime.push(System.currentTimeMillis());
                progressionHistoryValue.push(progression);
            }
        } else {
            if (lastPushedProgress < System.currentTimeMillis() - historyTimeStep) {
                //reg.addData(progression,System.currentTimeMillis());
                lastPushedProgress = System.currentTimeMillis();
                if ((int) (progression - progressionHistoryValue.lastElement()) >= 1) {
                    progressionHistoryTime.push(System.currentTimeMillis());
                    progressionHistoryValue.push(progression);
                }
                //Estimate end of computation
                SimpleRegression reg = new SimpleRegression();
                double prog[][] = new double[progressionHistoryTime.size() + 1][2];
                for (int t = 0; t < progressionHistoryTime.size(); t++) {
                    prog[t][0] = progressionHistoryValue.get(t);
                    prog[t][1] = progressionHistoryTime.get(t);
                }
                prog[progressionHistoryTime.size()][0] = progression;
                prog[progressionHistoryTime.size()][1] = System.currentTimeMillis();
                reg.addData(prog);
                lastEstimation = reg.predict(100);
                //If estimation fails, use simple estimation
                if (lastEstimation < System.currentTimeMillis() && progressionHistoryTime.size() > 1) {
                    lastEstimation = System.currentTimeMillis()
                            + (System.currentTimeMillis() - progressionHistoryTime.get(1)) * 100 / progression;
                }
            }
        }

        //Round
        progression = (((int) (progression * 100000)) / 100000.);
        if (lastEstimation > 0) {
            System.out.println(progression + " % remaining "
                    + getHumanTime((long) lastEstimation - System.currentTimeMillis()));
        } else {
            System.out.println(progression + " %");
        }

        try {
            Thread.sleep(updateInterval);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:org.rascalmpl.library.analysis.statistics.SimpleRegressions.java

SimpleRegression make(IList dataValues) {
    if (dataValues.length() <= 2)
        throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null,
                "SimpleRegression data should have more than 2 elements");
    SimpleRegression simple = new SimpleRegression();
    for (IValue v : dataValues) {
        ITuple t = (ITuple) v;//from w  w w . jav a  2  s  . c  om
        INumber x = (INumber) t.get(0);
        INumber y = (INumber) t.get(1);
        simple.addData(x.toReal().doubleValue(), y.toReal().doubleValue());
    }
    return simple;
}