Headlessly create a Dataset

I am trying to debug an issue with the synchronization between IJ1 and IJ2. This issue came up from PyImageJ, where the user took a numpy array wraped as a dataset, showed it in the IJ1 UI, and then ran an IJ1 plugin on it. This changed the display, but did not change the associated dataset.

I am attempting to replicate this purely on Java so I can debug it in an IDE. However, I’m having a hard time figuring out how to create a Dataset headlessly to get to the next step. What is the best way to do so? I tried making an array of doubles, but could not find a way of directly converting the array into a Dataset.

#@ DatasetService ds
Random r = new Random();
double[][] doubles = new double[512][512];
for (int i = 0; i < doubles.length; i++){
	for (int j = 0; j < doubles[i].length; j++)
		doubles[i][j] = r.nextGaussian();

dataset = ds.create(doubles)

does not work. Other things I’ve tried have not been headless, e.g. running ops to create images opens up an image display. Is there a simple way to do this?

You have DatasetService.create(RandomAccessibleInterval rai), which means you’d have to convert your double[] array into a RandomAccessibleInterval first.

We’re in #imglib2 land here. There is the ArrayImgs utility class that provides suitable methods:

#@ DatasetService ds

import net.imglib2.img.array.ArrayImgs

double[] pixels = [2,2,2,3,3,3,4,4,4,5,5,5]
long[] dims = [3,4]
img = ArrayImgs.doubles(pixels, dims)

dataset = ds.create(img)

Creating an image with ImageJ-Ops is also headless usually. You might have been misled by the fact that the result of the evaluation of the last line in a script will be considered as an output (and therefore be post-processed, i.e. displayed) when the script doesn’t define own outputs and is run from the script editor.
You can avoid this by adding null as the last line of your script.


Thank you very much! That did the trick.

Thank you also for the explanation of how I was misled by the script evaluation. I’ll have to keep an eye out for that in the future.