com.sliit.views.DataVisualizerPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.sliit.views.DataVisualizerPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sliit.views;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.evaluation.ThresholdCurve;
import weka.core.Instances;
import weka.core.Utils;
import weka.gui.beans.ScatterPlotMatrix;
import weka.gui.visualize.PlotData2D;
import weka.gui.visualize.ThresholdVisualizePanel;

/**
 *
 * @author heshanih
 */
public class DataVisualizerPanel extends javax.swing.JPanel {

    /**
     * Creates new form DataVisualizerPanel
     */
    public DataVisualizerPanel() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graphTabPane = new javax.swing.JTabbedPane();
        scatterplotpanel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        datasetPathText = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        processButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        graphTabPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                graphTabPaneStateChanged(evt);
            }
        });

        scatterplotpanel.setLayout(new java.awt.CardLayout());
        graphTabPane.addTab("Scatter Plot", scatterplotpanel);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 204, 255));
        jLabel6.setText("DATA VISUALISER");

        jLabel1.setText(" Dataset : ");

        datasetPathText.setText("D:\\SLIIT\\deadlocks\\data\\train.arff");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/file-explorer-icon.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        processButton.setText("Generate");
        processButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(datasetPathText, javax.swing.GroupLayout.PREFERRED_SIZE, 339,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(processButton))
                                .addComponent(graphTabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 1034,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(24, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addComponent(jLabel6).addGap(13, 13, 13).addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                                .createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(datasetPathText,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel1))
                                        .addComponent(processButton))
                                .addGap(15, 15, 15).addComponent(graphTabPane,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 443,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jButton3))
                        .addGap(30, 30, 30)));
    }// </editor-fold>//GEN-END:initComponents

    private void graphTabPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_graphTabPaneStateChanged

    }//GEN-LAST:event_graphTabPaneStateChanged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        JFileChooser jfcOpen = new JFileChooser();
        jfcOpen.showOpenDialog(null);
        File f = jfcOpen.getSelectedFile();
        datasetPathText.setText(f.getAbsoluteFile().toString());
    }//GEN-LAST:event_jButton3ActionPerformed

    private void processButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processButtonActionPerformed
        getScatterPlot();
    }//GEN-LAST:event_processButtonActionPerformed

    void getScatterPlot() {
        try {
            Reader r = new BufferedReader(new FileReader(datasetPathText.getText()));
            Instances inst = new Instances(r);
            final ScatterPlotMatrix scatterPlotMatrix = new ScatterPlotMatrix();
            scatterPlotMatrix.setInstances(inst);

            scatterplotpanel.removeAll();
            scatterplotpanel.add(scatterPlotMatrix, "scatterplotpanel", 0);
            scatterplotpanel.revalidate();

        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(ex.getMessage());
        }
    }

    void getRocCurve() {
        try {
            Instances data;
            data = new Instances(new BufferedReader(new FileReader(datasetPathText.getText())));
            data.setClassIndex(data.numAttributes() - 1);

            // train classifier
            Classifier cl = new NaiveBayes();
            Evaluation eval = new Evaluation(data);
            eval.crossValidateModel(cl, data, 10, new Random(1));

            // generate curve
            ThresholdCurve tc = new ThresholdCurve();
            int classIndex = 0;
            Instances result = tc.getCurve(eval.predictions(), classIndex);

            // plot curve
            ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
            vmc.setROCString("(Area under ROC = " + Utils.doubleToString(tc.getROCArea(result), 4) + ")");
            vmc.setName(result.relationName());
            PlotData2D tempd = new PlotData2D(result);
            tempd.setPlotName(result.relationName());
            tempd.addInstanceNumberAttribute();
            // specify which points are connected
            boolean[] cp = new boolean[result.numInstances()];
            for (int n = 1; n < cp.length; n++) {
                cp[n] = true;
            }
            tempd.setConnectPoints(cp);
            // add plot
            vmc.addPlot(tempd);

            // display curve
            String plotName = vmc.getName();
            final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Classifier Visualize: " + plotName);
            jf.setSize(500, 400);
            jf.getContentPane().setLayout(new BorderLayout());
            jf.getContentPane().add(vmc, BorderLayout.CENTER);
            jf.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent e) {
                    jf.dispose();
                }
            });
            jf.setVisible(true);
        } catch (IOException ex) {
            Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField datasetPathText;
    private javax.swing.JTabbedPane graphTabPane;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton processButton;
    private javax.swing.JPanel scatterplotpanel;
    // End of variables declaration//GEN-END:variables
}