Example usage for weka.core Instances insertAttributeAt

List of usage examples for weka.core Instances insertAttributeAt

Introduction

In this page you can find the example usage for weka.core Instances insertAttributeAt.

Prototype



public void insertAttributeAt(Attribute att, int position) 

Source Link

Document

Inserts an attribute at the given position (0 to numAttributes()) and sets all values to be missing.

Usage

From source file:gr.auth.ee.lcs.AbstractLearningClassifierSystem.java

License:Open Source License

/**
 * Initialize the rule population by clustering the train set and producing rules based upon the clusters.
 * The train set is initially divided in as many partitions as are the distinct label combinations.
 * @throws Exception /*from www  .j  a v a 2s .  c  om*/
 * 
 * @param trainSet
 *             the type of Instances train set
 * */

public ClassifierSet initializePopulation(final Instances trainset) throws Exception {

    final double gamma = SettingsLoader.getNumericSetting("CLUSTER_GAMMA", .2);

    int numberOfLabels = (int) SettingsLoader.getNumericSetting("numberOfLabels", 1);

    final Instances set = trainset;

    SimpleKMeans kmeans = new SimpleKMeans();
    kmeans.setSeed(10);
    kmeans.setPreserveInstancesOrder(true);

    /*
     * Table partitions will hold instances only with attributes.
     * On the contrary, table partitionsWithCLasses will hold only the labels
     */
    Instances[] partitions = InstancesUtility.partitionInstances(this, trainset);
    Instances[] partitionsWithCLasses = InstancesUtility.partitionInstances(this, trainset);

    /*
    * Instead of having multiple positions for the same label combination, use only one.
    * This is the one that will be used to "cover" the centroids.
    */

    for (int i = 0; i < partitionsWithCLasses.length; i++) {
        Instance temp = partitionsWithCLasses[i].instance(0);
        partitionsWithCLasses[i].delete();
        partitionsWithCLasses[i].add(temp);
    }

    /*
    * Delete the labels from the partitions.
    */
    String attributesIndicesForDeletion = "";

    for (int k = set.numAttributes() - numberOfLabels + 1; k <= set.numAttributes(); k++) {
        if (k != set.numAttributes())
            attributesIndicesForDeletion += k + ",";
        else
            attributesIndicesForDeletion += k;
    }
    /*    attributesIncicesForDeletion = 8,9,10,11,12,13,14 e.g. for 7 attributes and 7 labels. 
    * It does not start from 7 because it assumes that the user inputs the number. See the api.
    */
    for (int i = 0; i < partitions.length; i++) {
        Remove remove = new Remove();
        remove.setAttributeIndices(attributesIndicesForDeletion);
        remove.setInvertSelection(false);
        remove.setInputFormat(partitions[i]);
        partitions[i] = Filter.useFilter(partitions[i], remove);
    }
    // partitions now contains only attributes

    /*
    * delete the attributes from partitionsWithCLasses
    */
    String labelsIndicesForDeletion = "";

    for (int k = 1; k <= set.numAttributes() - numberOfLabels; k++) {
        if (k != set.numAttributes() - numberOfLabels)
            labelsIndicesForDeletion += k + ",";
        else
            labelsIndicesForDeletion += k;
    }
    /*    attributesIncicesForDeletion = 8,9,10,11,12,13,14 e.g. for 7 attributes and 7 labels. 
    * It does not start from 7 because it assumes that the user inputs the number. See the api.
    */
    for (int i = 0; i < partitionsWithCLasses.length; i++) {
        Remove remove = new Remove();
        remove.setAttributeIndices(labelsIndicesForDeletion);
        remove.setInvertSelection(false);
        remove.setInputFormat(partitionsWithCLasses[i]);
        partitionsWithCLasses[i] = Filter.useFilter(partitionsWithCLasses[i], remove);
        //System.out.println(partitionsWithCLasses[i]);
    }
    // partitionsWithCLasses now contains only labels

    int populationSize = (int) SettingsLoader.getNumericSetting("populationSize", 1500);

    // the set used to store the rules from all the clusters
    ClassifierSet initialClassifiers = new ClassifierSet(new FixedSizeSetWorstFitnessDeletion(this,
            populationSize, new RouletteWheelSelector(AbstractUpdateStrategy.COMPARISON_MODE_DELETION, true)));

    for (int i = 0; i < partitions.length; i++) {

        try {

            kmeans.setNumClusters((int) Math.ceil(gamma * partitions[i].numInstances()));
            kmeans.buildClusterer(partitions[i]);
            int[] assignments = kmeans.getAssignments();

            /*            int k=0;
                        for (int j = 0; j < assignments.length; j++) {
                           System.out.printf("Instance %d => Cluster %d ", k, assignments[j]);
                           k++;
                           System.out.println();
                    
                        }
                        System.out.println();*/

            Instances centroids = kmeans.getClusterCentroids();

            int numOfCentroidAttributes = centroids.numAttributes();

            /*
             * The centroids in this stage hold only attributes. To continue, we need to provide them the labels.
             * These are the ones we removed earlier.
             * But first, open up positions for attributes.
             * */

            for (int j = 0; j < numberOfLabels; j++) {
                Attribute label = new Attribute("label" + j);
                centroids.insertAttributeAt(label, numOfCentroidAttributes + j);
            }

            for (int centroidInstances = 0; centroidInstances < centroids.numInstances(); centroidInstances++) {
                for (int labels = 0; labels < numberOfLabels; labels++) {
                    centroids.instance(centroidInstances).setValue(numOfCentroidAttributes + labels,
                            partitionsWithCLasses[i].instance(0).value(labels));
                }
            }

            //System.out.println(centroids);
            double[][] centroidsArray = InstancesUtility.convertIntancesToDouble(centroids);

            for (int j = 0; j < centroidsArray.length; j++) {
                //System.out.printf("Instance %d => Cluster %d ", k, assignments[j]);
                final Classifier coveringClassifier = this.getClassifierTransformBridge()
                        .createRandomCoveringClassifier(centroidsArray[j]);

                coveringClassifier.setClassifierOrigin(Classifier.CLASSIFIER_ORIGIN_INIT);
                initialClassifiers.addClassifier(new Macroclassifier(coveringClassifier, 1), false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //System.out.println(initialClassifiers);
    return initialClassifiers;
}

From source file:lu.lippmann.cdb.datasetview.tabs.RegressionTabView.java

License:Open Source License

/**
 * {@inheritDoc}//from w w w.ja v a  2 s . c o m
 */
@Override
public void update0(final Instances dataSet) throws Exception {
    this.panel.removeAll();

    final Object[] attrNames = WekaDataStatsUtil.getAttributeNames(dataSet).toArray();
    final JComboBox xCombo = new JComboBox(attrNames);
    xCombo.setBorder(new TitledBorder("Attribute to evaluate"));

    final JXPanel comboPanel = new JXPanel();
    comboPanel.setLayout(new GridLayout(1, 2));
    comboPanel.add(xCombo);
    final JXButton jxb = new JXButton("Compute");
    comboPanel.add(jxb);
    this.panel.add(comboPanel, BorderLayout.NORTH);

    jxb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                final Regression reg = new Regression(dataSet, xCombo.getSelectedIndex());

                final Instances newds = new Instances(dataSet);
                newds.insertAttributeAt(new Attribute("_regEval_"), newds.numAttributes());

                final double[] estims = reg.getEstims();
                for (int i = 0; i < estims.length; i++) {
                    newds.instance(i).setValue(newds.numAttributes() - 1, estims[i]);
                }

                final ScatterPlotTabView scatterPlotView = new ScatterPlotTabView();
                scatterPlotView.update0(newds);
                panel.add(scatterPlotView.getComponent(), BorderLayout.CENTER);

                final JXPanel tblpanel = new JXPanel();
                tblpanel.setLayout(new BorderLayout());
                tblpanel.setBorder(new TitledBorder("Coefficients"));
                final JXTable tbl = new JXTable();
                final SingleColumnTableModel mdl = new SingleColumnTableModel("Coeffs");
                final java.util.List<String> l = new ArrayList<String>();
                l.add("R2: " + reg.getR2());
                int ii = 0;
                for (final double d : reg.getCoe()) {
                    if (ii == 0)
                        l.add(" -> " + d);
                    else
                        l.add(dataSet.attribute(ii - 1).name() + " -> " + d);
                    ii++;
                }
                mdl.setData(l);
                tbl.setModel(mdl);
                tbl.setEditable(true);
                tbl.setShowHorizontalLines(false);
                tbl.setShowVerticalLines(false);
                tbl.setVisibleRowCount(5);
                tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                tblpanel.add(tbl, BorderLayout.CENTER);
                panel.add(tblpanel, BorderLayout.SOUTH);
                //panel.add(new JXLabel("R2: "+reg.getR2()+", "+reg.getCoeDesc()),BorderLayout.SOUTH);
            } catch (Exception e1) {
                e1.printStackTrace();
                panel.add(new JXLabel("Error during computation: " + e1.getMessage()), BorderLayout.CENTER);
            }

        }
    });
}

