Example usage for org.apache.commons.beanutils ConvertUtils convert

List of usage examples for org.apache.commons.beanutils ConvertUtils convert

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConvertUtils convert.

Prototype

public static Object convert(String values[], Class clazz) 

Source Link

Document

Convert an array of specified values to an array of objects of the specified class (if possible).

For more details see ConvertUtilsBean.

Usage

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.forkjoin.NcdSectorIntegrationForkJoinTransformer.java

@Override
protected long[] getResultDataShape() {
    int[] areaShape = (int[]) ConvertUtils
            .convert(Arrays.copyOfRange(frames, frames.length - dimension, frames.length), int[].class);
    areaData = ROIProfile.area(areaShape, Dataset.FLOAT32, mask, intSector, doRadial, doAzimuthal, doFast);

    int areaDataRank = areaData[0].getRank();
    int[] areaDataShape = areaData[0].getShape();
    int secRank = frames.length - dimension + 1;
    long[] secFrames = Arrays.copyOf(frames, secRank);
    secFrames[secRank - 1] = areaDataShape[areaDataRank - 1];
    return secFrames;
}

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.forkjoin.NcdSectorIntegrationForkJoinTransformer.java

@Override
protected void writeAxisData() throws HDF5Exception {
    Dataset qaxis = calculateQaxisDataset();
    long[] qaxisShape = (long[]) ConvertUtils.convert(qaxis.getShape(), long[].class);

    UnitFormat unitFormat = UnitFormat.getUCUMInstance();
    String units = "a.u.";
    String axisName = "indecies";
    if (axisUnit != null) {
        units = unitFormat.format(axisUnit);
        axisName = "q";
    }//ww  w .  jav  a2s .  co  m
    int[] qaxisRank = new int[] { qaxis.getRank() };
    resultAxisDataID = NcdNexusUtils.makeaxis(resultGroupID, axisName, HDF5Constants.H5T_NATIVE_FLOAT,
            qaxisShape, qaxisRank, 1, units);

    int filespace_id = H5.H5Dget_space(resultAxisDataID);
    int type_id = H5.H5Dget_type(resultAxisDataID);
    int memspace_id = H5.H5Screate_simple(qaxis.getRank(), qaxisShape, null);
    H5.H5Sselect_all(filespace_id);
    H5.H5Dwrite(resultAxisDataID, type_id, memspace_id, filespace_id, HDF5Constants.H5P_DEFAULT,
            qaxis.getBuffer());

    H5.H5Sclose(filespace_id);
    H5.H5Sclose(memspace_id);
    H5.H5Tclose(type_id);

    if (qaxis.hasErrors()) {
        long[] qaxisShapeError = (long[]) ConvertUtils.convert(qaxis.getShape(), long[].class);
        resultAxisErrorsID = NcdNexusUtils.makedata(resultGroupID, axisName + "_errors",
                HDF5Constants.H5T_NATIVE_DOUBLE, qaxisShapeError, false, units);

        filespace_id = H5.H5Dget_space(resultAxisErrorsID);
        type_id = H5.H5Dget_type(resultAxisErrorsID);
        memspace_id = H5.H5Screate_simple(qaxis.getRank(), qaxisShapeError, null);
        H5.H5Sselect_all(filespace_id);
        H5.H5Dwrite(resultAxisErrorsID, type_id, memspace_id, filespace_id, HDF5Constants.H5P_DEFAULT,
                qaxis.getError().getBuffer());

        H5.H5Sclose(filespace_id);
        H5.H5Sclose(memspace_id);
        H5.H5Tclose(type_id);
    }

    if (doAzimuthal) {
        writeAzimuthalAxisData();
    }
}

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.forkjoin.NcdSectorIntegrationForkJoinTransformer.java

private void writeAzimuthalAxisData() throws HDF5Exception {
    Dataset azAxis = calculateAzimuthalAxisDataset();
    long[] azAxisShape = (long[]) ConvertUtils.convert(azAxis.getShape(), long[].class);

    String units = azAxis.getName();
    String axisName = "direction";
    int[] azAxisRank = new int[] { azAxis.getRank() };
    azimuthalAxisID = NcdNexusUtils.makeaxis(resultGroupID, axisName, HDF5Constants.H5T_NATIVE_DOUBLE,
            azAxisShape, azAxisRank, 1, units);

    int filespace_id = H5.H5Dget_space(azimuthalAxisID);
    int type_id = H5.H5Dget_type(azimuthalAxisID);
    int memspace_id = H5.H5Screate_simple(azAxis.getRank(), azAxisShape, null);
    H5.H5Sselect_all(filespace_id);/*from  w  w  w  .jav a2s.  com*/
    H5.H5Dwrite(azimuthalAxisID, type_id, memspace_id, filespace_id, HDF5Constants.H5P_DEFAULT,
            azAxis.getBuffer());

    H5.H5Sclose(filespace_id);
    H5.H5Sclose(memspace_id);
    H5.H5Tclose(type_id);
}

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.forkjoin.NcdSectorIntegrationForkJoinTransformer.java

