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

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

Introduction

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

Prototype

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

Source Link

Document

Converts an array of primitive booleans to objects.

Usage

From source file:ubic.gemma.persistence.service.expression.experiment.GeeqServiceImpl.java

private void scoreOutliers(Geeq gq, DoubleMatrix<BioAssay, BioAssay> cormat) {
    double score;
    boolean hasCorrMat = true;
    boolean hasNaNs = false;
    boolean outliers = true;

    if (cormat == null || cormat.rows() == 0) {
        hasCorrMat = false;//from  ww w  .  j av  a2s . c o m
    } else {
        // Check if cormat has NaNs (diagonal is not checked, but there really should not be NaNs on the diagonal)
        Double[] doubleArray = ArrayUtils.toObject(this.getLowerTriangle(cormat.getRawMatrix()));
        List<Double> list = new ArrayList<>(Arrays.asList(doubleArray));
        hasNaNs = list.contains(Double.NaN);

        outliers = outlierDetectionService.identifyOutliersByMedianCorrelation(cormat).size() > 0;
    }

    score = outliers ? GeeqServiceImpl.N_10 : GeeqServiceImpl.P_10; //
    gq.setCorrMatIssues((byte) (!hasCorrMat ? 1 : hasNaNs ? 2 : 0));
    gq.setqScoreOutliers(score);
}

From source file:ubic.gemma.persistence.service.expression.experiment.GeeqServiceImpl.java

private double[] getLowerTriCormat(DoubleMatrix<BioAssay, BioAssay> cormat) {
    if (cormat == null || cormat.rows() == 0) {
        return new double[] {};
    }/*  www  .j  ava 2 s. co m*/
    double[] corTri = this.getLowerTriangle(cormat.getRawMatrix());

    // We have to remove NaNs, some cormats have them (we notify user about this in the outlier score)
    // this is not very efficient, but the DoubleMatrix does not have a method to get an array of Doubles (not doubles)
    Double[] doubleArray = ArrayUtils.toObject(corTri);
    List<Double> list = new ArrayList<>(Arrays.asList(doubleArray));
    //noinspection StatementWithEmptyBody // because java standard libraries suck, we have to iterate like this to remove all NaNs, not just the first one.
    while (list.remove(Double.NaN)) {
    }

    return ArrayUtils.toPrimitive(list.toArray(new Double[0]));
}

From source file:uk.ac.diamond.scisoft.analysis.rcp.inspector.InspectionTab.java