From source file:lu.lippmann.cdb.datasetview.tasks.SetAttributeAsTimestampTask.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  ww .  j  a va  2s .  co m*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    final String s = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n", "Attribute selection",
            JOptionPane.PLAIN_MESSAGE, null, WekaDataStatsUtil.getNumericAttributesNames(dataSet).toArray(),
            "");

    if (s != null) {
        final Instances newds = new Instances(dataSet);
        newds.insertAttributeAt(new Attribute("date", "dd-MM-yyyy HH:mm"), newds.numAttributes());

        final int sidx = newds.attribute(s).index();

        for (int i = 0; i < newds.numInstances(); i++) {
            newds.instance(i).setValue(newds.numAttributes() - 1, newds.instance(i).value(sidx));
        }

        return newds;
    } else
        return dataSet;
}

From source file:lu.lippmann.cdb.ext.hydviga.data.StationsDataProvider.java

License:Open Source License

private Instances getDataSetForMap(final Collection<String> sel, final Collection<String> usable) {
    final Instances ds = new Instances("ds", new ArrayList<Attribute>(), 0);
    ds.insertAttributeAt(new Attribute("name", new ArrayList<String>(this.coordinatesMap.keySet())),
            ds.numAttributes());//w  w w. j  a v  a  2  s.  c  o  m
    ds.insertAttributeAt(new Attribute("x"), ds.numAttributes());
    ds.insertAttributeAt(new Attribute("y"), ds.numAttributes());
    ds.insertAttributeAt(
            new Attribute("status",
                    Arrays.asList(new String[] { SELECTED_STATUS, USABLE_STATUS, NOT_USABLE_STATUS })),
            ds.numAttributes());
    ds.setClassIndex(ds.numAttributes() - 1);

    final Set<String> coordSelected = new HashSet<String>();
    for (final String ssel : sel) {
        final String coordsKey = coordinatesMap.get(ssel)[0] + "-" + coordinatesMap.get(ssel)[1];
        coordSelected.add(coordsKey);
    }
    final Set<String> coordUsable = new HashSet<String>();
    for (final String uu : usable) {
        final String coordsKey = coordinatesMap.get(uu)[0] + "-" + coordinatesMap.get(uu)[1];
        coordUsable.add(coordsKey);
    }

    final Set<String> coordAlreadyLoaded = new HashSet<String>();
    for (final Map.Entry<String, double[]> entry : this.coordinatesMap.entrySet()) {
        final String coordsKey = entry.getValue()[0] + "-" + entry.getValue()[1];
        if (coordAlreadyLoaded.contains(coordsKey))
            continue;
        final Instance inst = new DenseInstance(1.0d, new double[] { 0d, 0d, 0d, 0d });
        inst.setDataset(ds);
        inst.setValue(0, entry.getKey());
        inst.setValue(1, entry.getValue()[0]);
        inst.setValue(2, entry.getValue()[1]);
        //System.out.println(sel+" "+entry.getKey());
        inst.setValue(3, (coordSelected.contains(coordsKey)) ? SELECTED_STATUS
                : ((coordUsable.contains(coordsKey)) ? USABLE_STATUS : NOT_USABLE_STATUS));
        ds.add(inst);
        coordAlreadyLoaded.add(coordsKey);
    }

    return ds;
}

