Search in sources :

Example 66 with DoubleType

use of net.imglib2.type.numeric.real.DoubleType in project imagej-ops by imagej.

the class OpServiceTest method testOpByType.

/**
 * Tests {@link OpService#op(Class, Object...)}.
 */
@Test
public void testOpByType() {
    final DoubleType value = new DoubleType(123.456);
    final Op op = ops.op(InfinityOp.class, value);
    assertSame(InfinityOp.class, op.getClass());
    assertFalse(Double.isInfinite(value.get()));
    op.run();
    assertTrue(Double.isInfinite(value.get()));
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) Test(org.junit.Test)

Example 67 with DoubleType

use of net.imglib2.type.numeric.real.DoubleType in project imagej-ops by imagej.

the class OpServiceTest method testAliases.

/**
 * Tests {@link OpService#run(String, Object...)} with op aliases.
 */
@Test
public void testAliases() {
    final DoubleType value = new DoubleType(123.456);
    assertFalse(Double.isInfinite(value.get()));
    final Object result = ops.run("infin", value);
    assertSame(value, result);
    assertTrue(Double.isInfinite(value.get()));
    value.set(0.0);
    assertFalse(Double.isInfinite(value.get()));
    final Object result2 = ops.run("inf", value);
    assertSame(value, result2);
    assertTrue(Double.isInfinite(value.get()));
    value.set(0.0);
    boolean noSuchAlias = false;
    try {
        ops.run("infini", value);
    } catch (final IllegalArgumentException exc) {
        noSuchAlias = true;
    }
    assertTrue(noSuchAlias);
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) Test(org.junit.Test)

Example 68 with DoubleType

use of net.imglib2.type.numeric.real.DoubleType in project imagej-ops by imagej.

the class OpServiceTest method testRunByType.

/**
 * Tests {@link OpService#run(Class, Object...)}.
 */
@Test
public void testRunByType() {
    final DoubleType value = new DoubleType(123.456);
    assertFalse(Double.isInfinite(value.get()));
    final Object result = ops.run(InfinityOp.class, value);
    assertSame(value, result);
    assertTrue(Double.isInfinite(value.get()));
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) Test(org.junit.Test)

Example 69 with DoubleType

use of net.imglib2.type.numeric.real.DoubleType in project imagej-ops by imagej.

the class BoxCount method calculate.

/**
 * Counts the number of foreground sections in the interval repeatedly with
 * different size sections
 *
 * @param input an n-dimensional binary interval
 * @return A list of (log(foreground count), -log(section size))
 *         {@link ValuePair} objects for curve fitting
 */
@Override
public List<ValuePair<DoubleType, DoubleType>> calculate(final RandomAccessibleInterval<B> input) {
    final List<ValuePair<DoubleType, DoubleType>> points = new ArrayList<>();
    final int dimensions = input.numDimensions();
    final long[] sizes = new long[dimensions];
    final long numTranslations = 1 + gridMoves;
    input.dimensions(sizes);
    for (long sectionSize = maxSize; sectionSize >= minSize; sectionSize /= scaling) {
        final long translationAmount = Math.max(1, sectionSize / numTranslations);
        final Stream<long[]> translations = translationStream(numTranslations, translationAmount, dimensions - 1, new long[dimensions]);
        final LongStream foregroundCounts = countTranslatedGrids(input, translations, sizes, sectionSize);
        final long foreground = foregroundCounts.min().orElse(0);
        final double logSize = -Math.log(sectionSize);
        final double logCount = Math.log(foreground);
        final ValuePair<DoubleType, DoubleType> point = new ValuePair<>(new DoubleType(logSize), new DoubleType(logCount));
        points.add(point);
    }
    return points;
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) ValuePair(net.imglib2.util.ValuePair) ArrayList(java.util.ArrayList) LongStream(java.util.stream.LongStream)

Example 70 with DoubleType

use of net.imglib2.type.numeric.real.DoubleType in project vcell by virtualcell.

the class VCellService method runSimulation.