private void pushImages(final IMonitor monitor, final Slice[] slices, final int[] order) {
    // work out slicing result
    final int[] shape = dataset.getShape();

    //      System.err.printf("Shape: %s; slicing: [%s]; order: %s\n", Arrays.toString(shape), Slice.createString(slices), Arrays.toString(order));

    int rank = shape.length;
    int ns = slices.length;
    // dimensions for iterating over (order.length == slices.length)
    int ids = ns > 3 ? 2 : 1;

    final List<Integer> gridDimNumber = new ArrayList<Integer>();
    final int[] gridShape = new int[ids];
    for (int i = 0; i < ids; i++) {
        // After dimension reordering, first two dimensions should be an image size
        // followed by grid dimensions
        int o = order[i + 2];
        gridDimNumber.add(o);// w  ww .j  ava 2s.com
        gridShape[i] = slices[o].getNumSteps();
    }

    try {
        //         System.err.printf("Grid: %s\n", Arrays.toString(gridShape));
        if (ids == 1) {
            SDAPlotter.setupNewImageGrid(explorerName, gridShape[0]);
        } else {
            SDAPlotter.setupNewImageGrid(explorerName, gridShape[1], gridShape[0]);
        }
    } catch (Exception e) {
        logger.warn("Problem with setting up image explorer", e);
    }

    // use position iterator ignoring first set of slicing axes
    int[] start = new int[rank];
    int[] stop = new int[rank];
    int[] step = new int[rank];
    Slice.convertFromSlice(slices, shape, start, stop, step);

    List<Integer> ignoreAxesList = new ArrayList<Integer>(Arrays.asList(ArrayUtils.toObject(order)));
    ignoreAxesList.removeAll(gridDimNumber);
    int[] ignoreAxes = ArrayUtils.toPrimitive(ignoreAxesList.toArray(new Integer[0]));
    PositionIterator it = new PositionIterator(shape, start.clone(), stop.clone(), step, ignoreAxes);
    int[] pos = it.getPos();

    int dimGridX = gridDimNumber.get(0);
    int gridX0 = start[dimGridX];
    int dimGridY = (ids == 1) ? -1 : gridDimNumber.get(1);
    int gridY0 = (ids == 1) ? -1 : start[dimGridY];

    try {
        setInspectionRunning();
        boolean memoryOK = true;
        while (!memoryOK || it.hasNext()) { // short-cut iteration when low on memory
            try {
                for (int i = 0; i < ids; i++) {
                    int o = gridDimNumber.get(i);
                    int b = pos[o];
                    start[o] = b;
                    stop[o] = b + 1;
                }
                Dataset slicedData = sliceData(monitor, start, stop, step);
                if (slicedData == null)
                    return;
                //               System.err.printf("Pos %s; start %s; stop %s; step %s; ", Arrays.toString(pos), Arrays.toString(start), Arrays.toString(stop), Arrays.toString(step));
                //               System.err.printf("Shape %s\n", Arrays.toString(slicedData.getShape()));

                Dataset reorderedData = slicedData.getTransposedView(order);

                reorderedData.setName(slicedData.getName());
                reorderedData.squeeze();
                if (reorderedData.getSize() < 1)
                    return;

                reorderedData.setName(dataset.getName() + Arrays.toString(pos));
                if (!canContinueInspection()) {
                    return;
                }

                if (explorer.isStopped()) {
                    stopInspection();
                    return;
                }

                if (ids == 1) {
                    SDAPlotter.plotImageToGrid(explorerName, reorderedData, true);
                } else {
                    int gridX = pos[dimGridX] - gridX0;
                    int gridY = pos[dimGridY] - gridY0;
                    SDAPlotter.plotImageToGrid(explorerName, reorderedData, gridX, gridY, true);
                }

                if (!memoryOK)
                    logger.warn("... memory reduction successful");
                memoryOK = true;
            } catch (OutOfMemoryError e) {
                if (!memoryOK) // only allow one GC per slice
                    throw e;
                memoryOK = false;
                logger.warn("Ran out of memory: attempting to reduce memory used...");
                System.gc();
                // try again after memory reduction
            }
        }
    } catch (Exception e) {
        logger.warn("Problem with sending data to image explorer", e);
    } finally {
        stopInspection();
    }
}

From source file:uk.ac.diamond.scisoft.analysis.rpc.flattening.helpers.GuiBeanHelper.java

/**
 * We know the intended type, so in many cases we can upgrade from flattened to the expected type e.g. Object[] to
 * List<Object>.//ww w .j a  v  a2s  . c o m
 * 
 * @param params
 * @param valueObj
 * @return upgraded valueObj, or the original if it matches
 */
private Serializable unflattenParam(GuiParameters params, Serializable valueObj) {
    Class<?> clazz = params.getStorageClass();
    if (clazz.isInstance(valueObj)) {
        return valueObj;
    } else if (clazz.equals(List.class)) {
        return new ArrayList<Object>(Arrays.asList((Object[]) valueObj));
    } else if (clazz.equals(Integer[].class)) {
        return ArrayUtils.toObject((int[]) valueObj);
    } else if (clazz.equals(GuiPlotMode.class)) {
        return GuiPlotMode.valueOf((String) valueObj);
    } else if (clazz.equals(UUID.class)) {
        return UUID.fromString(valueObj.toString());
    }
    return valueObj;
}