From source file:lu.lippmann.cdb.ext.hydviga.gaps.GapFiller.java

License:Open Source License

private Instances fillAllGaps(final Instances ds) throws Exception {
    Instances newds = new Instances(ds);

    final int firstDateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(newds);
    final String datename = newds.attribute(firstDateIdx).name();
    if (firstDateIdx == -1) {
        throw new Exception("No date attribute in this dataset!");
    }//from  ww w  .  jav a2  s  .c  om

    /* add a 'fake numerical' time field */
    newds.insertAttributeAt(new Attribute(datename + "_fake"), newds.numAttributes());
    for (int i = 0; i < newds.numInstances(); i++) {
        newds.instance(i).setValue(newds.numAttributes() - 1, newds.instance(i).value(firstDateIdx));
    }

    /* remove the 'true' time field */
    newds.deleteAttributeAt(firstDateIdx);

    /* process the dataset */
    newds = fillGaps0(newds);

    /* re-add the 'true' time field according to the 'fake numerical' time field */
    final String df = ds.attribute(firstDateIdx).getDateFormat();
    newds.insertAttributeAt(new Attribute(datename + "_new", df), newds.numAttributes());
    for (int i = 0; i < newds.numInstances(); i++) {
        newds.instance(i).setValue(newds.numAttributes() - 1,
                newds.instance(i).value(newds.numAttributes() - 2));
    }

    /* delete the 'fake numerical' time field */
    newds.deleteAttributeAt(newds.numAttributes() - 2);

    newds.sort(newds.numAttributes() - 1);

    return newds;
}

From source file:lu.lippmann.cdb.ext.hydviga.gaps.GapFiller.java

License:Open Source License

