-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/net/imagej/ops/filter/addUniformNoise/AddUniformNoiseIntegerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters