From e1212c0ce9420188cb613847f0b86d6bd37b9136 Mon Sep 17 00:00:00 2001 From: jlizier Date: Fri, 24 Nov 2017 00:09:21 +1100 Subject: [PATCH] Added AutoAnalyserLauncher applet, a one-stop shop for launching an AutoAnalyser applet. Gives buttons to launch each one. This requires some minor changes to the super AutoAnalyser class (to allow constructor to specify offset of the working directory from the demos/AutoAnalyser directory) and child classes to allow the new constructors --- .../demos/autoanalysis/AutoAnalyser.java | 40 ++-- .../demos/autoanalysis/AutoAnalyserAIS.java | 8 + .../demos/autoanalysis/AutoAnalyserCMI.java | 8 + .../demos/autoanalysis/AutoAnalyserCTE.java | 8 + .../AutoAnalyserChannelCalculator.java | 9 + .../autoanalysis/AutoAnalyserEntropy.java | 8 + .../autoanalysis/AutoAnalyserLauncher.java | 184 ++++++++++++++++++ .../demos/autoanalysis/AutoAnalyserMI.java | 8 + .../demos/autoanalysis/AutoAnalyserTE.java | 8 + 9 files changed, 270 insertions(+), 11 deletions(-) create mode 100644 demos/java/infodynamics/demos/autoanalysis/AutoAnalyserLauncher.java diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyser.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyser.java index 9278bad..2609937 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyser.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyser.java @@ -207,6 +207,11 @@ public abstract class AutoAnalyser extends JFrame protected String appletTitle; + // Specifies a prefix from the current working directory to + // the demos/AutoAnalyser/ directory. Is empty if it is assumed + // we are already in that directory. Can be set in the constructor + protected String pathToAutoAnalyserDir = ""; + public class TextAreaWithImage extends JTextArea { /** @@ -257,19 +262,28 @@ public abstract class AutoAnalyser extends JFrame * Constructor to generate the application windows */ public AutoAnalyser() { + this(""); + } + + /** + * Constructor to generate the application windows + */ + public AutoAnalyser(String pathToAutoAnalyserDir) { + + this.pathToAutoAnalyserDir = pathToAutoAnalyserDir; makeSpecificInitialisations(); // Build the swing applet - ImageIcon icon = new ImageIcon("../../JIDT-logo.png"); // Location for distributions + ImageIcon icon = new ImageIcon(pathToAutoAnalyserDir + "../../JIDT-logo.png"); // Location for distributions if (icon.getImageLoadStatus() != MediaTracker.COMPLETE) { - // Try the alternative image location for SVN checkouts - icon = new ImageIcon("../../web/JIDT-logo.png"); + // Try the alternative image location for git checkouts + icon = new ImageIcon(pathToAutoAnalyserDir + "../../web/JIDT-logo.png"); } setIconImage(icon.getImage()); - Image watermarkImage = (new ImageIcon("JIDT-logo-watermark.png")).getImage(); + Image watermarkImage = (new ImageIcon(pathToAutoAnalyserDir + "JIDT-logo-watermark.png")).getImage(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1100,670); @@ -612,7 +626,8 @@ public abstract class AutoAnalyser extends JFrame // one JFileChooser dataFileChooser; if (dataFile == null) { - dataFileChooser = new JFileChooser(System.getProperty("user.dir") + "/../data/"); + dataFileChooser = new JFileChooser( + System.getProperty("user.dir") + "/" + pathToAutoAnalyserDir + "../data/"); } else { dataFileChooser = new JFileChooser(dataFile); } @@ -746,11 +761,14 @@ public abstract class AutoAnalyser extends JFrame // Work out full path of the jar and code utilities: String jarLocation, pythonDemosLocation, matlabDemosLocation; try { - File jarLocationFile = new File(System.getProperty("user.dir") + "/../../infodynamics.jar"); + File jarLocationFile = new File(System.getProperty("user.dir") + + "/" + pathToAutoAnalyserDir + "../../infodynamics.jar"); jarLocation = jarLocationFile.getCanonicalPath(); - File pythonDemosLocationFile = new File(System.getProperty("user.dir") + "/../python"); + File pythonDemosLocationFile = new File(System.getProperty("user.dir") + + "/" + pathToAutoAnalyserDir + "../python"); pythonDemosLocation = pythonDemosLocationFile.getCanonicalPath(); - File matlabDemosLocationFile = new File(System.getProperty("user.dir") + "/../octave"); + File matlabDemosLocationFile = new File(System.getProperty("user.dir") + + "/" + pathToAutoAnalyserDir + "../octave"); matlabDemosLocation = matlabDemosLocationFile.getCanonicalPath(); } catch (IOException ioex) { JOptionPane.showMessageDialog(this, @@ -1234,15 +1252,15 @@ public abstract class AutoAnalyser extends JFrame // Now write the code to a file // 1. Java - FileWriter codeFileWriter = new FileWriter("../java/infodynamics/demos/autoanalysis/GeneratedCalculator.java"); + FileWriter codeFileWriter = new FileWriter(pathToAutoAnalyserDir + "../java/infodynamics/demos/autoanalysis/GeneratedCalculator.java"); codeFileWriter.write(javaCode.toString()); codeFileWriter.close(); // 2. Python - codeFileWriter = new FileWriter("GeneratedCalculator.py"); + codeFileWriter = new FileWriter(pathToAutoAnalyserDir + "GeneratedCalculator.py"); codeFileWriter.write(pythonCode.toString()); codeFileWriter.close(); // 3. Matlab - codeFileWriter = new FileWriter("GeneratedCalculator.m"); + codeFileWriter = new FileWriter(pathToAutoAnalyserDir + "GeneratedCalculator.m"); codeFileWriter.write(matlabCode.toString()); codeFileWriter.close(); diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserAIS.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserAIS.java index 46eee68..05a9532 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserAIS.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserAIS.java @@ -62,6 +62,14 @@ public class AutoAnalyserAIS extends AutoAnalyser { protected String[] kraskovPropertiesFieldNames; protected String[] kraskovPropertyDescriptions; + public AutoAnalyserAIS() { + super(); + } + + public AutoAnalyserAIS(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for a channel calculator */ diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCMI.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCMI.java index 4eb12db..596a004 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCMI.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCMI.java @@ -63,6 +63,14 @@ public class AutoAnalyserCMI extends AutoAnalyser protected static final String CALC_TYPE_KRASKOV_ALG1 = CALC_TYPE_KRASKOV + " alg. 1"; protected static final String CALC_TYPE_KRASKOV_ALG2 = CALC_TYPE_KRASKOV + " alg. 2"; + public AutoAnalyserCMI() { + super(); + } + + public AutoAnalyserCMI(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for CMI */ diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCTE.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCTE.java index 9c4161a..924b04d 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCTE.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserCTE.java @@ -61,6 +61,14 @@ public class AutoAnalyserCTE extends AutoAnalyser protected String[] kraskovPropertiesFieldNames; protected String[] kraskovPropertyDescriptions; + public AutoAnalyserCTE() { + super(); + } + + public AutoAnalyserCTE(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for Conditional TE */ diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserChannelCalculator.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserChannelCalculator.java index e53fd69..1398a5f 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserChannelCalculator.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserChannelCalculator.java @@ -53,6 +53,15 @@ public abstract class AutoAnalyserChannelCalculator extends AutoAnalyser { protected String[] kraskovProperties; protected String[] kraskovPropertiesFieldNames; protected String[] kraskovPropertyDescriptions; + + + public AutoAnalyserChannelCalculator() { + super(); + } + + public AutoAnalyserChannelCalculator(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } /** * Constructor to initialise the GUI for a channel calculator diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserEntropy.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserEntropy.java index 5e8ac79..b5e902a 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserEntropy.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserEntropy.java @@ -59,6 +59,14 @@ public class AutoAnalyserEntropy extends AutoAnalyser { protected String[] klPropertiesFieldNames; protected String[] klPropertyDescriptions; + public AutoAnalyserEntropy() { + super(); + } + + public AutoAnalyserEntropy(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for a channel calculator */ diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserLauncher.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserLauncher.java new file mode 100644 index 0000000..ccbc455 --- /dev/null +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserLauncher.java @@ -0,0 +1,184 @@ +/* + * Java Information Dynamics Toolkit (JIDT) + * Copyright (C) 2017, Joseph T. Lizier + * + * 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 infodynamics.demos.autoanalysis; + + +import javax.swing.BorderFactory; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JButton; +import javax.swing.SwingConstants; +import javax.swing.ToolTipManager; + +import java.awt.BorderLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.MediaTracker; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; +import java.lang.reflect.Constructor; + +/** + * This class provides a single GUI to launch the AutoAnalyser GUIs from. + * + * + * @author Joseph Lizier + * + */ +public class AutoAnalyserLauncher extends JFrame + implements ActionListener { + + /** + * Need serialVersionUID to be serializable + */ + private static final long serialVersionUID = 1L; + + protected JButton[] launcherButtons; + + protected String[] buttonLabels = { + "Entropy", + "Mutual Info", + "Conditional Mutual Info", + "Active Info Storage", + "Transfer Entropy", + "Conditional Transfer Entropy" + }; + + @SuppressWarnings("rawtypes") + protected Class[] launcherClasses = { + AutoAnalyserEntropy.class, + AutoAnalyserMI.class, + AutoAnalyserCMI.class, + AutoAnalyserAIS.class, + AutoAnalyserTE.class, + AutoAnalyserCTE.class + }; + + protected String appletTitle = "JIDT AutoAnalyser Launcher"; + + /** + * Constructor to generate the application windows + */ + public AutoAnalyserLauncher() { + + // Build the swing applet + + ImageIcon icon = new ImageIcon("JIDT-logo.png"); // Location for distributions + if (icon.getImageLoadStatus() != MediaTracker.COMPLETE) { + // Try the alternative image location for git checkouts + icon = new ImageIcon("web/JIDT-logo.png"); + } + setIconImage(icon.getImage()); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(250,300); + setTitle(appletTitle); + // Centre in the middle of the screen + setLocationRelativeTo(null); + + // Add buttons to launch each AutoAnalyser: + launcherButtons = new JButton[buttonLabels.length]; + for (int i = 0; i < launcherButtons.length; i++) { + launcherButtons[i] = new JButton(buttonLabels[i]); + launcherButtons[i].addActionListener(this); + } + + // Add all the components in: + /* + add(calcTypePanel, BorderLayout.NORTH); + add(dataFileChooserPanel, BorderLayout.EAST); + add(dataFileDescriptorPanel, BorderLayout.WEST); + add(computeButton, BorderLayout.SOUTH); + */ + // gridbag.setConstraints(calcTypePanel, c); + // add(calcTypePanel); + JPanel calcButtonsPanel = new JPanel(); + calcButtonsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); + GridBagLayout gridbag = new GridBagLayout(); + GridBagConstraints c = new GridBagConstraints(); + calcButtonsPanel.setLayout(gridbag); + c.anchor = GridBagConstraints.CENTER; // Not sure what I put EAST for? + + // Add the buttons for each AutoAnalyser + c.gridwidth = GridBagConstraints.REMAINDER; //end row + c.fill = GridBagConstraints.BOTH; + c.weightx = 1.0; + JLabel textLabel = new JLabel("Select AutoAnalyser to launch:"); + textLabel.setSize(10, 10); + textLabel.setHorizontalAlignment(SwingConstants.CENTER); + calcButtonsPanel.add(textLabel, c); + JLabel dummyLabel = new JLabel(" "); + dummyLabel.setSize(10, 10); + dummyLabel.setHorizontalAlignment(SwingConstants.CENTER); + calcButtonsPanel.add(dummyLabel, c); + for (int i = 0; i < launcherButtons.length; i++) { + launcherButtons[i].setHorizontalAlignment(SwingConstants.CENTER); + calcButtonsPanel.add(launcherButtons[i], c); + JLabel dummyLabel1 = new JLabel(" "); + dummyLabel1.setSize(10, 10); + calcButtonsPanel.add(dummyLabel1, c); + } + + // Add all panels into the frame with Border layout + add(calcButtonsPanel, BorderLayout.WEST); + + setVisible(true); + + // The default tool tip delay before dismissing was too short to read these, so + // I'm setting it to 30 sec. + ToolTipManager.sharedInstance().setDismissDelay(30000); + + } + + @Override + public void actionPerformed(ActionEvent e) { + // Check which (if any) button the action came from + for (int i = 0; i < launcherButtons.length; i++) { + if (e.getSource() == launcherButtons[i]) { + try { + // Call the constructor for the relevant AutoAnalyser + // passing in the offset to the AutoAnalyser directory. + @SuppressWarnings({ "rawtypes", "unchecked" }) + Constructor cons = launcherClasses[i].getConstructor(String.class); + cons.newInstance("demos/AutoAnalyser/"); + } catch (Exception ex) { + JOptionPane.showMessageDialog(null, ex.getMessage(), + "Error launching " + buttonLabels[i] + " AutoAnalyser", + JOptionPane.ERROR_MESSAGE); + ex.printStackTrace(); + System.out.println(); + return; + } + // The AutoAnalyser started correctly, so we can close this launcher + dispose(); + } + } + // Else nothing extra to do + } + + /** + * @param args + */ + public static void main(String[] args) { + new AutoAnalyserLauncher(); + } +} diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserMI.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserMI.java index b62db45..60dd95f 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserMI.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserMI.java @@ -53,6 +53,14 @@ public class AutoAnalyserMI extends AutoAnalyserChannelCalculator protected static final String CALC_TYPE_KRASKOV_ALG1 = CALC_TYPE_KRASKOV + " alg. 1"; protected static final String CALC_TYPE_KRASKOV_ALG2 = CALC_TYPE_KRASKOV + " alg. 2"; + public AutoAnalyserMI() { + super(); + } + + public AutoAnalyserMI(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for MI */ diff --git a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserTE.java b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserTE.java index e7a03b2..c8d63ce 100644 --- a/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserTE.java +++ b/demos/java/infodynamics/demos/autoanalysis/AutoAnalyserTE.java @@ -54,6 +54,14 @@ public class AutoAnalyserTE extends AutoAnalyserChannelCalculator protected static final String DISCRETE_PROPNAME_L_TAU = "l_TAU"; protected static final String DISCRETE_PROPNAME_DELAY = "DELAY"; + public AutoAnalyserTE() { + super(); + } + + public AutoAnalyserTE(String pathToAutoAnalyserDir) { + super(pathToAutoAnalyserDir); + } + /** * Constructor to initialise the GUI for TE */