private Instances fillAllGapsWithDiscretizedTime(final Instances ds) throws Exception {
    int firstDateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(ds);
    final String datename = ds.attribute(firstDateIdx).name();
    if (firstDateIdx == -1) {
        throw new Exception("No date attribute in this dataset!");
    }//from   w  w  w .j  a  v a2s  .c  o  m

    Instances newds = new Instances(ds);

    /* add discretized time */
    newds = WekaTimeSeriesUtil.buildDataSetWithDiscretizedTime(newds);

    /* add fake numerical time */
    newds.insertAttributeAt(new Attribute(datename + "_fake"), newds.numAttributes());
    for (int i = 0; i < newds.numInstances(); i++) {
        newds.instance(i).setValue(newds.numAttributes() - 1, newds.instance(i).value(firstDateIdx));
    }

    /* remove 'true' date */
    while (firstDateIdx != -1) {
        newds.deleteAttributeAt(firstDateIdx);
        firstDateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(newds);
    }

    /* transform nominal as binaries */
    for (int iidx : WekaDataStatsUtil.getNominalAttributesIndexes(newds)) {
        newds = WekaDataProcessingUtil.buildDataSetWithNominalAsBinary(newds, iidx);
    }

    /* rename attributes for which the name can occur issues in tree evaluation */
    for (int k = 0; k < newds.numAttributes(); k++) {
        String atn = newds.attribute(k).name();
        if (atn.contains("="))
            atn = atn.replaceAll("=", (int) (Math.random() * 1000) + "");
        if (atn.contains("<"))
            atn = atn.replaceAll("<", (int) (Math.random() * 1000) + "");
        if (atn.contains(">"))
            atn = atn.replaceAll(">", (int) (Math.random() * 1000) + "");
        if (atn.contains("."))
            atn = atn.replace(".", (int) (Math.random() * 1000) + "");
        newds = WekaDataProcessingUtil.renameAttribute(newds, k, atn);
    }

    /* replace missing values */
    newds = fillGaps0(newds);

    /* reconstruct date according to discretized time */
    final String df = ds.attribute(WekaDataStatsUtil.getFirstDateAttributeIdx(ds)).getDateFormat();
    newds.insertAttributeAt(new Attribute(datename + "_new", df), newds.numAttributes());
    final int newfirstDateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(newds);
    for (int i = 0; i < newds.numInstances(); i++) {
        final Instance inst = newds.instance(i);
        inst.setValue(newfirstDateIdx, newds.instance(i).value(newds.numAttributes() - 2));
    }

    /* sort by date ! */
    newds.sort(newfirstDateIdx);

    /* remove discretized time */
    final Set<String> toRemove = new HashSet<String>();
    for (int i = 0; i < newds.numAttributes(); i++) {
        if (newds.attribute(i).name().startsWith("t_"))
            toRemove.add(newds.attribute(i).name());
    }
    for (final String tr : toRemove)
        newds.deleteAttributeAt(newds.attribute(tr).index());

    /* delete the fake attribute time */
    newds.deleteAttributeAt(newds.numAttributes() - 2);

    return newds;
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapFillingFrame.java

License:Open Source License

private GapFillingCase refresh(final Algo algo, final int[] indexesOfUsedSeries, final boolean hideOthers,
        final boolean showError, final boolean zoom, final boolean showEnvelope, final boolean multAxis)
        throws Exception {
    if (!inBatchMode)
        this.centerPanel.removeAll();

    int[] arr = new int[] { attr.index(), dateIdx };
    for (final int iii : indexesOfUsedSeries) {
        arr = ArraysUtil.concat(arr, new int[] { dataSet.attribute(this.attrNames.get(iii)).index() });
    }/*  w w w  .  j  a v a2 s.co m*/

    Instances filteredDs = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet, arr);
    //System.out.println(filteredDs.toSummaryString());

    Attribute original = null;
    Instances filteredDsWithOriginal = null;
    if (this.isGapSimulated) {
        original = new Attribute("original");
        filteredDsWithOriginal = new Instances(filteredDs);
        filteredDsWithOriginal.insertAttributeAt(original, filteredDsWithOriginal.numAttributes() - 1);
        final Attribute origAttr = filteredDsWithOriginal.attribute(original.name());
        for (int ii = position - 1; ii < position + gapsize + 1; ii++) {
            filteredDsWithOriginal.instance(ii).setValue(origAttr, this.originalDataBeforeGapSimulation[ii]);
        }
    }

    filteredDs = WekaDataProcessingUtil.buildFilteredDataSet(filteredDs, 0, filteredDs.numAttributes() - 1,
            Math.max(0, this.position - this.valuesBeforeAndAfter),
            Math.min(this.position + this.gapsize + this.valuesBeforeAndAfter, filteredDs.numInstances() - 1));

    this.gapFiller = GapFillerFactory.getGapFiller(algo);

    final Instances completedds = this.gapFiller.fillGaps(filteredDs);
    final Instances diff = WekaTimeSeriesUtil.buildDiff(filteredDs, completedds);

    final int valuesToCheckForError = this.valuesBeforeAndAfter / 4;

    double maeByEnlargingGap = Double.NaN;
    double maeByAddingAGapBefore = Double.NaN;
    double maeByAddingAGapAfter = Double.NaN;
    double maeByComparingWithOriginal = Double.NaN;

    double rmseByEnlargingGap = Double.NaN;
    double rmseByAddingAGapBefore = Double.NaN;
    double rmseByAddingAGapAfter = Double.NaN;
    double rmseByComparingWithOriginal = Double.NaN;

    double rsrByEnlargingGap = Double.NaN;
    double rsrByAddingAGapBefore = Double.NaN;
    double rsrByAddingAGapAfter = Double.NaN;
    double rsrByComparingWithOriginal = Double.NaN;

    double pbiasByEnlargingGap = Double.NaN;
    double pbiasByAddingAGapBefore = Double.NaN;
    double pbiasByAddingAGapAfter = Double.NaN;
    double pbiasByComparingWithOriginal = Double.NaN;

    double nsByEnlargingGap = Double.NaN;
    double nsByAddingAGapBefore = Double.NaN;
    double nsByAddingAGapAfter = Double.NaN;
    double nsByComparingWithOriginal = Double.NaN;

    double indexOfAgreementByEnlargingGap = Double.NaN;
    double indexOfAgreementByAddingAGapBefore = Double.NaN;
    double indexOfAgreementByAddingAGapAfter = Double.NaN;
    double indexOfAgreementByComparingWithOriginal = Double.NaN;

    if (this.isGapSimulated) {
        //System.out.println(attr.index()+" begin="+(this.position)+" end="+(this.position+this.gapsize));

        final Instances correctedDataSet = buildCorrectedDataset(diff);

        final double[] cad = correctedDataSet.attributeToDoubleArray(attr.index());
        maeByComparingWithOriginal = MathsUtil.mae(this.originalDataBeforeGapSimulation, cad, this.position,
                this.position + this.gapsize);
        rmseByComparingWithOriginal = MathsUtil.rmse(this.originalDataBeforeGapSimulation, cad, this.position,
                this.position + this.gapsize);
        rsrByComparingWithOriginal = MathsUtil.rsr(this.originalDataBeforeGapSimulation, cad, this.position,
                this.position + this.gapsize);
        pbiasByComparingWithOriginal = MathsUtil.pbias(this.originalDataBeforeGapSimulation, cad, this.position,
                this.position + this.gapsize);
        nsByComparingWithOriginal = MathsUtil.nashSutcliffe(this.originalDataBeforeGapSimulation, cad,
                this.position, this.position + this.gapsize);
        indexOfAgreementByComparingWithOriginal = MathsUtil.indexOfAgreement(
                this.originalDataBeforeGapSimulation, cad, this.position, this.position + this.gapsize);
    } else {
        maeByEnlargingGap = this.gapFiller.evaluateMAEByEnlargingGap(filteredDs, valuesToCheckForError);
        maeByAddingAGapBefore = this.gapFiller.evaluateMAEByAddingAGapBefore(filteredDs, valuesToCheckForError);
        maeByAddingAGapAfter = this.gapFiller.evaluateMAEByAddingAGapAfter(filteredDs, valuesToCheckForError);

        rmseByEnlargingGap = this.gapFiller.evaluateRMSEByEnlargingGap(filteredDs, valuesToCheckForError);
        rmseByAddingAGapBefore = this.gapFiller.evaluateRMSEByAddingAGapBefore(filteredDs,
                valuesToCheckForError);
        rmseByAddingAGapAfter = this.gapFiller.evaluateRMSEByAddingAGapAfter(filteredDs, valuesToCheckForError);

        nsByEnlargingGap = this.gapFiller.evaluateNSByEnlargingGap(filteredDs, valuesToCheckForError);
        nsByAddingAGapBefore = this.gapFiller.evaluateNSByAddingAGapBefore(filteredDs, valuesToCheckForError);
        nsByAddingAGapAfter = this.gapFiller.evaluateNSByAddingAGapAfter(filteredDs, valuesToCheckForError);
    }

    if (hideOthers) {
        if (this.isGapSimulated) {
            filteredDs = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(filteredDsWithOriginal,
                    new int[] { 0, 1, filteredDsWithOriginal.attribute(original.name()).index() });
            filteredDs = WekaDataProcessingUtil.buildFilteredDataSet(filteredDs, 0,
                    filteredDs.numAttributes() - 1, Math.max(0, this.position - this.valuesBeforeAndAfter),
                    Math.min(this.position + this.gapsize + this.valuesBeforeAndAfter,
                            filteredDs.numInstances() - 1));
        } else {
            filteredDs = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(filteredDs,
                    new int[] { 0, 1 });
        }
    }

    final Instances decomposition = WekaTimeSeriesUtil.buildMergedDataSet(filteredDs, diff);

    final Attribute diffAttribute = decomposition.attribute(attr.name() + "_diff");

    final List<XYAnnotation> aaa = new ArrayList<XYAnnotation>();
    if (showError) {
        showError(this.isGapSimulated ? maeByComparingWithOriginal : maeByEnlargingGap, decomposition,
                diffAttribute, aaa);
    }

    if (showEnvelope) {
        final MainViewLoadingFrame loadingFrame = new MainViewLoadingFrame();
        loadingFrame.setVisible(true);
        loadingFrame.pack();
        loadingFrame.repaint();
        showEnvelope(arr, aaa);
        loadingFrame.setVisible(false);
    }

    if (!inBatchMode) {
        final ChartPanel cp;
        /*if (showError)
        {
           cp=TimeSeriesChartUtil.buildChartPanelForAllAttributesInterval(decomposition,WekaDataStatsUtil.getFirstDateAttributeIdx(decomposition),mae,diffAttribute.index());
        }
        else
        {*/
        cp = TimeSeriesChartUtil.buildChartPanelForAllAttributes(decomposition, multAxis,
                WekaDataStatsUtil.getFirstDateAttributeIdx(decomposition), null, aaa);
        /*}*/

        final Marker gapBeginMarker = new ValueMarker(
                dataSet.instance(Math.max(0, position - 1)).value(dateIdx));
        gapBeginMarker.setPaint(Color.RED);
        gapBeginMarker.setLabel("Gap begin");
        gapBeginMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        gapBeginMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        cp.getChart().getXYPlot().addDomainMarker(gapBeginMarker);

        final Marker gapEndMarker = new ValueMarker(
                dataSet.instance(Math.min(dataSet.numInstances() - 1, position + gapsize)).value(dateIdx));
        gapEndMarker.setPaint(Color.RED);
        gapEndMarker.setLabel("Gap end");
        gapEndMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        gapEndMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
        cp.getChart().getXYPlot().addDomainMarker(gapEndMarker);

        if (!zoom) {
            final NumberAxis na = (NumberAxis) (cp.getChart().getXYPlot().getRangeAxis());
            na.setRange(0, WekaDataStatsUtil.getMaxValue(dataSet, attrNames));
        }

        String errorInfo;
        if (!this.isGapSimulated) {
            errorInfo = "By enlarging the gap:\t MAE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(maeByEnlargingGap) + "\t RMSE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(rmseByEnlargingGap) + "\t NASH-SUTCLIFFE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(nsByEnlargingGap)
                    + "\nBy adding a gap before:\t MAE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(maeByAddingAGapBefore) + "\t RMSE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(rmseByAddingAGapBefore) + "\t NASH-SUTCLIFFE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(nsByAddingAGapBefore)
                    + "\nBy adding a gap after:\t MAE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(maeByAddingAGapAfter) + "\t RMSE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(rmseByAddingAGapAfter) + "\t NASH-SUTCLIFFE="
                    + FormatterUtil.DECIMAL_FORMAT_4.format(nsByAddingAGapAfter);
        } else {
            errorInfo = "MAE: " + FormatterUtil.DECIMAL_FORMAT_4.format(maeByComparingWithOriginal);
            errorInfo += "\n";
            errorInfo += "RMSE: " + FormatterUtil.DECIMAL_FORMAT_4.format(rmseByComparingWithOriginal);
            errorInfo += "\n";
            errorInfo += "RSR: " + FormatterUtil.DECIMAL_FORMAT_4.format(rsrByComparingWithOriginal);
            errorInfo += "\n";
            errorInfo += "PBIAS: " + FormatterUtil.DECIMAL_FORMAT_4.format(pbiasByComparingWithOriginal);
            errorInfo += "\n";
            errorInfo += "NASH-SUTCLIFFE: " + FormatterUtil.DECIMAL_FORMAT_4.format(nsByComparingWithOriginal);
            errorInfo += "\n";
            errorInfo += "INDEX OF AGREEMENT: "
                    + FormatterUtil.DECIMAL_FORMAT_4.format(indexOfAgreementByComparingWithOriginal);
        }
        cp.setBorder(new TitledBorder(""));
        final JTextArea errorTextArea = new JTextArea(errorInfo);
        errorTextArea.setBorder(new TitledBorder(""));
        this.centerPanel.add(errorTextArea, BorderLayout.NORTH);
        this.centerPanel.add(cp, BorderLayout.CENTER);

        final JXPanel cmdPanel = new JXPanel();
        final JXButton okButton = new JXButton("Ok");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                final Instances correctedDataSet = buildCorrectedDataset(diff);

                final DataChange change = new DataChange(correctedDataSet, TabView.DataChangeTypeEnum.Update);
                atv.pushDataChange(change);

                setVisible(false);
            }
        });
        cmdPanel.add(okButton);
        final JXButton cancelButton = new JXButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                setVisible(false);
            }
        });
        cmdPanel.add(cancelButton);
        this.centerPanel.add(cmdPanel, BorderLayout.SOUTH);

        this.centerPanel.updateUI();

        getContentPane().repaint();
    }

    final double globalMAE = (this.isGapSimulated) ? maeByComparingWithOriginal
            : ((maeByEnlargingGap + maeByAddingAGapBefore + maeByAddingAGapAfter) / 3);

    final double globalRMSE = (this.isGapSimulated) ? rmseByComparingWithOriginal
            : ((rmseByEnlargingGap + rmseByAddingAGapBefore + rmseByAddingAGapAfter) / 3);

    final double globalRSR = (this.isGapSimulated) ? rsrByComparingWithOriginal
            : ((rsrByEnlargingGap + rsrByAddingAGapBefore + rsrByAddingAGapAfter) / 3);

    final double globalPBIAS = (this.isGapSimulated) ? pbiasByComparingWithOriginal
            : ((pbiasByEnlargingGap + pbiasByAddingAGapBefore + pbiasByAddingAGapAfter) / 3);

    final double globalNS = (this.isGapSimulated) ? nsByComparingWithOriginal
            : ((nsByEnlargingGap + nsByAddingAGapBefore + nsByAddingAGapAfter) / 3);

    final double globalIndexOfAgreement = (this.isGapSimulated) ? indexOfAgreementByComparingWithOriginal
            : ((indexOfAgreementByEnlargingGap + indexOfAgreementByAddingAGapBefore
                    + indexOfAgreementByAddingAGapAfter) / 3);

    // usage logs for stats      
    final long firstTimestamp = (long) dataSet.instance(position).value(dateIdx);
    final boolean isDuringRising;
    if (nearest == null)
        isDuringRising = GapsUtil.isDuringRising(dataSet, position, gapsize,
                new int[] { dateIdx, attr.index() });
    else
        isDuringRising = GapsUtil.isDuringRising(dataSet, position, gapsize,
                new int[] { dateIdx, attr.index(), dataSet.attribute(nearest).index() });

    return new GapFillingCase(DateUtil.getSeason(firstTimestamp), DateUtil.getYear(firstTimestamp), algo,
            gapsize, position, attr, gcp.getCoordinates(attr.name())[0], gcp.getCoordinates(attr.name())[1],
            gcp.findDownstreamStation(attr.name()) != null, gcp.findUpstreamStation(attr.name()) != null,
            globalMAE, globalRMSE, globalRSR, globalPBIAS, globalNS, globalIndexOfAgreement,
            ArraysUtil.contains(indexesOfUsedSeries, attrNames.indexOf(mostSimilar)),
            ArraysUtil.contains(indexesOfUsedSeries, attrNames.indexOf(nearest)),
            ArraysUtil.contains(indexesOfUsedSeries, attrNames.indexOf(downstream)),
            ArraysUtil.contains(indexesOfUsedSeries, attrNames.indexOf(upstream)), isDuringRising,
            GapsUtil.measureHighMiddleLowInterval(dataSet, attr.index(), position - 1));
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java

