-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add forward and backward difference. * PartialDerivative: add tests * POM: bump parent to pom-imglib2 10.0.2 This makes the class LoopBuilder available. * PartialDerivative: use LoopBuilder to get clean code * Add formulae for finite diffierence methods Addresses comments in #44 Also adds formulae for central difference * Update author list of PartialDerivative.java Added: - @tibuch - @maarzt
- Loading branch information
1 parent
2b71c6d
commit 0454000
Showing
3 changed files
with
170 additions
and
70 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
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/test/java/net/imglib2/algorithm/gradient/PartialDerivativeTest.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 @@ | ||
package net.imglib2.algorithm.gradient; | ||
|
||
import net.imglib2.Interval; | ||
import net.imglib2.RandomAccessibleInterval; | ||
import net.imglib2.img.array.ArrayImgs; | ||
import net.imglib2.type.numeric.real.DoubleType; | ||
import net.imglib2.util.Intervals; | ||
import net.imglib2.view.Views; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
/** | ||
* Tests {@link PartialDerivative}. | ||
* | ||
* @author Matthias Arzt | ||
*/ | ||
public class PartialDerivativeTest | ||
{ | ||
|
||
private final double DELTA = 0.0001; | ||
|
||
private final RandomAccessibleInterval< DoubleType > testImage = | ||
createImage2dMinSizeValues( /* min: */ 0, 0, /* size: */ 5, 3, /* values: */ | ||
0.0, 0.0, 0.0, 0.0, 0.0, | ||
0.0, 2.0, 0.0, -1.0, 0.0, | ||
0.0, 0.0, 5.0, 0.0, 0.0 | ||
); | ||
|
||
private final RandomAccessibleInterval< DoubleType > centralDifferenceExpected = | ||
createImage2dMinSizeValues( /* min: */ 1, 0, /* size: */ 3, 3, /* values: */ | ||
0.0, 0.0, 0.0, | ||
0.0, -1.5, 0.0, | ||
2.5, 0.0, -2.5 | ||
); | ||
|
||
private final RandomAccessibleInterval< DoubleType > backwardDifferenceExpected = | ||
createImage2dMinSizeValues( /* min: */ 1, 0, /* size: */ 4, 3, /* values: */ | ||
0.0, 0.0, 0.0, 0.0, | ||
2.0, -2.0, -1.0, 1.0, | ||
0.0, 5.0, -5.0, 0.0 | ||
); | ||
|
||
public static RandomAccessibleInterval< DoubleType > createImage2dMinSizeValues( long minX, long minY, long sizeX, long sizeY, double... values ) | ||
{ | ||
Interval interval = Intervals.createMinSize( minX, minY, sizeX, sizeY ); | ||
return Views.translate( ArrayImgs.doubles( values, Intervals.dimensionsAsLongArray( interval ) ), | ||
Intervals.minAsLongArray( interval ) ); | ||
} | ||
|
||
@Test | ||
public void testGradientCentralDifferenceX() | ||
{ | ||
RandomAccessibleInterval< DoubleType > data = testImage; | ||
RandomAccessibleInterval< DoubleType > result = emptyArrayImg( centralDifferenceExpected ); | ||
PartialDerivative.gradientCentralDifference( data, result, 0 ); | ||
assertImagesEqual( centralDifferenceExpected, result ); | ||
} | ||
|
||
@Test | ||
public void testGradientBackwardDifferenceX() | ||
{ | ||
RandomAccessibleInterval< DoubleType > data = testImage; | ||
RandomAccessibleInterval< DoubleType > result = emptyArrayImg( backwardDifferenceExpected ); | ||
PartialDerivative.gradientBackwardDifference( data, result, 0 ); | ||
assertImagesEqual( backwardDifferenceExpected, result ); | ||
} | ||
|
||
@Test | ||
public void testForwardDifferenceX() | ||
{ | ||
RandomAccessibleInterval< DoubleType > data = testImage; | ||
RandomAccessibleInterval< DoubleType > expected = Views.translate( backwardDifferenceExpected, -1, 0 ); | ||
RandomAccessibleInterval< DoubleType > result = emptyArrayImg( expected ); | ||
PartialDerivative.gradientForwardDifference( data, result, 0 ); | ||
assertImagesEqual( expected, result ); | ||
} | ||
|
||
private void assertImagesEqual( RandomAccessibleInterval< DoubleType > expected, RandomAccessibleInterval< DoubleType > actual ) | ||
{ | ||
assertTrue( Intervals.equals( expected, actual ) ); | ||
assertArrayEquals( imageAsArray( expected ), imageAsArray( actual ), DELTA ); | ||
} | ||
|
||
private double[] imageAsArray( RandomAccessibleInterval< DoubleType > image ) | ||
{ | ||
List< Double > values = new ArrayList<>(); | ||
Views.iterable( image ).forEach( x -> values.add( x.getRealDouble() ) ); | ||
return values.stream().mapToDouble( x -> x ).toArray(); | ||
} | ||
|
||
private RandomAccessibleInterval< DoubleType > emptyArrayImg( Interval interval ) | ||
{ | ||
return Views.translate( ArrayImgs.doubles( Intervals.dimensionsAsLongArray( interval ) ), Intervals.minAsLongArray( interval ) ); | ||
} | ||
} |