Example usage for weka.core Instances instance

List of usage examples for weka.core Instances instance

Introduction

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

Prototype



publicInstance instance(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

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

License:Open Source License

/**
 * TODO: refactor it in order to use MathsUtil
 */// w ww.j av  a  2s  . c  o  m
public static double nashSutcliffe(final Instances expected, final Instances predicted, final int idx,
        final int begin, final int end) {
    int trueN = 0;
    double mean = 0d;
    final int l2 = Math.min(expected.numInstances() - 1, end);
    final int l1 = Math.max(0, begin);
    for (int i = l1; i < l2; i++) {
        if (expected.instance(i).hasMissingValue())
            continue;
        mean += expected.instance(i).value(idx);
        trueN++;
    }

    if (trueN == 0)
        /*throw new IllegalStateException();*/ return Double.NaN;

    mean /= trueN;

    double sumDiffs = 0d;
    double sumDiffsMean = 0d;
    for (int i = l1; i < l2; i++) {
        if (expected.instance(i).hasMissingValue())
            continue;

        final double diff = Math.abs(expected.instance(i).value(idx) - predicted.instance(i).value(idx));
        sumDiffs += diff * diff;

        final double diffMean = Math.abs(expected.instance(i).value(idx) - mean);
        sumDiffsMean += diffMean * diffMean;
    }
    return 1d - (sumDiffs / sumDiffsMean);
}

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

License:Open Source License

/**
 * {@inheritDoc}//  w  ww .jav  a 2  s  . c o  m
 */
@Override
Instances fillGaps0(final Instances ds) throws Exception {
    final Instances newds = WekaDataProcessingUtil.buildDataSetWithoutConstantAttributes(ds);

    final int attrWithMissingIdx = WekaDataStatsUtil.getFirstAttributeWithMissingValue(newds);
    if (attrWithMissingIdx == -1)
        throw new IllegalStateException();

    final Instances trainingSet = new Instances(newds, 0);
    for (int i = 0; i < newds.numInstances(); i++) {
        if (!newds.instance(i).hasMissingValue())
            trainingSet.add(newds.instance(i));
    }
    //System.out.println(trainingSet);      
    trainingSet.setClassIndex(attrWithMissingIdx);

    //System.out.println("Training (size="+trainingSet.numInstances()+") ...");      
    this.classifier.buildClassifier(trainingSet);
    //System.out.println("... trained!");

    newds.setClassIndex(attrWithMissingIdx);
    for (int i = 0; i < newds.numInstances(); i++) {
        if (newds.instance(i).isMissing(attrWithMissingIdx)) {
            final Instance newrecord = new DenseInstance(newds.instance(i));
            newrecord.setDataset(newds);
            final double newval = this.classifier.classifyInstance(newrecord);
            newds.instance(i).setValue(attrWithMissingIdx, newval);
        }
    }

    //System.out.println("initial -> "+ds.toSummaryString());
    //System.out.println("corrected -> "+newds.toSummaryString());

    this.model = this.classifier.toString();

    return newds;
}

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

License:Open Source License

/**
 * {@inheritDoc}/*from   w  w w . j a v  a2s  . co m*/
 */
@Override
Instances fillGaps0(final Instances ds) throws Exception {
    final Instances newds = WekaDataProcessingUtil.buildDataSetWithoutConstantAttributes(ds);
    final int numInstances = newds.numInstances();

    final int attrWithMissingIdx = WekaDataStatsUtil.getFirstAttributeWithMissingValue(newds);
    if (attrWithMissingIdx == -1)
        throw new IllegalStateException();

    final Instances trainingSet = new Instances(newds, 0);
    for (int i = 0; i < numInstances; i++) {
        if (!newds.instance(i).hasMissingValue())
            trainingSet.add(newds.instance(i));
    }
    //System.out.println(trainingSet);

    final Regression reg = new Regression(trainingSet, attrWithMissingIdx);
    final double[] coeffs = reg.getCoe();
    //System.out.println(reg.getR2());
    //System.out.println(reg.getCoeDesc());

    for (int i = 0; i < numInstances; i++) {
        if (newds.instance(i).isMissing(attrWithMissingIdx)) {
            double newval = coeffs[0];
            for (int j = 1; j < trainingSet.numAttributes(); j++) {
                if (j == attrWithMissingIdx)
                    continue;

                final String attrName = trainingSet.attribute(j).name();

                //System.out.println(reg.getCoef(attrName)+" * "+attrName);

                newval += reg.getCoef(attrName) * newds.instance(i).value(newds.attribute(attrName));
            }
            //System.out.println("oldval -> "+newds.instance(i).value(attrWithMissingIdx));
            //System.out.println("newval -> "+newval);
            newds.instance(i).setValue(attrWithMissingIdx, newval);
        }
    }

    //System.out.println("corrected -> "+newds);

    this.model = reg.getCoeDesc();

    return newds;
}

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

License:Open Source License

/**
 * Constructor./*  www. j  a v a2 s  .c o m*/
 */
public GapFillingFrame(final AbstractTabView atv, final Instances dataSet, final Attribute attr,
        final int dateIdx, final int valuesBeforeAndAfter, final int position, final int gapsize,
        final StationsDataProvider gcp, final boolean inBatchMode) {
    super();

    setTitle("Gap filling for " + attr.name() + " ("
            + dataSet.attribute(dateIdx).formatDate(dataSet.instance(position).value(dateIdx)) + " -> "
            + dataSet.attribute(dateIdx).formatDate(dataSet.instance(position + gapsize).value(dateIdx)) + ")");
    LogoHelper.setLogo(this);

    this.atv = atv;

    this.dataSet = dataSet;
    this.attr = attr;
    this.dateIdx = dateIdx;
    this.valuesBeforeAndAfter = valuesBeforeAndAfter;
    this.position = position;
    this.gapsize = gapsize;

    this.gcp = gcp;

    final Instances testds = WekaDataProcessingUtil.buildFilteredDataSet(dataSet, 0,
            dataSet.numAttributes() - 1, Math.max(0, position - valuesBeforeAndAfter),
            Math.min(position + gapsize + valuesBeforeAndAfter, dataSet.numInstances() - 1));

    this.attrNames = WekaTimeSeriesUtil.getNamesOfAttributesWithoutGap(testds);

    this.isGapSimulated = (this.attrNames.contains(attr.name()));
    this.originaldataSet = new Instances(dataSet);
    if (this.isGapSimulated) {
        setTitle(getTitle() + " [SIMULATED GAP]");
        /*final JXLabel fictiveGapLabel=new JXLabel("                                                                        FICTIVE GAP");
        fictiveGapLabel.setForeground(Color.RED);
        fictiveGapLabel.setFont(new Font(fictiveGapLabel.getFont().getName(), Font.PLAIN,fictiveGapLabel.getFont().getSize()*2));
        final JXPanel fictiveGapPanel=new JXPanel();
        fictiveGapPanel.setLayout(new BorderLayout());
        fictiveGapPanel.add(fictiveGapLabel,BorderLayout.CENTER);
        getContentPane().add(fictiveGapPanel,BorderLayout.NORTH);*/
        this.attrNames.remove(attr.name());
        this.originalDataBeforeGapSimulation = dataSet.attributeToDoubleArray(attr.index());
        for (int i = position; i < position + gapsize; i++)
            dataSet.instance(i).setMissing(attr);
    }

    final Object[] attrNamesObj = this.attrNames.toArray();

    this.centerPanel = new JXPanel();
    this.centerPanel.setLayout(new BorderLayout());
    getContentPane().add(this.centerPanel, BorderLayout.CENTER);

    //final JXPanel algoPanel=new JXPanel();
    //getContentPane().add(algoPanel,BorderLayout.NORTH);
    final JXPanel filterPanel = new JXPanel();
    //filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.Y_AXIS));      
    filterPanel.setLayout(new GridBagLayout());
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(10, 10, 10, 10);
    getContentPane().add(filterPanel, BorderLayout.WEST);

    final JXComboBox algoCombo = new JXComboBox(Algo.values());
    algoCombo.setBorder(new TitledBorder("Algorithm"));
    filterPanel.add(algoCombo, gbc);
    gbc.gridy++;

    final JXLabel infoLabel = new JXLabel("Usable = with no missing values on the period");
    //infoLabel.setBorder(new TitledBorder(""));
    filterPanel.add(infoLabel, gbc);
    gbc.gridy++;

    final JList<Object> timeSeriesList = new JList<Object>(attrNamesObj);
    timeSeriesList.setBorder(new TitledBorder("Usable time series"));
    timeSeriesList.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    final JScrollPane jcpMap = new JScrollPane(timeSeriesList);
    jcpMap.setPreferredSize(new Dimension(225, 150));
    jcpMap.setMinimumSize(new Dimension(225, 150));
    filterPanel.add(jcpMap, gbc);
    gbc.gridy++;

    final JXPanel mapPanel = new JXPanel();
    mapPanel.setBorder(new TitledBorder(""));
    mapPanel.setLayout(new BorderLayout());
    mapPanel.add(gcp.getMapPanel(Arrays.asList(attr.name()), this.attrNames, true), BorderLayout.CENTER);
    filterPanel.add(mapPanel, gbc);
    gbc.gridy++;

    final JXLabel mssLabel = new JXLabel(
            "<html>Most similar usable serie: <i>[... computation ...]</i></html>");
    mssLabel.setBorder(new TitledBorder(""));
    filterPanel.add(mssLabel, gbc);
    gbc.gridy++;

    final JXLabel nsLabel = new JXLabel("<html>Nearest usable serie: <i>[... computation ...]</i></html>");
    nsLabel.setBorder(new TitledBorder(""));
    filterPanel.add(nsLabel, gbc);
    gbc.gridy++;

    final JXLabel ussLabel = new JXLabel("<html>Upstream serie: <i>[... computation ...]</i></html>");
    ussLabel.setBorder(new TitledBorder(""));
    filterPanel.add(ussLabel, gbc);
    gbc.gridy++;

    final JXLabel dssLabel = new JXLabel("<html>Downstream serie: <i>[... computation ...]</i></html>");
    dssLabel.setBorder(new TitledBorder(""));
    filterPanel.add(dssLabel, gbc);
    gbc.gridy++;

    final JCheckBox hideOtherSeriesCB = new JCheckBox("Hide the others series");
    hideOtherSeriesCB.setSelected(DEFAULT_HIDE_OTHER_SERIES_OPTION);
    filterPanel.add(hideOtherSeriesCB, gbc);
    gbc.gridy++;

    final JCheckBox showErrorCB = new JCheckBox("Show error on plot");
    filterPanel.add(showErrorCB, gbc);
    gbc.gridy++;

    final JCheckBox zoomCB = new JCheckBox("Auto-adjusted size");
    zoomCB.setSelected(DEFAULT_ZOOM_OPTION);
    filterPanel.add(zoomCB, gbc);
    gbc.gridy++;

    final JCheckBox multAxisCB = new JCheckBox("Multiple axis");
    filterPanel.add(multAxisCB, gbc);
    gbc.gridy++;

    final JCheckBox showEnvelopeCB = new JCheckBox("Show envelope (all algorithms, SLOW)");
    filterPanel.add(showEnvelopeCB, gbc);
    gbc.gridy++;

    final JXButton showModelButton = new JXButton("Show the model");
    filterPanel.add(showModelButton, gbc);
    gbc.gridy++;

    showModelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final JXFrame dialog = new JXFrame();
            dialog.setTitle("Model");
            LogoHelper.setLogo(dialog);
            dialog.getContentPane().removeAll();
            dialog.getContentPane().setLayout(new BorderLayout());
            final JTextPane modelTxtPane = new JTextPane();
            modelTxtPane.setText(gapFiller.getModel());
            modelTxtPane.setBackground(Color.WHITE);
            modelTxtPane.setEditable(false);
            final JScrollPane jsp = new JScrollPane(modelTxtPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jsp.setSize(new Dimension(400 - 20, 400 - 20));
            dialog.getContentPane().add(jsp, BorderLayout.CENTER);
            dialog.setSize(new Dimension(400, 400));
            dialog.setLocationRelativeTo(centerPanel);
            dialog.pack();
            dialog.setVisible(true);
        }
    });

    algoCombo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
                showModelButton.setEnabled(gapFiller.hasExplicitModel());
            } catch (final Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    timeSeriesList.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(final ListSelectionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
                mapPanel.removeAll();
                final List<String> currentlySelected = new ArrayList<String>();
                currentlySelected.add(attr.name());
                for (final Object sel : timeSeriesList.getSelectedValues())
                    currentlySelected.add(sel.toString());
                mapPanel.add(gcp.getMapPanel(currentlySelected, attrNames, true), BorderLayout.CENTER);
            } catch (final Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    hideOtherSeriesCB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
            } catch (final Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    showErrorCB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    zoomCB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    showEnvelopeCB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    multAxisCB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()),
                        timeSeriesList.getSelectedIndices(), hideOtherSeriesCB.isSelected(),
                        showErrorCB.isSelected(), zoomCB.isSelected(), showEnvelopeCB.isSelected(),
                        multAxisCB.isSelected());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    this.inBatchMode = inBatchMode;

    if (!inBatchMode) {
        try {
            refresh(Algo.valueOf(algoCombo.getSelectedItem().toString()), new int[0],
                    DEFAULT_HIDE_OTHER_SERIES_OPTION, false, DEFAULT_ZOOM_OPTION, false, false);
            showModelButton.setEnabled(gapFiller.hasExplicitModel());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    if (!inBatchMode) {
        /* automatically select computed series */
        new AbstractSimpleAsync<Void>(true) {
            @Override
            public Void execute() throws Exception {
                mostSimilar = WekaTimeSeriesSimilarityUtil.findMostSimilarTimeSerie(testds, attr, attrNames,
                        false);
                mssLabel.setText("<html>Most similar usable serie: <b>" + mostSimilar + "</b></html>");

                nearest = gcp.findNearestStation(attr.name(), attrNames);
                nsLabel.setText("<html>Nearest usable serie: <b>" + nearest + "</b></html>");

                upstream = gcp.findUpstreamStation(attr.name(), attrNames);
                if (upstream != null) {
                    ussLabel.setText("<html>Upstream usable serie: <b>" + upstream + "</b></html>");
                } else {
                    ussLabel.setText("<html>Upstream usable serie: <b>N/A</b></html>");
                }

                downstream = gcp.findDownstreamStation(attr.name(), attrNames);
                if (downstream != null) {
                    dssLabel.setText("<html>Downstream usable serie: <b>" + downstream + "</b></html>");
                } else {
                    dssLabel.setText("<html>Downstream usable serie: <b>N/A</b></html>");
                }

                timeSeriesList.setSelectedIndices(
                        new int[] { attrNames.indexOf(mostSimilar), attrNames.indexOf(nearest),
                                attrNames.indexOf(upstream), attrNames.indexOf(downstream) });

                return null;
            }

            @Override
            public void onSuccess(final Void result) {
            }

            @Override
            public void onFailure(final Throwable caught) {
                caught.printStackTrace();
            }
        }.start();
    } else {
        try {
            mostSimilar = WekaTimeSeriesSimilarityUtil.findMostSimilarTimeSerie(testds, attr, attrNames, false);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        nearest = gcp.findNearestStation(attr.name(), attrNames);
        upstream = gcp.findUpstreamStation(attr.name(), attrNames);
        downstream = gcp.findDownstreamStation(attr.name(), attrNames);
    }
}

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 a 2 s.  c o  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.GapFillingFrame.java

License:Open Source License

private Instances buildCorrectedDataset(final Instances diff) {
    //System.out.println("Build a corrected dataset ...");

    final Instances correctedDataSet = new Instances(dataSet);
    final int corrNumInstances = correctedDataSet.numInstances();

    final int diffNumInstances = diff.numInstances();
    final int diffNumAttributes = diff.numAttributes();

    final int idxInDiff = 0;

    for (int k = 0; k < diffNumInstances; k++) {
        final Instance diffInstanceK = diff.instance(k);

        if (diffInstanceK.isMissing(idxInDiff))
            continue;

        final long timestamp = (long) diffInstanceK.value(diffNumAttributes - 1);

        for (int h = 0; h < corrNumInstances; h++) {
            if ((long) correctedDataSet.instance(h).value(dateIdx) == timestamp) {
                correctedDataSet.instance(h).setValue(attr, diffInstanceK.value(idxInDiff));
                break;
            }/*ww  w .ja  v  a 2  s  .  c  om*/
        }
    }

    //System.out.println("... corrected dataset built!");

    return correctedDataSet;
}

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

License:Open Source License

private void showEnvelope(final int[] arr, final List<XYAnnotation> aaa) throws Exception {
    final Color cc = ColorHelper.getColorForAString(attr.name());
    final Color newcc = new Color(cc.getRed(), cc.getGreen(), cc.getBlue(), cc.getAlpha() / 4).brighter();

    final Map<Double, Double> minMap = new HashMap<Double, Double>();
    final Map<Double, Double> maxMap = new HashMap<Double, Double>();

    for (final Algo aall : Algo.values()) {
        Instances filteredDs2 = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet, arr);

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

        final GapFiller gp = GapFillerFactory.getGapFiller(aall);

        final Instances completedds2 = gp.fillGaps(filteredDs2);
        final Instances diff2 = WekaTimeSeriesUtil.buildDiff(filteredDs2, completedds2);

        //final double mae2=this.gapFiller.evaluateMeanAbsoluteError(filteredDs2,valuesBeforeAndAfterForMAE);

        final Instances decomposition2 = WekaTimeSeriesUtil.buildMergedDataSet(filteredDs2, diff2);
        final Attribute diffAttribute2 = decomposition2.attribute(attr.name() + "_diff");

        final Attribute timestampDiffAttribute2 = decomposition2
                .attribute(WekaDataStatsUtil.getFirstDateAttributeIdx(decomposition2));

        for (int i = 1; i < decomposition2.numInstances() - 1; i++) {
            if (!decomposition2.instance(i).isMissing(diffAttribute2)/*&&i%10==0*/) {
                final double d = decomposition2.instance(i).value(diffAttribute2);
                final double timestamp = decomposition2.instance(i).value(timestampDiffAttribute2);

                aaa.add(new XYDrawableAnnotation(timestamp, d, 1, 1, new AnnotationDrawer(newcc)));

                if (!minMap.containsKey(timestamp) || minMap.get(timestamp) > d)
                    minMap.put(timestamp, d);
                if (!maxMap.containsKey(timestamp) || maxMap.get(timestamp) < d)
                    maxMap.put(timestamp, d);
            }/*  www .j  av a  2  s.co m*/
        }
    }

    for (final Map.Entry<Double, Double> entry : minMap.entrySet()) {
        final Double timestamp = entry.getKey();
        final Double min = minMap.get(timestamp);
        final Double max = maxMap.get(timestamp);
        if (min > max)
            throw new IllegalStateException("min>max -> " + min + ">" + max);
        else if (max > min) {
            final double step = (max - min) / 20d;
            for (double dd = min; dd <= max; dd += step) {
                aaa.add(new XYDrawableAnnotation(timestamp, dd, 1, 1, new AnnotationDrawer(newcc)));
            }
        }
    }

    System.out.println("done");
}

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

License:Open Source License

private void showError(final double mae, final Instances decomposition, final Attribute diffAttribute,
        final List<XYAnnotation> aaa) {
    //System.out.println("*************** SHOW ERROR **************************");
    final Attribute timestampDiffAttribute = decomposition
            .attribute(WekaDataStatsUtil.getFirstDateAttributeIdx(decomposition));
    final Color cc = ColorHelper.getColorForAString(diffAttribute.name());
    final Color newcc = new Color(cc.getRed(), cc.getGreen(), cc.getBlue(), cc.getAlpha() / 4).brighter();
    for (int i = 1; i < decomposition.numInstances() - 1; i++) {
        //if (i%10!=1) continue;
        if (!decomposition.instance(i).isMissing(diffAttribute)/*&&i%10==0*/) {
            final double d = decomposition.instance(i).value(diffAttribute);
            final double timestamp = decomposition.instance(i).value(timestampDiffAttribute);

            aaa.add(new XYDrawableAnnotation(timestamp, d + mae, 0.5, 0.5, new AnnotationDrawer(newcc)));
            aaa.add(new XYDrawableAnnotation(timestamp, d - mae, 0.5, 0.5, new AnnotationDrawer(newcc)));
            for (double dd = d - mae; dd <= d + mae; dd += mae / 20) {
                aaa.add(new XYDrawableAnnotation(timestamp, dd, 1, 1, new AnnotationDrawer(newcc)));
            }/* www .j a  v  a2s . c  o  m*/
            //aaa.add(new XYDrawableAnnotation(timestamp,d,1,1,new AnnotationDrawer(cc)));
        }
    }
    //System.out.println("*****************************************************");
}

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

License:Open Source License

/**
 * {@inheritDoc}/*from  w w w  .  ja v a2 s .c o  m*/
 */
@Override
public void update0(final Instances dataSet) throws Exception {
    if (this.dateAttributeField != null) {
        this.jxp.remove(this.dateAttributeField);
        this.dateAttributeField = null;
        this.jxp.updateUI();
    }

    final java.util.List<String> dateAttributeNames = WekaDataStatsUtil.getDateAttributeNames(dataSet);
    final boolean hasDateAttributes = (!dateAttributeNames.isEmpty())
    /*&&(WekaDataStatsUtil.getNumericAttributesIndexes(dataSet).size()>0)*/;

    if (hasDateAttributes) {
        this.dateAttributeField = new JXComboBox(dateAttributeNames.toArray());
        this.dateAttributeField.setBorder(new TitledBorder("Date attribute"));
        this.jxp.add(this.dateAttributeField, BorderLayout.SOUTH);
        this.dateAttributeField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fillTabs(dataSet);
            }
        });

        new AbstractSimpleAsync<Void>(true) {
            @Override
            public Void execute() throws Exception {
                fillTabs(dataSet);
                return null;
            }

            @Override
            public void onSuccess(Void result) {
            }

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        }.start();

        this.actionsForFictiveGapsPanel.removeAll();
        //final JComboBox seriesWithoutGapCB=new JComboBox(WekaTimeSeriesUtil.getNamesOfAttributesWithoutGap(dataSet).toArray());
        final JComboBox seriesWithoutGapCB = new JComboBox(
                WekaDataStatsUtil.getAttributeNames(dataSet).toArray());
        seriesWithoutGapCB.setBorder(new TitledBorder("Fictive gap in"));
        this.actionsForFictiveGapsPanel.add(seriesWithoutGapCB);
        final JComboBox sizeGapCB = new JComboBox(new Object[] { 10, 50, 100, 200, 400, 500 });
        sizeGapCB.setBorder(new TitledBorder("Size of the fictive gap"));
        this.actionsForFictiveGapsPanel.add(sizeGapCB);
        final Object[] partChoice = new Object[PARTS_COUNT];
        for (int iii = 0; iii < PARTS_COUNT; iii++) {
            partChoice[iii] = iii + "/" + PARTS_COUNT;
        }

        final JComboBox positionGapCB = new JComboBox(partChoice);
        positionGapCB.setBorder(new TitledBorder("Position of the fictive gap"));
        this.actionsForFictiveGapsPanel.add(positionGapCB);

        this.fictiveGapButton = new JXButton("Create a fictive gap");
        this.actionsForFictiveGapsPanel.add(this.fictiveGapButton);

        this.fictiveGapButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                /* create a fake gap */
                final Attribute attr = dataSet.attribute(seriesWithoutGapCB.getSelectedItem().toString());
                final int dateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(dataSet);
                final int position = ((positionGapCB.getSelectedIndex() + 1) * dataSet.numInstances())
                        / PARTS_COUNT;
                final int gapsize = Integer.valueOf(sizeGapCB.getSelectedItem().toString());

                /* show it */
                final GapFillingFrame jxf = new GapFillingFrame(getAbstractTabView(), new Instances(dataSet),
                        attr, dateIdx, GapsUtil.getCountOfValuesBeforeAndAfter(gapsize), position, gapsize, gcp,
                        false);
                //jxf.setSize(new Dimension(900,700));
                //jxf.setExtendedState(Frame.MAXIMIZED_BOTH);                        
                jxf.setLocationRelativeTo(jxp);
                jxf.setVisible(true);
                //jxf.setResizable(false);            
            }
        });

        this.showKnowledgeDBButton = new JXButton("Show KDB");
        this.actionsForFictiveGapsPanel.add(this.showKnowledgeDBButton);
        this.showKnowledgeDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {

                try {
                    final DatasetView view = new DatasetView("KnowledgeDB", eventPublisher, commandDispatcher,
                            applicationContext);
                    view.setDataSet(GapFillingKnowledgeDB.getKnowledgeDB()).setAsVisible(true);
                } catch (final Exception ee) {
                    ee.printStackTrace();
                }
            }
        });

        this.inspectKnowledgeDBButton = new JXButton("Inspect KDB");
        this.actionsForFictiveGapsPanel.add(this.inspectKnowledgeDBButton);
        this.inspectKnowledgeDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                try {
                    final int dateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(dataSet);
                    new GapFillingKnowledgeDBExplorerFrame(dataSet, dateIdx, gcp);
                } catch (final Exception ee) {
                    ee.printStackTrace();
                }
            }
        });

        this.showKnowledgeDBWithTrueCasesButton = new JXButton("Show KDB with true cases");
        this.actionsForFictiveGapsPanel.add(this.showKnowledgeDBWithTrueCasesButton);
        this.showKnowledgeDBWithTrueCasesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                try {
                    final StringBuilder sb = new StringBuilder("@relation blabla\n");
                    sb.append("@attribute serieName string\n");
                    sb.append("@attribute serieX numeric\n");
                    sb.append("@attribute serieY numeric\n");
                    sb.append("@attribute year numeric\n");
                    sb.append("@attribute season {Winter,Spring,Summer,Autumn}\n");
                    sb.append("@attribute gapSize numeric\n");
                    sb.append("@attribute gapPosition numeric\n");
                    sb.append("@attribute isDuringRising {true,false}\n");
                    sb.append("@attribute flow string\n");
                    sb.append("@attribute hasDownstream {false,true}\n");
                    sb.append("@attribute hasUpstream {false,true}\n");
                    sb.append("@attribute isReal {false,true}\n");
                    sb.append(
                            "@attribute algo {Interpolation,EM,REG,REPTREE,M5P,ZeroR,ANN,NEARESTNEIGHBOUR}\n");
                    sb.append("@attribute useDiscretizedTime {false,true}\n");
                    sb.append("@attribute useMostSimilar {false,true}\n");
                    sb.append("@attribute useNearest {true,false}\n");
                    sb.append("@attribute useDownstream {false,true}\n");
                    sb.append("@attribute useUpstream {true,false}\n");
                    sb.append("@attribute mae numeric\n");
                    sb.append("@attribute rmse numeric\n");
                    sb.append("@attribute rsr numeric\n");
                    sb.append("@attribute pbias numeric\n");
                    sb.append("@attribute ns numeric\n");
                    sb.append("@attribute ioa numeric\n");
                    sb.append("@attribute wasTheBestSolution {true,false}\n");

                    sb.append("@data\n");

                    /* true cases */
                    final Calendar cal = Calendar.getInstance();
                    final int dateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(dataSet);
                    final Instances gapsDescriptionsDataset = GapsUtil.buildGapsDescription(gcp, dataSet,
                            dateIdx);
                    final int gddc = gapsDescriptionsDataset.numInstances();
                    for (int i = 0; i < gddc; i++) {
                        final Instance trueCase = gapsDescriptionsDataset.instance(i);
                        sb.append(trueCase.stringValue(0)); // serie
                        sb.append(",");
                        sb.append(gcp.getCoordinates(trueCase.stringValue(0))[0]); // x
                        sb.append(",");
                        sb.append(gcp.getCoordinates(trueCase.stringValue(0))[1]); // y
                        sb.append(",");
                        cal.setTime(FormatterUtil.DATE_FORMAT.parse(trueCase.stringValue(1))); // year
                        sb.append(cal.get(Calendar.YEAR));
                        sb.append(",");
                        sb.append(trueCase.stringValue(2).split("/")[0]); // season
                        sb.append(",");
                        sb.append(trueCase.value(4)); //gapsize
                        sb.append(",");
                        sb.append(trueCase.value(5)); //gap position
                        sb.append(",");
                        sb.append(trueCase.stringValue(10).equals("true")); //rising
                        sb.append(",");
                        sb.append(trueCase.stringValue(11)); // flow
                        sb.append(",");
                        sb.append(!trueCase.stringValue(9).equals("n/a")); //downstream
                        sb.append(",");
                        sb.append(!trueCase.stringValue(8).equals("n/a")); // upstream
                        sb.append(",");
                        sb.append("true");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append(",");
                        sb.append("?");
                        sb.append("\n");
                    }

                    /* the simulated cases from the knowledge DB */
                    final Instances knowledgeDB = GapFillingKnowledgeDB.getKnowledgeDB();
                    final int kni = knowledgeDB.numInstances();
                    for (int i = 0; i < kni; i++) {
                        final Instance simulatedCase = knowledgeDB.instance(i);
                        sb.append(simulatedCase.stringValue(0)); // name
                        sb.append(",");
                        sb.append(simulatedCase.value(1)); //x
                        sb.append(",");
                        sb.append(simulatedCase.value(2)); //y               
                        sb.append(",");
                        sb.append(simulatedCase.value(6)); //year
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(5)); //season
                        sb.append(",");
                        sb.append(simulatedCase.value(3)); // size
                        sb.append(",");
                        sb.append(simulatedCase.value(4)); // position
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(7)); // rising
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(8)); //flow
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(9)); //downstream
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(10)); // upstream
                        sb.append(",");
                        sb.append("false"); // real
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(11)); //algo
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(12)); // discr time
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(13)); // most similar
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(14)); // nearest
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(15)); //downstream
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(16)); //upstream
                        sb.append(",");
                        sb.append(simulatedCase.value(17)); //mae
                        sb.append(",");
                        sb.append(simulatedCase.value(18)); //rmse
                        sb.append(",");
                        sb.append(simulatedCase.value(19)); //rsr
                        sb.append(",");
                        sb.append(simulatedCase.value(20)); //pbias
                        sb.append(",");
                        sb.append(simulatedCase.value(21)); //ns
                        sb.append(",");
                        sb.append(simulatedCase.value(22)); //ioa
                        sb.append(",");
                        sb.append(simulatedCase.stringValue(23)); // best                                    
                        sb.append("\n");
                    }

                    //System.out.println(sb.toString());

                    final Instances newds = WekaDataAccessUtil.loadInstancesFromARFFString(sb.toString(), false,
                            false);
                    final DatasetView view = new DatasetView("KnowledgeDB with true cases", eventPublisher,
                            commandDispatcher, applicationContext);
                    view.setDataSet(newds).setAsVisible(true);
                } catch (final Exception ee) {
                    ee.printStackTrace();
                }
            }
        });

        this.rebuildKnowledgeDBButton = new JXButton("Rebuild KDB");
        this.actionsForFictiveGapsPanel.add(this.rebuildKnowledgeDBButton);
        this.rebuildKnowledgeDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                rebuildKnowledgeDB(dataSet);
            }
        });
    } else {
        throw new Exception("No date attributes in the dataset.");
    }
}

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

License:Open Source License

public static ChartPanel buildGapChartPanel(final Instances dataSet, final int dateIdx, final Attribute attr,
        final int gapsize, final int position) throws Exception {
    Instances filteredDs = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet,
            new int[] { attr.index(), dateIdx });
    filteredDs = WekaDataProcessingUtil.buildFilteredDataSet(filteredDs, 0, filteredDs.numAttributes() - 1,
            Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize),
            Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize,
                    filteredDs.numInstances() - 1));

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

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

    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);

    addExportPopupMenu(filteredDs, cp);/*w w w .ja v a2 s.c  o  m*/

    return cp;
}