License:Open Source License

public static ChartPanel buildGapChartPanelWithCorrection(final Instances pdataSet, final int dateIdx,
        final Attribute attr, final int gapsize, final int position, final GapFiller gapFiller,
        final java.util.Collection<String> attrs) throws Exception {
    final Instances dataSetWithTheGap = new Instances(pdataSet);
    for (int i = position; i < position + gapsize; i++)
        dataSetWithTheGap.instance(i).setMissing(attr);

    int[] arr = new int[] { attr.index(), dateIdx };
    for (final String sss : attrs) {
        arr = ArraysUtil.concat(arr, new int[] { dataSetWithTheGap.attribute(sss).index() });
    }// w  ww. j av a  2s .  c  o  m

    Instances filteredDsWithTheGap = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSetWithTheGap,
            arr);
    filteredDsWithTheGap = WekaDataProcessingUtil.buildFilteredDataSet(filteredDsWithTheGap, 0,
            filteredDsWithTheGap.numAttributes() - 1,
            Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize),
            Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize,
                    filteredDsWithTheGap.numInstances() - 1));

    final Instances completedds = gapFiller.fillGaps(filteredDsWithTheGap);
    final Instances diff = WekaTimeSeriesUtil.buildDiff(filteredDsWithTheGap, completedds);

    Instances filteredDsWithoutTheGap = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(pdataSet, arr);
    filteredDsWithoutTheGap = WekaDataProcessingUtil.buildFilteredDataSet(filteredDsWithoutTheGap, 0,
            filteredDsWithoutTheGap.numAttributes() - 1,
            Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize),
            Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize,
                    filteredDsWithoutTheGap.numInstances() - 1));

    diff.insertAttributeAt(new Attribute(attr.name() + "_orig"), diff.numAttributes());
    for (int i = 0; i < filteredDsWithoutTheGap.numInstances(); i++) {
        diff.instance(i).setValue(diff.numAttributes() - 1,
                filteredDsWithoutTheGap.instance(i).value(filteredDsWithoutTheGap.attribute(attr.name())));
    }
    //System.out.println(attr.name()+"\n"+diff.toSummaryString());

    final java.util.List<String> toRemove = new java.util.ArrayList<String>();
    for (int j = 0; j < diff.numAttributes(); j++) {
        final String consideredAttrName = diff.attribute(j).name();
        if (!consideredAttrName.contains("timestamp") && !consideredAttrName.contains(attr.name()))
            toRemove.add(consideredAttrName);
    }
    diff.setClassIndex(-1);
    for (final String ssss : toRemove)
        diff.deleteAttributeAt(diff.attribute(ssss).index());
    //System.out.println(attr.name()+"\n"+diff.toSummaryString());

    final ChartPanel cp = TimeSeriesChartUtil.buildChartPanelForAllAttributes(diff, false,
            WekaDataStatsUtil.getFirstDateAttributeIdx(diff), null);

    final XYPlot xyp = (XYPlot) cp.getChart().getPlot();
    xyp.getDomainAxis().setLabel("");
    xyp.getRangeAxis().setLabel("");

    final Marker gapBeginMarker = new ValueMarker(
            dataSetWithTheGap.instance(Math.max(0, position - 1)).value(dateIdx));
    gapBeginMarker.setPaint(Color.RED);
    gapBeginMarker.setLabel("Gap begin");
    gapBeginMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    gapBeginMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
    cp.getChart().getXYPlot().addDomainMarker(gapBeginMarker);

    final Marker gapEndMarker = new ValueMarker(dataSetWithTheGap
            .instance(Math.min(dataSetWithTheGap.numInstances() - 1, position + gapsize)).value(dateIdx));
    gapEndMarker.setPaint(Color.RED);
    gapEndMarker.setLabel("Gap end");
    gapEndMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
    gapEndMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    cp.getChart().getXYPlot().addDomainMarker(gapEndMarker);

    addExportPopupMenu(diff, cp);

    return cp;
}

