Skip to content

Commit ab0b145

Browse files
authored
Merge pull request #76 from scijava/scijava/scijava-ops-engine/adapt-param-refinement
Use matched dependencies to refine adapted Op search
2 parents 448e95e + 2b3a61b commit ab0b145

8 files changed

Lines changed: 111 additions & 163 deletions

File tree

‎imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyArrayImg.java‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,3 @@ public void compute(final ArrayImg<T, A> input, final ArrayImg<T, A> output) {
6666

6767
}
6868
}
69-
70-
/**
71-
*@implNote op names='copy, copy.img', priority='10000.'
72-
*/
73-
class CopyArrayImgFunction<T extends NativeType<T>, A extends ArrayDataAccess<A>>
74-
implements Function<ArrayImg<T, A>, ArrayImg<T, A>> {
75-
76-
@OpDependency(name = "copy.img")
77-
private Computers.Arity1<ArrayImg<T, A>, ArrayImg<T, A>> copyOp;
78-
79-
/**
80-
* TODO
81-
*
82-
* @param input
83-
* @param copy
84-
*/
85-
@Override
86-
public ArrayImg<T, A> apply(ArrayImg<T, A> input) {
87-
// NB: Workaround for ArrayImgFactory not overriding create(Dimensions, T).
88-
final long[] dims = new long[input.numDimensions()];
89-
input.dimensions(dims);
90-
final ArrayImg<T, ?> copy = input.factory().create(dims, input.firstElement().createVariable());
91-
92-
// TODO: Find a way to guarantee the type.
93-
@SuppressWarnings("unchecked")
94-
final ArrayImg<T, A> typedCopy = (ArrayImg<T, A>) copy;
95-
copyOp.compute(input, typedCopy);
96-
97-
return typedCopy;
98-
}
99-
100-
}

‎imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyII.java‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,3 @@ public void compute(final IterableInterval<T> input, final IterableInterval<T> o
6767
mapped.compute(input, output);
6868
}
6969
}
70-
71-
/**
72-
*@implNote op names='copy, copy.iterableInterval', priority='1.0'
73-
*/
74-
class CopyIIFunction<T> implements Function<IterableInterval<T>, IterableInterval<T>> {
75-
76-
@OpDependency(name = "create.img")
77-
private BiFunction<Dimensions, T, IterableInterval<T>> imgCreator;
78-
@OpDependency(name = "copy.iterableInterval")
79-
private Computers.Arity1<IterableInterval<T>, IterableInterval<T>> copyOp;
80-
81-
/**
82-
* TODO
83-
*
84-
* @param input
85-
* @return the output
86-
*/
87-
@Override
88-
public IterableInterval<T> apply(IterableInterval<T> input) {
89-
IterableInterval<T> output = imgCreator.apply(input, input.firstElement());
90-
copyOp.compute(input, output);
91-
return output;
92-
}
93-
94-
}

‎imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyImg.java‎

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

‎imagej/imagej-ops2/src/main/java/net/imagej/ops2/create/Creators.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@
4040
import net.imglib2.RandomAccessibleInterval;
4141
import net.imglib2.img.Img;
4242
import net.imglib2.img.ImgFactory;
43+
import net.imglib2.img.array.ArrayImg;
4344
import net.imglib2.img.array.ArrayImgFactory;
45+
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
4446
import net.imglib2.roi.labeling.ImgLabeling;
4547
import net.imglib2.roi.labeling.LabelingMapping;
4648
import net.imglib2.type.BooleanType;
4749
import net.imglib2.type.NativeType;
50+
import net.imglib2.type.NativeTypeFactory;
4851
import net.imglib2.type.Type;
4952
import net.imglib2.type.logic.BitType;
5053
import net.imglib2.type.numeric.ComplexType;
@@ -68,7 +71,7 @@
6871
import org.scijava.function.Functions;
6972
import org.scijava.function.Producer;
7073

