org.glotaran.core.datadisplayers.common.ImageSVDPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.glotaran.core.datadisplayers.common.ImageSVDPanel.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 org.glotaran.core.datadisplayers.common;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import javax.swing.SpinnerNumberModel;
import org.glotaran.jfreechartcustom.GraphPanel;
import org.glotaran.jfreechartcustom.ImageUtilities;
import org.glotaran.jfreechartcustom.IntensImageDataset;
import org.glotaran.jfreechartcustom.RedGreenPaintScale;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.PaintScale;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.ujmp.core.Matrix;

/**
 *
 * @author Sergey
 */
public class ImageSVDPanel extends javax.swing.JPanel {

    /**
     * display results of the SVD decomposition for image like data
     * (FLIM,2D-spectra)
     */

    private final int MAX_NUMBER_SINGULAR_VALUES = 20;
    private Matrix[] svdResult;
    private JFreeChart leftSVChart;
    private JFreeChart rightSVChart;
    private int imageWitdth;
    private int imageHeight;
    private int timeSteps;
    private double[] xAxe;
    private double[] yAxe;
    private double[] timeAxe;
    private boolean calculated = false;

    public ImageSVDPanel() {
        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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel10 = new javax.swing.JPanel();
        jPSingValues = new javax.swing.JPanel();
        jPLeftSingVectors = new javax.swing.JPanel();
        jPRightSingVectors = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jToolBar3 = new javax.swing.JToolBar();
        jLabel10 = new javax.swing.JLabel();
        jSnumSV = new javax.swing.JSpinner();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jLabel11 = new javax.swing.JLabel();
        jTFtotalNumSV = new javax.swing.JTextField();
        jSeparator6 = new javax.swing.JToolBar.Separator();

        setLayout(new java.awt.GridBagLayout());

        jPanel10.setMinimumSize(new java.awt.Dimension(1000, 570));
        jPanel10.setPreferredSize(new java.awt.Dimension(500, 500));
        jPanel10.setLayout(new java.awt.GridBagLayout());

        jPSingValues.setBackground(new java.awt.Color(255, 255, 255));
        jPSingValues.setMinimumSize(new java.awt.Dimension(50, 50));
        jPSingValues.setPreferredSize(new java.awt.Dimension(100, 100));
        jPSingValues.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel10.add(jPSingValues, gridBagConstraints);

        jPLeftSingVectors.setBackground(new java.awt.Color(255, 255, 255));
        jPLeftSingVectors.setMinimumSize(new java.awt.Dimension(100, 100));
        jPLeftSingVectors.setPreferredSize(new java.awt.Dimension(100, 100));
        jPLeftSingVectors.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel10.add(jPLeftSingVectors, gridBagConstraints);

        jPRightSingVectors.setBackground(new java.awt.Color(255, 255, 255));
        jPRightSingVectors.setMinimumSize(new java.awt.Dimension(100, 100));
        jPRightSingVectors.setPreferredSize(new java.awt.Dimension(100, 100));
        jPRightSingVectors.setLayout(new java.awt.GridLayout(2, 2));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel10.add(jPRightSingVectors, gridBagConstraints);

        jLabel5.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel5,
                org.openide.util.NbBundle.getMessage(ImageSVDPanel.class, "ImageSVDPanel.jLabel5.text")); // NOI18N
        jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel10.add(jLabel5, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel10, gridBagConstraints);

