imagej / imagej/imagej-ops

IterableMax.compute( Iterable ) always returns DoubleType regardless of input generic parametr

Open
#559 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
94
Forks
44
PR merge metrics
No merged PRs in 30d

Description

This is a follow-up to a [bug report on the ImageJ forum](http://forum.imagej.net/t/stats-max-op-returns-wrong-result-type/11447) and mostly c&p of [my post in that thread](http://forum.imagej.net/t/stats-max-op-returns-wrong-result-type/11447/3?u=hanslovsky):

MWE
```java
import net.imagej.ImageJ;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.FloatArray;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;

public class Dummy
{

public static void main( final String[] args )
{
final ImageJ ij = new ImageJ();
final ArrayImg< FloatType, FloatArray > floats = ArrayImgs.floats( 1, 2, 3 );
final DoubleType doubleResult = ( DoubleType ) ( Object ) ij.op().stats().max( floats );
final FloatType floatResult = ij.op().stats().max( floats );
System.out.println( doubleResult + " " + floatResult );
ij.context().dispose();
}

```
throws this exception:
```
Exception in thread "main" java.lang.ClassCastException: net.imglib2.type.numeric.real.DoubleType cannot be cast to net.imglib2.type.numeric.real.FloatType
at Dummy.main(Dummy.java:16)
```
As you can see, that weird `( DoubleType ) ( Object )` cast works in line 15, but the expected correct line 16
```java
final FloatType floatResult = ij.op().stats().max( floats );
```
throws an exception.

I am not an imagej-ops expert by any means but here are my 2 cents: The culprit is probably [`AbstractStatsOp.createOutput`](https://github.com/imagej/imagej-ops/blob/caecb88fafcb5b7d769fdaffca724a09ed943896/src/main/java/net/imagej/ops/stats/AbstractStatsOp.java#L52): It always creates a `DoubleType`. [`IterableMax`](https://github.com/imagej/imagej-ops/blob/caecb88fafcb5b7d769fdaffca724a09ed943896/src/main/java/net/imagej/ops/stats/IterableMax.java) could override `createOutput`, e.g. something like this:
```java
@Override
public T createOutput( final Iterable< T > input )
{
return input.iterator().next().createVariable();
}
```
The problem here is that this will throw an exception if `input` has zero elements.

Contributor guide

No contributing guide indexed for this repository

Research direction

Run the supplied Java MWE first, then inspect AbstractStatsOp.createOutput and IterableMax, the two locations named in the report. Check how max handles FloatType input and empty Iterable input; the issue is complete when the result type matches the input generic without introducing an empty-input exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
computer-vision
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.