Using CLIJx in micromanager 2.0

Hi all,

I have been privately corresponding with Robert about getting some of the advanced filters in clijx running in a Micromanager 2.0 beanshell script. It seems we are quite close, but I am getting an error and am hoping for some guidance.

Here is the code I am trying:

import net.haesleinhuepf.clijx.CLIJx;
import net.haesleinhuepf.clij.converters.implementations.ClearCLBufferToImagePlusConverter;
import net.haesleinhuepf.clij.converters.implementations.ImagePlusToClearCLBufferConverter;

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;

// initialize CLIJ
CLIJx clijx = CLIJx.getInstance();

// get some test data
imp_input = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

// turn test data into a stack
stack = imp_input.getImageStack();
slice = imp_input.getProcessor();
stack.addSlice(slice);

// push image stack to GPU for use
gpu_input = clijx.push(new ImagePlus("gpu_input", stack));

// push input image to allocate an image of the correct size on GPU for output
gpu_output = clijx.push(imp_input);

// setup fusion parameters
number_of_substacks = 2;
sigmaX = 2;
sigmaY = 2;
sigmaZ = 0;
exponent = 2;

// run fusion
clijx.tenengradFusion(gpu_input, gpu_output, number_of_substacks, sigmaX, sigmaY, sigmaZ, exponent);

// get the fusion result back from the GPU and display
imp_output = clijx.pull(gpu_output);
imp_output.show();

// clean up memory on the GPU
gpu_input.close();
gpu_output.close();

The above code results in the following error,

Line 34: run-time error : fiji/util/gui/GenericDialogPlus

If I instead try another filter in clijx, for example blur or mean3D, I get the same error. I am able to push and pull the input/output images and they are the correct dimensions.

If I use the example script, everything works fine. If I run the above script in FIJI, I can generate the correct data. My guess is I am either invoking clijx incorrectly or passing the data around incorrectly.

Thanks!

1 Like

Hey @dpshepherd,

great to see you here. Sorry for the inconvenience. Apparently, the installation instructions for clij in micromanager were outdated. I just updated them and tested your script with a recent mm-gamma.

Then, there were some issues in the script you posted because the gpu_output image was 2D while the input is 3D.

Thus, I fixed it and also put in a block for properly showing error messages, e.g. if tenengradFusion crashes (I’m new to Beanshell and MM scripting; if you could tell me where error messages / stack traces show up, I would learn something :wink: )

Please try the updated script below and let me know if it works!

import net.haesleinhuepf.clijx.CLIJx;
import net.haesleinhuepf.clij.converters.implementations.ClearCLBufferToImagePlusConverter;
import net.haesleinhuepf.clij.converters.implementations.ImagePlusToClearCLBufferConverter;

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;

// initialize CLIJ
CLIJx clijx = CLIJx.getInstance();

// get some test data
imp_input = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

// turn test data into a stack
stack = imp_input.getImageStack();
slice = imp_input.getProcessor();
stack.addSlice(slice);

// push image stack to GPU for use
gpu_input = clijx.push(new ImagePlus("gpu_input", stack));

// create an image with correct size and type on GPU for output
gpu_output = clijx.create(new long[]{gpu_input.getWidth(), gpu_input.getHeight(), 1}, gpu_input.getNativeType());

// setup fusion parameters
number_of_substacks = 2;
sigmaX = 2;
sigmaY = 2;
sigmaZ = 0;
exponent = 2;

// run fusion

try {
    clijx.tenengradFusion(gpu_input, gpu_output, number_of_substacks, sigmaX, sigmaY, sigmaZ, exponent);
}
catch (Exception ex) {
    print("Hello Exception");
    print(ex.getStackTrace());
}

// get the fusion result back from the GPU and display
imp_output = clijx.pull(gpu_output);
imp_output.show();

// clean up memory on the GPU
gpu_input.close();
gpu_output.close();

Happy fusing!

Cheers,
Robert

Hi @haesleinhuepf,

With the new dependencies copied into the MM plugins folder and code changes, this now works. Thanks!

To make sure I understand your memory allocation: even though the output is one image - a 3D image type needs to allocated to the GPU?

In our use of beanshell in MM so far, errors show up in the Beanshell console within the script window that we use in Micromanager. But I’m not sure if that is universally true? And the level of information output to the console is not always detailed. Maybe @nicost can chime in? I’m sure I have said something incorrect given my inexperience with beanshell & MM.

Doug

1 Like

Great, happy to see clijed microscope control :slight_smile:

Yes, that’s right. I’d say it’s a bug, because the OpenCL code does work for 2D and 3D, but it does not support 3D-2D-conversion in this context. To be sure: use TenengradFusion for 3D images only. Before TenengradFusion becomes part of a stable release, I will have to figure this out. Maybe it’ll be on the list for CLIJ3 :wink: I’ll keep you posted.

Cheers,
Robert

Beanshell is a bit outdated, and that environment is rough for doing anything that needs thorough debugging. I usually get more output by setting Micro-Manager to debug level logging (Tools > Options) and monitor logging output, which you either can do in the IDE you use to run Micro-Manager, or tailing the current Corelog file (in directory Corelogs within the Micro-Manager directory).
For anything that gets more involved, I like to create a MM plugin that you can then fully debug in the IDE of your choice.

1 Like