Search in sources :

Example 1 with MarginalLikelihoodAnalysis

use of dr.inference.trace.MarginalLikelihoodAnalysis in project beast-mcmc by beast-dev.

the class AICMAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String fileName = xo.getStringAttribute(FILE_NAME);
    try {
        // Open file
        File file = new File(fileName);
        String name = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, name);
        fileName = file.getAbsolutePath();
        // Set analysisType
        String analysisType = "aicm";
        // Set bootstrapLength
        int bootstrapLength = xo.getAttribute(BOOTSTRAP_LENGTH, 1000);
        // Load traces and remove burnin
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();
        long burnin = xo.getAttribute(BURN_IN, maxState / 10);
        if (burnin < 0 || burnin >= maxState) {
            burnin = maxState / 10;
            System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
        }
        traces.setBurnIn(burnin);
        // Find likelihood column
        XMLObject cxo = xo.getChild(COLUMN_NAME);
        String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
        int traceIndex = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.equals(likelihoodName)) {
                traceIndex = i;
                break;
            }
        }
        if (traceIndex == -1) {
            throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
        }
        // Get samples and perform analysis
        List<Double> sample = traces.getValues(traceIndex);
        MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample, traces.getTraceName(traceIndex), (int) burnin, analysisType, bootstrapLength);
        System.out.println(analysis.toString());
        return analysis;
    } catch (FileNotFoundException fnfe) {
        throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (TraceException e) {
        throw new XMLParseException(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) MarginalLikelihoodAnalysis(dr.inference.trace.MarginalLikelihoodAnalysis) File(java.io.File)

Example 2 with MarginalLikelihoodAnalysis

use of dr.inference.trace.MarginalLikelihoodAnalysis in project beast-mcmc by beast-dev.

the class ArithmeticMeanAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String fileName = xo.getStringAttribute(FILE_NAME);
    try {
        // Open file
        File file = new File(fileName);
        String name = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, name);
        fileName = file.getAbsolutePath();
        // Set analysisType
        String analysisType = "arithmetic";
        // Set bootstrapLength
        int bootstrapLength = xo.getAttribute(BOOTSTRAP_LENGTH, 1000);
        // Load traces and remove burnin
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();
        long burnin = xo.getAttribute(BURN_IN, maxState / 10);
        if (burnin < 0 || burnin >= maxState) {
            burnin = maxState / 10;
            System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
        }
        traces.setBurnIn(burnin);
        // Find likelihood column
        XMLObject cxo = xo.getChild(COLUMN_NAME);
        String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
        int traceIndex = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.equals(likelihoodName)) {
                traceIndex = i;
                break;
            }
        }
        if (traceIndex == -1) {
            throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
        }
        // Get samples and perform analysis
        List<Double> sample = traces.getValues(traceIndex);
        MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample, traces.getTraceName(traceIndex), (int) burnin, analysisType, bootstrapLength);
        System.out.println(analysis.toString());
        return analysis;
    } catch (FileNotFoundException fnfe) {
        throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (TraceException e) {
        throw new XMLParseException(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) XMLObject(dr.xml.XMLObject) TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) MarginalLikelihoodAnalysis(dr.inference.trace.MarginalLikelihoodAnalysis) XMLParseException(dr.xml.XMLParseException) File(java.io.File)

Example 3 with MarginalLikelihoodAnalysis

use of dr.inference.trace.MarginalLikelihoodAnalysis in project beast-mcmc by beast-dev.

the class HarmonicMeanAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String fileName = xo.getStringAttribute(FILE_NAME);
    try {
        // Open file
        File file = new File(fileName);
        String name = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, name);
        fileName = file.getAbsolutePath();
        // Set analysisType
        String analysisType = "harmonic";
        boolean smoothedEstimate = false;
        if (xo.hasAttribute(SMOOTHED_ESTIMATE))
            smoothedEstimate = xo.getBooleanAttribute(SMOOTHED_ESTIMATE);
        if (smoothedEstimate)
            analysisType = "smoothed";
        // Set bootstrapLength
        int bootstrapLength = xo.getAttribute(BOOTSTRAP_LENGTH, 1000);
        // Load traces and remove burnin
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();
        long burnin = xo.getAttribute(BURN_IN, maxState / 10);
        if (burnin < 0 || burnin >= maxState) {
            burnin = (int) (maxState / 10);
            System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
        }
        traces.setBurnIn(burnin);
        // Find likelihood column
        XMLObject cxo = xo.getChild(COLUMN_NAME);
        String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
        int traceIndex = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.equals(likelihoodName)) {
                traceIndex = i;
                break;
            }
        }
        if (traceIndex == -1) {
            throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
        }
        // Get samples and perform analysis
        List<Double> sample = traces.getValues(traceIndex);
        MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample, traces.getTraceName(traceIndex), (int) burnin, analysisType, bootstrapLength);
        System.out.println(analysis.toString());
        return analysis;
    } catch (FileNotFoundException fnfe) {
        throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (TraceException e) {
        throw new XMLParseException(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) MarginalLikelihoodAnalysis(dr.inference.trace.MarginalLikelihoodAnalysis) File(java.io.File)

Example 4 with MarginalLikelihoodAnalysis

use of dr.inference.trace.MarginalLikelihoodAnalysis in project beast-mcmc by beast-dev.

the class MarginalLikelihoodAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String fileName = xo.getStringAttribute(FILE_NAME);
    try {
        File file = new File(fileName);
        String name = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, name);
        fileName = file.getAbsolutePath();
        XMLObject cxo = xo.getChild(COLUMN_NAME);
        String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();
        // leaving the burnin attribute off will result in 10% being used
        long burnin = xo.getAttribute(BURN_IN, maxState / 10);
        if (burnin < 0 || burnin >= maxState) {
            burnin = maxState / 10;
            System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
        }
        traces.setBurnIn(burnin);
        int traceIndex = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.equals(likelihoodName)) {
                traceIndex = i;
                break;
            }
        }
        if (traceIndex == -1) {
            throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
        }
        // Set analysisType
        String analysisType = "smoothed";
        boolean harmonicOnly = false;
        if (xo.hasAttribute(ONLY_HARMONIC))
            harmonicOnly = xo.getBooleanAttribute(ONLY_HARMONIC);
        if (harmonicOnly)
            analysisType = "harmonic";
        int bootstrapLength = cxo.getAttribute(BOOTSTRAP_LENGTH, 1000);
        List<Double> sample = traces.getValues(traceIndex);
        MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample, traces.getTraceName(traceIndex), (int) burnin, analysisType, bootstrapLength);
        System.out.println(analysis.toString());
        return analysis;
    } catch (FileNotFoundException fnfe) {
        throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (TraceException e) {
        throw new XMLParseException(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) MarginalLikelihoodAnalysis(dr.inference.trace.MarginalLikelihoodAnalysis) File(java.io.File)

Aggregations

LogFileTraces (dr.inference.trace.LogFileTraces)4 MarginalLikelihoodAnalysis (dr.inference.trace.MarginalLikelihoodAnalysis)4 TraceException (dr.inference.trace.TraceException)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 XMLObject (dr.xml.XMLObject)1 XMLParseException (dr.xml.XMLParseException)1