Friday, September 9, 2011

Using arrays in script (part 2.0): Results window

If the Array and List data structures are too limited in one of your IJ script, there is an interesting alternative for storing a data collection: The Results which is a specific window (tool?) working like a very basic spreadsheet...

The Results window is a specialized Table (a non-image window) working only with numbers and extensively used in many plugins like the Analyze > Analyze Particles... . Moreover, this window has specific menus to explore the data (distribution, statistics computation: min, max, average, standard deviation,etc.).
There are two utility functions to manipulate the data in a Results Window:
  • setResult(...): Creation and set Data
  • getResult(...): Get data

1-Create and fill the Results window

There is no specific function for the creation of the Results window, each time a new value is added in this table via the function setResult(..), the window, a new row or a new column are automatically created if required.
The function  setResult(...) has three arguments:
  1. the column name (a String)
  2. the row index (Notice that the first index is 0 (zero) !! )
  3. the value
Here is an example...
+++ IJ snippet: array_results_creation.ijm +++
// Array using Results Window in IJ script
// Jean-Christophe Taveau
// Fill the data column by column,
// the first time a new heading is found,
// the corresponding column is created.
// Column #1: faces
setResult("faces",0,4); // tetrahedron
setResult("faces",1,5); // pyramid
setResult("faces",2,5); // pentahedron (prism)
setResult("faces",3,6); // cube
setResult("faces",4,7); // heptahedron(prism)
setResult("faces",5,8); // octahedron
setResult("faces",6,9); // nonahedron(augmented cube)
setResult("faces",7,10); // decahedron (augm. pentag. prism)
// Column #2: edges
setResult("edges",0,6);
setResult("edges",1,10);
setResult("edges",2,9);
setResult("edges",3,12);
setResult("edges",4,15);
setResult("edges",5,12);
setResult("edges",6,16);
setResult("edges",7,19);
setResult("edges",8,10);
exit();
view raw gistfile1.js hosted with ❤ by GitHub
+++ End of IJ snippet +++

There are some constraints when defining rows:
  • Keep in mind that the first row has the index 0. 
  • You have to define the rows indexes by ascending order (an increment of 1) - we can't create rows #3, then #2 and #4, you must follow the ascending order (#2, #3, and #4).

2-Read the data

The function getResult(...) allows to read a value, you need two arguments:
  1. The column name ( a String)
  2. The row index .

+++ IJ snippet +++
// Array using Results Window in IJ script
// Jean-Christophe Taveau
// Clear the Log window
print("\\Clear");
// Read and display the data in the Log window
for (i=0;i<nResults;i++)
{
nfaces=getResult("faces",i);
nedges=getResult("edges",i);
print("The polyhedron #" + i + " has " + nfaces + " faces and " + nedges + " edges.");
}
exit();
view raw gistfile1.js hosted with ❤ by GitHub
+++ End of IJ snippet +++

Don't be confused between the indexes displayed in the Results window and the index of the getResult(...) function.
As shown in Fig. 1, the Results begins with the row # 1 ...

Fig. 1: Results window. The first row has the index 1.

...but in a macro/script to get the values of the first row, you need to type:
getResult("faces",0); // 4
getResult("edges",0); // 6

3- Utilily functions

nResults(): To get the rows number. That's very useful to scan all the values in a loop.


run("Clear Results"): To reset (and clear) the Results window.


4- Conclusion

Pros: easy access, good for large arrays (columns)
Cons: Only one unique Results table, strings not supported, no row access.

The Results window is a specialized version of the Table window ... and this is the subject of my next post.

No comments:

Post a Comment