<commit_before>package com.yahoo.labs.samoa.evaluation;

/*
 * #%L
 * SAMOA
 * %%
 * Copyright (C) 2013 Yahoo! Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.LinkedList;

import com.yahoo.labs.samoa.moa.AbstractMOAObject;
import com.yahoo.labs.samoa.moa.core.Measurement;
import com.yahoo.labs.samoa.instances.Instance;
import com.yahoo.labs.samoa.instances.Utils;

/**
 * Classification evaluator that performs basic incremental evaluation.
 * 
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (abifet at cs dot waikato dot ac dot nz)
 * @version $Revision: 7 $
 */
public class BasicClassificationPerformanceEvaluator extends AbstractMOAObject implements
        ClassificationPerformanceEvaluator {

    private static final long serialVersionUID = 1L;

    protected double weightObserved;

    protected double weightCorrect;

    protected double[] columnKappa;

    protected double[] rowKappa;

    protected int numClasses;

    private double weightCorrectNoChangeClassifier;

    private int lastSeenClass;

    @Override
    public void reset() {
        reset(this.numClasses);
    }

    public void reset(int numClasses) {
        this.numClasses = numClasses;
        this.rowKappa = new double[numClasses];
        this.columnKappa = new double[numClasses];
        for (int i = 0; i < this.numClasses; i++) {
            this.rowKappa[i] = 0.0;
            this.columnKappa[i] = 0.0;
        }
        this.weightObserved = 0.0;
        this.weightCorrect = 0.0;
        this.weightCorrectNoChangeClassifier = 0.0;
        this.lastSeenClass = 0;
    }
    
    private double[] increaseLength(double[] array, int newLength) {
    	if (array.length < newLength) {
    		double[] newArray = new double[newLength];
    		for (int i=0; i < newLength; i++) {
    			if (i<array.length)
    				newArray[i] = array[i];
    			else
    				newArray[i] = 0.0;
    		}
    		return newArray;
    	}
    	return array;
    }
    
    private void updateNumClasses(int numClasses) {
    	if (this.numClasses < numClasses) {
    		this.numClasses = numClasses;
    		this.rowKappa = increaseLength(this.rowKappa, numClasses);
    		this.columnKappa = increaseLength(this.columnKappa, numClasses);
    	}
    }

    @Override
    public void addResult(Instance inst, double[] classVotes) {
        addClassificationAttempt((int) inst.classValue(), classVotes, inst.weight());
    }

    @Override
    public void addClassificationAttempt(int trueClass, double[] classVotes, double weight) {
        if (weight > 0.0) {
            if (this.weightObserved == 0) {
                reset(classVotes.length); 
            } else if (classVotes.length != numClasses) {
            	updateNumClasses(classVotes.length);
            }
            this.weightObserved += weight;
            int predictedClass = Utils.maxIndex(classVotes);
            if (predictedClass == trueClass) {
                this.weightCorrect += weight;
            }
            if(rowKappa.length > 0){
            	this.rowKappa[predictedClass] += weight;
            }       
            if (columnKappa.length > 0) {
                this.columnKappa[trueClass] += weight;
            }
        }
        if (this.lastSeenClass == trueClass) {<commit_msg>Revert "Adjust length of kappa vectors if classVotes.length is different from current numClasses. (Are classVotes.length of all instances supposed to be the same?)"<commit_after>package com.yahoo.labs.samoa.evaluation;

/*
 * #%L
 * SAMOA
 * %%
 * Copyright (C) 2013 Yahoo! Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import com.yahoo.labs.samoa.moa.AbstractMOAObject;
import com.yahoo.labs.samoa.moa.core.Measurement;
import com.yahoo.labs.samoa.instances.Instance;
import com.yahoo.labs.samoa.instances.Utils;

/**
 * Classification evaluator that performs basic incremental evaluation.
 * 
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (abifet at cs dot waikato dot ac dot nz)
 * @version $Revision: 7 $
 */
public class BasicClassificationPerformanceEvaluator extends AbstractMOAObject implements
        ClassificationPerformanceEvaluator {

    private static final long serialVersionUID = 1L;

    protected double weightObserved;

    protected double weightCorrect;

    protected double[] columnKappa;

    protected double[] rowKappa;

    protected int numClasses;

    private double weightCorrectNoChangeClassifier;

    private int lastSeenClass;

    @Override
    public void reset() {
        reset(this.numClasses);
    }

    public void reset(int numClasses) {
        this.numClasses = numClasses;
        this.rowKappa = new double[numClasses];
        this.columnKappa = new double[numClasses];
        for (int i = 0; i < this.numClasses; i++) {
            this.rowKappa[i] = 0.0;
            this.columnKappa[i] = 0.0;
        }
        this.weightObserved = 0.0;
        this.weightCorrect = 0.0;
        this.weightCorrectNoChangeClassifier = 0.0;
        this.lastSeenClass = 0;
    }

    @Override
    public void addResult(Instance inst, double[] classVotes) {
        addClassificationAttempt((int) inst.classValue(), classVotes, inst.weight());
    }

    @Override
    public void addClassificationAttempt(int trueClass, double[] classVotes, double weight) {
        if (weight > 0.0) {
            if (this.weightObserved == 0) {
                reset(classVotes.length); 
            }
            this.weightObserved += weight;
            int predictedClass = Utils.maxIndex(classVotes);
            if (predictedClass == trueClass) {
                this.weightCorrect += weight;
            }
            if(rowKappa.length > 0){
            	this.rowKappa[predictedClass] += weight;
            }       
            if (columnKappa.length > 0) {
                this.columnKappa[trueClass] += weight;
            }
        }
        if (this.lastSeenClass == trueClass) {
{'fraction_non_alphanumeric': 0.09762076423936554, 'fraction_numerical': 0.00821917808219178, 'mean_word_length': 2.593782383419689, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2684287', 'n_tokens_mistral': 2119, 'n_tokens_neox': 1979, 'n_words': 552}