/** * Loads an experiment from a file. * * @param filename the file to load the experiment from * @return the experiment * @throws Exception if loading fails */ public static Experiment read(String filename) throws Exception { Experiment result; // KOML? if ((KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION))) { result = (Experiment) KOML.read(filename); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); result = (Experiment) xml.read(filename); } // binary else { FileInputStream fi = new FileInputStream(filename); ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(fi)); result = (Experiment) oi.readObject(); oi.close(); } return result; }
/** * Writes the experiment to disk. * * @param filename the file to write to * @param exp the experiment to save * @throws Exception if writing fails */ public static void write(String filename, Experiment exp) throws Exception { // KOML? if ((KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION))) { KOML.write(filename, exp); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); xml.write(filename, exp); } // binary else { FileOutputStream fo = new FileOutputStream(filename); ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream( fo)); oo.writeObject(exp); oo.close(); } }
/** * Validate the file format. After this bean is deserialized, classes for XML * serialization may not be in the classpath any more. * * @param ff the current file format to validate */ public Tag validateFileFormat(Tag ff) { Tag r = ff; if (ff.getID() == BINARY) { return ff; } if (ff.getID() == KOMLV && !KOML.isPresent()) { r = null; } if (ff.getID() == XSTREAM && !XStream.isPresent()) { r = null; } return r; }
/** * Loads an experiment from a file. * * @param filename the file to load the experiment from * @return the experiment * @throws Exception if loading fails */ public static Experiment read(String filename) throws Exception { Experiment result; // KOML? if ( (KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION)) ) { result = (Experiment) KOML.read(filename); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); result = (Experiment) xml.read(filename); } // binary else { FileInputStream fi = new FileInputStream(filename); ObjectInputStream oi = new ObjectInputStream( new BufferedInputStream(fi)); result = (Experiment)oi.readObject(); oi.close(); } return result; }
/** * Writes the experiment to disk. * * @param filename the file to write to * @param exp the experiment to save * @throws Exception if writing fails */ public static void write(String filename, Experiment exp) throws Exception { // KOML? if ( (KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION)) ) { KOML.write(filename, exp); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); xml.write(filename, exp); } // binary else { FileOutputStream fo = new FileOutputStream(filename); ObjectOutputStream oo = new ObjectOutputStream( new BufferedOutputStream(fo)); oo.writeObject(exp); oo.close(); } }
/** * Validate the file format. After this bean is deserialized, classes for * XML serialization may not be in the classpath any more. * * @param ff the current file format to validate */ public Tag validateFileFormat(Tag ff) { Tag r = ff; if (ff.getID() == BINARY) { return ff; } if (ff.getID() == KOMLV && !KOML.isPresent()) { r = null; } if (ff.getID() == XSTREAM && !XStream.isPresent()) { r = null; } return r; }
/** * Loads an experiment from a file. * * @param filename the file to load the experiment from * @return the experiment * @throws Exception if loading fails */ public static MekaExperiment read(String filename) throws Exception { MekaExperiment result; // KOML? if ((KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION))) { result = (MekaExperiment) KOML.read(filename); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); result = (MekaExperiment) xml.read(filename); } // binary else { FileInputStream fi = new FileInputStream(filename); ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(fi)); result = (MekaExperiment) oi.readObject(); oi.close(); } return result; }
/** * Writes the experiment to disk. * * @param filename the file to write to * @param exp the experiment to save * @throws Exception if writing fails */ public static void write(String filename, MekaExperiment exp) throws Exception { // KOML? if ((KOML.isPresent()) && (filename.toLowerCase().endsWith(KOML.FILE_EXTENSION))) { KOML.write(filename, exp); } // XML? else if (filename.toLowerCase().endsWith(".xml")) { XMLExperiment xml = new XMLExperiment(); xml.write(filename, exp); } // binary else { FileOutputStream fo = new FileOutputStream(filename); ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream( fo)); oo.writeObject(exp); oo.close(); } }
protected void setupFileChooser() { if (m_fileChooser == null) { m_fileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); } m_fileChooser.addChoosableFileFilter(m_binaryFilter); if (KOML.isPresent()) { m_fileChooser.addChoosableFileFilter(m_KOMLFilter); } if (XStream.isPresent()) { m_fileChooser.addChoosableFileFilter(m_XStreamFilter); } m_fileChooser.setFileFilter(m_binaryFilter); }
/** * Save a model in KOML deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model * (optional) */ public static void saveKOML(File saveTo, Object model, Instances header) throws Exception { Vector<Object> v = new Vector<Object>(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); KOML.write(saveTo.getAbsolutePath(), v); }
/** * Prompts the user to select an experiment file and loads it. */ private void openExperiment() { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment exp = Experiment.read(expFile.getAbsolutePath()); if (!setExperiment(exp)) { if (m_modePanel != null) m_modePanel.switchToAdvanced(exp); } System.err.println("Opened experiment:\n" + exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Couldn't open experiment file:\n" + expFile + "\nReason:\n" + ex.getMessage(), "Open Experiment", JOptionPane.ERROR_MESSAGE); // Pop up error dialog } }
/** * Prompts the user for a filename to save the experiment to, then saves * the experiment. */ private void saveExperiment() { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment.write(expFile.getAbsolutePath(), m_Exp); System.err.println("Saved experiment:\n" + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Couldn't save experiment file:\n" + expFile + "\nReason:\n" + ex.getMessage(), "Save Experiment", JOptionPane.ERROR_MESSAGE); } }
/** * Prompts the user to select an experiment file and loads it. */ private void openExperiment() { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) { expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) { expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) { expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } } try { Experiment exp = Experiment.read(expFile.getAbsolutePath()); setExperiment(exp); System.err.println("Opened experiment:\n" + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Couldn't open experiment file:\n" + expFile + "\nReason:\n" + ex.getMessage(), "Open Experiment", JOptionPane.ERROR_MESSAGE); // Pop up error dialog } }
/** * Prompts the user for a filename to save the experiment to, then saves the * experiment. */ private void saveExperiment() { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) { expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) { expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) { expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } } try { Experiment.write(expFile.getAbsolutePath(), m_Exp); System.err.println("Saved experiment:\n" + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Couldn't save experiment file:\n" + expFile + "\nReason:\n" + ex.getMessage(), "Save Experiment", JOptionPane.ERROR_MESSAGE); } }
/** * Helper routine to actually save the models. */ private void saveModel(String fileName, Instances trainHeader, Object model) { m_fileFormat = validateFileFormat(m_fileFormat); if (m_fileFormat == null) { // default to binary if validation fails m_fileFormat = s_fileFormatsAvailable.get(0); } try { switch (m_fileFormat.getID()) { case KOMLV: fileName = fileName + KOML.FILE_EXTENSION + FILE_EXTENSION; saveKOML(new File(fileName), model, trainHeader); break; case XSTREAM: fileName = fileName + XStream.FILE_EXTENSION + FILE_EXTENSION; saveXStream(new File(fileName), model, trainHeader); break; default: fileName = fileName + "." + FILE_EXTENSION; saveBinary(new File(fileName), model, trainHeader); break; } } catch (Exception ex) { stop(); // stop all processing System.err.println("[SerializedModelSaver] Problem saving model"); if (m_logger != null) { m_logger.logMessage("[SerializedModelSaver] " + statusMessagePrefix() + " Problem saving model"); m_logger.statusMessage(statusMessagePrefix() + "ERROR (See log for details)"); } } }
/** * Save a model in KOML deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model (optional) */ public static void saveKOML(File saveTo, Object model, Instances header) throws Exception { Vector v = new Vector(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); KOML.write(saveTo.getAbsolutePath(), v); }
/** * Prompts the user to select an experiment file and loads it. */ private void openExperiment() { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment exp = Experiment.read(expFile.getAbsolutePath()); setExperiment(exp); System.err.println("Opened experiment:\n" + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Couldn't open experiment file:\n" + expFile + "\nReason:\n" + ex.getMessage(), "Open Experiment", JOptionPane.ERROR_MESSAGE); // Pop up error dialog } }
/** * Helper routine to actually save the models. */ private void saveModel(String fileName, Instances trainHeader, Object model) { m_fileFormat = validateFileFormat(m_fileFormat); if (m_fileFormat == null) { // default to binary if validation fails m_fileFormat = s_fileFormatsAvailable.get(0); } try { switch (m_fileFormat.getID()) { case KOMLV: fileName = fileName + KOML.FILE_EXTENSION + FILE_EXTENSION; saveKOML(new File(fileName), model, trainHeader); break; case XSTREAM: fileName = fileName + XStream.FILE_EXTENSION + FILE_EXTENSION; saveXStream(new File(fileName), model, trainHeader); break; default: fileName = fileName + "." + FILE_EXTENSION; saveBinary(new File(fileName), model, trainHeader); break; } } catch (Exception ex) { System.err.println(Messages.getInstance().getString( "SerializedModelSaver_SaveModel_Error_Text")); if (m_logger != null) { m_logger.logMessage(Messages.getInstance().getString( "SerializedModelSaver_SaveModel_LogMessage_Text_First") + statusMessagePrefix() + Messages.getInstance().getString( "SerializedModelSaver_SaveModel_LogMessage_Text_Second") + " : " + ex.getMessage()); m_logger.statusMessage(statusMessagePrefix() + Messages.getInstance().getString( "SerializedModelSaver_SaveModel_StatusMessage_Text")); } } }
/** * Save a model in KOML deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model * (optional) */ public static void saveKOML(File saveTo, Object model, Instances header) throws Exception { Vector v = new Vector(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); KOML.write(saveTo.getAbsolutePath(), v); }
/** * Prompts the user to select an experiment file and loads it. */ private void openExperiment() { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment exp = Experiment.read(expFile.getAbsolutePath()); if (!setExperiment(exp)) { if (m_modePanel != null) m_modePanel.switchToAdvanced(exp); } System.err.println(Messages.getInstance().getString("SimpleSetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text") + exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, Messages.getInstance().getString("SimpleSetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_First") + expFile + Messages.getInstance().getString("SimpleSetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_Second") + ex.getMessage(), Messages.getInstance().getString("SimpleSetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_Third"), JOptionPane.ERROR_MESSAGE); // Pop up error dialog } }
/** * Prompts the user for a filename to save the experiment to, then saves * the experiment. */ private void saveExperiment() { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment.write(expFile.getAbsolutePath(), m_Exp); System.err.println(Messages.getInstance().getString("SimpleSetupPanel_SaveExperiment_Error_Text") + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, Messages.getInstance().getString("SimpleSetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_First") + expFile + Messages.getInstance().getString("SimpleSetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_Second") + ex.getMessage(), Messages.getInstance().getString("SimpleSetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_Third"), JOptionPane.ERROR_MESSAGE); } }
/** * Prompts the user to select an experiment file and loads it. */ private void openExperiment() { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment exp = Experiment.read(expFile.getAbsolutePath()); setExperiment(exp); System.err.println(Messages.getInstance().getString("SetupPanel_OpenExperiment_Error_Text") + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, Messages.getInstance().getString("SetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_First") + expFile + Messages.getInstance().getString("SetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_Second") + ex.getMessage(), Messages.getInstance().getString("SetupPanel_OpenExperiment_Exception_JOptionPaneShowMessageDialog_Text_Third"), JOptionPane.ERROR_MESSAGE); // Pop up error dialog } }
/** * Prompts the user for a filename to save the experiment to, then saves * the experiment. */ private void saveExperiment() { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File expFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_ExpFilter) { if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION)) expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION); } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!expFile.getName().toLowerCase().endsWith(".xml")) expFile = new File(expFile.getParent(), expFile.getName() + ".xml"); } try { Experiment.write(expFile.getAbsolutePath(), m_Exp); System.err.println(Messages.getInstance().getString("SetupPanel_SaveExperiment_Error_Text") + m_Exp); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, Messages.getInstance().getString("SetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_First") + expFile + Messages.getInstance().getString("SetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_Second") + ex.getMessage(), Messages.getInstance().getString("SetupPanel_SaveExperiment_Exception_JOptionPaneShowMessageDialog_Text_Third"), JOptionPane.ERROR_MESSAGE); } }