Skip to content

Commit a46cb81

Browse files
committed
WIP: port project ops
TODO: port tests
1 parent 3d27e3c commit a46cb81

2 files changed

Lines changed: 225 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.project;
31+
32+
import java.util.Iterator;
33+
34+
import net.imglib2.Cursor;
35+
import net.imglib2.IterableInterval;
36+
import net.imglib2.RandomAccess;
37+
import net.imglib2.RandomAccessibleInterval;
38+
39+
import org.scijava.Priority;
40+
import org.scijava.ops.core.Op;
41+
import org.scijava.ops.core.computer.Computer;
42+
import org.scijava.ops.core.computer.Computer3;
43+
import org.scijava.param.Parameter;
44+
import org.scijava.plugin.Plugin;
45+
import org.scijava.struct.ItemIO;
46+
47+
@Plugin(type = Op.class, name = "project", priority = Priority.LOW)
48+
@Parameter(key = "input")
49+
@Parameter(key = "op")
50+
@Parameter(key = "dim")
51+
@Parameter(key = "output", type = ItemIO.BOTH)
52+
public class ProjectRAIToIterableInterval<T, V>
53+
implements Computer3<RandomAccessibleInterval<T>, Computer<Iterable<T>, V>, Integer, IterableInterval<V>> {
54+
55+
@Override
56+
public void compute(final RandomAccessibleInterval<T> input, final Computer<Iterable<T>, V> method,
57+
final Integer dim, final IterableInterval<V> output) {
58+
if (input.numDimensions() != output.numDimensions() + 1)
59+
throw new IllegalArgumentException("Input must have one more dimension than output!");
60+
if (dim >= input.numDimensions())
61+
throw new IllegalArgumentException("The dimension provided to compute over does not exitst in the input!");
62+
63+
final Cursor<V> cursor = output.localizingCursor();
64+
final RandomAccess<T> access = input.randomAccess();
65+
66+
while (cursor.hasNext()) {
67+
cursor.fwd();
68+
for (int d = 0; d < input.numDimensions(); d++) {
69+
if (d != dim) {
70+
access.setPosition(cursor.getIntPosition(d - (d > dim ? -1 : 0)), d);
71+
}
72+
}
73+
74+
method.compute(new DimensionIterable(input.dimension(dim), access, dim), cursor.get());
75+
}
76+
}
77+
78+
final class DimensionIterable implements Iterable<T> {
79+
80+
private final long size;
81+
private final RandomAccess<T> access;
82+
private final int dim;
83+
84+
public DimensionIterable(final long size, final RandomAccess<T> access, final int dim) {
85+
this.size = size;
86+
this.access = access;
87+
this.dim = dim;
88+
}
89+
90+
@Override
91+
public Iterator<T> iterator() {
92+
return new Iterator<T>() {
93+
94+
int k = -1;
95+
96+
@Override
97+
public boolean hasNext() {
98+
return k < size - 1;
99+
}
100+
101+
@Override
102+
public T next() {
103+
k++;
104+
access.setPosition(k, dim);
105+
return access.get();
106+
}
107+
108+
@Override
109+
public void remove() {
110+
throw new UnsupportedOperationException("Not supported");
111+
}
112+
};
113+
}
114+
}
115+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.project;
31+
32+
import static org.junit.Assert.assertEquals;
33+
34+
import net.imagej.ops.AbstractOpTest;
35+
import net.imglib2.RandomAccess;
36+
import net.imglib2.img.Img;
37+
import net.imglib2.type.numeric.integer.UnsignedByteType;
38+
39+
import org.junit.Before;
40+
import org.junit.Test;
41+
import org.scijava.ops.core.computer.Computer;
42+
import org.scijava.ops.core.computer.GenericComputers;
43+
import org.scijava.ops.types.Nil;
44+
import org.scijava.ops.util.Computers;
45+
46+
public class ProjectTest extends AbstractOpTest {
47+
48+
private final int PROJECTION_DIM = 2;
49+
50+
private Img<UnsignedByteType> in;
51+
private Img<UnsignedByteType> out1;
52+
private Img<UnsignedByteType> out2;
53+
private Computer<Iterable<UnsignedByteType>, UnsignedByteType> op;
54+
private Computer<Iterable<UnsignedByteType>, UnsignedByteType> genericOp;
55+
56+
@Before
57+
public void initImg() {
58+
in = generateUnsignedByteArrayTestImg(false, 10, 10, 10);
59+
60+
final RandomAccess<UnsignedByteType> randomAccess = in.randomAccess();
61+
62+
// at each x,y,z fill with x+y
63+
for (int x = 0; x < 10; x++) {
64+
for (int y = 0; y < 10; y++) {
65+
for (int z = 0; z < 10; z++) {
66+
randomAccess.setPosition(new long[] { x, y, z });
67+
randomAccess.get().setReal(x + y);
68+
}
69+
}
70+
}
71+
72+
out1 = generateUnsignedByteArrayTestImg(false, 10, 10);
73+
out2 = generateUnsignedByteArrayTestImg(false, 10, 10);
74+
75+
op = Computers.unary(ops, "stats.sum", new Nil<Iterable<UnsignedByteType>>() {},
76+
new Nil<UnsignedByteType>() {});
77+
genericOp = GenericComputers.Computers.generic(op,
78+
new Nil<Computer<Iterable<UnsignedByteType>, UnsignedByteType>>() {}.getType());
79+
}
80+
81+
@Test
82+
public void testProjector() {
83+
//TODO: uncomment when this Op is ported (assuming it will be?)
84+
// ops.run(DefaultProjectParallel.class, out1, in, op, PROJECTION_DIM);
85+
// ops.run(DefaultProjectParallel.class, out2, in, op, PROJECTION_DIM);
86+
// testEquality(out1, out2);
87+
88+
ops.run("project", in, genericOp, PROJECTION_DIM, out1);
89+
ops.run("project", in, genericOp, PROJECTION_DIM, out2);
90+
testEquality(out1, out2);
91+
}
92+
93+
private void testEquality(final Img<UnsignedByteType> img1, final Img<UnsignedByteType> img2) {
94+
final RandomAccess<UnsignedByteType> img1RandomAccess = img1.randomAccess();
95+
final RandomAccess<UnsignedByteType> img2RandomAccess = img2.randomAccess();
96+
97+
// at each x,y position the sum projection should be (x+y) *size(z)
98+
for (int x = 0; x < 10; x++) {
99+
for (int y = 0; y < 10; y++) {
100+
img1RandomAccess.setPosition(new long[] { x, y });
101+
img2RandomAccess.setPosition(new long[] { x, y });
102+
103+
assertEquals(img1RandomAccess.get().get(), //
104+
in.dimension(PROJECTION_DIM) * (x + y));
105+
assertEquals(img2RandomAccess.get().get(), //
106+
in.dimension(PROJECTION_DIM) * (x + y));
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)