private static Task<List<Dataset>, SimulationState> runSimulation(final SimulationServiceImpl client, final VCellModel vCellModel, final SimulationSpec simSpec, final List<Species> outputSpecies, final boolean shouldCreateIndividualDatasets, final OpService opService, final DatasetService datasetService) throws IOException, XMLStreamException {
    final Task<List<Dataset>, SimulationState> task = new Task<List<Dataset>, SimulationState>() {

        @Override
        protected List<Dataset> doInBackground() throws Exception {
            setSubtask(SimulationState.notRun);
            final File sbmlSpatialFile = new File(vCellModel.getName() + ".xml");
            new SBMLWriter().write(vCellModel.getSbmlDocument(), sbmlSpatialFile);
            final SBMLModel model = new SBMLModel();
            model.setFilepath(sbmlSpatialFile.getAbsolutePath());
            final SimulationInfo simulationInfo = client.computeModel(model, simSpec);
            try {
                Thread.sleep(500);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
            setSubtask(SimulationState.running);
            while (client.getStatus(simulationInfo).getSimState() == SimulationState.running) {
                System.out.println("waiting for simulation results");
                try {
                    Thread.sleep(500);
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (client.getStatus(simulationInfo).getSimState() == SimulationState.failed) {
                setSubtask(SimulationState.failed);
                return null;
            }
            final List<Dataset> results = new ArrayList<>();
            final List<VariableInfo> vars = client.getVariableList(simulationInfo);
            final List<Double> times = client.getTimePoints(simulationInfo);
            for (final VariableInfo var : vars) {
                if (outputSpecies.stream().anyMatch(species -> species.getId().equals(var.getVariableVtuName()))) {
                    // Get data for first time point and determine dimensions
                    List<Double> data = client.getData(simulationInfo, var, 0);
                    final int[] dimensions = getDimensions(data, times);
                    final Img<DoubleType> img = opService.create().img(dimensions);
                    final RandomAccess<DoubleType> imgRA = img.randomAccess();
                    // Copy data to the ImgLib2 Img
                    for (int t = 0; t < times.size(); t++) {
                        data = client.getData(simulationInfo, var, t);
                        for (int d = 0; d < data.size(); d++) {
                            imgRA.setPosition(new int[] { d, t });
                            imgRA.get().set(data.get(d));
                        }
                    }
                    // Create ImageJ Dataset and add to results
                    final Dataset dataset = datasetService.create(img);
                    dataset.setName(var.getVariableVtuName());
                    results.add(dataset);
                }
            }
            // If desired, add all datasets with the same dimensions
            if (!shouldCreateIndividualDatasets && !results.isEmpty()) {
                // First, group datasets according to dimensions
                final List<List<Dataset>> datasetGroups = new ArrayList<>();
                final List<Dataset> initialGroup = new ArrayList<>();
                initialGroup.add(results.get(0));
                datasetGroups.add(initialGroup);
                for (int i = 1; i < results.size(); i++) {
                    final Dataset result = results.get(i);
                    for (final List<Dataset> datasetGroup : datasetGroups) {
                        final Dataset[] datasets = new Dataset[] { datasetGroup.get(0), result };
                        if (Datasets.areSameSize(datasets, 0, 1)) {
                            datasetGroup.add(result);
                        } else {
                            final List<Dataset> newGroup = new ArrayList<>();
                            newGroup.add(result);
                            datasetGroups.add(newGroup);
                        }
                    }
                }
                final List<Dataset> summedResults = new ArrayList<>();
                for (final List<Dataset> datasetGroup : datasetGroups) {
                    final Img<DoubleType> sum = opService.create().img(datasetGroup.get(0));
                    for (final Dataset dataset : datasetGroup) {
                        @SuppressWarnings("unchecked") final RandomAccessibleInterval<DoubleType> current = (Img<DoubleType>) dataset.getImgPlus().getImg();
                        opService.math().add(sum, sum, current);
                    }
                    final Dataset result = datasetService.create(sum);
                    result.setName(datasetGroup.stream().map(d -> d.getName()).collect(Collectors.joining("+")));
                    summedResults.add(result);
                }
                return summedResults;
            }
            setSubtask(SimulationState.done);
            return results;
        }
    };
    return task;
}
Also used : Task(org.vcell.imagej.common.gui.Task) SBMLModel(org.vcell.vcellij.api.SBMLModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Img(net.imglib2.img.Img) Dataset(net.imagej.Dataset) VariableInfo(org.vcell.vcellij.api.VariableInfo) SBMLWriter(org.sbml.jsbml.SBMLWriter) DoubleType(net.imglib2.type.numeric.real.DoubleType) SimulationState(org.vcell.vcellij.api.SimulationState) File(java.io.File) SimulationInfo(org.vcell.vcellij.api.SimulationInfo)

Aggregations

DoubleType (net.imglib2.type.numeric.real.DoubleType)142 Test (org.junit.Test)114 AbstractOpTest (net.imagej.ops.AbstractOpTest)104 LongType (net.imglib2.type.numeric.integer.LongType)37 Random (java.util.Random)19 Img (net.imglib2.img.Img)9 FinalInterval (net.imglib2.FinalInterval)8 ArrayList (java.util.ArrayList)7 Ops (net.imagej.ops.Ops)7 BitType (net.imglib2.type.logic.BitType)7 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)6 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)6 RealLocalizable (net.imglib2.RealLocalizable)6 LocalThresholdMethod (net.imagej.ops.threshold.LocalThresholdMethod)5 FinalDimensions (net.imglib2.FinalDimensions)5 CreateImgFromImg (net.imagej.ops.create.img.CreateImgFromImg)4 Dimensions (net.imglib2.Dimensions)4 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)4 Module (org.scijava.module.Module)4 CreateImgFromDimsAndType (net.imagej.ops.create.img.CreateImgFromDimsAndType)3