<commit_before><commit_msg>initial revision<commit_after>package de.lmu.ifi.dbs.evaluation;

import de.lmu.ifi.dbs.data.MetricalObject;
import de.lmu.ifi.dbs.database.Database;
import de.lmu.ifi.dbs.utilities.UnableToComplyException;
import de.lmu.ifi.dbs.utilities.optionhandling.OptionHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DisjointCrossValidationHoldout provides a set of partitions of a database to
 * perform cross-validation.
 * The test sets are guaranteed to be disjoint.
 * 
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class DisjointCrossValidationHoldout<M extends MetricalObject> extends RandomizedHoldout<M>
{
    /**
     * Parameter n for the number of folds.
     */
    public static final String N_P = "nfold";
    
    /**
     * Default number of folds.
     */
    public static final int N_DEFAULT = 10;
    
    /**
     * Description of the parameter n.
     */
    public static final String N_D = "<int>number of folds for cross-validation";
    
    /**
     * Holds the number of folds.
     */
    protected int nfold = N_DEFAULT;
    
    /**
     * Provides a holdout for n-fold cross-validation.
     * Additionally to the parameter seed, the parameter n is set.
     */
    public DisjointCrossValidationHoldout()
    {
        super();
        parameterToDescription.put(N_P+OptionHandler.EXPECTS_VALUE,N_D);
        optionHandler = new OptionHandler(parameterToDescription,DisjointCrossValidationHoldout.class.getName());
    }

    /**
     * Provides a set of n partitions of a database to
     * perform n-fold cross-validation.
     * 
     * @see de.lmu.ifi.dbs.evaluation.Holdout#partition(de.lmu.ifi.dbs.database.Database)
     */
    public TrainingAndTestSet<M>[] partition(Database<M> database)
    {
        TrainingAndTestSet<M>[] partitions = new TrainingAndTestSet[nfold];
        List<Integer> ids = database.getIDs();
        List<Integer>[] parts = new List[nfold];
        for(int i = 0; i < nfold; i++)
        {
            parts[i] = new ArrayList<Integer>();
        }
        for(Integer id : ids)
        {
            parts[random.nextInt(nfold)].add(id);
            
        }
        for(int i = 0; i < nfold; i++)
        {
            Map<Integer,List<Integer>> partition = new HashMap<Integer,List<Integer>>();
            List<Integer> training = new ArrayList<Integer>();
            for(int j = 0; j < nfold; j++)
            {
                if(j!=i)
                {
                    training.addAll(parts[j]);
                }
            }
            partition.put(0,training);
            partition.put(1,parts[i]);
            try
            {
                Map<Integer,Database<M>> part = database.partition(partition);
                partitions[i] = new TrainingAndTestSet<M>(part.get(0),part.get(1));
            }
            catch(UnableToComplyException e)
            {
                throw new RuntimeException(e);
            }
        }
        return partitions;
    }

    /**
     * 
     * @see de.lmu.ifi.dbs.utilities.optionhandling.Parameterizable#description()
     */
    public String description()
    {
        return "Provides an n-fold cross-validation holdout with disjoint test sets.";
    }

    /**
     * Sets the parameter n.
     * 
     * @see de.lmu.ifi.dbs.utilities.optionhandling.Parameterizable#setParameters(java.lang.String[])
     */
    public String[] setParameters(String[] args) throws IllegalArgumentException
    {
        String[] remainingParameters = optionHandler.grabOptions(args);
        if(optionHandler.isSet(N_P))
        {
            try
            {
                int nfold = Integer.parseInt(optionHandler.getOptionValue(N_P));
                if(nfold<1)
                {
                    throw new NumberFormatException("Parameter "+N_P+" is supposed to be a positiv integer. Found: "+optionHandler.getOptionValue(N_P));
                }
                this.nfold = nfold;
            }
            catch(NumberFormatException e)
            {
                throw new IllegalArgumentException("Parameter "+N_P+" is supposed to be a positiv integer. Found: "+optionHandler.getOptionValue(N_P),e);
            }
        }
        return remainingParameters;
    }
}
{'fraction_non_alphanumeric': 0.11123959296947271, 'fraction_numerical': 0.002312673450508788, 'mean_word_length': 2.091493924231594, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '374795', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1219, 'n_words': 269}