private void writeMaskMetadata(int datagroup_id)
        throws HDF5LibraryException, NullPointerException, HDF5Exception {
    long[] maskShape = (long[]) ConvertUtils.convert(mask.getShape(), long[].class);
    int mask_id = NcdNexusUtils.makedata(datagroup_id, "mask", HDF5Constants.H5T_NATIVE_INT8, maskShape, false,
            "pixels");
    int filespace_id = H5.H5Dget_space(mask_id);
    int type = H5.H5Dget_type(mask_id);
    int memspace_id = H5.H5Screate_simple(mask.getRank(), maskShape, null);
    H5.H5Sselect_all(filespace_id);/*from  w w  w . j  a v a2s.  c o  m*/
    H5.H5Dwrite(mask_id, type, memspace_id, filespace_id, HDF5Constants.H5P_DEFAULT,
            (DatasetUtils.cast(mask, Dataset.INT8)).getBuffer());

    H5.H5Sclose(filespace_id);
    H5.H5Sclose(memspace_id);
    H5.H5Tclose(type);
    H5.H5Dclose(mask_id);
}

From source file:uk.ac.diamond.scisoft.ncd.passerelle.actors.forkjoin.NcdSelectionForkJoinTransformer.java

@Override
protected long[] getResultDataShape() {
    selectedShape = Arrays.copyOf(frames, frames.length - dimension);
    indexList = NcdDataUtils.createSliceList(format, (int[]) ConvertUtils.convert(selectedShape, int[].class));
    for (int i = 0; i < selectedShape.length; i++) {
        selectedShape[i] = indexList.get(i).length;
    }//  w  ww  .  j  a  v a  2 s.  c  o  m
    long[] imageSize = Arrays.copyOfRange(frames, frames.length - dimension, frames.length);
    long[] resultDataShape = ArrayUtils.addAll(selectedShape, imageSize);
    return resultDataShape;
}

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

@Override
protected void doInitialize() throws InitializationException {
    super.doInitialize();

    try {/*from   w  w w  . java2  s .co m*/
        enabled = ((BooleanToken) isEnabled.getToken()).booleanValue();
        if (!enabled) {
            return;
        }

        dimension = ((IntToken) dimensionParam.getToken()).intValue();

        entryGroupID = ((IntToken) entryGroupParam.getToken()).intValue();
        processingGroupID = ((IntToken) processingGroupParam.getToken()).intValue();

        int[][] framesMatrix = ((IntMatrixToken) framesParam.getToken()).intMatrix();
        if (framesMatrix == null || framesMatrix.length != 1) {
            throw new InitializationException(ErrorCode.ACTOR_INITIALISATION_ERROR, "Invalid data shape", this,
                    null);
        }
        frames = (long[]) ConvertUtils.convert(framesMatrix[0], long[].class);

        int gridDimension = frames.length - dimension;
        if (gridDimension < 0) {
            throw new InitializationException(ErrorCode.ACTOR_INITIALISATION_ERROR,
                    "Data shape incompatible with the specified dimension", this, null);
        }
        if (gridDimension > 0) {
            grid = Arrays.copyOf(framesMatrix[0], gridDimension);
        } else {
            grid = null;
        }
    } catch (IllegalActionException e) {
        throw new InitializationException(ErrorCode.ACTOR_INITIALISATION_ERROR, "Error initializing NCD actor",
                this, e);
    }
}

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