From source file:uk.ac.diamond.scisoft.ncd.actors.NcdNexusTreeTransformer.java

private void writeNxsFile(String filePath, List<DataMessageComponent> cache, DataMessageComponent comp)
        throws NullPointerException, HDF5Exception {

    final List<IDataset> sets = MessageUtils.getDatasets(cache);
    Dataset data = (Dataset) sets.get(0);

    // add frame dimension
    int[] newShape = ArrayUtils.addAll(new int[] { 1, 1 }, data.getShape());
    data = data.reshape(newShape);//  w  w  w .  j  a  va 2 s.  c o  m

    int fapl = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS);
    H5.H5Pset_fclose_degree(fapl, HDF5Constants.H5F_CLOSE_STRONG);
    int nxsfile_handle = H5.H5Fcreate(filePath, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, fapl);
    H5.H5Pclose(fapl);
    int[] libversion = new int[3];
    H5.H5get_libversion(libversion);
    putattr(nxsfile_handle, "HDF5_version", StringUtils.join(ArrayUtils.toObject(libversion), "."));
    putattr(nxsfile_handle, "file_name", filePath);

    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String dt = format.format(date);
    putattr(nxsfile_handle, "file_time", dt);

    int entry_group_id = makegroup(nxsfile_handle, "entry1", "NXentry");

    int instrument_group_id = makegroup(entry_group_id, "instrument", "NXinstrument");
    int detector_group_id = makegroup(instrument_group_id, detector, "NXdetector");

    int datatype = -1;
    if (data instanceof ShortDataset) {
        datatype = HDF5Constants.H5T_NATIVE_SHORT;
    } else if (data instanceof IntegerDataset) {
        datatype = HDF5Constants.H5T_NATIVE_INT;
    } else if (data instanceof LongDataset) {
        datatype = HDF5Constants.H5T_NATIVE_LONG;
    } else if (data instanceof FloatDataset) {
        datatype = HDF5Constants.H5T_NATIVE_FLOAT;
    } else if (data instanceof DoubleDataset) {
        datatype = HDF5Constants.H5T_NATIVE_DOUBLE;
    }

    int input_data_id = makedata(detector_group_id, "data", datatype, data);
    putattr(input_data_id, "signal", 1);
    putattr(input_data_id, "units", "counts");
    make_sas_type(detector_group_id);

    int link_group_id = makegroup(entry_group_id, detector, "NXdata");
    H5.H5Lcreate_hard(detector_group_id, "./data", link_group_id, "./data", HDF5Constants.H5P_DEFAULT,
            HDF5Constants.H5P_DEFAULT);

    H5.H5Dclose(input_data_id);
    H5.H5Gclose(detector_group_id);
    H5.H5Gclose(instrument_group_id);
    H5.H5Gclose(link_group_id);
    H5.H5Gclose(entry_group_id);
    H5.H5Fclose(nxsfile_handle);
}

From source file:uk.ac.diamond.scisoft.ncd.core.utils.NcdDataUtils.java

/**
 * Method for generating all combination of elements supplied in the input array
 * //from w  ww. j av a2 s.c om
 * @param input
 *          Array containing arrays of elements.
 *          Resulting combinations will pick single element from every input array. 
 * @return
 *          Array containing all combinations of the input elements with only one element
 *          picked from every input array at a time.
 */
