package eu.humanbrainproject.mip.algorithms.rapidminer;

import com.rapidminer.example.Attribute;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.table.AttributeFactory;
import com.rapidminer.example.table.DoubleArrayDataRow;
import com.rapidminer.example.table.MemoryExampleTable;
import com.rapidminer.tools.Ontology;
import eu.humanbrainproject.mip.algorithms.db.DBException;

import java.util.LinkedList;
import java.util.List;

/**
 * Useful for tests
 */
public class NominalClassificationInputData extends InputData {

    private final String[][] sampleData;
    private final String[] labels;

    public NominalClassificationInputData(String[] featuresNames, String variableName, String[][] sampleData, String[] labels) {
        super(featuresNames, variableName, null);

        this.sampleData = sampleData;
        this.labels = labels;
    }

    protected ExampleSet createExampleSet() throws DBException {

        List<Attribute> attributes = new LinkedList<>();
        for (String featuresName : getFeaturesNames()) {
            attributes.add(AttributeFactory.createAttribute(featuresName, Ontology.REAL));
        }

        // Create label
        Attribute label = AttributeFactory.createAttribute(getVariableName(), Ontology.NOMINAL);
        attributes.add(label);

        // Create table
        MemoryExampleTable table = new MemoryExampleTable(attributes);

        // Fill the table
        for (int d = 0; d < sampleData.length; d++) {
            double[] tableData = new double[attributes.size()];
            System.arraycopy(sampleData[d], 0, tableData, 0, sampleData[d].length);

            // Maps the nominal classification to a double value
            tableData[sampleData[d].length] = label.getMapping().mapString(labels[d]);

            // Add data row
            table.addDataRow(new DoubleArrayDataRow(tableData));
        }

        // Create example set
        return table.createExampleSet(label);
    }
}

@@ -187,16 +187,68 @@
 actory;%0A
+import com.rapidminer.example.table.DataRowFactory;%0A
 import c
@@ -1194,10 +1194,13 @@
 ogy.
-RE
+NOMIN
 AL))
@@ -1621,79 +1621,161 @@
     
-System.arraycopy(sampleData%5Bd%5D, 0, tableData, 0, sampleData%5Bd%5D.length);
+for (int a = 0; a %3C sampleData%5Bd%5D.length; a++) %7B%0A                tableData%5Ba%5D = attributes.get(a).getMapping().mapString(sampleData%5Bd%5D%5Ba%5D);%0A            %7D
 %0A%0A  

Fix NominalClassificationInputData