Skip to content

Commit

Permalink
Separate Op into integer case and realType case
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gselzer committed May 19, 2020
1 parent 5440061 commit 04af3fe
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 11 deletions.
30 changes: 26 additions & 4 deletions src/main/java/net/imagej/ops/filter/FilterNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,44 @@ public <I extends RealType<I>, O extends RealType<O>> O addPoissonNoise(
return result;
}

// -- Uniform Noise --

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
public <I extends RealType<I>, O extends RealType<O>> O addUniformNoise(final O out,
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final double rangeMin, final double rangeMax)
{
@SuppressWarnings("unchecked")
final O result = (O) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
public <I extends RealType<I>, O extends RealType<O>> O addUniformNoise(final O out,
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final double rangeMin, final double rangeMax, final long seed)
{
@SuppressWarnings("unchecked")
final O result = (O) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax, seed);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final long rangeMin, final long rangeMax)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final long rangeMin, final long rangeMax, final long seed)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax, seed);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2018 ImageJ developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.ops.filter.addUniformNoise;

import java.math.BigInteger;

import net.imagej.ops.Ops;
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
import net.imglib2.type.numeric.IntegerType;

import org.scijava.Priority;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.util.MersenneTwisterFast;

/**
* Adds a pseudorandomly generated value {@code x} to a {@link IntegerType}
* {@code I}, such that {@code x} is (inclusively) bounded by {@code rangeMin}
* and {@code rangeMax} parameters, i.e. {@code rangeMin <= x <= rangeMax}.
*
* @author Gabe Selzer
*/
@Plugin(type = Ops.Filter.AddUniformNoise.class, priority = Priority.HIGH)
public class AddUniformNoiseIntegerType<I extends IntegerType<I>> extends
AbstractUnaryComputerOp<I, I> implements Ops.Filter.AddUniformNoise
{

/**
* The greatest that an input value can be decreased.
*/
@Parameter
private long rangeMin;

/**
* The greatest that an input value can be <b> increased </b>
*/
@Parameter
private long rangeMax;

/**
* If false, the Op will wrap outputs that are outside of the type bounds,
* instead of clamping them
*/
@Parameter(required = false)
private boolean clampOutput = true;

@Parameter(required = false)
private long seed = 0xabcdef1234567890L;
private long range;

private MersenneTwisterFast rng;

@Override
public void initialize() {
if (rng == null) rng = new MersenneTwisterFast(seed);
if (rangeMax < rangeMin) {
long temp = rangeMax;
rangeMax = rangeMin;
rangeMin = temp;
}
// MersenneTwister can only generate numbers that can fit into a long.
range = Math.subtractExact(rangeMax + 1, rangeMin);
}

@Override
public void compute(I input, I output) {
final double newVal = rng.nextLong(range) + rangeMin + input
.getRealDouble();

AddUniformNoiseRealType.setOutput(newVal, clampOutput, output);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,39 @@ public void initialize() {
if (!Double.isFinite(rangeMax - rangeMin))
throw new IllegalArgumentException("range not allowed by op.");
}

@Override
public void compute(I input, I output) {
final double newVal = (rangeMax - rangeMin) * rng.nextDouble(true, true) +
rangeMin + input.getRealDouble();
if (newVal > input.getMaxValue())
output.setReal(input.getMaxValue());
else if (newVal < input.getMinValue())
output.setReal(input.getMinValue());
else
output.setReal(newVal);

setOutput(newVal, true, output);
}

protected static <I extends RealType<I>> void setOutput(double newVal, boolean clampOutput, I output) {
// clamp output
if (clampOutput) {
output.setReal(Math.max(output.getMinValue(), Math.min(output.getMaxValue(),
newVal)));
}

// wrap output
else {
double outVal = newVal;
// when output larger than max value, add difference of output and max
// value to the min value
while (outVal > output.getMaxValue()) {
outVal = output.getMinValue() + (outVal - output.getMaxValue() - 1);
}
// when output smaller than min value, subtract difference of output and
// min value from the max value
while (outVal < output.getMinValue()) {
outVal = output.getMaxValue() - (output.getMinValue() - outVal - 1);
}

output.setReal(outVal);
}

}

}

0 comments on commit 04af3fe

Please sign in to comment.