public static ArrayList<int[]> generateCombinations(final ArrayList<int[]> input) {
    ArrayList<int[]> result = new ArrayList<int[]>();
    @SuppressWarnings("unchecked")
    ArrayList<int[]> tmpInput = (ArrayList<int[]>) input.clone();
    int[] first = tmpInput.remove(0);
    for (int i = 0; i < first.length; i++) {
        if (!tmpInput.isEmpty()) {
            ArrayList<int[]> recursive = generateCombinations(tmpInput);
            for (int j = 0; j < recursive.size(); j++) {
                ArrayList<Integer> tmp = new ArrayList<Integer>();
                tmp.add(first[i]);
                tmp.addAll(Arrays.asList(ArrayUtils.toObject(recursive.get(j))));
                result.add(ArrayUtils.toPrimitive(tmp.toArray(new Integer[] {})));
            }
        } else {
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            tmp.add(first[i]);
            result.add(ArrayUtils.toPrimitive(tmp.toArray(new Integer[] {})));
        }
    }
    return result;
}

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.service.DataReductionServiceImpl.java

private String createResultsFile(IDataReductionContext context, String inputfileName, String inputfilePath,
        String prefix) {/*from  w  ww  . j  av  a2 s .c  o  m*/

    String datetime = generateDateTimeStamp();
    String detNames = "_" + ((context.isEnableWaxs()) ? context.getWaxsDetectorName() : "")
            + ((context.isEnableSaxs()) ? context.getSaxsDetectorName() : "") + "_";
    final String filename = context.getWorkingDir() + File.separator + prefix + "_"
            + FilenameUtils.getBaseName(inputfileName) + detNames + datetime + ".nxs";

    int fid = -1;
    int entry_id = -1;
    try {
        int fapl = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS);
        H5.H5Pset_fclose_degree(fapl, HDF5Constants.H5F_CLOSE_WEAK);
        fid = H5.H5Fcreate(filename, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, fapl);
        H5.H5Pclose(fapl);

        int[] libversion = new int[3];
        H5.H5get_libversion(libversion);
        putattr(fid, "HDF5_version", StringUtils.join(ArrayUtils.toObject(libversion), "."));
        putattr(fid, "file_name", filename);

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String dt = format.format(date);
        putattr(fid, "file_time", dt);

        entry_id = NcdNexusUtils.makegroup(fid, "entry1", Nexus.ENTRY);

        final String calibration = context.getCalibrationName();
        if (calibration != null) {
            int calib_id = NcdNexusUtils.makegroup(entry_id, calibration, Nexus.DATA);
            H5.H5Lcreate_external(inputfilePath, "/entry1/" + calibration + "/data", calib_id, "data",
                    HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
            H5.H5Gclose(calib_id);
        }

        writeNCDMetadata(entry_id, inputfilePath);

        createInstrumentNode(entry_id, inputfilePath);

        if (context.isEnableWaxs()) {
            createDetectorNode(context.getWaxsDetectorName(), entry_id, inputfilePath);
        }

        if (context.isEnableSaxs()) {
            createDetectorNode(context.getSaxsDetectorName(), entry_id, inputfilePath);
        }

    } catch (HDF5Exception e) {
        throw new RuntimeException("Failed to create results NeXus file", e);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Invalind NeXus link path in the input data file", e);
    } finally {
        try {
            NcdNexusUtils.closeH5id(entry_id);
        } catch (Exception e2) {
            logger.error("Failed to close entry group in the NeXus results file", e2);
        } finally {
            try {
                NcdNexusUtils.closeH5id(fid);
            } catch (Exception e3) {
                logger.error("Failed to close reference id of the NeXus results file", e3);
            }
        }
    }
    return filename;
}

From source file:uk.ac.diamond.scisoft.xpdf.operations.XPDFAzimuthalIntegrationOperation.java