@Override
protected void process(ActorContext ctxt, ProcessRequest request, ProcessResponse response)
        throws ProcessingException {

    ManagedMessage receivedMsg = request.getMessage(input);
    if (!enabled) {
        response.addOutputMessage(output, receivedMsg);
        return;/*w w w. j  ava  2  s.  com*/
    }

    NcdProcessingSliceObject receivedObject;

    int filespaceID = -1;
    int typeID = -1;
    int memspaceID = -1;
    try {
        receivedObject = (NcdProcessingSliceObject) receivedMsg.getBodyContent();
        lock = receivedObject.getLock();
        SliceSettings sliceData = receivedObject.getSliceData();
        Dataset data = receivedObject.getData();
        Dataset qaxis = receivedObject.getAxis();

        Normalisation nm = new Normalisation();
        nm.setCalibChannel(normChannel);
        if (absScaling != null) {
            nm.setNormvalue(absScaling);
        }
        int[] dataShape = data.getShape();

        data = NcdDataUtils.flattenGridData(data, dimension);
        Dataset errors = data.getErrorBuffer();

        SliceSettings calibrationSliceParams = new SliceSettings(sliceData);
        calibrationSliceParams.setFrames(framesCal);
        Dataset dataCal = NcdNexusUtils.sliceInputData(calibrationSliceParams, calibrationIDs);
        Dataset calibngd = NcdDataUtils.flattenGridData(dataCal, 1);

        Object[] myobj = nm.process(data.getBuffer(), errors.getBuffer(), calibngd.getBuffer(),
                data.getShape()[0], data.getShape(), calibngd.getShape());

        float[] mydata = (float[]) myobj[0];
        double[] myerrors = (double[]) myobj[1];

        Dataset myres = new FloatDataset(mydata, dataShape);
        myres.setErrorBuffer(new DoubleDataset(myerrors, dataShape));

        int selectID = -1;
        int writeID = -1;

        lock.lock();

        long[] frames = sliceData.getFrames();
        long[] start_pos = (long[]) ConvertUtils.convert(sliceData.getStart(), long[].class);
        int sliceDim = sliceData.getSliceDim();
        int sliceSize = sliceData.getSliceSize();

        long[] start = Arrays.copyOf(start_pos, frames.length);

        long[] block = Arrays.copyOf(frames, frames.length);
        Arrays.fill(block, 0, sliceData.getSliceDim(), 1);
        block[sliceDim] = Math.min(frames[sliceDim] - start_pos[sliceDim], sliceSize);

        long[] count = new long[frames.length];
        Arrays.fill(count, 1);

        filespaceID = H5.H5Dget_space(normDataID);
        typeID = H5.H5Dget_type(normDataID);
        memspaceID = H5.H5Screate_simple(block.length, block, null);

        selectID = H5.H5Sselect_hyperslab(filespaceID, HDF5Constants.H5S_SELECT_SET, start, block, count,
                block);
        if (selectID < 0) {
            throw new HDF5Exception("Failed to allocate space fro writing Normalisation data");
        }

        writeID = H5.H5Dwrite(normDataID, typeID, memspaceID, filespaceID, HDF5Constants.H5P_DEFAULT, mydata);
        if (writeID < 0) {
            throw new HDF5Exception("Failed to write Normalisation data into the results file");
        }

        NcdNexusUtils.closeH5idList(new ArrayList<Integer>(Arrays.asList(memspaceID, typeID, filespaceID)));

        filespaceID = H5.H5Dget_space(normErrorsID);
        typeID = H5.H5Dget_type(normErrorsID);
        memspaceID = H5.H5Screate_simple(block.length, block, null);
        selectID = H5.H5Sselect_hyperslab(filespaceID, HDF5Constants.H5S_SELECT_SET, start, block, count,
                block);
        if (selectID < 0) {
            throw new HDF5Exception("Failed to allocate space for writing Normalisation error data");
        }
        writeID = H5.H5Dwrite(normErrorsID, typeID, memspaceID, filespaceID, HDF5Constants.H5P_DEFAULT,
                myres.getError().getBuffer());
        if (writeID < 0) {
            throw new HDF5Exception("Failed to write Normalisation error data into the results file");
        }

        ManagedMessage outputMsg = createMessageFromCauses(receivedMsg);
        NcdProcessingSliceObject obj = new NcdProcessingSliceObject(myres, qaxis, sliceData, lock);
        outputMsg.setBodyContent(obj, "application/octet-stream");
        response.addOutputMessage(output, outputMsg);
    } catch (MessageException e) {
        throw new ProcessingException(ErrorCode.ACTOR_EXECUTION_ERROR, e.getMessage(), this, e.getCause());
    } catch (HDF5LibraryException e) {
        throw new ProcessingException(ErrorCode.ACTOR_EXECUTION_ERROR, e.getMessage(), this, e.getCause());
    } catch (HDF5Exception e) {
        throw new ProcessingException(ErrorCode.ACTOR_EXECUTION_ERROR, e.getMessage(), this, e.getCause());
    } finally {
        if (lock != null && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
        try {
            NcdNexusUtils.closeH5idList(new ArrayList<Integer>(Arrays.asList(memspaceID, typeID, filespaceID)));
        } catch (HDF5LibraryException e) {
            throw new ProcessingException(ErrorCode.ACTOR_EXECUTION_ERROR, e.getMessage(), this, e.getCause());
        }
    }
}

From source file:uk.ac.diamond.scisoft.ncd.rcp.reduction.NcdLazyDataReductionTest.java

@Test
public void testLazyBackgroundSubtraction() throws HDF5Exception {

    LazyBackgroundSubtraction lazyBackgroundSubtraction = new LazyBackgroundSubtraction();
    int nxsFile = H5.H5Fopen(filename, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
    int entry_id = H5.H5Gopen(nxsFile, "entry1", HDF5Constants.H5P_DEFAULT);
    int processing_group_id = H5.H5Gopen(entry_id, "results", HDF5Constants.H5P_DEFAULT);

    DataSliceIdentifiers[] ids = NcdNexusUtilsTest.readDataId(bgFile, testDatasetName, "data", "errors");
    DataSliceIdentifiers bgIds = ids[0];
    H5.H5Sget_simple_extent_dims(bgIds.dataspace_id, bgShape, null);
    int[] bgShape_int = (int[]) ConvertUtils.convert(bgShape, int[].class);
    lazyBackgroundSubtraction.setBgScale((double) scaleBg);

    SliceSettings bgSliceParams = new SliceSettings(bgShape, 0, bgShape_int[0]);
    int[] start = new int[] { 0, 0, 0, 0 };
    bgSliceParams.setStart(start);//w ww. jav  a 2  s  .  co m
    Dataset bgData = NcdNexusUtils.sliceInputData(bgSliceParams, bgIds);
    int bgFrames = bgShape_int[0] * bgShape_int[1];
    bgData = bgData.sum(0).sum(0).idivide(bgFrames);

    DataSliceIdentifiers bgErrorIds = ids[1];
    Dataset bgError = NcdNexusUtils.sliceInputData(bgSliceParams, bgErrorIds);
    bgError = bgError.ipower(2).sum(0).sum(0).idivide(bgFrames);
    bgData.setErrorBuffer(bgError);

    lazyBackgroundSubtraction.setBgFile(bgFile);
    lazyBackgroundSubtraction.setBgDetector(testDatasetName);
    lazyBackgroundSubtraction.setBgScale((double) scaleBg);

    SliceSettings slice = new SliceSettings(shape, 0, (int) shape[0]);
    slice.setStart(new int[] { 0, 0, 0, 0, 0 });
    lazyBackgroundSubtraction.configure(dim, shape, processing_group_id);
    Dataset outData = lazyBackgroundSubtraction.execute(dim, data, bgData, slice, lock);
    Dataset outErrors = outData.getError();

    for (int h = 0; h < shape[0]; h++)
        for (int g = 0; g < shape[1]; g++)
            for (int k = 0; k < shape[2]; k++) {
                for (int i = 0; i < imageShape[0]; i++)
                    for (int j = 0; j < imageShape[1]; j++) {
                        float value = outData.getFloat(h, g, k, i, j);
                        double error = outErrors.getDouble(h, g, k, i, j);
                        float expected = g * shape[2] + k + (1.0f - scaleBg) * (i * imageShape[1] + j)
                                - scaleBg * (bgShape_int[1] - 1) / 2.0f;
                        double expectederr = Math
                                .sqrt(g * shape[2] + k + (1.0 + scaleBg * scaleBg) * (i * imageShape[1] + j)
                                        + scaleBg * scaleBg * (bgShape_int[1] - 1.0) / 2.0);

                        assertEquals(String.format("Test background subtraction frame for (%d, %d, %d, %d, %d)",
                                h, g, k, i, j), expected, value, 1e-4 * Math.abs(expected));
                        assertEquals(String.format(
                                "Test background subtraction frame error for (%d, %d, %d, %d, %d)", h, g, k, i,
                                j), expectederr, error, 1e-4 * Math.abs(expectederr));
                    }
            }
}

From source file:uk.ac.diamond.scisoft.ncd.rcp.reduction.NcdLazyDataReductionTest.java

@Test
public void testLazyAverage() throws Exception {

    int nxsFile = H5.H5Fopen(filename, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
    int entry_id = H5.H5Gopen(nxsFile, "entry1", HDF5Constants.H5P_DEFAULT);
    int processing_group_id = H5.H5Gopen(entry_id, "results", HDF5Constants.H5P_DEFAULT);

    DataSliceIdentifiers[] ids = NcdNexusUtilsTest.readDataId(filename, testDatasetName, "data", "errors");
    DataSliceIdentifiers input_ids = ids[0];
    long[] lstart = new long[] { 0, 0, 0, 0, 0 };
    long[] count = new long[] { 1, 1, 1, 1, 1 };
    input_ids.setSlice(lstart, shape, count, shape);

    DataSliceIdentifiers input_errors_ids = ids[1];
    input_errors_ids.setSlice(lstart, shape, count, shape);

    LazyAverage lazyAverage = new LazyAverage();
    lazyAverage.setAverageIndices(new int[] { 1, 3 });
    lazyAverage.configure(dim, (int[]) ConvertUtils.convert(shape, int[].class), processing_group_id, 100);
    lazyAverage.execute(input_ids, input_errors_ids);

    long[] shapeRes = new long[] { 1, shape[1], 1, imageShape[0], imageShape[1] };
    SliceSettings resultsSlice = new SliceSettings(shapeRes, 0, (int) shapeRes[0]);
    int[] start = new int[] { 0, 0, 0, 0, 0 };
    resultsSlice.setStart(start);//from  w  w  w  .  ja  va2 s .c om
    Dataset outDataset = NcdNexusUtils.sliceInputData(resultsSlice, input_ids);
    Dataset outErrors = NcdNexusUtils.sliceInputData(resultsSlice, input_errors_ids);
    Dataset dataErrors = data.getErrorBuffer();

    for (int k = 0; k < shape[1]; k++) {
        for (int i = 0; i < imageShape[0]; i++) {
            for (int j = 0; j < imageShape[1]; j++) {
                start = new int[] { 0, k, 0, i, j };
                int[] stop = new int[] { (int) shape[0], k + 1, (int) shape[2], i + 1, j + 1 };
                Dataset dataSlice = data.getSlice(start, stop, null);
                Dataset errorsSlice = dataErrors.getSlice(start, stop, null);
                double value = outDataset.getDouble(0, k, 0, i, j);
                double errors = outErrors.getDouble(0, k, 0, i, j);
                double expected = (Double) dataSlice.sum() / (shape[0] * shape[2]);
                double expectederrors = Math.sqrt((Double) errorsSlice.sum()) / (shape[0] * shape[2]);

                // This check fails for higher accuracy settings
                assertEquals(String.format("Test average frame for (%d, %d, %d)", k, i, j), expected, value,
                        1e-6 * expected);
                assertEquals(String.format("Test average frame errors for (%d, %d, %d)", k, i, j),
                        expectederrors, errors, 1e-6 * expectederrors);
            }
        }
    }
}

From source file:uk.ac.diamond.scisoft.ncd.rcp.utils.NcdNexusUtilsTest.java

public void testSliceInputData(int sliceDim, int sliceBatch, int frameStart) throws HDF5Exception {
    int[] start = new int[] { 0, 0, 0, 0 };
    start[sliceDim] = frameStart;/* ww w  .j  a v  a2 s  .com*/

    int[] stop = Arrays.copyOf(frames, frames.length);
    Arrays.fill(stop, 0, sliceDim + 1, 1);
    stop[sliceDim] = Math.min(frameStart + sliceBatch, frames[sliceDim]);

    int[] step = new int[] { 1, 1, 1, 1 };
    IDataset data = lazyDataset.getSlice(start, stop, step);

    DataSliceIdentifiers dr_id = readDataId(inputPath, detector, "data", null)[0];
    long[] frames_long = (long[]) ConvertUtils.convert(frames, long[].class);
    SliceSettings drSlice = new SliceSettings(frames_long, sliceDim, sliceBatch);
    drSlice.setStart(start);
    Dataset result = NcdNexusUtils.sliceInputData(drSlice, dr_id);

    for (int idx = 0; idx < data.getShape()[0]; idx++)
        for (int frame = 0; frame < data.getShape()[1]; frame++) {
            start = new int[] { idx, frame, 0, 0 };
            stop = new int[] { idx + 1, frame + 1, image[0], image[1] };
            Dataset testResult = result.getSlice(start, stop, null);
            Dataset testData = (Dataset) data.getSlice(start, stop, null);
            float valResult = testResult.max().floatValue();
            float valData = testData.max().floatValue();
            float acc = (float) Math.max(1e-6 * Math.abs(Math.sqrt(valResult * valResult + valData * valData)),
                    1e-10);
            assertArrayEquals(String.format("Data slicing test for frame (%d, %d) has failed.", idx, frame),
                    (float[]) testResult.getBuffer(), (float[]) testData.getBuffer(), acc);
        }
}