Example usage for org.apache.commons.lang ArrayUtils toPrimitive

List of usage examples for org.apache.commons.lang ArrayUtils toPrimitive

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils toPrimitive.

Prototype

public static boolean[] toPrimitive(Boolean[] array) 

Source Link

Document

Converts an array of object Booleans to primitives.

Usage

From source file:dkpro.similarity.experiments.wordpairs.io.SemanticRelatednessResultWriter.java

private String getCorrelations(Result result) {
    Map<String, Double> pearsonCorrelationMap = new HashMap<String, Double>();
    Map<String, Double> spearmanCorrelationMap = new HashMap<String, Double>();
    List<Double> goldList = result.getGoldList();
    for (String measure : result.getMeasures()) {
        List<Double> scoreList = result.getScoreList(measure);
        double pearsonCorrelation = new PearsonsCorrelation().correlation(
                ArrayUtils.toPrimitive(goldList.toArray(new Double[goldList.size()])),
                ArrayUtils.toPrimitive(scoreList.toArray(new Double[goldList.size()])));
        pearsonCorrelationMap.put(measure, pearsonCorrelation);

        double spearmanCorrelation = new SpearmansCorrelation().correlation(
                ArrayUtils.toPrimitive(goldList.toArray(new Double[goldList.size()])),
                ArrayUtils.toPrimitive(scoreList.toArray(new Double[goldList.size()])));
        spearmanCorrelationMap.put(measure, spearmanCorrelation);
    }/*from w  ww .  j  a v  a 2  s .co  m*/
    StringBuilder sb = new StringBuilder();
    sb.append("Pearson: ");
    for (String measure : result.getMeasures()) {
        sb.append(pearsonCorrelationMap.get(measure));
        sb.append(SEP);
    }
    sb.append(LF);
    sb.append("Spearman: ");
    for (String measure : result.getMeasures()) {
        sb.append(spearmanCorrelationMap.get(measure));
        sb.append(SEP);
    }

    return sb.toString();
}

From source file:com.intel.daal.spark.rdd.DistributedNumericTable.java

/**
 * Transforms a well-formed JavaDoubleRDD into a DistributedNumericTable that is backed
 * by HomogenNumericTables, with specified number of columns.
 * @param rdd - The source JavaDoubleRDD.
 * @param ncols - Number of columns in the result DistributedNumericTable.
 * @return A DistributedNumericTable./* w w w .  j  ava  2  s .c  o m*/
 * @throws IllegalArgumentException.
 */
public static DistributedNumericTable fromJavaDoubleRDD(final JavaDoubleRDD rdd, final int ncols)
        throws IllegalArgumentException {
    long count = rdd.count();
    if (count / ncols * ncols != count) {
        throw new IllegalArgumentException("Invalid NumericTable dimensions");
    }
    JavaRDD<List<Double>> arrays = rdd.glom();
    JavaRDD<Vector> vecrdd = arrays.flatMap(new FlatMapFunction<List<Double>, Vector>() {
        public List<Vector> call(List<Double> dlist) {
            int blocksize = dlist.size();
            Double[] array = dlist.toArray(new Double[blocksize]);
            final double[] unboxed = ArrayUtils.toPrimitive(array);
            ArrayList<Vector> veclist = new ArrayList<Vector>();
            long begin = 0;
            long end = ncols;
            while (begin < unboxed.length) {
                double[] row = ArrayUtils.subarray(unboxed, (int) begin, (int) end);
                DenseVector dv = new DenseVector(row);
                veclist.add(dv);
                begin = end;
                end += ncols;
            }

            return veclist;
        }
    });

    return fromJavaVectorRDD(vecrdd, 0);
}

From source file:com.flexive.war.beans.admin.main.UserGroupBean.java

/**
 * Updates a existing group./*from w  w  w .j  a v  a  2 s  .  co m*/
 *
 * @return the next page to render
 */
