Skip to content

Commit 2138118

Browse files
committed
Expand on Project Ops
1 parent e59977d commit 2138118

4 files changed

Lines changed: 120 additions & 130 deletions

File tree

‎scijava-ops-image/src/main/java/org/scijava/ops/image/transform/project/project/DefaultProjectParallel.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
import org.scijava.function.Computers;
4141

4242
/**
43-
* @param <T>
44-
* @param <V>
45-
* @implNote op name='transform.project', priority='99.'
43+
* <b>Projection</b> is the act of creating 1-dimensional slices of an n-dimensional image,
44+
* reducing that slice down to a single value, and combining those images back into a (n-1)-dimensional array
45+
*
46+
* @param <T> the type of input image elements
47+
* @param <V> the type of output image elements
48+
* @implNote op name='transform.project', priority='99.', hints="adaptation.FORBIDDEN"
49+
* @see ProjectParallelFunction for an Op that creates its own output
4650
*/
4751
public class DefaultProjectParallel<T, V> implements
4852
Computers.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<? super RandomAccessibleInterval<T>, V>, Integer, RandomAccessibleInterval<V>>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.scijava.ops.image.transform.project.project;
2+
3+
import net.imglib2.FinalDimensions;
4+
import net.imglib2.RandomAccessibleInterval;
5+
import org.scijava.function.Computers;
6+
import org.scijava.function.Functions;
7+
import org.scijava.function.Producer;
8+
import org.scijava.ops.spi.OpDependency;
9+
10+
import java.util.function.BiFunction;
11+
12+
/**
13+
* Wraps {@link DefaultProjectParallel}, but creates a new output image in the process.
14+
* @param <T> the type of input image elements
15+
* @param <V> the type of output image elements
16+
* @author Gabriel Selzer
17+
* @implNote op name='transform.project', priority='99.'
18+
*/
19+
public class ProjectParallelFunction<T, V> implements
20+
Functions.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<? super RandomAccessibleInterval<T>, V>, Integer, RandomAccessibleInterval<V>>
21+
{
22+
@OpDependency(name="transform.project")
23+
Computers.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<? super RandomAccessibleInterval<T>, V>, Integer, RandomAccessibleInterval<V>> projector;
24+
25+
@OpDependency(name="transform.translateView")
26+
BiFunction<RandomAccessibleInterval<V>, long[], RandomAccessibleInterval<V>> translator;
27+
28+
@OpDependency(name="create.type")
29+
Producer<V> typeCreator;
30+
31+
@OpDependency(name="create.img")
32+
BiFunction<FinalDimensions, V, RandomAccessibleInterval<V>> creator;
33+
34+
35+
/**
36+
*
37+
* @param input the input {@code n}-dimensional dataset
38+
* @param op the Op to project over {@code dim}
39+
* @param dim the dimension along {@code input} to project
40+
* @return a {@code n-1}-dimensional dataset
41+
*/
42+
@Override
43+
public RandomAccessibleInterval<V> apply(RandomAccessibleInterval<T> input, Computers.Arity1<? super RandomAccessibleInterval<T>, V> op, Integer dim) {
44+
var dims = new long[input.numDimensions() - 1];
45+
var min = new long[input.numDimensions() - 1];
46+
for(int i = 0; i < input.numDimensions() - 1; i++) {
47+
dims[i] = input.dimension(i >= dim ? i+1 : i);
48+
min[i] = input.min(i >= dim ? i+1 : i);
49+
}
50+
// Get an arbitrary instance of the output type
51+
var outImg = creator.apply(new FinalDimensions(dims), typeCreator.create());
52+
// translate by the minimum of the input img
53+
var translated = translator.apply(outImg, min);
54+
55+
projector.compute(input, op, dim, translated);
56+
return translated;
57+
}
58+
}

‎scijava-ops-image/src/main/java/org/scijava/ops/image/transform/project/project/ProjectRAIToIterableInterval.java‎

Lines changed: 0 additions & 122 deletions
This file was deleted.

