ScalingMechanism.java [src/java/m/ann/training/scale] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.ann.training.scale;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.bson.Document;
import utils.Data;
import utils.MongoAccess;

/**
 *
 * @author sidereus
 */
public abstract class ScalingMechanism {

    abstract public String getStrategy();


    abstract public DataSetIndices compute(Iterable<Document> d, double trainingPerc);


    public DataSetIndices compute(Data d, double trainingPerc) {
        throw new UnsupportedOperationException("Nothing implemented yet");
    }


    protected int getDataLength(Iterable<Document> d) {
        return d.iterator().next().get(MongoAccess.METADATA, Document.class).getInteger(MongoAccess.VALUES_COUNT);
    }


    protected int getDataLength(Data d) {
        return d.getDatalength();
    }


    protected int getTrainingLength(double trainingPerc, int dataLength) {
        return Math.toIntExact(Math.round(trainingPerc * dataLength));
    }


    protected List<Integer> getRange(int dataLength) {
        return IntStream.range(0, dataLength).boxed().collect(Collectors.toCollection(ArrayList::new));
    }


    protected void checkIndex(int val, Document metadata, int dataLength) {
        if (val < 0 || val > dataLength) {
            String varName = metadata.getString(MongoAccess.NAME);
            String msg = "Checking indices to keep. Val " + val + " variable " + varName + " out of bounds";
            throw new IndexOutOfBoundsException(msg);
        }
    }


    // Factory method
    public static ScalingMechanism split(String splitMechanism) {
        switch (splitMechanism.toLowerCase()) {
            case "simple":
                return new SimpleSplit();
            case "random":
                return new Random();
            case "samedistribution":
                return new SameDistribution();
            default:
                throw new IllegalArgumentException("splitMechanism");
        }
    }

}