71-
public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T extends Type<T>, C extends ComplexType<C>, W extends ComplexType<W> & NativeType<W>, B extends BooleanType<B>> {
74+
public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T extends Type<T>, C extends ComplexType<C>, W extends ComplexType<W> & NativeType<W>, B extends BooleanType<B>, A extends ArrayDataAccess<A>> {
7275

7376
/* ImgFactories */
7477

@@ -200,6 +203,17 @@ public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T ex
200203
public final Function<RandomAccessibleInterval<T>, Img<T>> imgFromRAI = (rai) -> imgFromDimsAndType.apply(rai,
201204
Util.getTypeFromInterval(rai));
202205

206+
/**
207+
* @input arrayImg
208+
* @output img
209+
* @implNote op names='create, create.img', priority='1000.'
210+
*/
211+
@SuppressWarnings("unchecked")
212+
public final Function<ArrayImg<N, A>, ArrayImg<N, A>> arrayImgFromArrayImg //
213+
= input -> (ArrayImg<N, A>) input //
214+
.factory() //
215+
.create(input.dimensionsAsLongArray());
216+
203217
/* IntegerType */
204218

205219
/**

‎imagej/imagej-ops2/src/test/java/net/imagej/ops2/OpRegressionTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class OpRegressionTest extends AbstractOpTest {
3939

4040
@Test
4141
public void opDiscoveryRegressionIT() {
42-
long expected = 1447;
42+
long expected = 1444;
4343
long actual = StreamSupport.stream(ops.infos().spliterator(), false).count();
4444
assertEquals(expected, actual);
4545
}

‎imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyIITest.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
import net.imglib2.Cursor;
3636
import net.imglib2.IterableInterval;
3737
import net.imglib2.img.Img;
38+
import net.imglib2.img.array.ArrayImg;
3839
import net.imglib2.img.array.ArrayImgFactory;
40+
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
3941
import net.imglib2.img.planar.PlanarImgFactory;
42+
import net.imglib2.type.NativeType;
4043
import net.imglib2.type.numeric.real.DoubleType;
4144
import net.imglib2.type.numeric.real.FloatType;
4245

@@ -68,7 +71,7 @@ public void createData() {
6871
}
6972

7073
@Test
71-
public void copyRAINoOutputTest() {
74+
public void copyIINoOutputTest() {
7275
IterableInterval<DoubleType> output = ops.op("copy.iterableInterval").arity1().input(input)
7376
.outType(new Nil<IterableInterval<DoubleType>>() {}).apply();
7477

@@ -94,7 +97,7 @@ public void copyTypeTest() {
9497
}
9598

9699
@Test
97-
public void copyRAIWithOutputTest() {
100+
public void copyIIWithOutputTest() {
98101
Img<DoubleType> output = input.factory().create(input, input.firstElement());
99102

100103
ops.op("copy.iterableInterval").arity1().input(input).output(output).compute();
@@ -105,5 +108,10 @@ public void copyRAIWithOutputTest() {
105108
while (inc.hasNext()) {
106109
assertEquals(inc.next().get(), outc.next().get(), 0.0);
107110
}
111+
Img<FloatType> bar = foo();
112+
}
113+
114+
private static <N extends NativeType<N>, A extends ArrayDataAccess<A>> ArrayImg<N, A> foo() {
115+
return null;
108116
}
109117
}

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AdaptationMatchingRoutine.java‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,45 @@ public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher,
9292

9393
try {
9494
// resolve adaptor dependencies
95-
// Type reifiedAdaptTo = Types.substituteTypeVariables(adaptor.output().getType(), map);
96-
// Type reifiedAdaptFrom = Types.substituteTypeVariables(adaptor.inputs().get(0).getType(), map);
97-
// Type reifiedAdaptorType = Types.parameterize(Function.class, new Type[] {reifiedAdaptFrom, reifiedAdaptTo});
98-
//
99-
// InfoChain adaptorChain = env.chainFromInfo(adaptor, Nil.of(
100-
// reifiedAdaptorType, conditions.hints()));
101-
List<InfoChain> depChains = adaptor.dependencies().stream().map(d -> inferOpRef(d, map)).map(ref -> {
95+
final Map<TypeVariable<?>, Type> adaptorBounds = new HashMap<>();
96+
final Map<TypeVariable<?>, Type> dependencyBounds = new HashMap<>();
97+
List<InfoChain> depChains = adaptor.dependencies().stream().map(d -> {
98+
OpRef ref = inferOpRef(d, map);
10299
Nil<?> type = Nil.of(ref.getType());
103-
Nil<?>[] args = Arrays.stream(ref.getArgs()).map(t -> Nil.of(t)).toArray(Nil[]::new);
100+
Nil<?>[] args = Arrays.stream(ref.getArgs()).map(Nil::of).toArray(
101+
Nil[]::new);
104102
Nil<?> outType = Nil.of(ref.getOutType());
105-
return env.infoChain(ref.getName(), type, args, outType, adaptationHints);
103+
InfoChain chain = env.infoChain(ref.getName(), type, args, outType,
104+
adaptationHints);
105+
// Check if the bounds of the dependency can inform the type of the
106+
// adapted Op
107+
final Type matchedOpType = chain.info().opType();
108+
// Find adaptor type variable bounds fulfilled by matched Op
109+
GenericAssignability.inferTypeVariables( //
110+
new Type[] { d.getType() }, //
111+
new Type[] { matchedOpType }, //
112+
dependencyBounds //
113+
);
114+
for (TypeVariable<?> typeVar : map.keySet()) {
115+
// Ignore TypeVariables not present in this particular dependency
116+
if (!dependencyBounds.containsKey(typeVar)) continue;
117+
Type matchedType = dependencyBounds.get(typeVar);
118+
// Resolve any type variables from the dependency ref that we can
119+
GenericAssignability.inferTypeVariables( //
120+
new Type[] { ref.getType() }, //
121+
new Type[] { matchedOpType }, //
122+
adaptorBounds //
123+
);
124+
Type mapped = Types.mapVarToTypes(matchedType, adaptorBounds);
125+
// If the type variable is more specific now, update it
126+
if (Types.isAssignable(mapped, map.get(typeVar))) {
127+
map.put(typeVar, mapped);
128+
}
129+
}
130+
dependencyBounds.clear();
131+
return chain;
106132
}).collect(Collectors.toList());
107-
InfoChain adaptorChain = new InfoChain(adaptor,
108-
depChains);
133+
InfoChain adaptorChain = new InfoChain(adaptor, depChains);
109134

110135
// grab the first type parameter from the OpInfo and search for
111136
// an Op that will then be adapted (this will be the only input of the
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
package org.scijava.ops.engine.adapt.functional;
3+
4+
import java.util.function.Function;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.scijava.function.Computers;
10+
import org.scijava.ops.engine.AbstractTestEnvironment;
11+
import org.scijava.ops.engine.adapt.functional.ComputersToFunctionsViaFunction.Computer1ToFunction1ViaFunction;
12+
import org.scijava.ops.spi.OpCollection;
13+
import org.scijava.ops.spi.OpField;
14+
import org.scijava.ops.spi.OpMethod;
15+
16+
public class ComputerToFunctionObjectOutputTest extends AbstractTestEnvironment
17+
implements OpCollection
18+
{
19+
20+
@BeforeAll
21+
public static void addNeededOps() {
22+
// Register Ops from this class
23+
ops.register(new ComputerToFunctionObjectOutputTest());
24+
// Register needed adapt Op
25+
ops.register(new Computer1ToFunction1ViaFunction<>());
26+
}
27+
28+
@OpMethod(names = "test.objectOutput", type = Computers.Arity1.class)
29+
public static void computer(final double[] input, final double[] output) {
30+
for (int i = 0; i < input.length; i++) {
31+
output[i] = input[i] + 1;
32+
}
33+
}
34+
35+
@OpField(names = "create")
36+
public final Function<double[], double[]> creator =
37+
in -> new double[in.length];
38+
39+
@Test
40+
public void testObjectOutput() {
41+
double[] input = { 1.0, 2.0, 3.0, 4.0 };
42+
double[] output = (double[]) ops.op("test.objectOutput").arity1().input(input).apply();
43+
for (int i = 0; i < input.length; i++) {
44+
Assertions.assertEquals(output[i], input[i] + 1);
45+
}
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)