All Downloads are FREE. Search and download functionalities are using the official Maven repository.

moa.gui.active.ALPreviewPanel Maven / Gradle / Ivy

Go to download

Massive On-line Analysis is an environment for massive data mining. MOA provides a framework for data stream mining and includes tools for evaluation and a collection of machine learning algorithms. Related to the WEKA project, also written in Java, while scaling to more demanding problems.

There is a newer version: 2024.07.0
Show newest version
/*
 *    ALPreviewPanel.java
 *    Original Work: Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
 *    @author Richard Kirkby ([email protected])
 *    Modified Work: Copyright (C) 2017 Otto-von-Guericke-University, Magdeburg, Germany
 *    @author Tim Sabsch ([email protected])
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program. If not, see .
 *    
 */
package moa.gui.active;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import moa.core.StringUtils;
import moa.evaluation.preview.Preview;
import moa.gui.PreviewPanel;
import moa.tasks.FailedTaskReport;
import moa.tasks.ResultPreviewListener;
import moa.tasks.meta.ALMainTask;
import moa.tasks.meta.ALPartitionEvaluationTask;
import moa.tasks.meta.ALTaskThread;

/**
 * ALPreviewPanel provides a graphical interface to display the latest preview
 * of a task thread.
 *
 * @author Tim Sabsch ([email protected])
 * @version $Revision: 1 $
 * @See PreviewPanel
 */
public class ALPreviewPanel extends JPanel implements ResultPreviewListener {

    private static final long serialVersionUID = 1L;

    protected ALTaskThread previewedThread;

    protected JLabel previewLabel;

    protected JButton refreshButton;

    protected JLabel autoRefreshLabel;
    
    protected JComboBox autoRefreshComboBox;

    protected ALTaskTextViewerPanel textViewerPanel;

    protected javax.swing.Timer autoRefreshTimer;

