/*******************************************************************************
 * DIANNE  - Framework for distributed artificial neural networks
 * Copyright (C) 2015  iMinds - IBCN - UGent
 *
 * This file is part of DIANNE.
 *
 * DIANNE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Tim Verbelen, Steven Bohez
 *******************************************************************************/
package be.iminds.iot.dianne.nn.eval.strategy;

import java.util.Map;

import be.iminds.iot.dianne.api.nn.eval.ClassificationEvaluation;
import be.iminds.iot.dianne.api.nn.eval.Evaluation;
import be.iminds.iot.dianne.tensor.Tensor;

public class BinaryClassificationEvaluationStrategy extends ClassificationEvaluationStrategy {
	
	
	protected void init(Map<String, String> config){
		// reset
		confusion = null;
	}
	
	protected float eval(Tensor output, Tensor target){
		if(confusion==null){
			int confusionSize = 2;
			confusion = new Tensor(confusionSize, confusionSize);
			confusion.fill(0.0f);
		}
		
		float err = 0;
		if(output.dim() == 2){
			// batch
			for(int i=0;i<output.size(0);i++){
				err += calculateError(output.select(0, i), target.select(0, i));
			}
		} else {
			err += calculateError(output, target); 
		}
		
		return err;
	}
	
	private float calculateError(Tensor out, Tensor expected){
		float error = 0.0f;
		
		int predicted = out.get(0) > 0.5f ? 1 : 0;
		int real = expected.get(0) > 0.5f ? 1 : 0;
		if(real!=predicted)
			error = 1.0f;
		
		confusion.set(confusion.get(real, predicted)+1, real, predicted);

		return error;
	}
	
	protected Evaluation finish(){
		ClassificationEvaluation eval = new ClassificationEvaluation();
		eval.confusionMatrix = confusion;
		return eval;
	}
}


@@ -1355,89 +1355,279 @@
 %0A%09%0A%09
-%0A%09protected void init(Map%3CString, String%3E config)%7B%0A%09%09// reset%0A%09%09confusion = null;
+private float threshold = 0.5f; // value before something is considered 1 class%0A%09%0A%09protected void init(Map%3CString, String%3E config)%7B%0A%09%09// reset%0A%09%09confusion = null;%0A%09%09%0A%09%09if(config.containsKey(%22threshold%22))%7B%0A%09%09%09this.threshold = Float.parseFloat(config.get(%22threshold%22));%0A%09%09%7D
 %0A%09%7D%0A
@@ -2173,36 +2173,41 @@
  = out.get(0) %3E 
-0.5f
+threshold
  ? 1 : 0;%0A%09%09int 
@@ -2231,20 +2231,25 @@
 et(0) %3E 
-0.5f
+threshold
  ? 1 : 0

add threshold for binary classification eval