Skip to content

Commit 6b91cde

Browse files
gselzerctrueden
authored andcommitted
Create Mean op as a test of the previous commit
1 parent aa88637 commit 6b91cde

5 files changed

Lines changed: 147 additions & 25 deletions

File tree

src/main/java/org/scijava/ops/math/Add.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import com.google.common.collect.Streams;
44

55
import java.math.BigInteger;
6-
import java.util.Arrays;
76
import java.util.function.BiFunction;
7+
import java.util.function.Function;
88
import java.util.stream.Stream;
9+
import java.util.stream.StreamSupport;
910

1011
import org.scijava.core.Priority;
1112
import org.scijava.ops.core.Op;
@@ -16,11 +17,11 @@
1617
import org.scijava.struct.ItemIO;
1718

1819
public class Add {
19-
20+
2021
public static final String NAMES = MathOps.ADD;
21-
22+
2223
// --------- Functions ---------
23-
24+
2425
@Plugin(type = Op.class, name = NAMES)
2526
@Parameter(key = "number1")
2627
@Parameter(key = "number2")
@@ -33,26 +34,26 @@ public Double apply(Double t, Double u) {
3334
}
3435

3536
// This Op is not needed anymore, can be handled via auto op transformation
36-
// @Plugin(type = Op.class, priority = Priority.HIGH, name = NAMES)
37-
// @Parameter(key = "array1")
38-
// @Parameter(key = "array2")
39-
// @Parameter(key = "resultArray", type = ItemIO.OUTPUT)
40-
// public static class MathPointwiseAddDoubleArraysFunction
41-
// implements BiFunction<double[], double[], double[]> {
42-
// @Override
43-
// public double[] apply(double[] arr1, double[] arr2) {
44-
// Stream<Double> s1 = Arrays.stream(arr1).boxed();
45-
// Stream<Double> s2 = Arrays.stream(arr2).boxed();
46-
// return Streams.zip(s1, s2, (num1, num2) -> num1 + num2).mapToDouble(Double::doubleValue).toArray();
47-
// }
48-
// }
49-
37+
// @Plugin(type = Op.class, priority = Priority.HIGH, name = NAMES)
38+
// @Parameter(key = "array1")
39+
// @Parameter(key = "array2")
40+
// @Parameter(key = "resultArray", type = ItemIO.OUTPUT)
41+
// public static class MathPointwiseAddDoubleArraysFunction
42+
// implements BiFunction<double[], double[], double[]> {
43+
// @Override
44+
// public double[] apply(double[] arr1, double[] arr2) {
45+
// Stream<Double> s1 = Arrays.stream(arr1).boxed();
46+
// Stream<Double> s2 = Arrays.stream(arr2).boxed();
47+
// return Streams.zip(s1, s2, (num1, num2) -> num1 + num2).mapToDouble(Double::doubleValue).toArray();
48+
// }
49+
// }
50+
5051
@Plugin(type = Op.class, priority = Priority.HIGH, name = NAMES)
5152
@Parameter(key = "iter1")
5253
@Parameter(key = "iter2")
5354
@Parameter(key = "resultArray", type = ItemIO.OUTPUT)
5455
public static class MathPointwiseAddIterablesFunction<M extends Number, I extends Iterable<M>>
55-
implements BiFunction<I, I, Iterable<Double>> {
56+
implements BiFunction<I, I, Iterable<Double>> {
5657
@Override
5758
public Iterable<Double> apply(I i1, I i2) {
5859
Stream<? extends Number> s1 = Streams.stream((Iterable<? extends Number>) i1);
@@ -62,7 +63,7 @@ public Iterable<Double> apply(I i1, I i2) {
6263
}
6364

6465
// --------- Computers ---------
65-
66+
6667
@Plugin(type = Op.class, name = NAMES)
6768
@Parameter(key = "integer1")
6869
@Parameter(key = "integer2")
@@ -86,9 +87,9 @@ public void compute(double[] in1, double[] in2, double[] out) {
8687
}
8788
}
8889
}
89-
90+
9091
// --------- Inplaces ---------
91-
92+
9293
@Plugin(type = Op.class, name = NAMES)
9394
@Parameter(key = "arrayIO", type = ItemIO.BOTH)
9495
@Parameter(key = "array1")
@@ -100,7 +101,31 @@ public void mutate(double[] io, double[] in2) {
100101
}
101102
}
102103
}
103-
104+
105+
@Plugin(type = Op.class, name = NAMES)
106+
@Parameter(key = "val1")
107+
@Parameter(key = "val2")
108+
@Parameter(key = "output", type = ItemIO.OUTPUT)
109+
public static class MathAddNumbersFunction <N extends Number> implements BiFunction<N, N, Double>{
110+
111+
@Override
112+
public Double apply(N in1, N in2) {
113+
return in1.doubleValue() + in2.doubleValue();
114+
}
115+
}
116+
117+
@Plugin(type = Op.class, name = NAMES)
118+
@Parameter(key = "iterable")
119+
@Parameter(key = "result", type = ItemIO.OUTPUT)
120+
public static class MathReductionAdd <N extends Number> implements Function<Iterable<N>, Double>{
121+
122+
@Override
123+
public Double apply(Iterable<N> iterable) {
124+
return StreamSupport.stream(iterable.spliterator(), false).mapToDouble(Number::doubleValue).sum();
125+
}
126+
}
127+
128+
104129
// @Op
105130
// @Parameter(key = "number1")
106131
// @Parameter(key = "number2")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ops.stats;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
6+
import org.scijava.ops.OpDependency;
7+
import org.scijava.ops.core.Op;
8+
import org.scijava.param.Parameter;
9+
import org.scijava.plugin.Plugin;
10+
import org.scijava.struct.ItemIO;
11+
12+
public class Mean {
13+
14+
@Plugin(type = Op.class, name = "stats.mean")
15+
@Parameter(key = "iterable")
16+
@Parameter(key = "mean", type = ItemIO.OUTPUT)
17+
public static class MeanFunction <N, O> implements Function<Iterable<N>, O>{
18+
19+
@OpDependency(name = "math.add")
20+
Function<Iterable<N>, O> sumFunc;
21+
22+
@OpDependency(name = "stats.size")
23+
Function<Iterable<N>, O> sizeFunc;
24+
25+
@OpDependency(name = "math.div")
26+
BiFunction<O, O, O> divFunc;
27+
28+
@Override
29+
public O apply(Iterable<N> iterable) {
30+
return divFunc.apply(sumFunc.apply(iterable), sizeFunc.apply(iterable));
31+
}
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ops.stats;
2+
3+
import java.util.function.Function;
4+
import java.util.stream.StreamSupport;
5+
6+
import org.scijava.ops.core.Op;
7+
import org.scijava.param.Parameter;
8+
import org.scijava.plugin.Plugin;
9+
import org.scijava.struct.ItemIO;
10+
11+
public class Size {
12+
13+
@Plugin(type = Op.class, name = "stats.size")
14+
@Parameter(key = "iterable")
15+
@Parameter(key = "size", type = ItemIO.OUTPUT)
16+
public static class StatsSizeFunction<T> implements Function<Iterable<T>, Long>{
17+
18+
@Override
19+
public Long apply(Iterable<T> iterable) {
20+
return StreamSupport.stream(iterable.spliterator(), false).count();
21+
}
22+
}
23+
24+
@Plugin(type = Op.class, name = "stats.size")
25+
@Parameter(key = "iterable")
26+
@Parameter(key = "size", type = ItemIO.OUTPUT)
27+
public static class StatsSizeFunctionDouble<T> implements Function<Iterable<T>, Double>{
28+
29+
@Override
30+
public Double apply(Iterable<T> iterable) {
31+
return (double) (long) StreamSupport.stream(iterable.spliterator(), false).count();
32+
}
33+
}
34+
}

src/test/java/org/scijava/ops/AbstractTestEnvironment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public void tearDown() {
2424
ops = null;
2525
}
2626

27-
static OpService ops() {
27+
public static OpService ops() {
2828
return ops;
2929
}
3030

31-
static boolean arrayEquals(double[] arr1, Double... arr2) {
31+
public static boolean arrayEquals(double[] arr1, Double... arr2) {
3232
return Arrays.deepEquals(Arrays.stream(arr1).boxed().toArray(Double[]::new), arr2);
3333
}
3434
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scijava.ops.stats;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.function.Function;
8+
9+
import org.junit.Test;
10+
import org.scijava.ops.AbstractTestEnvironment;
11+
import org.scijava.ops.types.Nil;
12+
import org.scijava.ops.util.Functions;
13+
14+
public class MeanTest <N extends Number> extends AbstractTestEnvironment{
15+
16+
@Test
17+
public void regressionTest() {
18+
19+
Function<Iterable<Integer>, Double> goodFunc = Functions.unary(ops(), "stats.mean", new Nil<Iterable<Integer>>() {}, new Nil<Double>() {});
20+
21+
List<Integer> goodNums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
22+
double expected = 5.5;
23+
24+
double actual = goodFunc.apply(goodNums);
25+
26+
assertEquals(expected, actual, 0);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)