Skip to content

Commit 04af3fe

Browse files
committed
Separate Op into integer case and realType case
This allows us to handle integer types and realTypes separately. The reason for this is as follows: It seems clear that we would want to allow floating point randomness when our type allows floating points If we always base our calculations on floating points, this results in edge effects when we add noise to integer types. This is due to rounding: suppose we have a pseudorandomly generated double between 0 and 2. Most IntegerTypes simply round that double to a long, integer, etc in setReal. Thus the probability we add 0 to the input is 0.25, 1 to the input is 0.5, and 2 to the input is 0.25. To get around this case we want to generate the random number differently when we have an integer type. For these reasons we have two different Ops to perform the different noise generation and have shared logic for setting the output. N.B. We do not allow wrapping for floating point types. This is because we operate within the following constraint: the number after the max of the type should map to the minimum of the type. In the case of floating points, what then should happen to a number equal to 0.5 + the maximum of the type? In this paradigm, we would expect it to be the minimum of the type - 0.5; this is impossible, however, since this would be outside of the range of the type. For this reason, we always clamp.
1 parent 5440061 commit 04af3fe

File tree

3 files changed

+155
-11
lines changed

3 files changed

+155
-11
lines changed

src/main/java/net/imagej/ops/filter/FilterNamespace.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,44 @@ public <I extends RealType<I>, O extends RealType<O>> O addPoissonNoise(
131131
return result;
132132
}
133133

134+
// -- Uniform Noise --
135+
134136
@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
135-
public <I extends RealType<I>, O extends RealType<O>> O addUniformNoise(final O out,
137+
public <I extends RealType<I>> I addUniformNoise(final I out,
136138
final I in, final double rangeMin, final double rangeMax)
137139
{
138140
@SuppressWarnings("unchecked")
139-
final O result = (O) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
141+
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
140142
rangeMax);
141143
return result;
142144
}
143145

144146
@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
145-
public <I extends RealType<I>, O extends RealType<O>> O addUniformNoise(final O out,
147+
public <I extends RealType<I>> I addUniformNoise(final I out,
146148
final I in, final double rangeMin, final double rangeMax, final long seed)
147149
{
148150
@SuppressWarnings("unchecked")
149-
final O result = (O) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
151+
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
152+
rangeMax, seed);
153+
return result;
154+
}
155+
156+
@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
157+
public <I extends RealType<I>> I addUniformNoise(final I out,
158+
final I in, final long rangeMin, final long rangeMax)
159+
{
160+
@SuppressWarnings("unchecked")
161+
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
162+
rangeMax);
163+
return result;
164+
}
165+
166+
@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
167+
public <I extends RealType<I>> I addUniformNoise(final I out,
168+
final I in, final long rangeMin, final long rangeMax, final long seed)
169+
{
170+
@SuppressWarnings("unchecked")
171+
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
150172
rangeMax, seed);
151173
return result;
152174
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.filter.addUniformNoise;
31+
32+
import java.math.BigInteger;
33+
34+
import net.imagej.ops.Ops;
35+
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
36+
import net.imglib2.type.numeric.IntegerType;
37+
38+
import org.scijava.Priority;
39+
import org.scijava.plugin.Parameter;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.util.MersenneTwisterFast;
42+
43+
/**
44+
* Adds a pseudorandomly generated value {@code x} to a {@link IntegerType}
45+
* {@code I}, such that {@code x} is (inclusively) bounded by {@code rangeMin}
46+
* and {@code rangeMax} parameters, i.e. {@code rangeMin <= x <= rangeMax}.
47+
*
48+
* @author Gabe Selzer
49+
*/
50+
@Plugin(type = Ops.Filter.AddUniformNoise.class, priority = Priority.HIGH)
51+
public class AddUniformNoiseIntegerType<I extends IntegerType<I>> extends
52+
AbstractUnaryComputerOp<I, I> implements Ops.Filter.AddUniformNoise
53+
{
54+
55+
/**
56+
* The greatest that an input value can be decreased.
57+
*/
58+
@Parameter
59+
private long rangeMin;
60+
61+
/**
62+
* The greatest that an input value can be <b> increased </b>
63+
*/
64+
@Parameter
65+
private long rangeMax;
66+
67+
/**
68+
* If false, the Op will wrap outputs that are outside of the type bounds,
69+
* instead of clamping them
70+
*/
71+
@Parameter(required = false)
72+
private boolean clampOutput = true;
73+
74+
@Parameter(required = false)
75+
private long seed = 0xabcdef1234567890L;
76+
private long range;
77+
78+
private MersenneTwisterFast rng;
79+
80+
@Override
81+
public void initialize() {
82+
if (rng == null) rng = new MersenneTwisterFast(seed);
83+
if (rangeMax < rangeMin) {
84+
long temp = rangeMax;
85+
rangeMax = rangeMin;
86+
rangeMin = temp;
87+
}
88+
// MersenneTwister can only generate numbers that can fit into a long.
89+
range = Math.subtractExact(rangeMax + 1, rangeMin);
90+
}
91+
92+
@Override
93+
public void compute(I input, I output) {
94+
final double newVal = rng.nextLong(range) + rangeMin + input
95+
.getRealDouble();
96+
97+
AddUniformNoiseRealType.setOutput(newVal, clampOutput, output);
98+
}
99+
100+
}

src/main/java/net/imagej/ops/filter/addUniformNoise/AddUniformNoiseRealType.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,39 @@ public void initialize() {
8181
if (!Double.isFinite(rangeMax - rangeMin))
8282
throw new IllegalArgumentException("range not allowed by op.");
8383
}
84-
84+
8585
@Override
8686
public void compute(I input, I output) {
8787
final double newVal = (rangeMax - rangeMin) * rng.nextDouble(true, true) +
8888
rangeMin + input.getRealDouble();
89-
if (newVal > input.getMaxValue())
90-
output.setReal(input.getMaxValue());
91-
else if (newVal < input.getMinValue())
92-
output.setReal(input.getMinValue());
93-
else
94-
output.setReal(newVal);
89+
90+
setOutput(newVal, true, output);
91+
}
92+
93+
protected static <I extends RealType<I>> void setOutput(double newVal, boolean clampOutput, I output) {
94+
// clamp output
95+
if (clampOutput) {
96+
output.setReal(Math.max(output.getMinValue(), Math.min(output.getMaxValue(),
97+
newVal)));
98+
}
99+
100+
// wrap output
101+
else {
102+
double outVal = newVal;
103+
// when output larger than max value, add difference of output and max
104+
// value to the min value
105+
while (outVal > output.getMaxValue()) {
106+
outVal = output.getMinValue() + (outVal - output.getMaxValue() - 1);
107+
}
108+
// when output smaller than min value, subtract difference of output and
109+
// min value from the max value
110+
while (outVal < output.getMinValue()) {
111+
outVal = output.getMaxValue() - (output.getMinValue() - outVal - 1);
112+
}
113+
114+
output.setReal(outVal);
115+
}
116+
95117
}
96118

97119
}

0 commit comments

Comments
 (0)