br.preprocess.forms.ReducaoPCA.java Source code

Java tutorial

Introduction

Here is the source code for br.preprocess.forms.ReducaoPCA.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 br.preprocess.forms;

import br.preprocess.App;
import br.preprocess.core.Chart;
import br.preprocess.core.Dataset;
import br.preprocess.core.KNearestNeighbour;
import br.preprocess.core.PrincipalComponentAnalysis;
import br.preprocess.utils.Utils;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import weka.core.Instance;

/**
 *
 * @author Fernando
 */
public class ReducaoPCA extends javax.swing.JDialog {

    private final App parent;
    private final Chart ch;
    private final Dataset ds;
    private final PrincipalComponentAnalysis pca;
    private final KNearestNeighbour knn;

    /**
     * Creates new form WindowModel
     * @param parent
     * @param modal
     */
    public ReducaoPCA(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent = (App) parent;
        this.setLocation(parent.getLocation());
        this.ch = new Chart(this.redChart.getWidth(), this.redChart.getHeight(), "PCA - Espalhamento", "Eixo X",
                "Eixo Y", Chart.CHART_SCATTER, false);
        Utils.addPlot(this.redChart, ch);
        this.ds = this.parent.getDsManipulavel().clonar();
        for (int i = 0; i < this.ds.getNumClass(); i++)
            this.ch.addXYSerie("Classe " + (i + 1));
        this.knn = this.parent.getKnn();
        this.pca = new PrincipalComponentAnalysis();
    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        redChart = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        redLimiar = new javax.swing.JSpinner();
        redPre = new javax.swing.JButton();
        redAplic = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        redStats = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        redEixX = new javax.swing.JComboBox();
        redEixY = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(" Principal Component Analysis (PCA)");
        setIconImage(null);
        setResizable(false);

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

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

        javax.swing.GroupLayout redChartLayout = new javax.swing.GroupLayout(redChart);
        redChart.setLayout(redChartLayout);
        redChartLayout.setHorizontalGroup(redChartLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        redChartLayout.setVerticalGroup(redChartLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 277, Short.MAX_VALUE));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Configurao"));

        jLabel2.setText(
                "<html>\n<p>\nA avaliao de desempenho ser realizada atravs do <strong>k-NN</strong> com a <strong>configurao atual</strong>.\n</p>\n<p>\n*A base de dados ser automaticamente normalizada, caso ainda no seja.\n</p>\n</html>");

        jLabel3.setText("Porcentagem de varincia coberta pelas componentes principais:");

        redLimiar.setModel(new javax.swing.SpinnerNumberModel(0.95d, 0.1d, 1.0d, 0.05d));

        redPre.setText("Pr-Visualizar");
        redPre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redPreActionPerformed(evt);
            }
        });

        redAplic.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        redAplic.setText("Aplicar*");
        redAplic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redAplicActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2)
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(redLimiar, javax.swing.GroupLayout.PREFERRED_SIZE, 55,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(redPre, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(redAplic, javax.swing.GroupLayout.PREFERRED_SIZE, 83,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3)
                        .addComponent(redLimiar, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(redAplic).addComponent(redPre))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        redStats.setFont(new java.awt.Font("Courier New", 0, 10)); // NOI18N
        redStats.setFocusable(false);
        jScrollPane2.setViewportView(redStats);

        jLabel1.setText("Eixo X:");

        jLabel4.setText("Eixo Y:");

        redEixX.setMaximumSize(new java.awt.Dimension(384, 32767));
        redEixX.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                changeAxis(evt);
            }
        });

        redEixY.setMaximumSize(new java.awt.Dimension(384, 32767));
        redEixY.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                changeAxis(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(redChart, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(redEixY, javax.swing.GroupLayout.PREFERRED_SIZE, 384,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(redEixX, javax.swing.GroupLayout.PREFERRED_SIZE, 384,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane2))
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel1)
                                                .addComponent(redEixX, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel4).addComponent(redEixY,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(redChart, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                        .addComponent(jScrollPane2))
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void redAplicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redAplicActionPerformed
        if (this.pca.getDataset() != null) {
            this.parent.insertLog("Aplicou reduo com PCA na base de dados " + this.ds.getDatasetName());
            this.parent.setDsManipulavel(this.pca.getDataset());
            this.parent.updateAll();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this,
                    "Somente  possvel aplicar PCA se pr-visualizar os resultados antes.");
        }
    }//GEN-LAST:event_redAplicActionPerformed

    private void redPreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redPreActionPerformed
        double split = this.parent.getSplitRatio();
        double varCovered = (double) this.redLimiar.getValue();
        Dataset dsLocal = this.ds.clonar();
        String stat = "";
        this.knn.setDataset(dsLocal, split);
        if (this.knn.execute()) {
            stat += "(k-NN) Sem reduo: " + (this.knn.getEvl().pctCorrect() / 100) + "\n\n";
            this.pca.setDataset(dsLocal);
            this.pca.setVarCovered(varCovered);
            if (this.pca.reduceDataset()) {
                this.knn.setDataset(this.pca.getDataset(), split);
                if (this.knn.execute())
                    stat += "(k-NN) Com reduo: " + (this.knn.getEvl().pctCorrect() / 100) + "\n\n";
                stat += this.pca.getInfo();
            }
            this.redStats.setText(stat);
            DefaultComboBoxModel dcbm = new DefaultComboBoxModel(), dcbm2 = new DefaultComboBoxModel();
            for (int i = 0; i < this.pca.getNumComponents(); i++) {
                dcbm.insertElementAt("PC" + (i + 1) + " " + this.pca.getComponentNameAt(i), i);
                dcbm2.insertElementAt("PC" + (i + 1) + " " + this.pca.getComponentNameAt(i), i);
            }
            this.redEixX.setModel(dcbm);
            this.redEixY.setModel(dcbm2);
            this.redEixX.setSelectedIndex(0);
            this.redEixY.setSelectedIndex(0);
        }
    }//GEN-LAST:event_redPreActionPerformed

    private void changeAxis(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_changeAxis
        this.changeChart(this.redEixX.getSelectedIndex(), this.redEixY.getSelectedIndex());
    }//GEN-LAST:event_changeAxis

    public void changeChart(int indexX, int indexY) {
        if (indexX > -1 && indexY > -1) {
            this.ch.clear();
            for (int i = 0; i < this.pca.getDataset().getNumClass(); i++)
                for (Instance sample : this.pca.getDataset().getDataset())
                    if (sample.value(sample.classIndex()) == i)
                        this.ch.update(sample.value(indexX), sample.value(indexY), i);
            Utils.addPlot(this.redChart, this.ch);
        }
    }

    public void open() {
        this.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton redAplic;
    private javax.swing.JPanel redChart;
    private javax.swing.JComboBox redEixX;
    private javax.swing.JComboBox redEixY;
    private javax.swing.JSpinner redLimiar;
    private javax.swing.JButton redPre;
    private javax.swing.JTextPane redStats;
    // End of variables declaration//GEN-END:variables
}