From source file:marytts.tools.voiceimport.PauseDurationTrainer.java

License:Open Source License

private Instances enterDurations(Instances data, List<Integer> durs) {

    // System.out.println("discretizing durations...");

    // now discretize and set target attributes (= pause durations)
    // for that, first train discretizer
    GmmDiscretizer discr = GmmDiscretizer.trainDiscretizer(durs, 6, true);

    // used to store the collected values
    ArrayList<String> targetVals = new ArrayList<String>();

    for (int mappedDur : discr.getPossibleValues()) {
        targetVals.add(mappedDur + "ms");
    }/* w ww.  j a va  2  s  .  co  m*/

    // FastVector attributeDeclarations = data.;

    // attribute declaration finished
    data.insertAttributeAt(new Attribute("target", targetVals), data.numAttributes());

    for (int i = 0; i < durs.size(); i++) {

        Instance currInst = data.instance(i);
        int dur = durs.get(i);

        // System.out.println(" mapping " + dur + " to " + discr.discretize(dur) + " - bi:" +
        // data.instance(i).value(data.attribute("breakindex")));

        currInst.setValue(data.numAttributes() - 1, discr.discretize(dur) + "ms");

    }

    // Make the last attribute be the class
    data.setClassIndex(data.numAttributes() - 1);

    return data;
}

