Search in sources :

Example 41 with XMLParser

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);
}
Also used : LogNormalDistributionModel(beast.math.distributions.LogNormalDistributionModel) RealParameter(beast.core.parameter.RealParameter) XMLParser(beast.util.XMLParser) Test(org.junit.Test)

Example 42 with XMLParser

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());
        ;
    }
}
Also used : ArrayList(java.util.ArrayList) MCMC(beast.core.MCMC) FilenameFilter(java.io.FilenameFilter) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 43 with XMLParser

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);
}
Also used : MCMC(beast.core.MCMC) XMLParser(beast.util.XMLParser) File(java.io.File) Test(org.junit.Test)

Example 44 with XMLParser

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());
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) XMLProducer(beast.util.XMLProducer) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 45 with XMLParser

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());
}
Also used : XMLProducer(beast.util.XMLProducer) Taxon(beast.evolution.alignment.Taxon) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File) Test(org.junit.Test)

Aggregations

XMLParser (beast.util.XMLParser)46 File (java.io.File)36 ArrayList (java.util.ArrayList)29 Test (org.junit.Test)27 Expectation (test.beast.beast2vs1.trace.Expectation)23 LogAnalyser (test.beast.beast2vs1.trace.LogAnalyser)23 BEASTInterface (beast.core.BEASTInterface)11 XMLProducer (beast.util.XMLProducer)5 IOException (java.io.IOException)5 FilenameFilter (java.io.FilenameFilter)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 SAXException (org.xml.sax.SAXException)4 MCMC (beast.core.MCMC)3 Alignment (beast.evolution.alignment.Alignment)3 XMLParserException (beast.util.XMLParserException)3 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 Input (beast.core.Input)2 StateNode (beast.core.StateNode)2 CompoundDistribution (beast.core.util.CompoundDistribution)2