|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImgLib2: a general-purpose, multidimensional image processing library. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2018 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld, |
| 6 | + * John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke, |
| 7 | + * Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner, |
| 8 | + * Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert, |
| 9 | + * Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin, |
| 10 | + * Jean-Yves Tinevez and Michael Zinsmaier. |
| 11 | + * %% |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimer in the documentation |
| 19 | + * and/or other materials provided with the distribution. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 25 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * #L% |
| 33 | + */ |
| 34 | + |
| 35 | +package net.imglib2.test; |
| 36 | + |
| 37 | +import net.imglib2.Cursor; |
| 38 | +import net.imglib2.Interval; |
| 39 | +import net.imglib2.Localizable; |
| 40 | +import net.imglib2.RandomAccessibleInterval; |
| 41 | +import net.imglib2.type.numeric.IntegerType; |
| 42 | +import net.imglib2.type.numeric.RealType; |
| 43 | +import net.imglib2.type.operators.ValueEquals; |
| 44 | +import net.imglib2.util.Intervals; |
| 45 | +import net.imglib2.util.Pair; |
| 46 | +import net.imglib2.view.IntervalView; |
| 47 | +import net.imglib2.view.Views; |
| 48 | + |
| 49 | +import java.util.Arrays; |
| 50 | +import java.util.StringJoiner; |
| 51 | +import java.util.function.BiPredicate; |
| 52 | + |
| 53 | +public class ImgLib2Assert |
| 54 | +{ |
| 55 | + private ImgLib2Assert() |
| 56 | + { |
| 57 | + // prevent from instantiation |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Throws an AssertionError, if the content or intervals of the two images differ. |
| 62 | + * Comparision is done pixel wise using {@link ValueEquals#valueEquals(Object)}. |
| 63 | + */ |
| 64 | + public static < A extends ValueEquals< B >, B > |
| 65 | + void assertImageEquals( final RandomAccessibleInterval< ? extends A > actual, final RandomAccessibleInterval< ? extends B > expected ) |
| 66 | + { |
| 67 | + assertImageEquals( actual, expected, ValueEquals::valueEquals ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Throws an AssertionError, if the content or intervals of the two images differ. |
| 72 | + * Comparision is done pixel wise. Two pixels are considered equal, if the values |
| 73 | + * returned by {@link RealType#getRealDouble()} differ by less than "tolerance". |
| 74 | + */ |
| 75 | + public static void assertImageEqualsRealType( final RandomAccessibleInterval< ? extends RealType< ? > > actual, final RandomAccessibleInterval< ? extends RealType< ? > > expected, double tolerance ) |
| 76 | + { |
| 77 | + assertImageEquals( actual, expected, ( a, e ) -> Math.abs( a.getRealDouble() - e.getRealDouble() ) <= tolerance ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Throws an AssertionError, if the content or intervals of the two images differ. |
| 82 | + * Comparision is done pixel wise. Two pixels are considered equal, if the values |
| 83 | + * returned by {@link IntegerType#getIntegerLong()} are equal. |
| 84 | + */ |
| 85 | + public static void assertImageEqualsIntegerType( final RandomAccessibleInterval< ? extends IntegerType< ? > > actual, final RandomAccessibleInterval< ? extends IntegerType< ? > > expected ) |
| 86 | + { |
| 87 | + assertImageEquals( actual, expected, ( a, e ) -> a.getIntegerLong() == e.getIntegerLong() ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Throws an AssertionError, if the content or intervals of the two images differ. |
| 92 | + * Comparision is done pixel wise. Two pixels are considered equal, if the give |
| 93 | + * predicate returns true. |
| 94 | + */ |
| 95 | + public static < A, B > |
| 96 | + void assertImageEquals( final RandomAccessibleInterval< ? extends A > a, final RandomAccessibleInterval< ? extends B > b, BiPredicate< A, B > equals ) |
| 97 | + { |
| 98 | + assertIntervalEquals( a, b ); |
| 99 | + IntervalView< ? extends Pair< ? extends A, ? extends B > > pairs = Views.interval( Views.pair( a, b ), b ); |
| 100 | + Cursor< ? extends Pair< ? extends A, ? extends B > > cursor = pairs.cursor(); |
| 101 | + while ( cursor.hasNext() ) |
| 102 | + { |
| 103 | + Pair< ? extends A, ? extends B > p = cursor.next(); |
| 104 | + if ( !equals.test( p.getA(), p.getB() ) ) |
| 105 | + fail( "Pixel values differ on coordinate " + |
| 106 | + positionToString( cursor ) + ", expected: " |
| 107 | + + p.getA() + " actual: " + p.getB() ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Throws an AssertionError, if the two Intervals differ. |
| 113 | + */ |
| 114 | + public static void assertIntervalEquals( Interval a, Interval b ) |
| 115 | + { |
| 116 | + if ( !Intervals.equals( a, b ) ) |
| 117 | + fail( "Intervals are different, expected: " + intervalToString( a ) + ", actual: " + intervalToString( b ) ); |
| 118 | + } |
| 119 | + |
| 120 | + // -- Helper methods -- |
| 121 | + |
| 122 | + private static String positionToString( Localizable localizable ) |
| 123 | + { |
| 124 | + StringJoiner joiner = new StringJoiner( ", " ); |
| 125 | + for ( int i = 0, n = localizable.numDimensions(); i < n; i++ ) |
| 126 | + joiner.add( String.valueOf( localizable.getIntPosition( i ) ) ); |
| 127 | + return "(" + joiner + ")"; |
| 128 | + } |
| 129 | + |
| 130 | + static String intervalToString( Interval a ) |
| 131 | + { |
| 132 | + return "{min=" + Arrays.toString( Intervals.minAsLongArray( a ) ) + |
| 133 | + ", max=" + Arrays.toString( Intervals.maxAsLongArray( a ) ) + "}"; |
| 134 | + } |
| 135 | + |
| 136 | + private static void fail( String message ) |
| 137 | + { |
| 138 | + throw new AssertionError( message ); |
| 139 | + } |
| 140 | +} |
0 commit comments