use of gdsc.smlm.ij.settings.ParameterSettings in project GDSC-SMLM by aherbert.
the class BatchPeakFit method setParameters.
/**
* Modify the XML document using the specified values for the given parameter. For each value
* call the method recursively for the next parameter. If there are no more parameters
* then add the XML document to the xmlSettings.
*
* @param parameters
* The list of parameters
* @param i
* Parameter to process
* @param doc
* The XML document containing all the current parameters
* @param xmlSettings
* A list of XML parameter settings
*/
private void setParameters(ArrayList<ParameterSettings> parameters, int i, Document doc, ArrayList<String> xmlSettings) {
if (i < parameters.size()) {
ParameterSettings param = parameters.get(i);
NodeList nodes = doc.getElementsByTagName(param.name);
if (nodes.getLength() == 1) {
// For each value, set the parameter and move to the next
String[] values = param.value.split(",");
for (String value : values) {
Node node = nodes.item(0);
node.setTextContent(value);
setParameters(parameters, i + 1, doc, xmlSettings);
}
} else {
// Just move to the next parameter
setParameters(parameters, i + 1, doc, xmlSettings);
}
} else {
// Add the final XML to the parameters to run
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
xmlSettings.add(writer.getBuffer().toString());
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
use of gdsc.smlm.ij.settings.ParameterSettings 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();
}
}
}
Aggregations