‎scijava-ops-image/src/test/java/org/scijava/ops/image/transform/project/ProjectTest.java‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
package org.scijava.ops.image.transform.project;
3131

32-
import static org.junit.jupiter.api.Assertions.assertEquals;
33-
3432
import net.imglib2.FinalInterval;
3533
import net.imglib2.RandomAccessibleInterval;
3634
import net.imglib2.img.array.ArrayImgs;
@@ -47,6 +45,11 @@
4745
import org.scijava.ops.api.OpBuilder;
4846
import org.scijava.types.Nil;
4947

48+
import java.util.Random;
49+
import java.util.function.Function;
50+
51+
import static org.junit.jupiter.api.Assertions.*;
52+
5053
public class ProjectTest extends AbstractOpTest {
5154

5255
private final int PROJECTION_DIM = 2;
@@ -62,9 +65,9 @@ public void initImg() {
6265
final RandomAccess<UnsignedByteType> randomAccess = in.randomAccess();
6366

6467
// at each x,y,z fill with x+y
65-
for (int x = 0; x < 10; x++) {
66-
for (int y = 0; y < 10; y++) {
67-
for (int z = 0; z < 10; z++) {
68+
for (int x = 0; x < in.dimension(0); x++) {
69+
for (int y = 0; y < in.dimension(1); y++) {
70+
for (int z = 0; z < in.dimension(2); z++) {
6871
randomAccess.setPosition(new long[] { x, y, z });
6972
randomAccess.get().setReal(x + y);
7073
}
@@ -114,7 +117,54 @@ public void testInterval() {
114117
// 2 + 3 + 4 = 9
115118
assertEquals(9, outCursor.next().get());
116119
}
120+
}
121+
122+
/**
123+
* Ensures {@code "transform.project"} can run as a {@link Function}
124+
*/
125+
@Test
126+
public void testFunction() {
127+
var op = ops.op("stats.sum").input(in).outType(UnsignedByteType.class).computer();
128+
129+
RandomAccessibleInterval<UnsignedByteType> output = ops.op("transform.project").input(in, op, 2).outType(new Nil<RandomAccessibleInterval<UnsignedByteType>>() {}).apply();
130+
131+
for (int x = 0; x < output.dimension(0); x++) {
132+
for (int y = 0; y < output.dimension(1); y++) {
133+
assertEquals(in.dimension(2) * (x + y), output.getAt(x, y).getIntegerLong());
134+
}
135+
}
136+
}
117137

138+
/**
139+
* Ensures {@code "transform.project"} runs only within the passed interval.
140+
*/
141+
@Test
142+
public void testFunctionInterval() {
143+
// Set up img[x, y, z] = z
144+
var input = ArrayImgs.unsignedBytes(10, 10, 10);
145+
for(int x = 0; x < 10; x++) {
146+
for(int y = 0; y < 10; y++) {
147+
for(int z = 0; z < 10; z++) {
148+
input.getAt(x, y, z).set(z);
149+
}
150+
}
151+
}
152+
// Create an interval containing 1<=x<=3, 1<=y<=4, 2<=z<=4
153+
var intervaled = Views.interval(input, new FinalInterval(new long[] {1, 1, 2}, new long[] {3, 4, 4}));
154+
155+
// Project on the interval
156+
var op = ops.op("stats.sum").input(intervaled).outType(UnsignedByteType.class).computer();
157+
var out = ops.op("transform.project").input(intervaled, op, PROJECTION_DIM)
158+
.outType(new Nil<RandomAccessibleInterval<UnsignedByteType>>() {}).apply();
159+
160+
// Assert that the projection (summation) only covered z=2, z=3, z=4
161+
var outCursor = out.cursor();
162+
while (outCursor.hasNext()) {
163+
// 2 + 3 + 4 = 9
164+
assertEquals(9, outCursor.next().get());
165+
}
166+
assertArrayEquals(new long[] {1, 1}, out.minAsLongArray());
167+
assertArrayEquals(new long[] {3, 4}, out.maxAsLongArray());
118168
}
119169

120170
private void testEquality(final Img<UnsignedByteType> img1,

0 commit comments

Comments
 (0)