Skip to content

Commit

Permalink
correct casting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Sep 30, 2024
1 parent cba377b commit 1d80600
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.ByteType;
import net.imglib2.type.numeric.integer.IntType;
Expand All @@ -45,56 +46,16 @@
public class AbstractTensorPixelTransformation extends AbstractTensorTransformation
{

private FloatUnaryOperator fun;
private DoubleUnaryOperator dun;
private ByteUnaryOperator bun;
private UByteUnaryOperator ubun;
private ShortUnaryOperator sun;
private UShortUnaryOperator usun;
private IntUnaryOperator iun;
private UIntUnaryOperator uiun;
private LongUnaryOperator lun;
private DoubleUnitaryOperator dun;

protected AbstractTensorPixelTransformation( final String name)
{
super( name );
}

protected void setFloatUnitaryOperator(final FloatUnaryOperator fun) {
this.fun = fun;
}

protected void setDoubleUnitaryOperator(final DoubleUnaryOperator fun) {
protected void setDoubleUnitaryOperator(final DoubleUnitaryOperator fun) {
this.dun = fun;
}

protected void setByteUnitaryOperator(final ByteUnaryOperator fun) {
this.bun = fun;
}

protected void setUByteUnitaryOperator(final UByteUnaryOperator fun) {
this.ubun = fun;
}

protected void setShortUnitaryOperator(final ShortUnaryOperator fun) {
this.sun = fun;
}

protected void setUShortUnitaryOperator(final UShortUnaryOperator fun) {
this.usun = fun;
}

protected void setIntUnitaryOperator(final IntUnaryOperator fun) {
this.iun = fun;
}

protected void setUIntUnitaryOperator(final UIntUnaryOperator fun) {
this.uiun = fun;
}

protected void setLongUnitaryOperator(final LongUnaryOperator fun) {
this.lun = fun;
}

@Override
public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply( final Tensor< R > input )
Expand All @@ -104,112 +65,26 @@ public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply(
return output;
}

@SuppressWarnings("unchecked")
@Override
public < R extends RealType< R > & NativeType< R > >
void applyInPlace( final Tensor< R > input )
{
if (input.getData().getAt(0) instanceof FloatType && fun != null) {
if (input.getData().getAt(0) instanceof IntegerType && dun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<FloatType>) input.getData() )
.setImages( input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( fun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof DoubleType && dun != null) {
.forEachPixel( i -> i.setReal( Math.floor(dun.applyAs( i.getRealDouble() )) ) );
} else if (dun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<DoubleType>) input.getData() )
.setImages( input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( dun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof ByteType && bun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<ByteType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( bun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedByteType && ubun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedByteType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( ubun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof ShortType && sun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<ShortType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( sun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedShortType && usun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedShortType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( usun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof IntType && iun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<IntType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( iun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedIntType && uiun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedIntType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( uiun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof LongType && lun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<LongType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( lun.applyAs( i.get() ) ) );
} else {
throw new IllegalArgumentException("Unsupported data type.");
.forEachPixel( i -> i.setReal( dun.applyAs( i.getRealDouble() ) ) );
}
}

@FunctionalInterface
public interface FloatUnaryOperator
{
float applyAs( float in );
}

@FunctionalInterface
public interface DoubleUnaryOperator
public interface DoubleUnitaryOperator
{
double applyAs( double in );
}

@FunctionalInterface
public interface ByteUnaryOperator
{
byte applyAs( byte in );
}

@FunctionalInterface
public interface UByteUnaryOperator
{
int applyAs( int i );
}

@FunctionalInterface
public interface ShortUnaryOperator
{
short applyAs( short in );
}

@FunctionalInterface
public interface UShortUnaryOperator
{
int applyAs( int i );
}

@FunctionalInterface
public interface IntUnaryOperator
{
int applyAs( int in );
}

@FunctionalInterface
public interface UIntUnaryOperator
{
long applyAs( long in );
}

@FunctionalInterface
public interface LongUnaryOperator
{
long applyAs( long in );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ public class BinarizeTransformation extends AbstractTensorPixelTransformation
public BinarizeTransformation()
{
super( name );
super.setFloatUnitaryOperator(v -> ( v >= threshold ) ? 1f : 0f);
super.setDoubleUnitaryOperator(v -> ( v >= threshold ) ? 1d : 0d);
super.setByteUnitaryOperator(v -> ( v >= threshold ) ? (byte) 1 : 0);
super.setUByteUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setShortUnitaryOperator(v -> ( v >= threshold ) ? (short) 1 : 0);
super.setUShortUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setIntUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setUIntUnitaryOperator(v -> ( v >= threshold ) ? 1L : 0L);
super.setLongUnitaryOperator(v -> ( v >= threshold ) ? 1L : 0L);
}

public void setThreshold(Object threshold) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ public class ClipTransformation extends AbstractTensorPixelTransformation
public ClipTransformation()
{
super(name);
super.setFloatUnitaryOperator(v -> v >= max ? max.floatValue() : (v < min ? min.floatValue() : v));
super.setDoubleUnitaryOperator(v -> v >= max ? max.doubleValue() : (v < min ? min.doubleValue() : v));
super.setByteUnitaryOperator(v -> v >= max ? max.byteValue() : (v < min ? min.byteValue() : v));
super.setUByteUnitaryOperator(v -> v >= max ? max.intValue() : (v < min ? min.intValue() : v));
super.setShortUnitaryOperator(v -> v >= max ? max.shortValue() : (v < min ? min.shortValue() : v));
super.setUShortUnitaryOperator(v -> v >= max ? max.intValue() : (v < min ? min.intValue() : v));
super.setIntUnitaryOperator(v -> v >= max ? max.intValue() : (v < min ? min.intValue() : v));
super.setUIntUnitaryOperator(v -> v >= max ? max.longValue() : (v < min ? min.longValue() : v));
super.setLongUnitaryOperator(v -> v >= max ? max.longValue() : (v < min ? min.longValue() : v));
}

public void setMin(Object min) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ public class SigmoidTransformation extends AbstractTensorPixelTransformation
public SigmoidTransformation()
{
super(name);
super.setFloatUnitaryOperator( v -> ( float ) ( 1. / ( 1. + Math.exp( -v ) ) ) );
super.setDoubleUnitaryOperator(v -> ( double ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setByteUnitaryOperator(v -> ( byte ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setUByteUnitaryOperator(v -> ( int ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setShortUnitaryOperator(v -> ( short ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setUShortUnitaryOperator(v -> ( int ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setIntUnitaryOperator(v -> ( int ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setUIntUnitaryOperator(v -> ( long ) ( 1. / ( 1. + Math.exp( -v ) ) ));
super.setLongUnitaryOperator(v -> ( long ) ( 1. / ( 1. + Math.exp( -v ) ) ));
}

public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply( final Tensor< R > input )
Expand Down

0 comments on commit 1d80600

Please sign in to comment.