        jToolBar3.setRollover(true);
        jToolBar3.setMinimumSize(new java.awt.Dimension(438, 23));
        jToolBar3.setPreferredSize(new java.awt.Dimension(460, 23));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel10,
                org.openide.util.NbBundle.getMessage(ImageSVDPanel.class, "ImageSVDPanel.jLabel10.text")); // NOI18N
        jToolBar3.add(jLabel10);

        jSnumSV.setMaximumSize(new java.awt.Dimension(45, 20));
        jSnumSV.setMinimumSize(new java.awt.Dimension(45, 20));
        jSnumSV.setPreferredSize(new java.awt.Dimension(45, 20));
        jSnumSV.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSnumSVStateChanged(evt);
            }
        });
        jToolBar3.add(jSnumSV);
        jToolBar3.add(jSeparator5);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel11,
                org.openide.util.NbBundle.getMessage(ImageSVDPanel.class, "ImageSVDPanel.jLabel11.text")); // NOI18N
        jToolBar3.add(jLabel11);

        jTFtotalNumSV.setEditable(false);
        jTFtotalNumSV.setText(
                org.openide.util.NbBundle.getMessage(ImageSVDPanel.class, "ImageSVDPanel.jTFtotalNumSV.text")); // NOI18N
        jTFtotalNumSV.setMaximumSize(new java.awt.Dimension(100, 20));
        jTFtotalNumSV.setMinimumSize(new java.awt.Dimension(45, 20));
        jTFtotalNumSV.setPreferredSize(new java.awt.Dimension(55, 20));
        jToolBar3.add(jTFtotalNumSV);
        jToolBar3.add(jSeparator6);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(jToolBar3, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jSnumSVStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSnumSVStateChanged
        updateSVDPlots();
    }//GEN-LAST:event_jSnumSVStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPLeftSingVectors;
    private javax.swing.JPanel jPRightSingVectors;
    private javax.swing.JPanel jPSingValues;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JSpinner jSnumSV;
    private javax.swing.JTextField jTFtotalNumSV;
    private javax.swing.JToolBar jToolBar3;
    // End of variables declaration//GEN-END:variables

    private void updateSVDPlots() {
        XYSeriesCollection lSVCollection = new XYSeriesCollection();
        XYSeries seria;
        for (int j = 0; j < (Integer) jSnumSV.getValue(); j++) {
            seria = new XYSeries("LSV" + j + 1);
            for (int i = 0; i < timeSteps; i++) {
                seria.add(timeAxe[i], svdResult[0].getAsDouble((long) i, j));
            }
            lSVCollection.addSeries(seria);
        }
        leftSVChart.getXYPlot().setDataset(lSVCollection);

        double[] tempRsingVec = null;
        double minVal = 0;
        double maxVal = 0;

        if (jPRightSingVectors.getComponentCount() < (Integer) jSnumSV.getValue()) {
            double tempValue;
            for (int i = jPRightSingVectors.getComponentCount(); i < (Integer) jSnumSV.getValue(); i++) {
                tempRsingVec = new double[imageWitdth * imageHeight];
                for (int j = 0; j < imageWitdth * imageHeight; j++) {
                    tempValue = svdResult[2].getAsDouble(j, i);
                    tempRsingVec[j] = tempValue;
                    minVal = minVal > tempValue ? tempValue : minVal;
                    maxVal = maxVal < tempValue ? tempValue : maxVal;
                }
                IntensImageDataset rSingVec = new IntensImageDataset(imageWitdth, imageHeight, tempRsingVec);
                PaintScale ps = new RedGreenPaintScale(minVal, maxVal);
                JFreeChart rSingVect = CommonDataDispTools.createScatChart(
                        ImageUtilities.createColorCodedImage(rSingVec, ps), ps, imageWitdth, imageHeight);
                //            rSingVect.setTitle("R Singular vector " + String.valueOf(j + 1));
                //rSingVect.getTitle().setFont(new Font(tracechart.getTitle().getFont().getFontName(), Font.PLAIN, 12));
                ChartPanel rSingVectPanel = new ChartPanel(rSingVect);
                rSingVectPanel.setFillZoomRectangle(true);
                rSingVectPanel.setMouseWheelEnabled(true);
                jPRightSingVectors.add(rSingVectPanel);
            }

        } else {
            for (int i = jPRightSingVectors.getComponentCount() - 1; i >= (Integer) jSnumSV.getValue(); i--) {
                jPRightSingVectors.remove(i);
                jPRightSingVectors.repaint();
            }
        }
        jPRightSingVectors.validate();
    }

    public void createSVDPlots() {

        int maxSpinnerNumberModel = Math.min(MAX_NUMBER_SINGULAR_VALUES, (int) svdResult[1].getRowCount());
        jTFtotalNumSV
                .setText("Max " + maxSpinnerNumberModel + " of  " + String.valueOf(svdResult[1].getRowCount()));
        jSnumSV.setModel(new SpinnerNumberModel((int) 1, (int) 0, maxSpinnerNumberModel, (int) 1));

        //creare collection with first 2 LSV
        XYSeriesCollection lSVCollection = new XYSeriesCollection();
        XYSeries seria;
        seria = new XYSeries("LSV1");
        for (int i = 0; i < timeSteps; i++) {
            seria.add(timeAxe[i], svdResult[0].getAsDouble((long) i, 0));
        }
        lSVCollection.addSeries(seria);

        //creare chart for 2 LSV
        leftSVChart = ChartFactory.createXYLineChart("Left singular vectors", "Time (~s)", null, lSVCollection,
                PlotOrientation.VERTICAL, false, false, false);
        //leftSVChart.getTitle().setFont(new Font(leftSVChart.getTitle().getFont().getFontName(), Font.PLAIN, 12));
        leftSVChart.setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT);
        GraphPanel chpan = new GraphPanel(leftSVChart);
        jPLeftSingVectors.removeAll();
        jPLeftSingVectors.add(chpan);

        //creare collection with first RSV
        double[] tempRsingVec = null;
        double minVal = 0;
        double maxVal = 0;

        //            seria = new XYSeries("RSV" + (j + 1));
        tempRsingVec = new double[imageWitdth * imageHeight];
        double tempValue;
        for (int i = 0; i < imageWitdth * imageHeight; i++) {
            tempValue = svdResult[2].getAsDouble(i, 0);
            tempRsingVec[i] = tempValue;
            minVal = minVal > tempValue ? tempValue : minVal;
            maxVal = maxVal < tempValue ? tempValue : maxVal;
        }

        IntensImageDataset rSingVec = new IntensImageDataset(imageWitdth, imageHeight, tempRsingVec);
        PaintScale ps = new RedGreenPaintScale(minVal, maxVal);
        JFreeChart rSingVect = CommonDataDispTools
                .createScatChart(ImageUtilities.createColorCodedImage(rSingVec, ps), ps, imageWitdth, imageHeight);
        //            rSingVect.setTitle("R Singular vector " + String.valueOf(j + 1));
        //rSingVect.getTitle().setFont(new Font(tracechart.getTitle().getFont().getFontName(), Font.PLAIN, 12));
        ChartPanel rSingVectPanel = new ChartPanel(rSingVect);
        rSingVectPanel.setFillZoomRectangle(true);
        rSingVectPanel.setMouseWheelEnabled(true);

        jPRightSingVectors.removeAll();
        jPRightSingVectors.add(rSingVectPanel);

        //creare collection with singular values
        XYSeriesCollection sVCollection = new XYSeriesCollection();
        seria = new XYSeries("SV");
        for (int i = 0; i < maxSpinnerNumberModel; i++) {
            seria.add(i + 1, svdResult[1].getAsDouble((long) i, (long) i));
        }
        sVCollection.addSeries(seria);

        //create chart for singular values
        JFreeChart tracechart = ChartFactory.createXYLineChart("Screeplot", "Singular Value index (n)", null,
                sVCollection, PlotOrientation.VERTICAL, false, false, false);
        LogAxis logAxe = new LogAxis("Log(SVn)");
        final NumberAxis domainAxis = (NumberAxis) tracechart.getXYPlot().getDomainAxis();
        domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        tracechart.getXYPlot().setRangeAxis(logAxe);
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) tracechart.getXYPlot().getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);
        renderer.setBaseFillPaint(Color.white);
        renderer.setSeriesStroke(0, new BasicStroke(1.0f));
        renderer.setSeriesOutlineStroke(0, new BasicStroke(1.0f));
        renderer.setSeriesShape(0, new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));

        //tracechart.getTitle().setFont(new Font(tracechart.getTitle().getFont().getFontName(), Font.PLAIN, 12));
        tracechart.setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT);

        chpan = new GraphPanel(tracechart);
        //add chart with 2 RSV to JPannel
        jPSingValues.removeAll();
        jPSingValues.add(chpan);
    }

    public void initialiseSVDPlots(int t, int x, int y, double[] time, double[] xAx, double[] yAx) {
        //        svdResult = results;
        timeSteps = t;
        timeAxe = time;
        imageHeight = y;
        imageWitdth = x;
        xAxe = xAx;
        yAxe = yAx;
        if (svdResult != null) {
            calculated = true;
        } else {
            calculated = false;
        }
    }

    public boolean isCalculated() {
        return calculated;
    }

    public void setSVDResults(Matrix[] results) {
        svdResult = results;
        if (svdResult != null) {
            calculated = true;
        } else {
            calculated = false;
        }
    }

    public Matrix[] getSVDResults() {
        return svdResult;
    }
}