List of usage examples for org.apache.commons.lang3 ArrayUtils toPrimitive
public static boolean[] toPrimitive(final Boolean[] array)
Converts an array of object Booleans to primitives.
This method returns null for a null input array.
From source file:ubic.basecode.dataStructure.matrix.SparseRaggedDoubleMatrix.java
@Override public double[] getRow(int i) { return ArrayUtils.toPrimitive(matrix.get(i).toArray(new Double[] {})); }
From source file:ubic.basecode.io.ByteArrayConverter.java
/** * @param darray * @return byte[] */ public byte[] doubleArrayToBytes(Double[] darray) { return doubleArrayToBytes(ArrayUtils.toPrimitive(darray)); }
From source file:ubic.basecode.math.linearmodels.LeastSquaresFit.java
/** * Perform OLS when there might be missing values, for a single vector of data y. If y doesn't have any missing * values this works normally./*from w w w . ja va 2 s . co m*/ * * Has side effect of filling in this.qrs and this.residualDofs, so run this "in order". * * @param row * @param y the data to fit. For weighted ls, you must supply y*w * @param des the design matrix. For weighted ls, you must supply des*w. * @return the coefficients (a.k.a. x) */ private DoubleMatrix1D lsfWmissing(Integer row, DoubleMatrix1D y, DoubleMatrix2D des) { Algebra solver = new Algebra(); // This can potentially be improved by getting the indices of non-missing values and using that to make slices. List<Double> ywithoutMissingList = new ArrayList<>(y.size()); int size = y.size(); boolean hasAssign = !this.assign.isEmpty(); int countNonMissing = 0; for (int i = 0; i < size; i++) { double v = y.getQuick(i); if (!Double.isNaN(v) && !Double.isInfinite(v)) { countNonMissing++; } } if (countNonMissing < 3) { /* * return nothing. */ DoubleMatrix1D re = new DenseDoubleMatrix1D(des.columns()); re.assign(Double.NaN); log.debug("Not enough non-missing values"); this.addQR(row, null, null); this.residualDofs.add(countNonMissing - des.columns()); if (hasAssign) this.assigns.add(new ArrayList<Integer>()); return re; } double[][] rawDesignWithoutMissing = new double[countNonMissing][]; int index = 0; boolean missing = false; BitVector bv = new BitVector(size); for (int i = 0; i < size; i++) { double yi = y.getQuick(i); if (Double.isNaN(yi) || Double.isInfinite(yi)) { missing = true; continue; } ywithoutMissingList.add(yi); bv.set(i); rawDesignWithoutMissing[index++] = des.viewRow(i).toArray(); } double[] yWithoutMissing = ArrayUtils.toPrimitive(ywithoutMissingList.toArray(new Double[] {})); DenseDoubleMatrix2D yWithoutMissingAsMatrix = new DenseDoubleMatrix2D(new double[][] { yWithoutMissing }); DoubleMatrix2D designWithoutMissing = new DenseDoubleMatrix2D(rawDesignWithoutMissing); boolean fail = false; List<Integer> droppedColumns = new ArrayList<>(); designWithoutMissing = this.cleanDesign(designWithoutMissing, yWithoutMissingAsMatrix.size(), droppedColumns); if (designWithoutMissing.columns() == 0 || designWithoutMissing.columns() > designWithoutMissing.rows()) { fail = true; } if (fail) { DoubleMatrix1D re = new DenseDoubleMatrix1D(des.columns()); re.assign(Double.NaN); this.addQR(row, null, null); this.residualDofs.add(countNonMissing - des.columns()); if (hasAssign) this.assigns.add(new ArrayList<Integer>()); return re; } QRDecomposition rqr = null; if (this.weights != null) { rqr = new QRDecomposition(designWithoutMissing); addQR(row, null, rqr); } else if (missing) { rqr = this.getQR(bv); if (rqr == null) { rqr = new QRDecomposition(designWithoutMissing); addQR(row, bv, rqr); } } else { // in the case of weighted least squares, the Design matrix has different weights // for every row observation, so recompute qr everytime. if (this.qr == null) { rqr = new QRDecomposition(des); } else { // presumably not weighted.Why would this be set already, though? Is this ever reached? rqr = this.qr; } } this.addQR(row, bv, rqr); int pivots = rqr.getRank(); int rdof = yWithoutMissingAsMatrix.size() - pivots; this.residualDofs.add(rdof); DoubleMatrix2D coefs = rqr.solve(solver.transpose(yWithoutMissingAsMatrix)); /* * Put NaNs in for missing coefficients that were dropped from our estimation. */ if (designWithoutMissing.columns() < des.columns()) { DoubleMatrix1D col = coefs.viewColumn(0); DoubleMatrix1D result = new DenseDoubleMatrix1D(des.columns()); result.assign(Double.NaN); int k = 0; List<Integer> assignForRow = new ArrayList<>(); for (int i = 0; i < des.columns(); i++) { if (droppedColumns.contains(i)) { // leave it as NaN. continue; } if (hasAssign) assignForRow.add(this.assign.get(i)); assert k < col.size(); result.set(i, col.get(k)); k++; } if (hasAssign) assigns.add(assignForRow); return result; } if (hasAssign) assigns.add(this.assign); return coefs.viewColumn(0); }
From source file:ubic.basecode.math.linearmodels.MeanVarianceEstimator.java
/** * First ensures that x values are strictly increasing and performs a loess fit afterwards. The loess fit are * determined by <code>BANDWIDTH</code> and <code>ROBUSTNESS_ITERS</code>. * //from ww w. j a v a2 s . c om * @param xy * @return loessFit or null if there are less than 3 data points */ private DoubleMatrix2D loessFit(DoubleMatrix2D xy) { assert xy != null; DoubleMatrix1D sx = xy.viewColumn(0); DoubleMatrix1D sy = xy.viewColumn(1); Map<Double, Double> map = new TreeMap<>(); for (int i = 0; i < sx.size(); i++) { if (Double.isNaN(sx.get(i)) || Double.isInfinite(sx.get(i)) || Double.isNaN(sy.get(i)) || Double.isInfinite(sy.get(i))) { continue; } map.put(sx.get(i), sy.get(i)); } DoubleMatrix2D xyChecked = new DenseDoubleMatrix2D(map.size(), 2); xyChecked.viewColumn(0).assign(ArrayUtils.toPrimitive(map.keySet().toArray(new Double[0]))); xyChecked.viewColumn(1).assign(ArrayUtils.toPrimitive(map.values().toArray(new Double[0]))); // in R: // loess(c(1:5),c(1:5)^2,f=0.5,iter=3) // Note: we start to loose some precision here in comparison with R's loess FIXME why? does it matter? DoubleMatrix2D loessFit = new DenseDoubleMatrix2D(xyChecked.rows(), xyChecked.columns()); // try { // fit a loess curve LoessInterpolator loessInterpolator = new LoessInterpolator(MeanVarianceEstimator.BANDWIDTH, MeanVarianceEstimator.ROBUSTNESS_ITERS); double[] loessY = loessInterpolator.smooth(xyChecked.viewColumn(0).toArray(), xyChecked.viewColumn(1).toArray()); loessFit.viewColumn(0).assign(xyChecked.viewColumn(0)); loessFit.viewColumn(1).assign(loessY); return loessFit; }
From source file:ubic.basecode.math.linearmodels.ModeratedTstatTest.java
@Test public void testLimma() throws Exception { DoubleMatrixReader f = new DoubleMatrixReader(); DoubleMatrix<String, String> testMatrix = f .read(this.getClass().getResourceAsStream("/data/limmatest.data.txt")); StringMatrixReader of = new StringMatrixReader(); StringMatrix<String, String> sampleInfo = of .read(this.getClass().getResourceAsStream("/data/limmatest.design.txt")); DesignMatrix d = new DesignMatrix(sampleInfo, true); LeastSquaresFit fit = new LeastSquaresFit(d, testMatrix); Map<String, LinearModelSummary> sums = fit.summarizeByKeys(true); assertEquals(100, sums.size());// w w w .j a v a 2s .c o m for (LinearModelSummary lms : sums.values()) { GenericAnovaResult a = lms.getAnova(); assertNotNull(a); } LinearModelSummary s = sums.get("Gene 1"); assertNotNull(s); assertEquals(0.2264, s.getContrastCoefficients().get(0, 0), 0.001); // f = new DoubleMatrixReader(); DoubleMatrix<String, String> expectedEffects = f .read(this.getClass().getResourceAsStream("/data/limmatest.fit.effects.txt")); double[] expEffects1 = expectedEffects.getRowByName("Gene 1"); Double[] effects = ArrayUtils.subarray(s.getEffects(), 0, 2); assertArrayEquals(ArrayUtils.subarray(expEffects1, 0, 2), ArrayUtils.toPrimitive(effects), 1e-7); Double[] stdevUnscaled = s.getStdevUnscaled(); // assertEquals(0.5773502692, stdevUnscaled[0], 1e-8); assertEquals(0.8164965809, stdevUnscaled[1], 1e-8); Double sigma = s.getSigma(); assertEquals(0.3069360050, sigma, 0.0001); ModeratedTstat.ebayes(fit); DoubleMatrix1D squeezedVars = fit.getVarPost(); DoubleMatrix<String, String> expectedvars = f .read(this.getClass().getResourceAsStream("/data/limmatest.fit.squeezevar.txt")); assertArrayEquals(expectedvars.viewColumn(0).toArray(), squeezedVars.toArray(), 1e-7); sums = fit.summarizeByKeys(true); }
From source file:ubic.basecode.util.r.AbstractRClient.java
@Override public String dataFrame(ObjectMatrix<String, String, Object> matrix) { /*/*from w ww . j ava 2 s .co m*/ * Extract columns, convert */ List<String> colNames = matrix.getColNames(); List<String> rowNames = matrix.getRowNames(); assert colNames.size() == matrix.columns(); String colV = assignStringList(colNames); String rowV = assignStringList(rowNames); List<String> terms = new ArrayList<String>(); for (int j = 0; j < colNames.size(); j++) { Object[] column; Object v = matrix.getEntry(0, j); if (v instanceof Number) { column = new Double[matrix.rows()]; } else if (v instanceof Boolean) { column = new String[matrix.rows()]; } else if (v instanceof String) { column = new String[matrix.rows()]; } else { throw new IllegalArgumentException( "Sorry, can't make data frame from values of type " + v.getClass().getName()); } for (int i = 0; i < matrix.rows(); i++) { Object value = matrix.getEntry(i, j); if (matrix.isMissing(i, j)) { column[i] = null; } else if (value instanceof Number) { column[i] = ((Number) value).doubleValue(); } else if (value instanceof Boolean) { column[i] = ((Boolean) value) ? "T" : "F"; } else if (value instanceof String) { column[i] = value; } } if (v instanceof Number) { assign(colNames.get(j), ArrayUtils.toPrimitive((Double[]) column)); terms.add(colNames.get(j)); } else if (v instanceof Boolean) { assignFactor(colNames.get(j), Arrays.asList((String[]) column)); terms.add(colNames.get(j)); } else if (v instanceof String) { assignFactor(colNames.get(j), Arrays.asList((String[]) column)); terms.add(colNames.get(j)); } } String varName = "df." + variableIdentityNumber(matrix); String command = varName + "<-data.frame(" + StringUtils.join(terms, ",") + ", row.names=" + rowV + " )"; log.debug(command); eval(command); eval("names(" + varName + ")<-" + colV); return varName; }
From source file:ubic.basecode.util.r.AbstractRClient.java
@Override @SuppressWarnings({ "unchecked" }) public LinearModelSummary linearModel(double[] data, Map<String, List<?>> factors) { String datName = RandomStringUtils.randomAlphabetic(10); assign(datName, data);// w ww . j a va2 s. com for (String factorName : factors.keySet()) { List<?> list = factors.get(factorName); if (list.iterator().next() instanceof String) { assignFactor(factorName, (List<String>) list); } else { // treat is as a numeric covariate List<Double> d = new ArrayList<Double>(); for (Object object : list) { d.add((Double) object); } assign(factorName, ArrayUtils.toPrimitive(d.toArray(new Double[] {}))); } } String modelDeclaration = datName + " ~ " + StringUtils.join(factors.keySet(), "+"); String lmName = RandomStringUtils.randomAlphabetic(10); String command = lmName + "<-lm(" + modelDeclaration + ", na.action=na.exclude)"; log.debug(command); voidEval(command); REXP lmsum = eval("summary(" + lmName + ")"); REXP anova = eval("anova(" + lmName + ")"); return new LinearModelSummary(lmsum, anova, factors.keySet().toArray(new String[] {})); }
From source file:ubic.gemma.core.analysis.expression.diff.AbstractDifferentialExpressionAnalyzer.java
/** * @param pvalues pvalues/*from ww w . j ava2 s .co m*/ * @return Qvalues, or null if they could not be computed. */ double[] benjaminiHochberg(Double[] pvalues) { DoubleMatrix1D benjaminiHochberg = MultipleTestCorrection .benjaminiHochberg(new DenseDoubleMatrix1D(ArrayUtils.toPrimitive(pvalues))); if (benjaminiHochberg == null) { return null; } return benjaminiHochberg.toArray(); }
From source file:ubic.gemma.core.analysis.expression.diff.LinearModelAnalyzer.java
/** * Fill in the ranks and qvalues in the results. * * @param pvaluesForQvalue Map of factorName to results. */// www . j a v a2 s . co m private void getRanksAndQvalues( Map<String, ? extends Collection<DifferentialExpressionAnalysisResult>> resultLists, Map<String, List<Double>> pvaluesForQvalue) { /* * qvalues and ranks, requires second pass over the result objects. */ for (String fName : pvaluesForQvalue.keySet()) { Collection<Double> pvals = pvaluesForQvalue.get(fName); if (pvals.isEmpty()) { LinearModelAnalyzer.log.warn("No pvalues for " + fName + ", ignoring."); continue; } LinearModelAnalyzer.log.info(pvals.size() + " pvalues for " + fName); Double[] pvalArray = pvals.toArray(new Double[] {}); for (Double aPvalArray : pvalArray) { assert aPvalArray != null; assert !Double.isNaN(aPvalArray); } double[] ranks = super.computeRanks(ArrayUtils.toPrimitive(pvalArray)); if (ranks == null) { LinearModelAnalyzer.log.error("Ranks could not be computed " + fName); continue; } assert pvalArray.length == resultLists.get(fName).size() : pvalArray.length + " != " + resultLists.get(fName).size(); assert pvalArray.length == ranks.length; double[] qvalues = super.benjaminiHochberg(pvalArray); assert qvalues.length == resultLists.get(fName).size(); int i = 0; for (DifferentialExpressionAnalysisResult pr : resultLists.get(fName)) { pr.setCorrectedPvalue(this.nan2Null(qvalues[i])); pr.setRank(this.nan2Null(ranks[i])); i++; } } }
From source file:ubic.gemma.core.analysis.preprocess.ProcessedExpressionDataVectorCreateHelperServiceImpl.java
private DoubleArrayList getRanks(ExpressionDataDoubleMatrix intensities, ProcessedExpressionDataVectorDao.RankMethod method) { ProcessedExpressionDataVectorCreateHelperServiceImpl.log.debug("Getting ranks"); assert intensities != null; DoubleArrayList result = new DoubleArrayList(intensities.rows()); for (ExpressionDataMatrixRowElement de : intensities.getRowElements()) { double[] rowObj = ArrayUtils.toPrimitive(intensities.getRow(de.getDesignElement())); double valueForRank = Double.MIN_VALUE; if (rowObj != null) { DoubleArrayList row = new DoubleArrayList(rowObj); switch (method) { case max: valueForRank = DescriptiveWithMissing.max(row); break; case mean: valueForRank = DescriptiveWithMissing.mean(row); break; default: throw new UnsupportedOperationException(); }/* w w w.j a v a2s. c o m*/ } result.add(valueForRank); } return Rank.rankTransform(result); }