From 29089c52ead4645895acc601c535c6adaa2a84af Mon Sep 17 00:00:00 2001 From: Albert Cardona Date: Thu, 15 Nov 2018 14:04:48 -0500 Subject: [PATCH 1/2] ImgMath: add static methods to wrap a Number and a RandomAccessibleInterval. Useful for e.g. copying an image into another, or filling an image with a number: compute(img(source)).into(target) compute(number(255)).into(target) --- src/main/java/net/imglib2/algorithm/math/ImgMath.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/net/imglib2/algorithm/math/ImgMath.java b/src/main/java/net/imglib2/algorithm/math/ImgMath.java index e5a8e7477..e9298c638 100644 --- a/src/main/java/net/imglib2/algorithm/math/ImgMath.java +++ b/src/main/java/net/imglib2/algorithm/math/ImgMath.java @@ -232,4 +232,14 @@ static public final Else ELSE( final Object o ) { return new Else( o ); } + + static public final < T extends RealType< T > > ImgSource< T > img( final RandomAccessibleInterval< T > rai ) + { + return new ImgSource< T >( rai ); + } + + static public final NumberSource number( final Number number ) + { + return new NumberSource( number ); + } } From 4598798180cb40c5b3bfc225f92d4532e14fda97 Mon Sep 17 00:00:00 2001 From: Albert Cardona Date: Thu, 15 Nov 2018 14:30:07 -0500 Subject: [PATCH 2/2] ImgMath: fix critical error in VarargsFunction: when the length of the array was 2, the first operation was being duplicated. While this wouldn't be an issue in java, it shows in scripting languages that fail to use the 2-arg constructor and instead use the varags one. --- src/main/java/net/imglib2/algorithm/math/Add.java | 6 ++++-- src/main/java/net/imglib2/algorithm/math/Compute.java | 4 ++-- src/main/java/net/imglib2/algorithm/math/Div.java | 6 ++++-- src/main/java/net/imglib2/algorithm/math/Equal.java | 4 +++- src/main/java/net/imglib2/algorithm/math/GreaterThan.java | 6 ++++-- src/main/java/net/imglib2/algorithm/math/ImgSource.java | 4 +++- src/main/java/net/imglib2/algorithm/math/LessThan.java | 4 +++- src/main/java/net/imglib2/algorithm/math/Let.java | 4 +++- src/main/java/net/imglib2/algorithm/math/Max.java | 4 +++- src/main/java/net/imglib2/algorithm/math/Min.java | 4 +++- src/main/java/net/imglib2/algorithm/math/Mul.java | 4 +++- src/main/java/net/imglib2/algorithm/math/NotEqual.java | 4 +++- src/main/java/net/imglib2/algorithm/math/Sub.java | 4 +++- .../imglib2/algorithm/math/abstractions/AUnaryFunction.java | 2 +- .../algorithm/math/abstractions/VarargsFunction.java | 5 ++++- 15 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/imglib2/algorithm/math/Add.java b/src/main/java/net/imglib2/algorithm/math/Add.java index f5dbf1c0e..8ca30fa78 100644 --- a/src/main/java/net/imglib2/algorithm/math/Add.java +++ b/src/main/java/net/imglib2/algorithm/math/Add.java @@ -26,8 +26,10 @@ public < O extends RealType< O > > Addition< O > reInit( final O tmp, final Map< String, O > bindings, final Converter< RealType< ? >, O > converter, - Map< Variable< O >, OFunction< O > > imgSources ) + final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Addition< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Addition< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Compute.java b/src/main/java/net/imglib2/algorithm/math/Compute.java index 378fc2781..c3b8bb4d7 100644 --- a/src/main/java/net/imglib2/algorithm/math/Compute.java +++ b/src/main/java/net/imglib2/algorithm/math/Compute.java @@ -79,7 +79,7 @@ public final void convert( final RealType< ? > input, final O output) output.setReal( input.getRealDouble() ); } }; - + // Recursive copy: initializes interval iterators and sets temporary computation holder final OFunction< O > f = this.operation.reInit( target.randomAccess().get().createVariable(), @@ -87,7 +87,7 @@ public final void convert( final RealType< ? > input, final O output) converter, null ); // Check compatible iteration order and dimensions - if ( compatible_iteration_order ) + if ( this.compatible_iteration_order ) { // Evaluate function for every pixel for ( final O output : Views.iterable( target ) ) diff --git a/src/main/java/net/imglib2/algorithm/math/Div.java b/src/main/java/net/imglib2/algorithm/math/Div.java index 76420d5ce..74ea95649 100644 --- a/src/main/java/net/imglib2/algorithm/math/Div.java +++ b/src/main/java/net/imglib2/algorithm/math/Div.java @@ -27,8 +27,10 @@ public < O extends RealType< O > > Division< O > reInit( final O tmp, final Map< String, O > bindings, final Converter< RealType< ? >, O > converter, - Map< Variable< O >, OFunction< O > > imgSources ) + final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Division< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Division< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Equal.java b/src/main/java/net/imglib2/algorithm/math/Equal.java index 4b6579205..d9e85bee6 100644 --- a/src/main/java/net/imglib2/algorithm/math/Equal.java +++ b/src/main/java/net/imglib2/algorithm/math/Equal.java @@ -23,6 +23,8 @@ public < O extends RealType< O > > OFunction< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Equality< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Equality< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/GreaterThan.java b/src/main/java/net/imglib2/algorithm/math/GreaterThan.java index 614fef24c..9a8ac2dae 100644 --- a/src/main/java/net/imglib2/algorithm/math/GreaterThan.java +++ b/src/main/java/net/imglib2/algorithm/math/GreaterThan.java @@ -21,8 +21,10 @@ public < O extends RealType< O > > IsGreaterThan< O > reInit( final O tmp, final Map< String, O > bindings, final Converter< RealType< ? >, O > converter, - Map< Variable< O >, OFunction< O > > imgSources ) + final Map< Variable< O >, OFunction< O > > imgSources ) { - return new IsGreaterThan< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new IsGreaterThan< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/ImgSource.java b/src/main/java/net/imglib2/algorithm/math/ImgSource.java index 3beae8ef4..a93713ca6 100644 --- a/src/main/java/net/imglib2/algorithm/math/ImgSource.java +++ b/src/main/java/net/imglib2/algorithm/math/ImgSource.java @@ -29,7 +29,9 @@ public < O extends RealType< O > > OFunction< O > reInit( final Map< Variable< O >, OFunction< O > > imgSources ) { // Optimization: if input image type is the same or a subclass of - // the output image type (represented here by tmp), then avoid the converter. + // the output image type (represented here by tmp), then avoid the converter + // but only if the targetImg is different than this.rai: otherwise, an intermediate + // computation result holder must be used (the scrap). final OFunction< O > s; if ( tmp.getClass().isAssignableFrom( this.rai.randomAccess().get().getClass() ) ) s = new ImgSourceIterableDirect< O >( ( RandomAccessibleInterval< O > )this.rai ); diff --git a/src/main/java/net/imglib2/algorithm/math/LessThan.java b/src/main/java/net/imglib2/algorithm/math/LessThan.java index a7f0f719e..7903abe11 100644 --- a/src/main/java/net/imglib2/algorithm/math/LessThan.java +++ b/src/main/java/net/imglib2/algorithm/math/LessThan.java @@ -23,6 +23,8 @@ public < O extends RealType< O > > IsLessThan< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new IsLessThan< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new IsLessThan< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Let.java b/src/main/java/net/imglib2/algorithm/math/Let.java index 2a5cff4d3..c9be4ec97 100644 --- a/src/main/java/net/imglib2/algorithm/math/Let.java +++ b/src/main/java/net/imglib2/algorithm/math/Let.java @@ -68,7 +68,9 @@ public < O extends RealType< O > > LetBinding< O > reInit( final O scrap = tmp.copy(); final Map< String, O > rebind = new HashMap<>( bindings ); rebind.put( this.varName, scrap ); - return new LetBinding< O >( scrap, this.varName, this.varValue.reInit( tmp, rebind, converter, imgSources ), this.body.reInit( tmp, rebind, converter, imgSources ) ); + return new LetBinding< O >( scrap, this.varName, + this.varValue.reInit( tmp, rebind, converter, imgSources ), + this.body.reInit( tmp, rebind, converter, imgSources ) ); } @Override diff --git a/src/main/java/net/imglib2/algorithm/math/Max.java b/src/main/java/net/imglib2/algorithm/math/Max.java index 540eff44d..12b2a05a9 100644 --- a/src/main/java/net/imglib2/algorithm/math/Max.java +++ b/src/main/java/net/imglib2/algorithm/math/Max.java @@ -28,6 +28,8 @@ public < O extends RealType< O > > Maximum< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Maximum< O >( this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Maximum< O >( + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Min.java b/src/main/java/net/imglib2/algorithm/math/Min.java index 2bb3581ed..a6ace5cd2 100644 --- a/src/main/java/net/imglib2/algorithm/math/Min.java +++ b/src/main/java/net/imglib2/algorithm/math/Min.java @@ -28,6 +28,8 @@ public < O extends RealType< O > > Minimum< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Minimum< O >( this.a.reInit(tmp, bindings, converter, imgSources), this.b.reInit(tmp, bindings, converter, imgSources) ); + return new Minimum< O >( + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Mul.java b/src/main/java/net/imglib2/algorithm/math/Mul.java index f716e5a1b..489bbb3ba 100644 --- a/src/main/java/net/imglib2/algorithm/math/Mul.java +++ b/src/main/java/net/imglib2/algorithm/math/Mul.java @@ -28,6 +28,8 @@ public < O extends RealType< O > > Multiplication< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Multiplication< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Multiplication< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/NotEqual.java b/src/main/java/net/imglib2/algorithm/math/NotEqual.java index d00dd822b..fdc5df5b3 100644 --- a/src/main/java/net/imglib2/algorithm/math/NotEqual.java +++ b/src/main/java/net/imglib2/algorithm/math/NotEqual.java @@ -22,6 +22,8 @@ public < O extends RealType< O > > NotEquality< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new NotEquality< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new NotEquality< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/Sub.java b/src/main/java/net/imglib2/algorithm/math/Sub.java index cdb11fccb..dbc063129 100644 --- a/src/main/java/net/imglib2/algorithm/math/Sub.java +++ b/src/main/java/net/imglib2/algorithm/math/Sub.java @@ -28,6 +28,8 @@ public < O extends RealType< O > > Subtraction< O > reInit( final Converter< RealType< ? >, O > converter, final Map< Variable< O >, OFunction< O > > imgSources ) { - return new Subtraction< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) ); + return new Subtraction< O >( tmp.copy(), + this.a.reInit( tmp, bindings, converter, imgSources ), + this.b.reInit( tmp, bindings, converter, imgSources ) ); } } \ No newline at end of file diff --git a/src/main/java/net/imglib2/algorithm/math/abstractions/AUnaryFunction.java b/src/main/java/net/imglib2/algorithm/math/abstractions/AUnaryFunction.java index e1f3e245e..32f09d952 100644 --- a/src/main/java/net/imglib2/algorithm/math/abstractions/AUnaryFunction.java +++ b/src/main/java/net/imglib2/algorithm/math/abstractions/AUnaryFunction.java @@ -2,7 +2,7 @@ import net.imglib2.type.numeric.RealType; -abstract public class AUnaryFunction extends VarargsFunction implements IUnaryFunction +abstract public class AUnaryFunction implements IUnaryFunction { protected final IFunction a; diff --git a/src/main/java/net/imglib2/algorithm/math/abstractions/VarargsFunction.java b/src/main/java/net/imglib2/algorithm/math/abstractions/VarargsFunction.java index 411d4f33d..e29d8d6a4 100644 --- a/src/main/java/net/imglib2/algorithm/math/abstractions/VarargsFunction.java +++ b/src/main/java/net/imglib2/algorithm/math/abstractions/VarargsFunction.java @@ -5,8 +5,11 @@ abstract public class VarargsFunction { final public IFunction[] wrapMap( final Object[] obs ) - { + { try { + if ( 2 == obs.length ) + return new IFunction[]{ Util.wrap( obs[ 0 ] ), Util.wrap( obs[ 1 ]) }; + final Constructor< ? > constructor = this.getClass().getConstructor( new Class[]{ Object.class, Object.class } ); ABinaryFunction a = ( ABinaryFunction )constructor.newInstance( obs[0], obs[1] ); ABinaryFunction b;