package net.imagej.ops.coloc.icq;

import org.scijava.ItemIO;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import net.imagej.ops.AbstractOp;
import net.imagej.ops.Contingent;
import net.imagej.ops.Ops;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Intervals;
import net.imglib2.util.Pair;

/**
 * This algorithm calculates Li et al.'s ICQ (intensity correlation quotient).
 *
 * @param <T>
 *            Type of the first image
 * @param <U>
 *            Type of the second image
 */
@Plugin(type=Ops.Coloc.ICQ.class)
public class LiICQ<T extends RealType<T>,U extends RealType<U>> extends AbstractOp implements Ops.Coloc.ICQ {
	/** the resulting ICQ value. */
	@Parameter(type=ItemIO.OUTPUT)
	private double icqValue;

	@Parameter
	private Iterable<Pair<T, U>> samples;
	
	@Parameter
	private double mean1;
	
	@Parameter
	private double mean2;
	
	@Override
	public void run() {
		// variables to count the positive and negative results
		// of Li's product of the difference of means.
		long numPositiveProducts = 0;
		long numNegativeProducts = 0;
		// iterate over image
		for (Pair<T, U> value:samples) {
			
			double ch1 = value.getA().getRealDouble();
			double ch2 = value.getB().getRealDouble();

			double productOfDifferenceOfMeans = (mean1 - ch1) * (mean2 - ch2);

			// check for positive and negative values
			if (productOfDifferenceOfMeans < 0.0 )
				++numNegativeProducts;
			else
				++numPositiveProducts;
		}

		/* calculate Li's ICQ value by dividing the amount of "positive pixels" to the
		 * total number of pixels. Then shift it in the -0.5,0.5 range.
		 */
		icqValue = ( (double) numPositiveProducts / (double) (numNegativeProducts + numPositiveProducts) ) - 0.5;
	}
}

@@ -187,109 +187,11 @@
 ops.
-Contingent;%0Aimport net.imagej.ops.Ops;%0Aimport net.imglib2.Cursor;%0Aimport net.imglib2.IterableInterval
+Ops
 ;%0Aim
@@ -234,43 +234,8 @@
 pe;%0A
-import net.imglib2.util.Intervals;%0A
 impo
@@ -468,17 +468,19 @@
 gin(type
-=
+ = 
 Ops.Colo
@@ -533,16 +533,17 @@
 Type%3CT%3E,
+ 
 U extend
@@ -652,17 +652,19 @@
 ter(type
-=
+ = 
 ItemIO.O
@@ -748,17 +748,16 @@
 amples;%0A
-%09
 %0A%09@Param
@@ -784,17 +784,16 @@
  mean1;%0A
-%09
 %0A%09@Param
@@ -820,17 +820,16 @@
  mean2;%0A
-%09
 %0A%09@Overr
@@ -1055,16 +1055,22 @@
 %0A%09%09for (
+final 
 Pair%3CT, 
@@ -1081,9 +1081,11 @@
 alue
-:
+ : 
 samp
@@ -1095,15 +1095,18 @@
 ) %7B%0A
-%09%09%09
 %0A%09%09%09
+final 
 doub
@@ -1143,24 +1143,30 @@
 ouble();%0A%09%09%09
+final 
 double ch2 =
@@ -1200,16 +1200,22 @@
 ();%0A%0A%09%09%09
+final 
 double p
@@ -1358,17 +1358,16 @@
 ns %3C 0.0
- 
 )%0A%09%09%09%09++
@@ -1431,16 +1431,21 @@
 %09%7D%0A%0A%09%09/*
+%0A%09%09 *
  calcula
@@ -1509,20 +1509,20 @@
 els%22
+%0A%09%09 *
  to the
-%0A%09%09 *
  tot
@@ -1599,17 +1599,16 @@
 alue = (
- 
 (double)
@@ -1682,17 +1682,16 @@
 roducts)
- 
 ) - 0.5;

clean up style