use of beast.util.XMLParser in project beast2 by CompEvol.
the class LogNormalDistributionModelTest method testCalcLogP2.
@Test
public void testCalcLogP2() throws Exception {
// does the same as testCalcLogP(), but with by constructing object through XML
String xml = "<input spec='beast.math.distributions.LogNormalDistributionModel' " + "offset='1200' " + "M='2000' " + "S='0.6' " + "meanInRealSpace='true'/>";
RealParameter p = new RealParameter(new Double[] { 2952.6747000000014 });
XMLParser parser = new XMLParser();
LogNormalDistributionModel logNormal = (LogNormalDistributionModel) parser.parseBareFragment(xml, true);
double f0 = logNormal.calcLogP(p);
assertEquals(-7.880210654973873, f0, 1e-10);
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class ExampleXmlParsingTest method test_ThatXmlExamplesRun.
public void test_ThatXmlExamplesRun(String dir) {
try {
Logger.FILE_MODE = Logger.LogFileMode.overwrite;
System.out.println("Test that XML Examples run in " + dir);
File exampleDir = new File(dir);
String[] exampleFiles = exampleDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
});
List<String> failedFiles = new ArrayList<String>();
int seed = 127;
for (String fileName : exampleFiles) {
Randomizer.setSeed(seed);
// need more than one to prevent trouble with multiMCMC logs
seed += 10;
System.out.println("Processing " + fileName);
XMLParser parser = new XMLParser();
try {
beast.core.Runnable runable = parser.parseFile(new File(dir + "/" + fileName));
if (runable instanceof MCMC) {
MCMC mcmc = (MCMC) runable;
mcmc.setInputValue("preBurnin", 0);
mcmc.setInputValue("chainLength", 1000l);
mcmc.run();
}
} catch (Exception e) {
System.out.println("ExampleXmlParsing::Failed for " + fileName + ": " + e.getMessage());
failedFiles.add(fileName);
}
System.out.println("Done " + fileName);
}
if (failedFiles.size() > 0) {
System.out.println("\ntest_ThatXmlExamplesRun::Failed for : " + failedFiles.toString());
} else {
System.out.println("SUCCESS!!!");
}
assertTrue(failedFiles.toString(), failedFiles.size() == 0);
} catch (Exception e) {
System.out.println("exception thrown ");
System.out.println(e.getMessage());
;
}
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class ResumeTest method test_ThatXmlExampleResumes.
@Test
public void test_ThatXmlExampleResumes() throws Exception {
Randomizer.setSeed(127);
Logger.FILE_MODE = Logger.LogFileMode.overwrite;
String dir = System.getProperty("user.dir") + "/examples";
String fileName = dir + "/" + XML_FILE;
System.out.println("Processing " + fileName);
XMLParser parser = new XMLParser();
beast.core.Runnable runable = parser.parseFile(new File(fileName));
runable.setStateFile("tmp.state", false);
if (runable instanceof MCMC) {
MCMC mcmc = (MCMC) runable;
mcmc.setInputValue("preBurnin", 0);
mcmc.setInputValue("chainLength", 1000l);
mcmc.run();
}
System.out.println("Done " + fileName);
System.out.println("Resuming " + fileName);
Logger.FILE_MODE = Logger.LogFileMode.resume;
parser = new XMLParser();
runable = parser.parseFile(new File(fileName));
runable.setStateFile("tmp.state", true);
if (runable instanceof MCMC) {
MCMC mcmc = (MCMC) runable;
mcmc.setInputValue("preBurnin", 0);
mcmc.setInputValue("chainLength", 1000l);
mcmc.run();
}
System.out.println("Done " + fileName);
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class XMLProducerTest method test_ThatXmlExamplesProduces.
/**
* parse all XML files in the given directory, then produce XML from it, and see if the produced XML still parses *
*/
public void test_ThatXmlExamplesProduces(String dir, List<String> exceptions) {
try {
Randomizer.setSeed(127);
Logger.FILE_MODE = Logger.LogFileMode.overwrite;
System.out.println("Test XML Examples in " + dir);
File exampleDir = new File(dir);
String[] exampleFiles = exampleDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
});
List<String> failedFiles = new ArrayList<String>();
for (String fileName : exampleFiles) {
if (exceptions.contains(fileName)) {
System.out.println("Skipping exception " + fileName);
} else {
System.out.println("Processing " + fileName);
XMLProducer producer = new XMLProducer();
BEASTInterface o = null;
try {
o = producer.parseFile(new File(dir + "/" + fileName));
} catch (Exception e) {
o = null;
}
if (o != null) {
String xml = producer.toXML(o);
// FileWriter outfile = new FileWriter(new File("/tmp/XMLProducerTest.xml"));
// outfile.write(xml);
// outfile.close();
XMLParser parser2 = new XMLParser();
try {
parser2.parseFragment(xml, false);
} catch (Exception e) {
System.out.println("test_ThatXmlExamplesProduces::Failed for " + fileName + ": " + e.getMessage());
failedFiles.add(fileName);
}
}
System.out.println("Done " + fileName);
}
}
if (failedFiles.size() > 0) {
System.out.println("\ntest_ThatXmlExamplesProduces::Failed for : " + failedFiles.toString());
} else {
System.out.println("\ntest_ThatXmlExamplesProduces::Success");
}
assertTrue(failedFiles.toString(), failedFiles.size() == 0);
} catch (Exception e) {
System.out.println("exception thrown ");
System.out.println(e.getMessage());
}
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class XMLTest method testAnnotatedConstructor2.
@Test
public void testAnnotatedConstructor2() throws Exception {
List<Taxon> taxa = new ArrayList<>();
taxa.add(new Taxon("first one"));
taxa.add(new Taxon("second one"));
AnnotatedRunnableTestClass t = new AnnotatedRunnableTestClass(3, taxa);
XMLProducer producer = new XMLProducer();
String xml = producer.toXML(t);
assertEquals(3, (int) t.getParam1());
FileWriter outfile = new FileWriter(new File("/tmp/XMLTest.xml"));
outfile.write(xml);
outfile.close();
XMLParser parser = new XMLParser();
BEASTInterface b = parser.parseFile(new File("/tmp/XMLTest.xml"));
assertEquals(3, (int) ((AnnotatedRunnableTestClass) b).getParam1());
assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
}
Aggregations