MOA 12.03
Real Time Analytics for Data Streams
TaskTextViewerPanel.java
Go to the documentation of this file.
00001 /*
00002  *    TaskTextViewerPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *    @author Jansen (moa@cs.rwth-aachen.de)
00006  *
00007  *    This program is free software; you can redistribute it and/or modify
00008  *    it under the terms of the GNU General Public License as published by
00009  *    the Free Software Foundation; either version 3 of the License, or
00010  *    (at your option) any later version.
00011  *
00012  *    This program is distributed in the hope that it will be useful,
00013  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *    GNU General Public License for more details.
00016  *
00017  *    You should have received a copy of the GNU General Public License
00018  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00019  *    
00020  */
00021 package moa.gui;
00022 
00023 import java.awt.BorderLayout;
00024 import java.awt.Dimension;
00025 import java.awt.Font;
00026 import java.awt.GridLayout;
00027 import java.awt.Point;
00028 import java.awt.event.ActionEvent;
00029 import java.awt.event.ActionListener;
00030 import java.io.BufferedWriter;
00031 import java.io.File;
00032 import java.io.FileWriter;
00033 import java.io.IOException;
00034 import java.io.PrintWriter;
00035 import java.util.Scanner;
00036 
00037 import javax.swing.JButton;
00038 import javax.swing.JFileChooser;
00039 import javax.swing.JPanel;
00040 import javax.swing.JScrollPane;
00041 import javax.swing.JTextArea;
00042 import moa.evaluation.Accuracy;
00043 import moa.evaluation.MeasureCollection;
00044 
00051 public class TaskTextViewerPanel extends JPanel implements ActionListener {
00052 
00053     private static final long serialVersionUID = 1L;
00054 
00055     public static String exportFileExtension = "txt";
00056 
00057     protected JTextArea textArea;
00058 
00059     protected JScrollPane scrollPane;
00060 
00061     protected JButton exportButton;
00062 
00063     private javax.swing.JPanel topWrapper;
00064 
00065     private javax.swing.JSplitPane jSplitPane1;
00066 
00067     public TaskTextViewerPanel() {
00068         java.awt.GridBagConstraints gridBagConstraints;
00069 
00070         jSplitPane1 = new javax.swing.JSplitPane();
00071         topWrapper = new javax.swing.JPanel();
00072 
00073         this.textArea = new JTextArea();
00074         this.textArea.setEditable(false);
00075         this.textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
00076         this.exportButton = new JButton("Export as .txt file...");
00077         this.exportButton.setEnabled(false);
00078         JPanel buttonPanel = new JPanel();
00079         buttonPanel.setLayout(new GridLayout(1, 2));
00080         buttonPanel.add(this.exportButton);
00081         topWrapper.setLayout(new BorderLayout());
00082         this.scrollPane = new JScrollPane(this.textArea);
00083         topWrapper.add(this.scrollPane, BorderLayout.CENTER);
00084         topWrapper.add(buttonPanel, BorderLayout.SOUTH);
00085         this.exportButton.addActionListener(new ActionListener() {
00086 
00087             public void actionPerformed(ActionEvent e) {
00088                 JFileChooser fileChooser = new JFileChooser();
00089                 fileChooser.setAcceptAllFileFilterUsed(true);
00090                 fileChooser.addChoosableFileFilter(new FileExtensionFilter(
00091                         exportFileExtension));
00092                 if (fileChooser.showSaveDialog(TaskTextViewerPanel.this) == JFileChooser.APPROVE_OPTION) {
00093                     File chosenFile = fileChooser.getSelectedFile();
00094                     String fileName = chosenFile.getPath();
00095                     if (!chosenFile.exists()
00096                             && !fileName.endsWith(exportFileExtension)) {
00097                         fileName = fileName + "." + exportFileExtension;
00098                     }
00099                     try {
00100                         PrintWriter out = new PrintWriter(new BufferedWriter(
00101                                 new FileWriter(fileName)));
00102                         out.write(TaskTextViewerPanel.this.textArea.getText());
00103                         out.close();
00104                     } catch (IOException ioe) {
00105                         GUIUtils.showExceptionDialog(
00106                                 TaskTextViewerPanel.this.exportButton,
00107                                 "Problem saving file " + fileName, ioe);
00108                     }
00109                 }
00110             }
00111         });
00112         //topWrapper.add(this.scrollPane);
00113         //topWrapper.add(buttonPanel);
00114 
00115         panelEvalOutput = new javax.swing.JPanel();
00116         clusteringVisualEvalPanel1 = new moa.gui.clustertab.ClusteringVisualEvalPanel();
00117         graphPanel = new javax.swing.JPanel();
00118         graphPanelControlTop = new javax.swing.JPanel();
00119         buttonZoomInY = new javax.swing.JButton();
00120         buttonZoomOutY = new javax.swing.JButton();
00121         labelEvents = new javax.swing.JLabel();
00122         graphScrollPanel = new javax.swing.JScrollPane();
00123         graphCanvas = new moa.gui.visualization.GraphCanvas();
00124         graphPanelControlBottom = new javax.swing.JPanel();
00125         buttonZoomInX = new javax.swing.JButton();
00126         buttonZoomOutX = new javax.swing.JButton();
00127 
00128         setLayout(new java.awt.GridBagLayout());
00129 
00130         jSplitPane1.setDividerLocation(200);
00131         jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
00132 
00133         //topWrapper.setPreferredSize(new java.awt.Dimension(688, 500));
00134         //topWrapper.setLayout(new java.awt.GridBagLayout());
00135 
00136         jSplitPane1.setLeftComponent(topWrapper);
00137 
00138         panelEvalOutput.setBorder(javax.swing.BorderFactory.createTitledBorder("Evaluation"));
00139         panelEvalOutput.setLayout(new java.awt.GridBagLayout());
00140 
00141         clusteringVisualEvalPanel1.setMinimumSize(new java.awt.Dimension(280, 118));
00142         clusteringVisualEvalPanel1.setPreferredSize(new java.awt.Dimension(290, 115));
00143         gridBagConstraints = new java.awt.GridBagConstraints();
00144         gridBagConstraints.gridx = 0;
00145         gridBagConstraints.gridy = 0;
00146         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00147         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00148         gridBagConstraints.weighty = 1.0;
00149         panelEvalOutput.add(clusteringVisualEvalPanel1, gridBagConstraints);
00150 
00151 
00152 
00153         graphPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Plot"));
00154         graphPanel.setPreferredSize(new java.awt.Dimension(530, 115));
00155         graphPanel.setLayout(new java.awt.GridBagLayout());
00156 
00157         graphPanelControlTop.setLayout(new java.awt.GridBagLayout());
00158 
00159         buttonZoomInY.setText("Zoom in Y");
00160         buttonZoomInY.addActionListener(new java.awt.event.ActionListener() {
00161 
00162             public void actionPerformed(java.awt.event.ActionEvent evt) {
00163                 buttonZoomInYActionPerformed(evt);
00164             }
00165         });
00166         gridBagConstraints = new java.awt.GridBagConstraints();
00167         gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
00168         graphPanelControlTop.add(buttonZoomInY, gridBagConstraints);
00169 
00170         buttonZoomOutY.setText("Zoom out Y");
00171         buttonZoomOutY.addActionListener(new java.awt.event.ActionListener() {
00172 
00173             public void actionPerformed(java.awt.event.ActionEvent evt) {
00174                 buttonZoomOutYActionPerformed(evt);
00175             }
00176         });
00177         gridBagConstraints = new java.awt.GridBagConstraints();
00178         gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
00179         graphPanelControlTop.add(buttonZoomOutY, gridBagConstraints);
00180 
00181         labelEvents.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
00182         gridBagConstraints = new java.awt.GridBagConstraints();
00183         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00184         gridBagConstraints.weightx = 1.0;
00185         gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
00186         graphPanelControlTop.add(labelEvents, gridBagConstraints);
00187 
00188         gridBagConstraints = new java.awt.GridBagConstraints();
00189         gridBagConstraints.gridx = 0;
00190         gridBagConstraints.gridy = 0;
00191         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00192         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00193         gridBagConstraints.weightx = 1.0;
00194         graphPanel.add(graphPanelControlTop, gridBagConstraints);
00195 
00196         graphCanvas.setPreferredSize(new java.awt.Dimension(500, 111));
00197 
00198         javax.swing.GroupLayout graphCanvasLayout = new javax.swing.GroupLayout(graphCanvas);
00199         graphCanvas.setLayout(graphCanvasLayout);
00200         graphCanvasLayout.setHorizontalGroup(
00201                 graphCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 515, Short.MAX_VALUE));
00202         graphCanvasLayout.setVerticalGroup(
00203                 graphCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 128, Short.MAX_VALUE));
00204 
00205         graphScrollPanel.setViewportView(graphCanvas);
00206 
00207         gridBagConstraints = new java.awt.GridBagConstraints();
00208         gridBagConstraints.gridx = 0;
00209         gridBagConstraints.gridy = 1;
00210         gridBagConstraints.gridwidth = 2;
00211         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00212         gridBagConstraints.weightx = 1.0;
00213         gridBagConstraints.weighty = 1.0;
00214         gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
00215         graphPanel.add(graphScrollPanel, gridBagConstraints);
00216 
00217         buttonZoomInX.setText("Zoom in X");
00218         buttonZoomInX.addActionListener(new java.awt.event.ActionListener() {
00219 
00220             public void actionPerformed(java.awt.event.ActionEvent evt) {
00221                 buttonZoomInXActionPerformed(evt);
00222             }
00223         });
00224         graphPanelControlBottom.add(buttonZoomInX);
00225 
00226         buttonZoomOutX.setText("Zoom out X");
00227         buttonZoomOutX.addActionListener(new java.awt.event.ActionListener() {
00228 
00229             public void actionPerformed(java.awt.event.ActionEvent evt) {
00230                 buttonZoomOutXActionPerformed(evt);
00231             }
00232         });
00233         graphPanelControlBottom.add(buttonZoomOutX);
00234 
00235         gridBagConstraints = new java.awt.GridBagConstraints();
00236         gridBagConstraints.gridx = 1;
00237         gridBagConstraints.gridy = 0;
00238         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00239         graphPanel.add(graphPanelControlBottom, gridBagConstraints);
00240 
00241         gridBagConstraints = new java.awt.GridBagConstraints();
00242         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00243         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00244         gridBagConstraints.weightx = 2.0;
00245         gridBagConstraints.weighty = 1.0;
00246         panelEvalOutput.add(graphPanel, gridBagConstraints);
00247 
00248         jSplitPane1.setRightComponent(panelEvalOutput);
00249 
00250         gridBagConstraints = new java.awt.GridBagConstraints();
00251         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00252         gridBagConstraints.weightx = 1.0;
00253         gridBagConstraints.weighty = 1.0;
00254         add(jSplitPane1, gridBagConstraints);
00255 
00256         acc1[0] = new Accuracy();
00257 
00258         acc2[0] = new Accuracy();
00259         clusteringVisualEvalPanel1.setMeasures(acc1, acc2, this);
00260         this.graphCanvas.setGraph(acc1[0], acc2[0], 0, 1000);
00261 
00262 
00263     }
00264 
00265     public void setText(String newText) {
00266         Point p = this.scrollPane.getViewport().getViewPosition();
00267         this.textArea.setText(newText);
00268         this.scrollPane.getViewport().setViewPosition(p);
00269         this.exportButton.setEnabled(newText != null);
00270         setGraph(newText);
00271     }
00272 
00273     protected MeasureCollection[] acc1 = new MeasureCollection[1];
00274 
00275     protected MeasureCollection[] acc2 = new MeasureCollection[1];
00276 
00277     protected String secondLine = "";
00278 
00279     protected double round(double d) {
00280         return Math.abs(Math.rint(d * 100) / 100);
00281     }
00282 
00283     public void setGraph(String preview) {
00284         //Change the graph when there is change in the text
00285          double processFrequency = 1000;
00286         if (preview != null && !preview.equals("")) {
00287             MeasureCollection oldAccuracy = acc1[0];
00288             acc1[0] = new Accuracy();
00289             Scanner scanner = new Scanner(preview);
00290             String firstLine = scanner.nextLine();
00291             boolean isSecondLine = true;
00292            
00293             boolean isPrequential = firstLine.startsWith("learning evaluation instances,evaluation time");
00294             boolean isHoldOut = firstLine.startsWith("evaluation instances,to");
00295             if (isPrequential || isHoldOut) {
00296                 int accuracyColumn = 4;
00297                 int kappaColumn = 5;
00298                 int RamColumn = 2;
00299                 int timeColumn = 1;
00300                 int memoryColumn = 7;
00301                 String[] tokensFirstLine = firstLine.split(",");
00302                 int i = 0;
00303                 for (String s : tokensFirstLine) {
00304 
00305                     if (s.equals("classifications correct (percent)")) {
00306                         accuracyColumn = i;
00307                     } else if (s.equals("Kappa Statistic (percent)")) {
00308                         kappaColumn = i;
00309                     } else if (s.equals("model cost (RAM-Hours)")) {
00310                         RamColumn = i;
00311                     } else if (s.equals("evaluation time (cpu seconds)")
00312                             || s.equals("total train time")) {
00313                         timeColumn = i;
00314                     } else if (s.equals("model serialized size (bytes)")) {
00315                         memoryColumn = i;
00316                     }
00317                     i++;
00318                 }
00319                 while (scanner.hasNextLine()) {
00320                     String line = scanner.nextLine();
00321                     String[] tokens = line.split(",");
00322                     this.acc1[0].addValue(0, round(Double.parseDouble(tokens[accuracyColumn])));
00323                     this.acc1[0].addValue(1, round(Double.parseDouble(tokens[kappaColumn])));
00324                     if (!isHoldOut) {
00325                         this.acc1[0].addValue(2, Math.abs(Double.parseDouble(tokens[RamColumn])));
00326                     }
00327                     this.acc1[0].addValue(3, round(Double.parseDouble(tokens[timeColumn])));
00328                     this.acc1[0].addValue(4, round(Double.parseDouble(tokens[memoryColumn]) / (1024 * 1024)));
00329 
00330                     if (isSecondLine == true) {
00331                         processFrequency = Math.abs(Double.parseDouble(tokens[0]));
00332                         isSecondLine = false;
00333                         if (acc1[0].getValue(0, 0) != oldAccuracy.getValue(0, 0)) { //(!line.equals(secondLine)) {
00334                             //If we are in a new task, compare with the previous
00335                             secondLine = line;
00336                             if (processFrequency == this.graphCanvas.getProcessFrequency()) {
00337                                 acc2[0] = oldAccuracy;
00338                             }
00339                         }
00340                     }
00341                 }
00342             } else {
00343                  this.acc2[0] = new Accuracy();
00344             }
00345             
00346         } else {
00347             this.acc1[0] = new Accuracy();
00348             this.acc2[0] = new Accuracy();
00349         }
00350         this.graphCanvas.setGraph(acc1[0], acc2[0], this.graphCanvas.getMeasureSelected(), (int) processFrequency);
00351         this.graphCanvas.updateCanvas();
00352         this.clusteringVisualEvalPanel1.update();
00353 
00354 
00355     }
00356 
00357     private void scrollPane0MouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_scrollPane0MouseWheelMoved
00358         streamPanel0.setZoom(evt.getX(), evt.getY(), (-1) * evt.getWheelRotation(), scrollPane0);
00359     }//GEN-LAST:event_scrollPane0MouseWheelMoved
00360 
00361     private void buttonZoomInXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomInXActionPerformed
00362         graphCanvas.scaleXResolution(false);
00363     }//GEN-LAST:event_buttonZoomInXActionPerformed
00364 
00365     private void buttonZoomOutYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomOutYActionPerformed
00366         graphCanvas.setSize(new Dimension(graphCanvas.getWidth(), (int) (graphCanvas.getHeight() * 0.8)));
00367         graphCanvas.setPreferredSize(new Dimension(graphCanvas.getWidth(), (int) (graphCanvas.getHeight() * 0.8)));
00368         this.graphCanvas.updateCanvas(true);
00369     }//GEN-LAST:event_buttonZoomOutYActionPerformed
00370 
00371     private void buttonZoomOutXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomOutXActionPerformed
00372         graphCanvas.scaleXResolution(true);
00373     }//GEN-LAST:event_buttonZoomOutXActionPerformed
00374 
00375     private void buttonZoomInYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomInYActionPerformed
00376         graphCanvas.setSize(new Dimension(graphCanvas.getWidth(), (int) (graphCanvas.getHeight() * 1.2)));
00377         graphCanvas.setPreferredSize(new Dimension(graphCanvas.getWidth(), (int) (graphCanvas.getHeight() * 1.2)));
00378         this.graphCanvas.updateCanvas(true);
00379     }//GEN-LAST:event_buttonZoomInYActionPerformed
00380 
00381     private void buttonRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRunActionPerformed
00382         // TODO add your handling code here:
00383     }//GEN-LAST:event_buttonRunActionPerformed
00384 
00385     // Variables declaration - do not modify//GEN-BEGIN:variables
00386     private javax.swing.JButton buttonRun;
00387 
00388     private javax.swing.JButton buttonScreenshot;
00389 
00390     private javax.swing.JButton buttonStop;
00391 
00392     private javax.swing.JButton buttonZoomInX;
00393 
00394     private javax.swing.JButton buttonZoomInY;
00395 
00396     private javax.swing.JButton buttonZoomOutX;
00397 
00398     private javax.swing.JButton buttonZoomOutY;
00399 
00400     private javax.swing.JCheckBox checkboxDrawClustering;
00401 
00402     private javax.swing.JCheckBox checkboxDrawGT;
00403 
00404     private javax.swing.JCheckBox checkboxDrawMicro;
00405 
00406     private javax.swing.JCheckBox checkboxDrawPoints;
00407 
00408     private moa.gui.clustertab.ClusteringVisualEvalPanel clusteringVisualEvalPanel1;
00409 
00410     private javax.swing.JComboBox comboX;
00411 
00412     private javax.swing.JComboBox comboY;
00413 
00414     private moa.gui.visualization.GraphCanvas graphCanvas;
00415 
00416     private javax.swing.JPanel graphPanel;
00417 
00418     private javax.swing.JPanel graphPanelControlBottom;
00419 
00420     private javax.swing.JPanel graphPanelControlTop;
00421 
00422     private javax.swing.JScrollPane graphScrollPanel;
00423 
00424     private javax.swing.JLabel jLabel1;
00425 
00426     private javax.swing.JLabel labelEvents;
00427 
00428     private javax.swing.JLabel labelNumPause;
00429 
00430     private javax.swing.JLabel labelX;
00431 
00432     private javax.swing.JLabel labelY;
00433 
00434     private javax.swing.JLabel label_processed_points;
00435 
00436     private javax.swing.JLabel label_processed_points_value;
00437 
00438     private javax.swing.JTextField numPauseAfterPoints;
00439 
00440     private javax.swing.JPanel panelControl;
00441 
00442     private javax.swing.JPanel panelEvalOutput;
00443 
00444     private javax.swing.JPanel panelVisualWrapper;
00445 
00446     private javax.swing.JScrollPane scrollPane0;
00447 
00448     private javax.swing.JScrollPane scrollPane1;
00449 
00450     private javax.swing.JSlider speedSlider;
00451 
00452     private javax.swing.JSplitPane splitVisual;
00453 
00454     private moa.gui.visualization.StreamPanel streamPanel0;
00455 
00456     private moa.gui.visualization.StreamPanel streamPanel1;
00457 
00458     @Override
00459     public void actionPerformed(ActionEvent e) {
00460         //reacte on graph selection and find out which measure was selected
00461         int selected = Integer.parseInt(e.getActionCommand());
00462         int counter = selected;
00463         int m_select = 0;
00464         int m_select_offset = 0;
00465         boolean found = false;
00466         for (int i = 0; i < acc1.length; i++) {
00467             for (int j = 0; j < acc1[i].getNumMeasures(); j++) {
00468                 if (acc1[i].isEnabled(j)) {
00469                     counter--;
00470                     if (counter < 0) {
00471                         m_select = i;
00472                         m_select_offset = j;
00473                         found = true;
00474                         break;
00475                     }
00476                 }
00477             }
00478             if (found) {
00479                 break;
00480             }
00481         }
00482         this.graphCanvas.setGraph(acc1[m_select], acc2[m_select], m_select_offset, this.graphCanvas.getProcessFrequency());
00483     }
00484 }
 All Classes Namespaces Files Functions Variables Enumerations