<commit_before>/*
 * This file is part of ELKI:
 * Environment for Developing KDD-Applications Supported by Index-Structures
 *
 * Copyright (C) 2019
 * ELKI Development Team
 *
 * This program 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/>.
 */
package elki.evaluation;

import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Pattern;

import elki.clustering.trivial.ByLabelClustering;
import elki.clustering.trivial.ByLabelOrAllInOneClustering;
import elki.clustering.trivial.ReferenceClustering;
import elki.data.Cluster;
import elki.data.Clustering;
import elki.data.type.NoSupportedDataTypeException;
import elki.database.Database;
import elki.evaluation.clustering.EvaluateClustering;
import elki.evaluation.outlier.*;
import elki.logging.Logging;
import elki.result.Metadata;
import elki.result.ResultUtil;
import elki.result.outlier.OutlierResult;
import elki.utilities.optionhandling.AbstractParameterizer;
import elki.utilities.scaling.LinearScaling;

/**
 * Evaluator that tries to auto-run a number of evaluation methods.
 *
 * @author <NAME>
 * @since 0.5.0
 *
 * @opt nodefillcolor LemonChiffon
 *
 * @assoc - - - OutlierResult
 * @assoc - - - Clustering
 * @composed - - - OutlierROCCurve
 * @composed - - - OutlierPrecisionAtKCurve
 * @composed - - - OutlierPrecisionRecallCurve
 * @composed - - - ComputeOutlierHistogram
 * @composed - - - EvaluateClustering
 */
public class AutomaticEvaluation implements Evaluator {
  /**
   * Class logger
   */
  private static final Logging LOG = Logging.getLogger(AutomaticEvaluation.class);

  @Override
  public void processNewResult(Object newResult) {
    autoEvaluateClusterings(newResult);
    autoEvaluateOutliers(newResult);
  }

  protected void autoEvaluateOutliers(Object newResult) {
    Collection<OutlierResult> outliers = ResultUtil.filterResults(newResult, OutlierResult.class);
    if(LOG.isDebugging()) {
      LOG.debug("Number of new outlier results: " + outliers.size());
    }
    if(!outliers.isEmpty()) {
      Database db = ResultUtil.findDatabase(newResult);
      ensureClusteringResult(db);
      Collection<Clustering<?>> clusterings = ResultUtil.filterResults(db, Clustering.class);
      if(clusterings.isEmpty()) {
        LOG.warning("Could not find a clustering result, even after running 'ensureClusteringResult'?!?");
        return;
      }
      Clustering<?> basec = clusterings.iterator().next();
      // Find minority class label
      int min = Integer.MAX_VALUE;
      int total = 0;
      String label = null;
      if(basec.getAllClusters().size() > 1) {
        for(Cluster<?> c : basec.getAllClusters()) {
          final int csize = c.getIDs().size();
          total += csize;
          if(csize < min) {
            min = csize;
            label = c.getName();
          }
        }
      }
      if(label == null) {
        LOG.warning("Could not evaluate outlier results, as I could not find a minority label.");
        return;
      }
      if(min == 1) {
        LOG.warning("The minority class label had a single object. Try using 'ClassLabelFilter' to identify the class label column.");
      }
      if(min > 0.05 * total) {
        LOG.warning("The minority class I discovered (labeled '" + label + "') has " + (min * 100. / total) + "% of objects. Outlier classes should be more rare!");
      }
      LOG.verbose("Evaluating using minority class: " + label);
      Pattern pat = Pattern.compile("^" + Pattern.quote(label) + "$");
      // Evaluate rankings.
      new OutlierRankingEvaluation(pat).processNewResult(newResult);
      // Compute ROC curve
      new OutlierROCCurve(pat).processNewResult(newResult);
      // Compute Precision at k
      new OutlierPrecisionAtKCurve(pat, min << 1).processNewResult(newResult);
      // Compute ROC curve
      new OutlierPrecisionRecallCurve(pat).processNewResult(newResult);
      // Compute outlier histogram
      new ComputeOutlierHistogram(pat, 50, new LinearScaling(), false).processNewResult(newResult);
    }
  }

