List of usage examples for org.apache.commons.lang ArrayUtils toPrimitive
public static boolean[] toPrimitive(Boolean[] array)
Converts an array of object Booleans to primitives.
From source file:tools.descartes.wcf.forecasting.AbstractRForecaster.java
@Override public final IForecastResult forecast(final int numForecastSteps) { if (rBridge == null) return null; final ITimeSeries<Double> history = this.getTsOriginal(); final String varNameValues = RServerBridgeControl.uniqueVarname(); final String varNameModel = RServerBridgeControl.uniqueVarname(); final String varNameForecast = RServerBridgeControl.uniqueVarname(); final double[] values; List<Double> allHistory = new ArrayList<Double>(history.getValues()); // remove NullValues only if strategy is not "Croston" (forecasting for intermitted demands) if (strategy == ForecastStrategyEnum.CROST) { Double[] histValues = new Double[allHistory.size()]; histValues = allHistory.toArray(histValues); values = ArrayUtils.toPrimitive(histValues); } else {//from ww w. jav a 2 s . co m Double[] histValuesNotNull = removeNullValues(allHistory); values = ArrayUtils.toPrimitive(histValuesNotNull); } /* * 0. Assign values to temporal variable */ long startFC = System.currentTimeMillis(); AbstractRForecaster.rBridge.assign(varNameValues, values); //frequency for time series object in R --> needed for MASE calculation. AbstractRForecaster.rBridge.toTS(varNameValues, history.getFrequency()); /* * 1. Compute stochastic model for forecast */ if (this.modelFunc == null) { // In this case, the values are the model ... AbstractRForecaster.rBridge.assign(varNameModel, values); AbstractRForecaster.rBridge.toTS(varNameModel, history.getFrequency()); } else { final String[] additionalModelParams = this.getModelFuncParams(); StringBuffer params = new StringBuffer(); params.append(varNameValues); if (null != additionalModelParams) { for (String param : additionalModelParams) { params.append(","); params.append(param); } } AbstractRForecaster.rBridge.e(String.format("%s<<-%s(%s)", varNameModel, this.modelFunc, params)); } /* * 2. Perform forecast based on stochastic model */ if (this.getConfidenceLevel() == 0) { AbstractRForecaster.rBridge.e(String.format("%s<<-%s(%s,h=%d)", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps)); } else { AbstractRForecaster.rBridge.e(String.format("%s<<-%s(%s,h=%d,level=c(%d))", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps, this.getConfidenceLevel())); } /* * 3. Calculate Forecast Quality Metric */ double fcQuality; if (this.modelFunc == null || this.strategy == ForecastStrategyEnum.TBATS) { fcQuality = AbstractRForecaster.rBridge.eDbl("accuracy(" + varNameForecast + ")[6]"); } else { fcQuality = AbstractRForecaster.rBridge.eDbl("accuracy(" + varNameModel + ")[6]"); } final double[] lowerValues = AbstractRForecaster.rBridge.eDblArr(lowerOperationOnResult(varNameForecast)); final double[] forecastValues = AbstractRForecaster.rBridge .eDblArr(forecastOperationOnResult(varNameForecast)); final double[] upperValues = AbstractRForecaster.rBridge.eDblArr(upperOperationOnResult(varNameForecast)); // remove temporal variable: AbstractRForecaster.rBridge.e(String.format("rm(%s)", varNameModel)); AbstractRForecaster.rBridge.e(String.format("rm(%s)", varNameValues)); AbstractRForecaster.rBridge.e(String.format("rm(%s)", varNameForecast)); long endFC = System.currentTimeMillis(); ITimeSeries<Double> tsForecast = this.prepareForecastTS(); ITimeSeries<Double> tsLower; ITimeSeries<Double> tsUpper; tsForecast.appendAll(ArrayUtils.toObject(forecastValues)); if (this.getConfidenceLevel() == 0) { tsLower = tsForecast; tsUpper = tsForecast; } else { tsLower = this.prepareForecastTS(); tsLower.appendAll(ArrayUtils.toObject(lowerValues)); tsUpper = this.prepareForecastTS(); tsUpper.appendAll(ArrayUtils.toObject(upperValues)); } return new ForecastResult(tsForecast, this.getTsOriginal(), this.getConfidenceLevel(), fcQuality, tsLower, tsUpper, strategy, endFC - startFC); }
From source file:treeNormalizer.structure.internal.treeBucketNode.java
/** * ermittelt alle Kinder des Knotens, welche node sind * * @param node der zu suchende Knoten//www . ja va2 s .c om * @return eine Liste der Kinderpositionen */ public int[] findChilds(treeBucketNode node) { List<Integer> list = new ArrayList<>(); if (node == null) { return new int[] {}; } ArrayList<treeBucketNode> list2 = getChilds(); for (int i = 0; i < list2.size(); i++) { if (list2.get(i) == null) { continue; } if (node.getId() == list2.get(i).getId()) { list.add(i); } } return ArrayUtils.toPrimitive(list.toArray(new Integer[list.size()])); }
From source file:ubc.pavlab.aspiredb.server.service.BurdenAnalysisServiceImpl.java
/** * @param statName//from w w w. j a v a2 s .c o m * @param patientIds * @return double[] array of the statName values for all of the patientIds */ private double[] getPatientStats(CnvBurdenAnalysisPerSubject statName, Collection<String> patientIds, Map<String, Map<CnvBurdenAnalysisPerSubject, Double>> patientIdStats) { Collection<Double> result = new ArrayList<>(); for (String patientId : patientIds) { Double val = patientIdStats.get(patientId).get(statName); if (val == null) { continue; } result.add(val); } return ArrayUtils.toPrimitive(result.toArray(new Double[0])); }
From source file:ubc.pavlab.aspiredb.server.service.BurdenAnalysisServiceImpl.java
@SuppressWarnings("boxing") private double[] removeNaNs(double[] d) { Collection<Double> ret = new ArrayList<>(); for (int i = 0; i < d.length; i++) { if (Double.isInfinite(d[i]) || Double.isNaN(d[i])) { continue; }/* ww w.j av a 2 s. c o m*/ ret.add(d[i]); } return ArrayUtils.toPrimitive(ret.toArray(new Double[ret.size()])); }
From source file:ubic.gemma.analysis.expression.coexpression.Gene2GenePopulationServiceImpl.java
/** * @param queryGene//from w w w . java 2 s .c o m * @param usedLinks number of links we stored for this gene. If < 0, this will not be used. * @param expressionExperiments * @return true if node degree was computed. * @see completeNodeDegreeComputations(boolean) for the final computation. */ private boolean computeNodeDegree(Gene queryGene, int usedLinks, Collection<? extends BioAssaySet> expressionExperiments) { Map<BioAssaySet, Double> nodeDegrees = geneService.getGeneCoexpressionNodeDegree(queryGene, expressionExperiments); /* * The values are relative ranks based on probes. The ranks are "per data set". We assume these are uniform * under the null, like pvalues. There are a lot of ties, but this really shouldn't matter. I */ if (nodeDegrees == null || nodeDegrees.isEmpty()) return false; GeneCoexpressionNodeDegree n = GeneCoexpressionNodeDegree.Factory.newInstance(); n.setGene(queryGene); n.setNumTests(nodeDegrees.size()); if (usedLinks >= 0) n.setNumLinks(usedLinks); // we populate the rank later. double[] array = ArrayUtils.toPrimitive(nodeDegrees.values().toArray(new Double[] {})); DoubleArrayList vals = new DoubleArrayList(array); /* * Because everything is rank transformed, we use the median and mad. Later we re-rank everything anyway. We * compute a p-value based on Fisher's method (see above about assuming the ranks are uniform under the null). * This is a bit screwey, because we're going to have a lot of really bad p-values. The final distribution of * pvalues is really messed up. But we're going to just use ranks anyway. */ double pvalue = MetaAnalysis.fisherCombinePvalues(vals); n.setPvalue(pvalue); n.setMedian(Descriptive.median(vals)); n.setMedianDeviation(DescriptiveWithMissing.mad(vals)); String distribution = computeDistribution(vals); n.setDistribution(distribution); // so we can compute summary stats at the end. this.allGeneNodeDegrees.put(queryGene, n); return true; }
From source file:ubic.gemma.analysis.expression.diff.AbstractDifferentialExpressionAnalyzer.java
/** * @param pvalues//w w w . j av a 2s . co m * @return Qvalues, or null if they could not be computed. */ protected 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.analysis.expression.diff.LinearModelAnalyzer.java
/** * Fill in the ranks and qvalues in the results. * // w w w.ja v a 2 s. c om * @param resultLists * @param pvaluesForQvalue Map of factorName to results. */ 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()) { log.warn("No pvalues for " + fName + ", ignoring."); continue; } log.info(pvals.size() + " pvalues for " + fName); Double[] pvalArray = pvals.toArray(new Double[] {}); for (int i = 0; i < pvalArray.length; i++) { assert pvalArray[i] != null; assert !Double.isNaN(pvalArray[i]); // if ( Double.isNaN( pvalArray[i] ) ) { // log.warn( "Pvalue was NaN" ); // } // // if ( pvalArray[i] == null ) { // log.warn( "Pvalue was null" ); // pvalArray[i] = Double.NaN; // } } double[] ranks = super.computeRanks(ArrayUtils.toPrimitive(pvalArray)); if (ranks == null) { 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(); if (qvalues == null) { log.warn("Corrected pvalues could not be computed for " + fName); continue; } int i = 0; for (DifferentialExpressionAnalysisResult pr : resultLists.get(fName)) { pr.setCorrectedPvalue(nan2Null(qvalues[i])); pr.setRank(nan2Null(ranks[i])); i++; } } }
From source file:ubic.gemma.analysis.preprocess.ProcessedExpressionDataVectorCreateHelperServiceImpl.java
/** * @param intensities/*from w w w. j a va 2s. co m*/ * @return */ private DoubleArrayList getRanks(ExpressionDataDoubleMatrix intensities, ProcessedExpressionDataVectorDao.RankMethod method) { log.debug("Getting ranks"); 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; } } result.add(valueForRank); } return Rank.rankTransform(result); }
From source file:ubic.gemma.analysis.preprocess.TwoChannelMissingValuesImpl.java
/** * Attempt to compute 'missing value' information for a two-channel data set. We attempt to do this even if we are * missing background intensity information or one intensity channel, though obviously it is better to have all four * sets of values.//from w w w .ja va2 s . c o m * * @param source * @param preferred * @param signalChannelA * @param signalChannelB * @param bkgChannelA * @param bkgChannelB * @param signalToNoiseThreshold * @param extraMissingValueIndicators * @return DesignElementDataVectors corresponding to a new PRESENTCALL quantitation type for the design elements and * biomaterial dimension represented in the inputs. * @see computeMissingValues( ExpressionExperiment expExp, double signalToNoiseThreshold ) */ protected Collection<RawExpressionDataVector> computeMissingValues(ExpressionExperiment source, ExpressionDataDoubleMatrix preferred, ExpressionDataDoubleMatrix signalChannelA, ExpressionDataDoubleMatrix signalChannelB, ExpressionDataDoubleMatrix bkgChannelA, ExpressionDataDoubleMatrix bkgChannelB, double signalToNoiseThreshold, Collection<Double> extraMissingValueIndicators) { boolean okToProceed = validate(preferred, signalChannelA, signalChannelB, bkgChannelA, bkgChannelB, signalToNoiseThreshold); Collection<RawExpressionDataVector> results = new HashSet<RawExpressionDataVector>(); if (!okToProceed) { log.warn("Missing value computation cannot proceed"); return results; } ByteArrayConverter converter = new ByteArrayConverter(); int count = 0; ExpressionDataDoubleMatrix baseChannel = signalChannelA == null ? signalChannelB : signalChannelA; Double signalThreshold = Double.NaN; if (bkgChannelA == null && bkgChannelB == null) { signalThreshold = computeSignalThreshold(preferred, signalChannelA, signalChannelB, baseChannel); } QuantitationType present = getMissingDataQuantitationType(signalToNoiseThreshold, signalThreshold); source.getQuantitationTypes().add(present); for (ExpressionDataMatrixRowElement element : baseChannel.getRowElements()) { CompositeSequence designElement = element.getDesignElement(); RawExpressionDataVector vect = RawExpressionDataVector.Factory.newInstance(); vect.setQuantitationType(present); vect.setExpressionExperiment(source); vect.setDesignElement(designElement); assert baseChannel != null; vect.setBioAssayDimension(baseChannel.getBioAssayDimension(designElement)); int numCols = preferred.columns(designElement); Boolean[] detectionCalls = new Boolean[numCols]; Double[] prefRow = preferred.getRow(designElement); Double[] signalA = null; if (signalChannelA != null) { signalA = signalChannelA.getRow(designElement); } Double[] signalB = null; if (signalChannelB != null) { signalB = signalChannelB.getRow(designElement); } Double[] bkgA = null; Double[] bkgB = null; if (bkgChannelA != null) bkgA = bkgChannelA.getRow(designElement); if (bkgChannelB != null) bkgB = bkgChannelB.getRow(designElement); // columns only for this designelement! boolean gaps = false; // we use this to track for (int col = 0; col < numCols; col++) { // If the "preferred" value is already missing, we retain that, or if it is a special value Double pref = prefRow == null ? Double.NaN : prefRow[col]; if (pref == null || pref.isNaN() || (extraMissingValueIndicators != null && extraMissingValueIndicators.contains(pref))) { detectionCalls[col] = false; continue; } Double bkgAV = Double.NaN; Double bkgBV = Double.NaN; if (bkgA != null) bkgAV = bkgA[col]; if (bkgB != null) bkgBV = bkgB[col]; Double sigAV = (signalA == null || signalA[col] == null) ? Double.NaN : signalA[col]; Double sigBV = (signalB == null || signalB[col] == null) ? Double.NaN : signalB[col]; /* * Missing values here wreak havoc. Sometimes in multiarray studies data are missing. */ Boolean call = computeCall(signalToNoiseThreshold, signalThreshold, sigAV, sigBV, bkgAV, bkgBV); if (call == null) gaps = true; detectionCalls[col] = call; } if (gaps) { fillGapsInCalls(detectionCalls); } vect.setData(converter.booleanArrayToBytes(ArrayUtils.toPrimitive(detectionCalls))); results.add(vect); if (++count % 4000 == 0) { log.info(count + " vectors examined for missing values, " + results.size() + " vectors generated so far."); } } log.info("Finished: " + count + " vectors examined for missing values"); results = twoChannelMissingValueHelperService.persist(source, results); return results; }
From source file:ubic.gemma.analysis.service.ExpressionDataMatrixServiceImpl.java
@Override public DenseDoubleMatrix<Gene, ExpressionExperiment> getRankMatrix(Collection<Gene> genes, Collection<ExpressionExperiment> ees, ProcessedExpressionDataVectorDao.RankMethod method) { DenseDoubleMatrix<Gene, ExpressionExperiment> matrix = new DenseDoubleMatrix<Gene, ExpressionExperiment>( genes.size(), ees.size());/* w w w. j a v a2 s. c o m*/ Map<ExpressionExperiment, Map<Gene, Collection<Double>>> ranks = processedExpressionDataVectorService .getRanks(ees, genes, method); matrix.setRowNames(new ArrayList<Gene>(genes)); matrix.setColumnNames(new ArrayList<ExpressionExperiment>(ees)); for (int i = 0; i < matrix.rows(); i++) { for (int j = 0; j < matrix.columns(); j++) { matrix.setByKeys(matrix.getRowName(i), matrix.getColName(j), Double.NaN); } } for (Gene g : matrix.getRowNames()) { for (ExpressionExperiment e : matrix.getColNames()) { if (ranks.containsKey(e)) { Collection<Double> r = ranks.get(e).get(g); // compute median of collection. Double[] ar = new Double[r.size()]; r.toArray(ar); double[] dar = ArrayUtils.toPrimitive(ar); double medianRank = Descriptive.median(new DoubleArrayList(dar)); matrix.setByKeys(g, e, medianRank); } } } return matrix; }