Skip to content

Commit 80c4afc

Browse files
committed
Beautify NoiseAdders
1 parent 82c3f2c commit 80c4afc

1 file changed

Lines changed: 54 additions & 82 deletions

File tree

  • scijava-ops-image/src/main/java/org/scijava/ops/image/filter/addNoise

scijava-ops-image/src/main/java/org/scijava/ops/image/filter/addNoise/NoiseAdders.java

Lines changed: 54 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,21 @@
3434
import net.imglib2.RandomAccessibleInterval;
3535
import net.imglib2.loops.LoopBuilder;
3636
import net.imglib2.type.numeric.RealType;
37-
38-
import org.scijava.function.Computers;
37+
import org.scijava.ops.spi.Nullable;
3938

4039
/**
4140
* Contains Ops designed to add noise to populated images.
4241
*
4342
* @author Gabriel Selzer
44-
*
45-
* @param <I>
46-
* type of input
47-
* @param <O>
48-
* type of output
4943
*/
50-
public class NoiseAdders<I extends RealType<I>, O extends RealType<O>> {
44+
public class NoiseAdders {
45+
46+
private static final Long defaultSeed = 0xabcdef1234567890L;
5147

5248
/**
5349
* Sets the real component of an output real number to the addition of the real
5450
* component of an input real number with an amount of Gaussian noise.
55-
*
51+
* <p>
5652
* Note that this Op has changed relative to the older implementations; before
5753
* it operated on RealTypes, we now only provide the operation on
5854
* Iterable<RealType>s. This is due to the nature of {@link Random}: The old
@@ -63,46 +59,31 @@ public class NoiseAdders<I extends RealType<I>, O extends RealType<O>> {
6359
* {@link Iterable}. Since the {@link Random} is created upon every call of the
6460
* Op it ensures that given the same seed and input data the output will always
6561
* be the same.
66-
* @input input
67-
* @input rangeMin
68-
* @input rangeMax
69-
* @input rangeStdDev
70-
* @input seed
71-
* @container output
72-
* @implNote op names='filter.addNoise'
73-
*/
74-
public final Computers.Arity5<RandomAccessibleInterval<I>, Double, Double, Double, Long, RandomAccessibleInterval<O>> addNoiseInterval = (
75-
input, rangeMin, rangeMax, rangeStdDev, seed, output) -> {
76-
addNoise(input, output, rangeMin, rangeMax, rangeStdDev, new Random(seed));
77-
};
78-
79-
/**
80-
* Convenience Op for when the user does not pass through a seed (default seed
81-
* taken from past implementation).
82-
* @input input
83-
* @input rangeMin
84-
* @input rangeMax
85-
* @input rangeStdDev
86-
* @container output
87-
* @implNote op names='filter.addNoise'
62+
* @param input
63+
* @param rangeMin
64+
* @param rangeMax
65+
* @param rangeStdDev
66+
* @param seed
67+
* @param output
68+
* @implNote op names='filter.addNoise', type=Computer
8869
*/
89-
public final Computers.Arity4<RandomAccessibleInterval<I>, Double, Double, Double, RandomAccessibleInterval<O>> addNoiseIntervalSeedless = (
90-
input, rangeMin, rangeMax, rangeStdDev,
91-
output) -> addNoiseInterval.compute(input, rangeMin, rangeMax, rangeStdDev, 0xabcdef1234567890L, output);
92-
93-
// -- Static utility methods --
94-
95-
// Runs the below method on every element of the input iterables.
96-
public static <I extends RealType<I>, O extends RealType<O>> void addNoise(
97-
final RandomAccessibleInterval<I> input,
98-
final RandomAccessibleInterval<O> output, final double rangeMin,
99-
final double rangeMax, final double rangeStdDev, final Random rng)
100-
{
70+
public static <I extends RealType<I>, O extends RealType<O>> void addNoiseInterval( //
71+
final RandomAccessibleInterval<I> input, //
72+
final Double rangeMin, //
73+
final Double rangeMax, //
74+
final Double rangeStdDev, //
75+
@Nullable Long seed, //
76+
final RandomAccessibleInterval<O> output //
77+
) {
78+
if (seed == null) {
79+
seed = defaultSeed;
80+
}
81+
Random rng = new Random(seed);
10182
LoopBuilder.setImages(input, output).multiThreaded().forEachPixel((in,
102-
out) -> {
83+
out) -> {
10384
addNoise(in, out, rangeMin, rangeMax, rangeStdDev, rng);
10485
});
105-
}
86+
};
10687

10788
// Copied from the previous implementation of addNoise
10889
public static <I extends RealType<I>, O extends RealType<O>> void addNoise(final I input, final O output,
@@ -134,50 +115,42 @@ public static <I extends RealType<I>, O extends RealType<O>> void addNoise(final
134115
* </p>
135116
*
136117
* @author Jan Eglinger
137-
*
138-
* Note that this Op has changed relative to the older implementations;
139-
* before it operated on RealTypes, we now only provide the operation on
140-
* Iterable<RealType>s. This is due to the nature of {@link Random}: The
141-
* old implementation saved a {@link Random} and used it on each
142-
* {@link RealType} passed to the Op. This provided no deterministic
143-
* output, as the same input would yield two different outputs if called
144-
* in succession. Thus in this iteration of the Op we make it a
145-
* requirement that the input must be an {@link Iterable}. Since the
146-
* {@link Random} is created upon every call of the Op it ensures that
147-
* given the same seed and input data the output will always be the
148-
* same.
118+
* <p>
119+
* Note that this Op has changed relative to the older implementations;
120+
* before it operated on RealTypes, we now only provide the operation on
121+
* Iterable<RealType>s. This is due to the nature of {@link Random}: The
122+
* old implementation saved a {@link Random} and used it on each
123+
* {@link RealType} passed to the Op. This provided no deterministic
124+
* output, as the same input would yield two different outputs if called
125+
* in succession. Thus in this iteration of the Op we make it a
126+
* requirement that the input must be an {@link Iterable}. Since the
127+
* {@link Random} is created upon every call of the Op it ensures that
128+
* given the same seed and input data the output will always be the
129+
* same.
149130
*
150-
* @input input
151-
* @input seed
152-
* @container output
153-
* @implNote op names='filter.addPoissonNoise'
154-
*/
155-
public final Computers.Arity2<RandomAccessibleInterval<I>, Long, RandomAccessibleInterval<O>> addPoissonNoiseInterval = (input, seed,
156-
output) -> addPoissonNoise(input, new Random(seed), output);
157-
158-
/**
159-
* Convenience Op for when the user does not pass through a seed (default seed
160-
* taken from past implementation).
161-
* @input input
162-
* @container output
163-
* @implNote op names='filter.addPoissonNoise'
131+
* @param input
132+
* @param seed
133+
* @param output
134+
* @implNote op names='filter.addPoissonNoise', type=Computer
164135
*/
165-
public final Computers.Arity1<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>> addPoissonNoiseIntervalSeedless = (input,
166-
output) -> addPoissonNoise(input, new Random(0xabcdef1234567890L), output);
167-
168-
// -- Static utility methods --
169-
170-
// Runs the below method on every element of the input iterables.
171-
public static <I extends RealType<I>, O extends RealType<O>> void
172-
addPoissonNoise(final RandomAccessibleInterval<I> input, final Random rng,
173-
final RandomAccessibleInterval<O> output)
136+
public static <I extends RealType<I>, O extends RealType<O>> void addPoissonNoiseInterval ( //
137+
final RandomAccessibleInterval<I> input, //
138+
@Nullable Long seed, //
139+
final RandomAccessibleInterval<O> output //
140+
)
174141
{
142+
if (seed == null) {
143+
seed = defaultSeed;
144+
}
145+
Random rng = new Random(seed);
175146
LoopBuilder.setImages(input, output).multiThreaded().forEachPixel((in,
176-
out) -> {
147+
out) -> {
177148
addPoissonNoise(in, rng, out);
178149
});
179150
}
180151

152+
// -- Static utility methods --
153+
181154
// Copied from the previous implementation of addNoise
182155
public static <I extends RealType<I>, O extends RealType<O>> void addPoissonNoise(final I input, final Random rng,
183156
final O output) {
@@ -189,7 +162,6 @@ public static <I extends RealType<I>, O extends RealType<O>> void addPoissonNois
189162
p *= rng.nextDouble();
190163
} while (p >= l);
191164
output.setReal(k - 1);
192-
return;
193165
}
194166

195167
}

0 commit comments

Comments
 (0)