@Override
protected OperationData process(IDataset input, IMonitor monitor) throws OperationException {

    // get the XPDF metadata
    XPDFMetadata xMeta = input.getFirstMetadata(XPDFMetadata.class);

    // create the Operation that will do the processing
    AzimuthalPixelIntegrationOperation<XPDFAzimuthalIntegrationInternalModel> internalOperation = new AzimuthalPixelIntegrationOperation<XPDFAzimuthalIntegrationInternalModel>();

    // create the fake model that will supply the parameters to the internal Operation
    XPDFAzimuthalIntegrationInternalModel internalModel = new XPDFAzimuthalIntegrationInternalModel(
            model.getAzimuthalRange(), model.getRadialRange(), model.isPixelSplitting(),
            model.getNumberOfBins());// w  ww  . j  a  v a2s.  c o  m

    internalOperation.setModel(internalModel);

    // Resize the data to be more like it was in the execute() method
    SliceFromSeriesMetadata ssm = getSliceSeriesMetadata(input);
    // Get the dimensions in the original data
    int[] dataDims = ssm.getDataDimensions();
    int maxDimension = Collections.max(Arrays.asList(ArrayUtils.toObject(dataDims)));
    int[] newShape = new int[maxDimension + 1];
    int[] oldShape = input.getShape();
    // All elements of the new shape are 1
    Arrays.fill(newShape, 1);
    // except those that are in the data
    for (int dim = 0; dim < oldShape.length; dim++)
        newShape[dataDims[dim]] = oldShape[dim];

    IDataset reshapedInput = input.clone();
    // The reshaped data is now (hopefully) the same shape as input was in this.execute()
    reshapedInput.resize(newShape);

    OperationData output = internalOperation.execute(reshapedInput, monitor);

    // Set the axis coordinate to q
    if (xMeta != null) {
        xMeta.getSample().getTrace().setAxisAngle(false);
    }

    output.getData().addMetadata(xMeta);

    return output;

    //      OperationData superResult = super.process(input, monitor);
    //
    //      // The downstream processing relies on metadata to tell if the axis is 2 or q
    //      if (xMeta != null) {
    //         
    //         if (model.getAxisType() != XAxis.ANGLE)
    //            xMeta.getSample().getTrace().setAxisAngle(false);
    //      
    //         superResult.getData().setMetadata(xMeta);
    //      }
    //      return superResult;
}

From source file:uk.ac.diamond.scisoft.xpdf.views.SampleParametersTest.java

@Test
public void testSetGetDimensions() {
    testSetGetComposition();/*from   www  . j a  v  a 2 s . c o m*/
    cap.setShape("cylinder");
    double[] dims = new double[] { 0.15, 0.16 };
    cap.setDimensions(dims);
    assertEquals("New dimensions not set/got correctly", Arrays.asList(ArrayUtils.toObject(dims)),
            Arrays.asList(ArrayUtils.toObject(cap.getDimensions())));
}

From source file:uk.gov.gchq.gaffer.sketches.datasketches.sampling.function.aggregate.ReservoirLongUnionAggregatorTest.java

@Test
public void testAggregate() {
    final ReservoirLongsUnionAggregator unionAggregator = new ReservoirLongsUnionAggregator();
    unionAggregator.init();//from w w  w .j a v  a  2s. c  om

    unionAggregator._aggregate(union1);
    ReservoirLongsSketch currentState = unionAggregator._state().getResult();
    assertEquals(3L, currentState.getN());
    assertEquals(3, currentState.getNumSamples());
    // As less items have been added than the capacity, the sample should exactly match what was added.
    Set<Long> samples = new HashSet<>(Arrays.asList(ArrayUtils.toObject(currentState.getSamples())));
    Set<Long> expectedSamples = new HashSet<>();
    expectedSamples.add(1L);
    expectedSamples.add(2L);
    expectedSamples.add(3L);
    assertEquals(expectedSamples, samples);

    unionAggregator._aggregate(union2);
    currentState = unionAggregator._state().getResult();
    assertEquals(99L, currentState.getN());
    assertEquals(20L, currentState.getNumSamples());
    // As more items have been added than the capacity, we can't know exactly what items will be present
    // in the sample but we can check that they are all from the set of things we added.
    samples = new HashSet<>(Arrays.asList(ArrayUtils.toObject(currentState.getSamples())));
    for (long l = 4L; l < 100; l++) {
        expectedSamples.add(l);
    }
    assertTrue(expectedSamples.containsAll(samples));
}