Skip to content

Commit 430bcdb

Browse files
Treiblesschorlectrueden
authored andcommitted
Add test for secondary args
1 parent 7014a23 commit 430bcdb

2 files changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.scijava.ops.math;
2+
3+
import java.util.Arrays;
4+
import java.util.function.Function;
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 Normalize {
12+
13+
public static final String NAMES = "math.minmax";
14+
15+
@Plugin(type = Op.class, name = NAMES)
16+
@Parameter(key = "numbers")
17+
@Parameter(key = "normalized", type = ItemIO.OUTPUT)
18+
public static class MathMinMaxNormalizeFunction implements Function<double[], double[]> {
19+
20+
@Parameter
21+
private Double newMin;
22+
23+
@Parameter(required = false)
24+
private Double newMax;
25+
26+
@Override
27+
public double[] apply(double[] t) {
28+
if (newMax == null) {
29+
newMax = 1.0;
30+
}
31+
if (newMin >= newMax) {
32+
throw new IllegalStateException("Min must be smaller than max.");
33+
}
34+
35+
double min = Arrays.stream(t).min().getAsDouble();
36+
double max = Arrays.stream(t).max().getAsDouble();
37+
38+
return Arrays.stream(t).map(d -> norm(d, min, max)).toArray();
39+
}
40+
41+
private double norm(double d, double dataMin, double dataMax) {
42+
return newMin + (((d - dataMin)*(newMax - newMin))/(dataMax - dataMin));
43+
}
44+
}
45+
46+
47+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava 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 org.scijava.ops;
31+
import java.util.Arrays;
32+
import java.util.function.Function;
33+
import java.util.stream.IntStream;
34+
35+
import static org.junit.Assert.assertTrue;
36+
37+
import org.junit.Test;
38+
import org.scijava.param.ValidityException;
39+
import org.scijava.ops.types.Nil;
40+
41+
public class OptionalParametersTest extends AbstractTestEnvironment {
42+
43+
Nil<Double> nilDouble = new Nil<Double>() {
44+
};
45+
46+
Nil<double[]> nilDoubleArray = new Nil<double[]>() {
47+
};
48+
49+
@Test
50+
public void testSecondaryArgs() throws ValidityException {
51+
double min = 1.0;
52+
double max = 2.9;
53+
54+
// Min and max set
55+
Function<double[], double[]> normFunction = ops().findOp( //
56+
"minmax", new Nil<Function<double[], double[]>>() {
57+
}, //
58+
new Nil[] { nilDoubleArray, nilDouble, nilDouble }, //
59+
nilDoubleArray,
60+
min, max//
61+
);
62+
63+
// Max is not required, one arg is give for required min, max will be default 1.0
64+
double[] nums = IntStream.range(0, 10).mapToDouble(i -> i).toArray();
65+
double[] res = normFunction.apply(nums);
66+
assertTrue(Math.abs(max - Arrays.stream(res).max().getAsDouble()) < 0.000001);
67+
assertTrue(Math.abs(min - Arrays.stream(res).min().getAsDouble()) < 0.000001);
68+
69+
normFunction = ops().findOp( //
70+
"minmax", new Nil<Function<double[], double[]>>() {
71+
}, //
72+
new Nil[] { nilDoubleArray, nilDouble, nilDouble }, //
73+
nilDoubleArray,
74+
0.5//
75+
);
76+
77+
res = normFunction.apply(nums);
78+
assertTrue(Math.abs(1.0 - Arrays.stream(res).max().getAsDouble()) < 0.000001);
79+
assertTrue(Math.abs(0.5 - Arrays.stream(res).min().getAsDouble()) < 0.000001);
80+
}
81+
82+
@Test(expected = IllegalStateException.class)
83+
public void testRequiredMissing() throws ValidityException {
84+
// One required, but none are given
85+
ops().findOp( //
86+
"minmax", new Nil<Function<double[], double[]>>() {
87+
}, //
88+
new Nil[] { nilDoubleArray, nilDouble, nilDouble }, //
89+
nilDoubleArray
90+
);
91+
}
92+
93+
@Test(expected = IllegalStateException.class)
94+
public void testTooMannyArgs() throws ValidityException {
95+
// One required, one optional, but three given
96+
ops().findOp( //
97+
"minmax", new Nil<Function<double[], double[]>>() {
98+
}, //
99+
new Nil[] { nilDoubleArray, nilDouble, nilDouble }, //
100+
nilDoubleArray,
101+
1,2,3
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)