From source file:meka.classifiers.multilabel.meta.MBR.java

License:Open Source License

@Override
public void buildClassifier(Instances data) throws Exception {
    testCapabilities(data);// w ww  .  j  av a 2 s.  c om

    int c = data.classIndex();

    // Base BR

    if (getDebug())
        System.out.println("Build BR Base (" + c + " models)");
    m_BASE = (BR) AbstractClassifier.forName(getClassifier().getClass().getName(),
            ((AbstractClassifier) getClassifier()).getOptions());
    m_BASE.buildClassifier(data);

    // Meta BR

    if (getDebug())
        System.out.println("Prepare Meta data           ");
    Instances meta_data = new Instances(data);

    FastVector BinaryClass = new FastVector(c);
    BinaryClass.addElement("0");
    BinaryClass.addElement("1");

    for (int i = 0; i < c; i++) {
        meta_data.insertAttributeAt(new Attribute("metaclass" + i, BinaryClass), c);
    }

    for (int i = 0; i < data.numInstances(); i++) {
        double cfn[] = m_BASE.distributionForInstance(data.instance(i));
        for (int a = 0; a < cfn.length; a++) {
            meta_data.instance(i).setValue(a + c, cfn[a]);
        }
    }

    meta_data.setClassIndex(c);
    m_InstancesTemplate = new Instances(meta_data, 0);

    if (getDebug())
        System.out.println("Build BR Meta (" + c + " models)");

    m_META = (BR) AbstractClassifier.forName(getClassifier().getClass().getName(),
            ((AbstractClassifier) getClassifier()).getOptions());
    m_META.buildClassifier(meta_data);
}