This notebook covers how to create and use ImageJ's net.imagej.table
package.
It also describes other sorts of tables which the SciJava notebook can render.
#@ImageJ ij
// Behind a firewall? Configure your proxy settings here.
//System.setProperty("http.proxyHost","myproxy.domain")
//System.setProperty("http.proxyPort","8080")
"ImageJ is ready to go."
ImageJ is ready to go.
Before we dive into creating ImageJ Table
objects, it is helpful to know about the table rendering capabilities of SciJava notebook. If your goal is just to render a table for display in a SciJava notebook like this one, then it can be done using a simple map data structure:
[
"apple": "fruit",
"orange": "fruit",
"broccoli": "vegetable",
"milk": "dairy",
"yogurt": "dairy"
]
Key | Value |
---|---|
apple | fruit |
orange | fruit |
broccoli | vegetable |
milk | dairy |
yogurt | dairy |
If you use a list of maps, there will be column headers:
[
["Food": "apple", "Category": "fruit"],
["Food": "orange", "Category": "fruit"],
["Food": "broccoli", "Category": "vegetable"],
["Food": "milk", "Category": "dairy"],
["Food": "yogurt", "Category": "dairy"]
]
Food | Category |
---|---|
apple | fruit |
orange | fruit |
broccoli | vegetable |
milk | dairy |
yogurt | dairy |
Table
s¶ImageJ offers a set of interfaces for tables in the net.imagej.table
package. The base interface is Table
, which is a List
of typed Column
s. These columns offer improved type safety and storage efficiency over the "maps" and "list of maps" approaches shown above. ImageJ provides several built-in sorts of columns backed by SciJava's PrimitiveArray
utility classes; for example, DoubleColumn
is a column backed by a DoubleArray
, which is in turn backed by a double[]
which grows dynamically as needed.
To illustrate this structure, here is an example which creates a small ImageJ table from scratch:
import net.imagej.table.DoubleColumn
import net.imagej.table.GenericColumn
import net.imagej.table.DefaultGenericTable
// Create two columns.
nameColumn = new GenericColumn("Town")
populationColumn = new DoubleColumn("Population")
// Fill the columns with information about the largest towns in the world.
nameColumn.add("Karachi")
populationColumn.add(23500000d)
nameColumn.add("Bejing")
populationColumn.add(21516000d)
nameColumn.add("Sao Paolo")
populationColumn.add(21292893d)
// But actually, the largest town is Shanghai,
// so let's add it at the beginning of the table.
nameColumn.add(0, "Shanghai")
populationColumn.add(0, 24256800d)
// Create the table.
table = new DefaultGenericTable()
table.add(nameColumn)
table.add(populationColumn)
table
Town | Population | |
---|---|---|
Shanghai | 2.42568E7 | |
Karachi | 2.35E7 | |
Bejing | 2.1516E7 | |
Sao Paolo | 2.1292893E7 |
Here is another way to create a table, using the set(int col, int row, T value)
method:
import net.imagej.table.DefaultGenericTable
colCount = 7
rowCount = 5
spreadsheet = new DefaultGenericTable(colCount, rowCount)
for (col = 0; col < colCount; col++) {
letter = (char) ((int) 'A' + col)
spreadsheet.setColumnHeader(col, "" + letter)
for (row = 0; row < rowCount; row++) {
data = "" + letter + (row + 1)
spreadsheet.set(col, row, data)
}
}
spreadsheet
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
A1 | B1 | C1 | D1 | E1 | F1 | G1 | |
A2 | B2 | C2 | D2 | E2 | F2 | G2 | |
A3 | B3 | C3 | D3 | E3 | F3 | G3 | |
A4 | B4 | C4 | D4 | E4 | F4 | G4 | |
A5 | B5 | C5 | D5 | E5 | F5 | G5 |
When created this way, the columns are all GenericColumn
instances:
spreadsheet.stream().map{column -> [
"Header": column.getHeader(),
"Column type": column.getClass().getName(),
"Data type": column.getType()
]}.collect()
Header | Column type | Data type |
---|---|---|
A | net.imagej.table.GenericColumn | class java.lang.Object |
B | net.imagej.table.GenericColumn | class java.lang.Object |
C | net.imagej.table.GenericColumn | class java.lang.Object |
D | net.imagej.table.GenericColumn | class java.lang.Object |
E | net.imagej.table.GenericColumn | class java.lang.Object |
F | net.imagej.table.GenericColumn | class java.lang.Object |
G | net.imagej.table.GenericColumn | class java.lang.Object |
GenericColumn
stores each table cell as an Object
. For better space performance, it is encouraged to instead use column types with efficient storage as appropriate. For example, if you know a column will consist only of short
values, then use a ShortColumn
.
Read out the header of the second column:
header = table.get(1).getHeader()
Population
Get a certain column.
populationColumn = table.get("Population")
2.42568E7 |
2.35E7 |
2.1516E7 |
2.1292893E7 |
Get a value from the first line in the column.
populationOfLargestTown = populationColumn.get(0)
Table
API methods¶The Table
interface provides many convenience methods. Here is a complete list:
ij.notebook().methods(table)
name | arguments | returns |
---|---|---|
add | java.lang.Object | boolean |
add | net.imagej.table.Column | boolean |
add | int, java.lang.Object | void |
add | int, net.imagej.table.Column | void |
addAll | java.util.Collection | boolean |
addAll | int, java.util.Collection | boolean |
appendColumn | <none> | net.imagej.table.Column |
appendColumn | java.lang.String | net.imagej.table.Column |
appendColumns | [Ljava.lang.String; | java.util.ArrayList |
appendColumns | [Ljava.lang.String; | java.util.List |
appendColumns | int | java.util.ArrayList |
appendColumns | int | java.util.List |
appendRow | <none> | void |
appendRow | java.lang.String | void |
appendRows | [Ljava.lang.String; | void |
appendRows | int | void |
clear | <none> | void |
clone | <none> | java.lang.Object |
contains | java.lang.Object | boolean |
containsAll | java.util.Collection | boolean |
ensureCapacity | int | void |
equals | java.lang.Object | boolean |
forEach | java.util.function.Consumer | void |
get | int | java.lang.Object |
get | int | net.imagej.table.Column |
get | java.lang.String | net.imagej.table.Column |
get | int, int | java.lang.Object |
get | java.lang.String, int | java.lang.Object |
getClass | <none> | java.lang.Class |
getColumnCount | <none> | int |
getColumnHeader | int | java.lang.String |
getColumnIndex | java.lang.String | int |
getRowCount | <none> | int |
getRowHeader | int | java.lang.String |
getRowIndex | java.lang.String | int |
hashCode | <none> | int |
indexOf | java.lang.Object | int |
insertColumn | int | net.imagej.table.Column |
insertColumn | int, java.lang.String | net.imagej.table.Column |
insertColumns | int, [Ljava.lang.String; | java.util.ArrayList |
insertColumns | int, [Ljava.lang.String; | java.util.List |
insertColumns | int, int | java.util.ArrayList |
insertColumns | int, int | java.util.List |
insertRow | int | void |
insertRow | int, java.lang.String | void |
insertRows | int, [Ljava.lang.String; | void |
insertRows | int, int | void |
isEmpty | <none> | boolean |
iterator | <none> | java.util.Iterator |
lastIndexOf | java.lang.Object | int |
listIterator | <none> | java.util.ListIterator |
listIterator | int | java.util.ListIterator |
notify | <none> | void |
notifyAll | <none> | void |
parallelStream | <none> | java.util.stream.Stream |
remove | int | java.lang.Object |
remove | int | net.imagej.table.Column |
remove | java.lang.Object | boolean |
removeAll | java.util.Collection | boolean |
removeColumn | int | net.imagej.table.Column |
removeColumn | java.lang.String | net.imagej.table.Column |
removeColumns | [Ljava.lang.String; | java.util.ArrayList |
removeColumns | [Ljava.lang.String; | java.util.List |
removeColumns | int, int | java.util.ArrayList |
removeColumns | int, int | java.util.List |
removeIf | java.util.function.Predicate | boolean |
removeRow | int | void |
removeRow | java.lang.String | void |
removeRows | [Ljava.lang.String; | void |
removeRows | int, int | void |
replaceAll | java.util.function.UnaryOperator | void |
retainAll | java.util.Collection | boolean |
set | int, java.lang.Object | java.lang.Object |
set | int, net.imagej.table.Column | net.imagej.table.Column |
set | int, int, java.lang.Object | void |
set | java.lang.String, int, java.lang.Object | void |
setColumnCount | int | void |
setColumnHeader | int, java.lang.String | void |
setDimensions | int, int | void |
setRowCount | int | void |
setRowHeader | int, java.lang.String | void |
setSize | int | void |
size | <none> | int |
sort | java.util.Comparator | void |
spliterator | <none> | java.util.Spliterator |
stream | <none> | java.util.stream.Stream |
subList | int, int | java.util.List |
toArray | <none> | [Ljava.lang.Object; |
toArray | [Ljava.lang.Object; | [Ljava.lang.Object; |
toString | <none> | java.lang.String |
trimToSize | <none> | void |
wait | <none> | void |
wait | long | void |
wait | long, int | void |