  protected void autoEvaluateClusterings(Object newResult) {
    Collection<Clustering<?>> clusterings = ResultUtil.filterResults(newResult, Clustering.class);
    if(LOG.isDebugging()) {
      LOG.warning("Number of new clustering results: " + clusterings.size());
    }
    for(Iterator<Clustering<?>> c = clusterings.iterator(); c.hasNext();) {
      if(c.next() instanceof ReferenceClustering) {
        c.remove();
      }
    }<commit_msg>Cleanups.<commit_after>/*
 * This file is part of ELKI:
 * Environment for Developing KDD-Applications Supported by Index-Structures
 *
 * Copyright (C) 2019
 * ELKI Development Team
 *
 * This program 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/>.
 */
package elki.evaluation;

import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Pattern;

import elki.clustering.trivial.ByLabelClustering;
import elki.clustering.trivial.ByLabelOrAllInOneClustering;
import elki.clustering.trivial.ReferenceClustering;
import elki.data.Cluster;
import elki.data.Clustering;
import elki.data.type.NoSupportedDataTypeException;
import elki.database.Database;
import elki.evaluation.clustering.EvaluateClustering;
import elki.evaluation.outlier.*;
import elki.logging.Logging;
import elki.result.Metadata;
import elki.result.ResultUtil;
import elki.result.outlier.OutlierResult;
import elki.utilities.optionhandling.AbstractParameterizer;
import elki.utilities.scaling.LinearScaling;

/**
 * Evaluator that tries to auto-run a number of evaluation methods.
 *
 * @author <NAME>
 * @since 0.5.0
 *
 * @opt nodefillcolor LemonChiffon
 *
 * @assoc - - - OutlierResult
 * @assoc - - - Clustering
 * @composed - - - OutlierROCCurve
 * @composed - - - OutlierPrecisionAtKCurve
 * @composed - - - OutlierPrecisionRecallCurve
 * @composed - - - ComputeOutlierHistogram
 * @composed - - - EvaluateClustering
 */
public class AutomaticEvaluation implements Evaluator {
  /**
   * Class logger
   */
  private static final Logging LOG = Logging.getLogger(AutomaticEvaluation.class);

  @Override
  public void processNewResult(Object newResult) {
    autoEvaluateClusterings(newResult);
    autoEvaluateOutliers(newResult);
  }

  protected void autoEvaluateOutliers(Object newResult) {
    Collection<OutlierResult> outliers = ResultUtil.filterResults(newResult, OutlierResult.class);
    if(LOG.isDebugging()) {
      LOG.debug("Number of new outlier results: " + outliers.size());
    }
    if(!outliers.isEmpty()) {
      Database db = ResultUtil.findDatabase(newResult);
      ensureClusteringResult(db);
      Collection<Clustering<?>> clusterings = ResultUtil.filterResults(db, Clustering.class);
      if(clusterings.isEmpty()) {
        LOG.warning("Could not find a clustering result, even after running 'ensureClusteringResult'?!?");
        return;
      }
      Clustering<?> basec = clusterings.iterator().next();
      // Find minority class label
      int min = Integer.MAX_VALUE;
      int total = 0;
      String label = null;
      if(basec.getAllClusters().size() > 1) {
        for(Cluster<?> c : basec.getAllClusters()) {
          final int csize = c.getIDs().size();
          total += csize;
          if(csize < min) {
            min = csize;
            label = c.getName();
          }
        }
      }
      if(label == null) {
        LOG.warning("Could not evaluate outlier results, as I could not find a minority label.");
        return;
      }
      if(min == 1) {
        LOG.warning("The minority class label had a single object. Try using 'ClassLabelFilter' to identify the class label column.");
      }
      if(min > 0.05 * total) {
        LOG.warning("The minority class I discovered (labeled '" + label + "') has " + (min * 100. / total) + "% of objects. Outlier classes should be more rare!");
      }
      LOG.verbose("Evaluating using minority class: " + label);
      Pattern pat = Pattern.compile("^" + Pattern.quote(label) + "$");
      // Evaluate rankings.
      new OutlierRankingEvaluation(pat).processNewResult(newResult);
      // Compute ROC curve
      new OutlierROCCurve(pat).processNewResult(newResult);
      // Compute Precision at k
      new OutlierPrecisionAtKCurve(pat, min << 1).processNewResult(newResult);
      // Compute ROC curve
      new OutlierPrecisionRecallCurve(pat).processNewResult(newResult);
      // Compute outlier histogram
      new ComputeOutlierHistogram(pat, 50, new LinearScaling(), false).processNewResult(newResult);
    }
  }

  protected void autoEvaluateClusterings(Object newResult) {
    Collection<Clustering<?>> clusterings = ResultUtil.filterResults(newResult, Clustering.class);
    if(LOG.isDebugging()) {
      LOG.debug("Number of new clustering results: " + clusterings.size());
    }
    for(Iterator<Clustering<?>> c = clusterings.iterator(); c.hasNext();) {
      if(c.next() instanceof ReferenceClustering) {
        c.remove();
      }
    }
{'fraction_non_alphanumeric': 0.10745353894820087, 'fraction_numerical': 0.0039541320680110716, 'mean_word_length': 3.4333917616126204, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5105231', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2836, 'n_words': 800}