    /**
     * Initialises the underlying ALTaskTextViewerPanel and the refresh components.
     */
    public ALPreviewPanel() {
    	this.textViewerPanel = new ALTaskTextViewerPanel(); 
    	this.previewLabel = new JLabel("No preview available");
    	this.refreshButton = new JButton("Refresh");
    	this.autoRefreshLabel = new JLabel("Auto refresh: ");
    	this.autoRefreshComboBox = new JComboBox(PreviewPanel.autoFreqStrings);
        this.autoRefreshComboBox.setSelectedIndex(1); // default to 1 sec
        JPanel controlPanel = new JPanel();
        controlPanel.add(this.previewLabel);
        controlPanel.add(this.refreshButton);
        controlPanel.add(this.autoRefreshLabel);
        controlPanel.add(this.autoRefreshComboBox);
        setLayout(new BorderLayout());
        add(controlPanel, BorderLayout.NORTH);
        add(this.textViewerPanel, BorderLayout.CENTER);
        this.refreshButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                refresh();
            }
        });
        this.autoRefreshTimer = new javax.swing.Timer(1000,
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        refresh();
                    }
                });
        this.autoRefreshComboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                updateAutoRefreshTimer();
            }
        });
        setTaskThreadToPreview(null);
    }

    /**
     * Refreshes the preview.
     */
    private void refresh() {
        if (this.previewedThread != null) {
            if (this.previewedThread.isComplete()) {
                setLatestPreview();
                disableRefresh();
            } else {
                this.previewedThread.getPreview(ALPreviewPanel.this);
            }
        }
    }

    /**
     * Sets the TaskThread that will be previewed.
     * @param thread TaskThread to be previewed
     */
	public void setTaskThreadToPreview(ALTaskThread thread) {
        this.previewedThread = thread;
        setLatestPreview();
        if (thread == null) {
            disableRefresh();
        } else if (!thread.isComplete()) {
            enableRefresh();
        }
    }

    /**
     * Requests the latest preview and sends it to the TextViewerPanel to
     * display it.
     */
    private void setLatestPreview() {

    	
    	if(this.previewedThread != null && this.previewedThread.failed())
    	{
    		FailedTaskReport failedTaskReport = (FailedTaskReport) this.previewedThread.getFinalResult();
    		this.textViewerPanel.setErrorText(failedTaskReport);
    		this.textViewerPanel.setGraph(null, null);
    	}
    	else
    	{
        	Preview preview = null;
    		if ((this.previewedThread != null) && this.previewedThread.isComplete()) {
    			// cancelled, completed or failed task
    			// TODO if the task is failed, the finalResult is a FailedTaskReport, which is not a Preview
    			preview = (Preview) this.previewedThread.getFinalResult();
    			this.previewLabel.setText("Final result");
    			disableRefresh();
    		} else if (this.previewedThread != null){
    			// running task
    			preview = (Preview) this.previewedThread.getLatestResultPreview();
    			double grabTime = this.previewedThread.getLatestPreviewGrabTimeSeconds();
    			String grabString = " (" + StringUtils.secondsToDHMSString(grabTime) + ")";
    			if (preview == null) {
    				this.previewLabel.setText("No preview available" + grabString);
    			} else {
    				this.previewLabel.setText("Preview" + grabString);
    			}
    		} else {
    			// no thread
    			this.previewLabel.setText("No preview available");
    			preview = null;
    		}
        	
    		this.textViewerPanel.setText(preview);
    		this.textViewerPanel.setGraph(preview, getColorCodings(this.previewedThread));
    	}
    }
    
    /**
     * Reads the color codings of the subtasks.
     * @return array of color codings, one for each subtask
     */
    private Color[] getColorCodings(ALTaskThread thread) {
    	if (thread == null) {
    		return null;
    	}
    	
    	ALMainTask task = (ALMainTask) thread.getTask();
    	
    	List subtaskThreads = task.getSubtaskThreads();
    	
    	if (subtaskThreads.size() == 0) {
    		// no hierarchical thread, e.g. ALPrequentialEvaluationTask
    		return new Color[]{task.getColorCoding()};
    	}
    	
    	
    	if (task.getClass() == ALPartitionEvaluationTask.class) {
    		// if the task is a cross validation task, it displays the mean
    		// over the underlying params. The color coding therefore 
    		// corresponds to the color coding of each of its subtasks.
    		return getColorCodings(subtaskThreads.get(0));
    	}
    	
    	Color[] colors = new Color[subtaskThreads.size()];
    	for (int i = 0; i < subtaskThreads.size(); i++) {
    		ALMainTask subtask = (ALMainTask) subtaskThreads.get(i).getTask();
    		colors[i] = subtask.getColorCoding();
    	}
    	return colors;
    }

    /**
     * Updates the refresh timer by the setting of the "Auto refresh" box.
     */
    private void updateAutoRefreshTimer() {
        int autoDelay = PreviewPanel.autoFreqTimeSecs[this.autoRefreshComboBox.getSelectedIndex()];
        if (autoDelay > 0) {
            if (this.autoRefreshTimer.isRunning()) {
                this.autoRefreshTimer.stop();
            }
            this.autoRefreshTimer.setDelay(autoDelay * 1000);
            this.autoRefreshTimer.start();
        } else {
            this.autoRefreshTimer.stop();
        }
    }

    /**
     * Disables refreshing.
     */
    private void disableRefresh() {
        this.refreshButton.setEnabled(false);
        this.autoRefreshLabel.setEnabled(false);
        this.autoRefreshComboBox.setEnabled(false);
        this.autoRefreshTimer.stop();
    }

    /**
     * Enables refreshing.
     */
    private void enableRefresh() {
        this.refreshButton.setEnabled(true);
        this.autoRefreshLabel.setEnabled(true);
        this.autoRefreshComboBox.setEnabled(true);
        updateAutoRefreshTimer();
    }

    @Override
    public void latestPreviewChanged() {
        setTaskThreadToPreview(this.previewedThread);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy