Difference between revisions of "Tips for developers"

(How to show a plot: describe how to update a Plot)
(Using ImageJ effectively: using ij.Prefs)
Line 98: Line 98:
  
 
This [http://pacific.mpi-cbg.de/javadoc/ij/process/ImageProcessor.html class] also has some other goodies, such as methods for convolution.
 
This [http://pacific.mpi-cbg.de/javadoc/ij/process/ImageProcessor.html class] also has some other goodies, such as methods for convolution.
 +
 +
== How to store settings persistently ==
 +
 +
ImageJ (and therefore Fiji, too) has a way to store key/value pairs persistently, i.e. they are available even after a restart. The settings are stored in a file called ''IJ_Prefs.txt'' in the subdirectory called ''.imagej/'' in your home directory (on Windows, directly in your home directory).
 +
 +
'''Note:''' the settings are only saved upon <u>regular</u> exit of Fiji; If you kill the process, or if the Java Runtime crashes, they are <u>not</u> saved. You can ask for the settings to be saved explicitly, though.
 +
 +
Example:
 +
 +
<source lang="java">
 +
import ij.Prefs;
 +
 +
...
 +
 +
    // save key/value pairs
 +
    Prefs.set("my.persistent.name", "Grizzly Adams");
 +
    Prefs.set("my.persistent.number", 10);
 +
 +
    ....
 +
 +
    // retrieve the values (with default values)
 +
    int myNumber = Prefs.get("my.persistent.number", 0);
 +
 +
    // explicitly save the preferences _now_
 +
    Prefs.savePreferences();
 +
</source>
 +
 +
'''Note:''' do <u>not</u> use the ''getString()'' or ''getInt()''; These methods do not have any setter methods, and they do <u>not</u> access the same values as the ''get()'' method (''get()'' actually prefixes the keys with a dot)!

Revision as of 04:11, 21 July 2010

An unsorted list of hints that you might find useful:

compile & execute a class

You do not need to call javac yourself with a long classpath:

$ ./fiji --javac YourClass.java

and you can call its main() method just as easy:

$ ./fiji YourClass.class argument1 argument2

rapidly prototype a plugin

It is often easier to start out with a Jython, JRuby or BeanShell script, as you do not have to care about strict typing, exceptions or recompiling. Just place your script (with the correct extension -- .py, .rb or .bsh) into the plugins/ folder and execute the script. Fiji will always execute the current version of the script, so you can edit and run the script without restarting Fiji.

Of course, it is even more convenient to use the Script Editor...

Once you have working code, you can turn it into a proper plugin (this is easiest with BeanShell, as its syntax is closest to Java already), adding strict typing and exception handling as needed.

find the .jar file containing a certain class

Sometimes, the compiler complains about a class not having a certain method or interface, but you know it must contain it. More often than not, that class exists in different versions in your classpath. Find out with

$ ./fiji bin/find-jar-for-class.py the.class.youre.looking.For

If you want to do that with an installed Fiji (i.e. when bin/ is missing), you can start the Script Editor and execute a BeanShell like this:

import ij.IJ;
        
print(IJ.getClassLoader()
        .loadClass("the.class.youre.looking.For")
        .getResource("For.class").toString());

This will output the URL to the .class file, including the path to the enclosing .jar file.

Using ImageJ effectively

ImageJ has a simple API, but it is also big, so here are a few pointers to some useful parts.

How to read a file into an ImagePlus

 ImagePlus image = IJ.openImage(path);

Making a new image stack -- quickly!

 ImagePlus image = IJ.createInstance("my image", "RGB", 640, 480, 20);

How to display an exception in a window

This is especially useful on Windows, where you usually do not see the console:

 IJ.handleException(exception);

This is available since ImageJ 1.43g, as well as the option to set a different exception handler using

 IJ.setExceptionHandler(new IJ.ExceptionHandler() {
    public void handle(Throwable exception) {
       // do something
    }
 });

How to show a plot

ImageJ offers the ij.gui.Plot class to make a window showing a plot. Use it like this:

Plot plot = new Plot("The window title", "labels on the x-axis", "labels on the y-axis",
    float_array_of_x_values, float_array_of_y_values);
plot.show();

Instead of float arrays, you can also use double arrays.

If you need to update the plot at some stage, you need to save the return value of show():

Plot plot = new Plot("The window title", "labels on the x-axis", "labels on the y-axis",
    float_array_of_x_values, float_array_of_y_values);
PlotWindow window = plot.show();
// ...
Plot update = new Plot("Dummy", "x labels", "y labels", x_values, y_values);
window.drawPlot(update);

Duplicate, or convert between, ImageProcessor types

The ImageProcessor class has several useful methods: duplicate(), convertToByte(), convertToFloat(), convertToRGB(), and convertToShort().

This class also has some other goodies, such as methods for convolution.

How to store settings persistently

ImageJ (and therefore Fiji, too) has a way to store key/value pairs persistently, i.e. they are available even after a restart. The settings are stored in a file called IJ_Prefs.txt in the subdirectory called .imagej/ in your home directory (on Windows, directly in your home directory).

Note: the settings are only saved upon regular exit of Fiji; If you kill the process, or if the Java Runtime crashes, they are not saved. You can ask for the settings to be saved explicitly, though.

Example:

import ij.Prefs;

...

    // save key/value pairs
    Prefs.set("my.persistent.name", "Grizzly Adams");
    Prefs.set("my.persistent.number", 10);

    ....

    // retrieve the values (with default values)
    int myNumber = Prefs.get("my.persistent.number", 0);

    // explicitly save the preferences _now_
    Prefs.savePreferences();

Note: do not use the getString() or getInt(); These methods do not have any setter methods, and they do not access the same values as the get() method (get() actually prefixes the keys with a dot)!