List of usage examples for weka.core Instances attribute
publicAttribute attribute(String name)
From source file:lu.lippmann.cdb.ext.hydviga.cbr.GapFillingKnowledgeDBAnalyzer.java
License:Open Source License
private static int getCountOfFictiveGaps(final Instances newkdb) { final Set<String> set = new HashSet<String>(); for (int i = 0; i < newkdb.numInstances(); i++) { final String key = newkdb.instance(i).stringValue(newkdb.attribute("serieName").index()) + "-" + newkdb.instance(i).value(newkdb.attribute("gapSize").index()) + "-" + newkdb.instance(i).value(newkdb.attribute("gapPosition").index()); set.add(key);/*from ww w . j a v a2 s . co m*/ } return set.size(); }
From source file:lu.lippmann.cdb.ext.hydviga.cbr.GapFillingKnowledgeDBAnalyzer.java
License:Open Source License
/** * Main method.// w w w .j ava2 s . co m * @param args command line arguments */ public static void main(final String[] args) { try { HydroRunner.init(false); Instances newkdb = new Instances(GapFillingKnowledgeDB.getKnowledgeDB()); System.out.println("Considered fictive gaps -> " + getCountOfFictiveGaps(newkdb)); System.out.println(newkdb.toSummaryString()); newkdb = WekaDataProcessingUtil.filterDataSetOnNominalValue(newkdb, newkdb.attribute("useDownstream").index(), "false"); newkdb = WekaDataProcessingUtil.filterDataSetOnNominalValue(newkdb, newkdb.attribute("useUpstream").index(), "false"); //newkdb=WekaDataProcessingUtil.filterDataSetOnNominalValue(newkdb,newkdb.attribute("useNearest").index(),"false"); //newkdb=WekaDataProcessingUtil.filterDataSetOnNominalValue(newkdb,newkdb.attribute("useMostSimilar").index(),"false"); //System.out.println(newkdb.toSummaryString()); Instances withGoodNashSutcliffe = new Instances(newkdb, 0); for (int i = 0; i < newkdb.numInstances(); i++) { if (newkdb.instance(i).value(newkdb.attribute("NashSutcliffe").index()) > 0.5d) { withGoodNashSutcliffe.add(new DenseInstance(1d, newkdb.instance(i).toDoubleArray())); } } System.out.println(withGoodNashSutcliffe.numInstances() + " / " + newkdb.numInstances()); final double perc = (double) getCountOfFictiveGaps(withGoodNashSutcliffe) / getCountOfFictiveGaps(newkdb); System.out.println("Fictive gaps that are infilled with a good Nash-Sutcliffe -> " + getCountOfFictiveGaps(withGoodNashSutcliffe) + " (" + perc + "%)"); WekaDataAccessUtil.saveInstancesIntoARFFFile(withGoodNashSutcliffe, new File("./withGoodNashSutcliffe.arff")); } catch (final Exception e) { e.printStackTrace(); } }
From source file:lu.lippmann.cdb.ext.hydviga.data.StationsDataProvider.java
License:Open Source License
private ChartPanel buildMapPanel(final Instances dataSet, final int xidx, final int yidx, final boolean withLegend) { final XYSeriesCollection data = new XYSeriesCollection(); final Map<Integer, java.util.List<Instance>> filteredInstances = new HashMap<Integer, java.util.List<Instance>>(); final int classIndex = dataSet.classIndex(); if (classIndex < 0) { final XYSeries series = new XYSeries("Serie", false); for (int i = 0; i < dataSet.numInstances(); i++) { series.add(dataSet.instance(i).value(xidx), dataSet.instance(i).value(yidx)); }/*from ww w. ja v a2 s .c o m*/ data.addSeries(series); } else { final Set<String> pvs = new TreeSet<String>( WekaDataStatsUtil.getPresentValuesForNominalAttribute(dataSet, classIndex)); int p = 0; for (final String pv : pvs) { final XYSeries series = new XYSeries(pv, false); for (int i = 0; i < dataSet.numInstances(); i++) { if (dataSet.instance(i).stringValue(classIndex).equals(pv)) { if (!filteredInstances.containsKey(p)) { filteredInstances.put(p, new ArrayList<Instance>()); } filteredInstances.get(p).add(dataSet.instance(i)); series.add(dataSet.instance(i).value(xidx), dataSet.instance(i).value(yidx)); } } data.addSeries(series); p++; } } final JFreeChart chart = ChartFactory.createScatterPlot(null, // chart title dataSet.attribute(xidx).name(), // x axis label dataSet.attribute(yidx).name(), // y axis label data, // data PlotOrientation.VERTICAL, withLegend, // include legend true, // tooltips false // urls ); final XYPlot xyPlot = (XYPlot) chart.getPlot(); xyPlot.setBackgroundImage(shapeImage); final XYItemRenderer renderer = xyPlot.getRenderer(); final XYToolTipGenerator gen = new XYToolTipGenerator() { @Override public String generateToolTip(XYDataset dataset, int series, int item) { if (classIndex < 0) { return InstanceFormatter.htmlFormat(dataSet.instance(item), true); } else { return InstanceFormatter.htmlFormat(filteredInstances.get(series).get(item), true); } } }; xyPlot.getRangeAxis().setVisible(false); xyPlot.getDomainAxis().setVisible(false); xyPlot.getRangeAxis().setLowerBound(60000); xyPlot.getRangeAxis().setUpperBound(135000); xyPlot.getDomainAxis().setLowerBound(45000); xyPlot.getDomainAxis().setUpperBound(110000); xyPlot.setDomainGridlinesVisible(false); xyPlot.setRangeGridlinesVisible(false); xyPlot.setBackgroundPaint(Color.white); int nbSeries; if (classIndex < 0) { nbSeries = 1; } else { nbSeries = filteredInstances.keySet().size(); } for (int i = 0; i < nbSeries; i++) { renderer.setSeriesToolTipGenerator(i, gen); } final XYItemLabelGenerator lg = new XYItemLabelGenerator() { @Override public String generateLabel(final XYDataset ds, final int series, final int item) { final Instance iii = filteredInstances.get(series).get(item); if (iii.stringValue(3).equals(SELECTED_STATUS)) { final String label = iii.stringValue(0); return label.substring(0, label.length() - 4); } else return null; } }; xyPlot.getRenderer().setBaseItemLabelGenerator(lg); xyPlot.getRenderer().setBaseItemLabelsVisible(true); xyPlot.getRenderer().setBaseItemLabelFont(new Font("Tahoma", Font.PLAIN, 12)); xyPlot.getRenderer().setSeriesPaint(1, Color.BLUE); xyPlot.getRenderer().setSeriesPaint(0, new Color(210, 210, 210)); xyPlot.getRenderer().setSeriesPaint(2, Color.DARK_GRAY); //System.out.println("shape -> "+xyPlot.getRenderer().getSeriesStroke(0)); final ChartPanel cp = new ChartPanel(chart); cp.setDomainZoomable(false); cp.setRangeZoomable(false); return cp; }
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 www .ja va 2 s. c o m*/ /* 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!"); }// w w w . ja v a2s . c om 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.gaps.GapFillerRegressions.java
License:Open Source License
/** * {@inheritDoc}/* www. j a va 2 s . c o 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.HydroRunner.java
License:Open Source License
private static void showDistances(final Instances dataSet, final int tsidx) { for (int maxValues : new int[] { 1000, 5000, 10000 }) { System.out.println(""); System.out.println("DTW from " + dataSet.attribute(tsidx).name() + " (first " + maxValues + " values)"); final double[] startArray1 = ArraysUtil.firstValues(dataSet.attributeToDoubleArray(tsidx), maxValues); for (int i = 0; i < dataSet.numAttributes(); i++) { if (dataSet.attribute(i).isDate()) continue; if (i == tsidx) continue; final double[] startArray2 = ArraysUtil.lastValues(dataSet.attributeToDoubleArray(i), maxValues); final double distance = new DynamicTimeWarping(startArray1, startArray2).getDistance(); System.out.println("\t to " + dataSet.attribute(i).name() + ": " + distance); }/*from www.ja v a 2 s. c o m*/ System.out.println(""); System.out.println(""); System.out.println("DTW from " + dataSet.attribute(tsidx).name() + " (last " + maxValues + " values)"); final double[] endArray1 = ArraysUtil.lastValues(dataSet.attributeToDoubleArray(tsidx), maxValues); for (int i = 0; i < dataSet.numAttributes(); i++) { if (dataSet.attribute(i).isDate()) continue; if (i == tsidx) continue; final double[] endArray2 = ArraysUtil.lastValues(dataSet.attributeToDoubleArray(i), maxValues); final double distance = new DynamicTimeWarping(endArray1, endArray2).getDistance(); System.out.println("\t to " + dataSet.attribute(i).name() + ": " + distance); } System.out.println(""); } }
From source file:lu.lippmann.cdb.ext.hydviga.HydroRunner.java
License:Open Source License
private static Instances buildDatasetH_(final String name) throws Exception { final String usedName = name.replaceAll("-", "_"); final String SEP = "-"; final String dateFormat = "dd" + SEP + "MM" + SEP + "yyyy" + SEP + "HH:mm:ss"; final StringBuilder sb = new StringBuilder(); sb.append("@relation " + PROPERTIES.getProperty("prefix") + usedName + " \n"); sb.append("@attribute ").append(usedName).append("_d date ").append(dateFormat).append(" \n"); sb.append("@attribute ").append(usedName).append("_val numeric \n"); sb.append("@data \n"); final Instances emptyds = WekaDataAccessUtil.loadInstancesFromARFFString(sb.toString(), false); final String fn = PROPERTIES.getProperty("initialDataPath") + PROPERTIES.getProperty("prefix") + name + ".txt"; final File file = new File(fn); final Scanner input = new Scanner(file); while (input.hasNext()) { final String nextLine = input.nextLine(); final String[] array = nextLine.split("\t"); if (array.length == 5) { final String DDMMYYYY = array[0]; /* first 'column' */ String HH_MM_SS = array[2]; if (HH_MM_SS.startsWith("24")) HH_MM_SS = "00" + HH_MM_SS.substring(2); final String formattedDate = DDMMYYYY.replaceAll("/", SEP) + SEP + HH_MM_SS; try { System.out.println("'" + HH_MM_SS + "'"); if (!(HH_MM_SS.endsWith("00") || HH_MM_SS.endsWith("15") || HH_MM_SS.endsWith("30") || HH_MM_SS.endsWith("45"))) { throw new Exception("hour not managed -> " + HH_MM_SS); }//w ww . ja v a 2 s . co m emptyds.attribute(0).parseDate(formattedDate); String val = array[1]; if (val.contains("---")) val = "?"; sb.append(formattedDate).append(",").append(val).append("\n"); } catch (ParseException pe) { System.out.println(name + " -> " + pe.getMessage()); //continue; } /* second 'column' */ String HH_MM_SS2 = array[4]; if (HH_MM_SS2.startsWith("24")) HH_MM_SS2 = "00" + HH_MM_SS2.substring(2); final String formattedDate2 = DDMMYYYY.replaceAll("/", SEP) + SEP + HH_MM_SS2; try { if (!(HH_MM_SS2.endsWith("00") || HH_MM_SS2.endsWith("15") || HH_MM_SS2.endsWith("30") || HH_MM_SS2.endsWith("45"))) { throw new Exception("hour not managed -> " + HH_MM_SS2); } emptyds.attribute(0).parseDate(formattedDate2); String val2 = array[3]; if (val2.contains("---")) val2 = "?"; sb.append(formattedDate2).append(",").append(val2).append("\n"); } catch (ParseException pe) { System.out.println(name + " -> " + pe.getMessage()); //continue; } } else { final String DDMMYYYY = array[0]; String HH_MM_SS = array[1]; if (HH_MM_SS.startsWith("24")) HH_MM_SS = "00" + HH_MM_SS.substring(2); final String formattedDate = DDMMYYYY.replaceAll("/", SEP) + SEP + HH_MM_SS; try { emptyds.attribute(0).parseDate(formattedDate); } catch (ParseException pe) { System.out.println(name + " -> " + pe.getMessage()); continue; } String val = array[2]; if (val.contains("---")) val = "?"; sb.append(formattedDate).append(",").append(val).append("\n"); } } input.close(); final Instances ds = WekaDataAccessUtil.loadInstancesFromARFFString(sb.toString(), false); return ds; }
From source file:lu.lippmann.cdb.ext.hydviga.ui.GapFillingFrame.java
License:Open Source License
/** * Constructor./* w w w . java2 s. c om*/ */ 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 va2 s . c om*/ 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)); }