Search in sources :

Example 1 with BatchSettings

use of gdsc.smlm.ij.settings.BatchSettings in project GDSC-SMLM by aherbert.

the class BatchPeakFit method loadSettings.

private BatchSettings loadSettings(String configurationFilename) {
    BatchSettings settings = null;
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(configurationFilename);
        settings = (BatchSettings) xs.fromXML(fs);
    } catch (ClassCastException ex) {
    //ex.printStackTrace();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (XStreamException ex) {
        ex.printStackTrace();
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return settings;
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) BatchSettings(gdsc.smlm.ij.settings.BatchSettings) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 2 with BatchSettings

use of gdsc.smlm.ij.settings.BatchSettings in project GDSC-SMLM by aherbert.

the class BatchPeakFit method itemStateChanged.

/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
public void itemStateChanged(ItemEvent e) {
    // When the checkbox is clicked, create a default configuration file and update the
    // GenericDialog with the file location.		
    Checkbox cb = (Checkbox) e.getSource();
    if (cb.getState()) {
        cb.setState(false);
        Document doc = getDefaultSettingsXmlDocument();
        if (doc == null)
            return;
        try {
            // Look for nodes that are part of the fit configuration
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//gdsc.smlm.engine.FitEngineConfiguration//*");
            // For each node, add the name and value to the BatchParameters
            BatchSettings batchSettings = new BatchSettings();
            batchSettings.resultsDirectory = System.getProperty("java.io.tmpdir");
            batchSettings.images.add("/path/to/image.tif");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (// Only nodes with a single text entry
                node.getChildNodes().getLength() == 1) {
                    batchSettings.parameters.add(new ParameterSettings(node.getNodeName(), node.getTextContent()));
                }
            }
            // Save the settings file
            String[] path = Utils.decodePath(configFilenameText.getText());
            OpenDialog chooser = new OpenDialog("Settings_file", path[0], path[1]);
            if (chooser.getFileName() != null) {
                String newFilename = chooser.getDirectory() + chooser.getFileName();
                if (!newFilename.endsWith(".xml"))
                    newFilename += ".xml";
                FileOutputStream fs = null;
                try {
                    fs = new FileOutputStream(newFilename);
                    xs.toXML(batchSettings, fs);
                } finally {
                    if (fs != null) {
                        fs.close();
                    }
                }
                // Update dialog filename
                configFilenameText.setText(newFilename);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) ParameterSettings(gdsc.smlm.ij.settings.ParameterSettings) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) XStreamException(com.thoughtworks.xstream.XStreamException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OpenDialog(ij.io.OpenDialog) XPathFactory(javax.xml.xpath.XPathFactory) BatchSettings(gdsc.smlm.ij.settings.BatchSettings) Checkbox(java.awt.Checkbox) FileOutputStream(java.io.FileOutputStream)

Example 3 with BatchSettings

use of gdsc.smlm.ij.settings.BatchSettings in project GDSC-SMLM by aherbert.

the class BatchPeakFit method runBatch.

/**
	 * Reads the batch configuration file. For each parameter variation, create a fitting configuration. Then run the
	 * fit engine on each input image using each configuration.
	 * 
	 * @param configurationFilename
	 */
private void runBatch(String configurationFilename) {
    BatchSettings settings = loadSettings(configurationFilename);
    if (settings == null || settings.parameters.isEmpty()) {
        IJ.log("No settings for the fitting engine");
        return;
    }
    if (!new File(settings.resultsDirectory).exists()) {
        if (!new File(settings.resultsDirectory).mkdirs()) {
            IJ.log("Unable to create the results directory: " + settings.resultsDirectory);
            return;
        }
    }
    Document doc = getDefaultSettingsXmlDocument();
    if (doc == null)
        return;
    // Create XML for each variation
    ArrayList<String> xmlSettings = new ArrayList<String>();
    setParameters(settings.parameters, 0, doc, xmlSettings);
    // Run all the variants on the input images
    for (String imageFilename : settings.images) {
        ImagePlus imp = IJ.openImage(imageFilename);
        if (imp == null) {
            IJ.log("Unable to load image: " + imageFilename);
            continue;
        }
        processImage(settings, imp, xmlSettings);
    }
}
Also used : BatchSettings(gdsc.smlm.ij.settings.BatchSettings) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) File(java.io.File) ImagePlus(ij.ImagePlus)

Aggregations

BatchSettings (gdsc.smlm.ij.settings.BatchSettings)3 XStreamException (com.thoughtworks.xstream.XStreamException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Document (org.w3c.dom.Document)2 ParameterSettings (gdsc.smlm.ij.settings.ParameterSettings)1 ImagePlus (ij.ImagePlus)1 OpenDialog (ij.io.OpenDialog)1 Checkbox (java.awt.Checkbox)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathFactory (javax.xml.xpath.XPathFactory)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1