public String update() {
    try {
        // Update group data
        getUserGroupEngine().update(currentData.id, currentData.name, currentData.color);
        // Update the role assignments
        try {
            getUserGroupEngine().setRoles(this.currentData.id, ArrayUtils.toPrimitive(getRoles()));
            new FxFacesMsgInfo("UserGroup.nfo.updated", currentData.name).addToContext();
        } catch (Exception exc) {
            new FxFacesMsgErr(exc).addToContext();
        }
        // Reload data
        edit();
    } catch (Exception exc) {
        new FxFacesMsgErr(exc).addToContext();
    }
    return "userGroupEdit";
}

From source file:dk.dma.ais.view.rest.AisStoreResource.java

@GET
@Path("/history/raw")
@Produces("application/octet-stream")
public StreamingOutput historyRaw(@Context UriInfo info, @QueryParam("mmsi") List<Integer> mmsis) {
    Iterable<AisPacket> query = getHistory(info,
            ArrayUtils.toPrimitive(mmsis.toArray(new Integer[mmsis.size()])));
    return StreamingUtil.createStreamingOutput(query, AisPacketOutputSinks.OUTPUT_TO_TEXT);
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.singlecell.explore.EnclosingBallController.java

public void plotYTBalls(TrackDataHolder trackDataHolder) {
    int selectedIndexEpsilon = exploreTrackController.getExploreTrackPanel().getEnclosingBallEpsCombobox()
            .getSelectedIndex();/*from  w  ww .  ja v a 2  s.c  om*/
    List<List<EnclosingBall>> ytEnclosingBallList = trackDataHolder.getStepCentricDataHolder()
            .getyTEnclosingBalls();
    List<EnclosingBall> yTempBalls = ytEnclosingBallList.get(selectedIndexEpsilon);
    // get the track coordinates matrix and transpose it
    Double[][] transpose2DArray = AnalysisUtils
            .transpose2DArray(trackDataHolder.getStepCentricDataHolder().getCoordinatesMatrix());
    // we get the y coordinates and the time information
    double[] yCoordinates = ArrayUtils.toPrimitive(AnalysisUtils.excludeNullValues(transpose2DArray[1]));
    double[] timeIndexes = trackDataHolder.getStepCentricDataHolder().getTimeIndexes();
    // we create the series and set its key
    XYSeries ytSeries = JFreeChartUtils.generateXYSeries(timeIndexes, yCoordinates);
    String seriesKey = "track " + trackDataHolder.getTrack().getTrackNumber() + ", well "
            + trackDataHolder.getTrack().getWellHasImagingType().getWell();
    ytSeries.setKey(seriesKey);
    // we then create the XYSeriesCollection and use it to make a new line chart
    XYSeriesCollection ytSeriesCollection = new XYSeriesCollection(ytSeries);
    JFreeChart chart = ChartFactory.createXYLineChart(seriesKey + " - enclosing balls", "time", "y (m)",
            ytSeriesCollection, PlotOrientation.VERTICAL, false, true, false);
    XYPlot xyPlot = chart.getXYPlot();
    xyPlot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    NumberAxis yaxis = (NumberAxis) xyPlot.getRangeAxis();
    yaxis.setAutoRangeIncludesZero(false);
    JFreeChartUtils.setupXYPlot(xyPlot);
    JFreeChartUtils.setupSingleTrackPlot(chart,
            exploreTrackController.getExploreTrackPanel().getTracksList().getSelectedIndex(), true);
    yTBallsChartPanel.setChart(chart);
    yTempBalls.stream().forEach((ball) -> {
        xyPlot.addAnnotation(new XYShapeAnnotation(ball.getShape(), JFreeChartUtils.getDashedLine(),
                GuiUtils.getDefaultColor()));
    });
}

From source file:dk.dma.ais.view.rest.AisStoreResource.java

@GET
@Path("/history/html")
@Produces("text/html")
public StreamingOutput historyHtml(@Context UriInfo info, @QueryParam("mmsi") List<Integer> mmsis) {
    Iterable<AisPacket> query = getHistory(info,
            ArrayUtils.toPrimitive(mmsis.toArray(new Integer[mmsis.size()])));
    return StreamingUtil.createStreamingOutput(query, AisPacketOutputSinks.OUTPUT_TO_HTML);
}

From source file:com.compomics.cell_coord.gui.controller.computation.ComputationDataController.java

/**
 *
 * @param track/*w  ww  .  j  a  v  a  2s .com*/
 */
private void plotAngles(Track track) {
    double[] values = ArrayUtils.toPrimitive(ComputationUtils.excludeNullValues(track.getAngles()));
    HistogramDataset histogramDataset = new HistogramDataset();
    histogramDataset.addSeries("", values, 5);
    String title = "angles for track: " + track.getTrackid();
    JFreeChart jFreeChart = ChartFactory.createHistogram(title, "angle", "count", histogramDataset,
            PlotOrientation.VERTICAL, true, true, true);
    ChartPanel chartPanel = new ChartPanel(jFreeChart);
    computationDataPanel.getAnglePlotPanel().removeAll();
    computationDataPanel.getAnglePlotPanel().add(chartPanel, gridBagConstraints);
    computationDataPanel.getAnglePlotPanel().revalidate();
    computationDataPanel.getAnglePlotPanel().repaint();
}

From source file:de.dfki.madm.anomalydetection.operator.statistical_based.RobustPCAOperator.java

@Override
public void doWork() throws OperatorException {
    // check whether all attributes are numerical
    ExampleSet exampleSet = exampleSetInput.getData(ExampleSet.class);

    Tools.onlyNonMissingValues(exampleSet, "PCA");
    Tools.onlyNumericalAttributes(exampleSet, "PCA");

    // Get normal probability.
    double normProb = getParameterAsDouble(PARAMETER_OUTLIER_PROBABILITY);
    int olInst = exampleSet.size() - (int) Math.floor(exampleSet.size() * normProb);
    log("Ignoring " + olInst + " anomalyous instances for robustness.");

    // The robust estimate is based on removing top outliers first based on Mahalanobis distance (MD).
    // Since MD is the same as the outlier score when using all PCs, the PCA is done twice:
    // First with all examples, second with top-outliers removed (robust)

    // First PCA for outlier removal
    // create covariance matrix
    Matrix covarianceMatrix = CovarianceMatrix.getCovarianceMatrix(exampleSet);

    // EigenVector and EigenValues of the covariance matrix
    EigenvalueDecomposition eigenvalueDecomposition = covarianceMatrix.eig();

    // create and deliver results
    double[] eigenvalues = eigenvalueDecomposition.getRealEigenvalues();
    Matrix eigenvectorMatrix = eigenvalueDecomposition.getV();
    double[][] eigenvectors = eigenvectorMatrix.getArray();

    PCAModel model = new PCAModel(exampleSet, eigenvalues, eigenvectors);

    // Perform transformation
    ExampleSet res = model.apply((ExampleSet) exampleSet.clone());

    // Compute simple list with MDs and sort according to MD.
    List<double[]> l = new LinkedList<double[]>();
    double eIdx = 0;
    for (Example example : res) {
        double md = 0.0;
        int aNr = 0;
        for (Attribute attr : example.getAttributes()) {
            double pcscore = example.getValue(attr);
            md += (pcscore * pcscore) / model.getEigenvalue(aNr);
            aNr++;//  w ww . ja v a 2s .co m
        }
        double[] x = { md, eIdx };
        l.add(x);
        eIdx++;
    }
    Collections.sort(l, new Comparator<double[]>() {
        public int compare(double[] first, double[] second) {
            return Double.compare(second[0], first[0]);
        }
    });
    // Out of the list, create array with outlier-indexes and array (mapping) with good instances. 
    Iterator<double[]> iter = l.iterator();
    int[] olMapping = new int[olInst];
    for (int i = 0; i < olInst; i++) {
        olMapping[i] = (int) ((double[]) iter.next())[1];
    }
    Arrays.sort(olMapping);
    int[] mapping = new int[exampleSet.size() - olInst];
    int olc = 0;
    int ctr = 0;
    for (int i = 0; i < exampleSet.size(); i++) {
        if (olc == olInst) { // Add last elements after last outlier
            mapping[ctr++] = i;
            continue;
        }
        if (olMapping[olc] != i) {
            mapping[ctr++] = i;
        } else {
            olc++;
        }
    }
    ExampleSet robustExampleSet = new MappedExampleSet(exampleSet, mapping); // creates a new example set without the top outliers.

    // ---
    // Second PCA (robust)
    covarianceMatrix = CovarianceMatrix.getCovarianceMatrix(robustExampleSet);
    eigenvalueDecomposition = covarianceMatrix.eig();

    // create and deliver results
    eigenvalues = eigenvalueDecomposition.getRealEigenvalues();
    eigenvectorMatrix = eigenvalueDecomposition.getV();
    eigenvectors = eigenvectorMatrix.getArray();

    // Apply on original set
    model = new PCAModel(exampleSet, eigenvalues, eigenvectors);

    // Perform transformation
    res = model.apply((ExampleSet) exampleSet.clone());

    // Sort eigenvalues
    Arrays.sort(eigenvalues);
    ArrayUtils.reverse(eigenvalues);

    // if necessary reduce nbr of dimensions ...
    int reductionType = getParameterAsInt(PARAMETER_REDUCTION_TYPE);
    List<Integer> pcList = new ArrayList<Integer>();
    if (reductionType == PCS_ALL) {
        for (int i = 0; i < exampleSet.getAttributes().size(); i++) {
            pcList.add(i);
        }
    }
    if (reductionType == PCS_TOP || reductionType == PCS_BOTH) {
        //top
        switch (getParameterAsInt(PARAMETER_TOP_METHODS)) {
        case PCS_TOP_FIX:
            for (int i = 0; i < getParameterAsInt(PARAMETER_NUMBER_OF_COMPONENTS_TOP); i++) {
                pcList.add(i);
            }
            break;
        case PCS_TOP_VAR:
            double var = getParameterAsDouble(PARAMETER_VARIANCE_THRESHOLD);
            boolean last = false;
            for (int i = 0; i < exampleSet.getAttributes().size(); i++) {
                if (model.getCumulativeVariance(i) < var) {
                    pcList.add(i);
                } else if (!last) { // we need to add another PC to meet the minimum requirement.
                    last = true;
                    pcList.add(i);
                }
            }
            break;
        }
    }
    if (reductionType == PCS_LOWER || reductionType == PCS_BOTH) {
        //lower
        switch (getParameterAsInt(PARAMETER_LOW_METHODS)) {
        case PCS_LOW_FIX:
            for (int i = exampleSet.getAttributes().size()
                    - getParameterAsInt(PARAMETER_NUMBER_OF_COMPONENTS_LOW); i < exampleSet.getAttributes()
                            .size(); i++) {
                pcList.add(i);
            }
            break;
        case PCS_LOW_VAL:
            double val = getParameterAsDouble(PARAMETER_VALUE_THRESHOLD);
            for (int i = 0; i < eigenvalues.length; i++) {
                if (eigenvalues[i] <= val) {
                    if (pcList.size() == 0) {
                        pcList.add(i);
                    } else if (pcList.get(pcList.size() - 1).intValue() < i) {
                        pcList.add(i);
                    }
                }
            }
            break;
        }
    }
    int[] opcs = ArrayUtils.toPrimitive(pcList.toArray(new Integer[pcList.size()]));

    if (opcs.length == 0) {
        throw new UserError(this,
                "Parameters thresholds are selected such that they did not match any principal component. Lower variance or increase eigenvalue threshold.");
    }
    if (opcs.length == exampleSet.getAttributes().size()) {
        log("Using all PCs for score.");
    } else {
        log("Using following PCs for score: " + Arrays.toString(opcs));
    }

    // Normalize by Chi-Dist with d degrees of freedom
    double scoreNormalizer = 1.0;
    ChiSquaredDistributionImpl chi = new ChiSquaredDistributionImpl(opcs.length);
    try {
        scoreNormalizer = chi.inverseCumulativeProbability(normProb);
    } catch (MathException e) {
        System.err.println(e);
    }
    log("Normalizing score with chi cumulative propability: " + scoreNormalizer);

    // compute scores
    Attribute scoreAttr = AttributeFactory.createAttribute("outlier", Ontology.REAL);
    exampleSet.getExampleTable().addAttribute(scoreAttr);
    exampleSet.getAttributes().setOutlier(scoreAttr);
    for (int exNr = 0; exNr < exampleSet.size(); exNr++) {
        Example orig = exampleSet.getExample(exNr);
        Example pc = res.getExample(exNr);
        double oscore = 0.0;
        int aNr = 0;
        ctr = 0;
        for (Attribute attr : pc.getAttributes()) {
            if (ctr < opcs.length && opcs[ctr] != aNr) { // we skip this dimension
                aNr++;
                continue;
            }
            double pcscore = pc.getValue(attr);
            oscore += (pcscore * pcscore) / model.getEigenvalue(aNr);
            aNr++;
            ctr++;
        }
        orig.setValue(scoreAttr, oscore / scoreNormalizer);
    }
    exampleSetOutput.deliver(exampleSet);
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private void processDDI(BufferedInputStream ddiStream, SDIOMetadata smd) throws IOException {
    XMLStreamReader xmlr = null;/*from   www  .  jav a2s. c o  m*/
    try {
        xmlr = xmlInputFactory.createXMLStreamReader(ddiStream);
        //processDDI( xmlr, smd );
        xmlr.nextTag();
        xmlr.require(XMLStreamConstants.START_ELEMENT, null, "codeBook");
        processCodeBook(xmlr, smd);
        dbgLog.info("processed DDI.");

    } catch (XMLStreamException ex) {
        Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new IOException(ex.getMessage());
    } finally {
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (XMLStreamException ex) {
            // The message in the exception should contain diagnostics
            // information -- what was wrong with the DDI, etc.
            throw new IOException(ex.getMessage());
        }
        if (ddiStream != null) {
            ddiStream.close();
        }
    }

    // Having processed the entire ddi, we should have obtained all the metadata
    // describing the data set.
    // Configure the SMD metadata object:

    if (getVarQnty() > 0) {
        smd.getFileInformation().put("varQnty", getVarQnty());
        dbgLog.info("var quantity: " + getVarQnty());
        // TODO:
        // Validate the value against the actual number of variable sections
        // found in the DDI.
    } else {
        throw new IOException("Failed to obtain the variable quantity from the DDI supplied.");
    }

    if (getCaseQnty() > 0) {
        smd.getFileInformation().put("caseQnty", getCaseQnty());
    }
    // It's ok if caseQnty was not defined in the DDI, we'll try to read
    // the tab file supplied and assume that the number of lines is the
    // number of observations.

    smd.setVariableName(variableNameList.toArray(new String[variableNameList.size()]));

    // "minimal" variable types: SPSS type binary definition:
    // 0 means numeric, >0 means string.

    smd.setVariableTypeMinimal(
            ArrayUtils.toPrimitive(variableTypeList.toArray(new Integer[variableTypeList.size()])));

    // This is how the "discrete" and "continuous" numeric values are
    // distinguished in the data set metadata:

    smd.setDecimalVariables(decimalVariableSet);

    //TODO: smd.getFileInformation().put("caseWeightVariableName", caseWeightVariableName);

    smd.setVariableFormat(printFormatList);
    smd.setVariableFormatName(printFormatNameTable);
    smd.setVariableFormatCategory(formatCategoryTable); //TODO: verify

    // Store the variable labels, if supplied:

    if (!variableLabelMap.isEmpty()) {
        smd.setVariableLabel(variableLabelMap);
    }

    // Value labels, if supplied:

    if (!valueLabelTable.isEmpty()) {
        smd.setValueLabelTable(valueLabelTable);
        smd.setValueLabelMappingTable(valueVariableMappingTable);
    }

    // And missing values:

    if (!missingValueTable.isEmpty()) {
        smd.setMissingValueTable(missingValueTable);
    }

}

From source file:dk.dma.ais.view.rest.AisStoreResource.java

@GET
@Path("/history/prefixed")
@Produces("application/octet-stream")
public StreamingOutput historyPrefixed(@Context UriInfo info, @QueryParam("mmsi") List<Integer> mmsis) {
    Iterable<AisPacket> query = getHistory(info,
            ArrayUtils.toPrimitive(mmsis.toArray(new Integer[mmsis.size()])));
    return StreamingUtil.createStreamingOutput(query, AisPacketOutputSinks.OUTPUT_PREFIXED_SENTENCES);
}