Search in sources :

Example 1 with TraceException

use of dr.inference.trace.TraceException 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 TraceException

use of dr.inference.trace.TraceException 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 TraceException

use of dr.inference.trace.TraceException 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 TraceException

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

the class ParameterParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double[] values = null;
    double[] uppers;
    double[] lowers;
    if (xo.hasAttribute(DIMENSION)) {
        values = new double[xo.getIntegerAttribute(DIMENSION)];
    }
    if (xo.hasAttribute(FILENAME)) {
        //read samples from a file (used for a reference prior) and calculate the mean
        String fileName = xo.getStringAttribute(FILENAME);
        File file = new File(fileName);
        fileName = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, fileName);
        fileName = file.getAbsolutePath();
        String columnName = "";
        if (xo.hasAttribute(PARAMETERCOLUMN)) {
            columnName = xo.getStringAttribute(PARAMETERCOLUMN);
        } else {
            throw new XMLParseException("when providing a file name you must provide a parameter column as well");
        }
        //if a number for the burnin is not given, use 0 ...
        int burnin;
        if (xo.hasAttribute(BURNIN)) {
            burnin = xo.getIntegerAttribute(BURNIN);
        } else {
            burnin = 0;
        }
        LogFileTraces traces = new LogFileTraces(fileName, file);
        List parameterSamples = null;
        try {
            traces.loadTraces();
            traces.setBurnIn(burnin);
            int traceIndexParameter = -1;
            for (int i = 0; i < traces.getTraceCount(); i++) {
                String traceName = traces.getTraceName(i);
                if (traceName.trim().equals(columnName)) {
                    traceIndexParameter = i;
                }
            }
            if (traceIndexParameter == -1) {
                throw new XMLParseException("Column '" + columnName + "' can not be found for " + getParserName() + " element.");
            }
            parameterSamples = traces.getValues(traceIndexParameter);
        } catch (TraceException e) {
            throw new XMLParseException(e.getMessage());
        } catch (IOException ioe) {
            throw new XMLParseException(ioe.getMessage());
        }
        values = new double[1];
        for (int i = 0, stop = parameterSamples.size(); i < stop; i++) {
            values[0] += ((Double) parameterSamples.get(i)) / ((double) stop);
        }
        System.out.println("Number of samples: " + parameterSamples.size());
        System.out.println("Parameter mean: " + values[0]);
    } else if (xo.hasAttribute(VALUE)) {
        if (values == null) {
            values = xo.getDoubleArrayAttribute(VALUE);
        } else {
            double[] v = xo.getDoubleArrayAttribute(VALUE);
            if (v.length == values.length) {
                System.arraycopy(v, 0, values, 0, v.length);
            } else if (v.length == 1) {
                for (int i = 0; i < values.length; i++) {
                    values[i] = v[0];
                }
            } else {
                throw new XMLParseException("value string must have 1 value or dimension values");
            }
        }
    } else {
        if (xo.hasAttribute(DIMENSION)) {
            values = new double[xo.getIntegerAttribute(DIMENSION)];
        } else {
            // parameter dimension will get set correctly by TreeModel presumably.
            if (!xo.hasChildNamed(RANDOMIZE)) {
                return new Parameter.Default(1);
            }
            values = new double[1];
            values[0] = 1.0;
        }
    }
    uppers = new double[values.length];
    for (int i = 0; i < values.length; i++) {
        uppers[i] = Double.POSITIVE_INFINITY;
    }
    lowers = new double[values.length];
    for (int i = 0; i < values.length; i++) {
        lowers[i] = Double.NEGATIVE_INFINITY;
    }
    if (xo.hasAttribute(UPPER)) {
        double[] v = xo.getDoubleArrayAttribute(UPPER);
        if (v.length == uppers.length) {
            System.arraycopy(v, 0, uppers, 0, v.length);
        } else if (v.length == 1) {
            for (int i = 0; i < uppers.length; i++) {
                uppers[i] = v[0];
            }
        } else {
            throw new XMLParseException("uppers string must have 1 value or dimension values");
        }
    }
    if (xo.hasAttribute(LOWER)) {
        double[] v = xo.getDoubleArrayAttribute(LOWER);
        if (v.length == lowers.length) {
            System.arraycopy(v, 0, lowers, 0, v.length);
        } else if (v.length == 1) {
            for (int i = 0; i < lowers.length; i++) {
                lowers[i] = v[0];
            }
        } else {
            throw new XMLParseException("lowers string must have 1 value or dimension values");
        }
    }
    if ((uppers.length != values.length)) {
        throw new XMLParseException("value and upper limit strings have different dimension, in parameter");
    }
    if ((lowers.length != values.length)) {
        throw new XMLParseException("value and lower limit strings have different dimension, in parameter");
    }
    // check if uppers and lowers are consistent
    for (int i = 0; i < values.length; i++) {
        if (uppers[i] < lowers[i]) {
            throw new XMLParseException("upper is lower than lower, in parameter");
        }
    }
    if (xo.hasChildNamed(RANDOMIZE)) {
        Distribution distribution = (Distribution) xo.getChild(RANDOMIZE).getChild(Distribution.class);
        for (int i = 0; i < values.length; i++) {
            do {
                // Not an efficient way to draw random variables, but this is currently the only general interface
                values[i] = distribution.quantile(MathUtils.nextDouble());
            } while (values[i] < lowers[i] || values[i] > uppers[i]);
        }
    } else {
        // make values consistent with bounds
        for (int i = 0; i < values.length; i++) {
            if (uppers[i] < values[i])
                values[i] = uppers[i];
        }
        for (int i = 0; i < values.length; i++) {
            if (lowers[i] > values[i])
                values[i] = lowers[i];
        }
    }
    Parameter param = new Parameter.Default(values.length);
    for (int i = 0; i < values.length; i++) {
        param.setParameterValue(i, values[i]);
    }
    param.addBounds(new Parameter.DefaultBounds(uppers, lowers));
    return param;
}
Also used : IOException(java.io.IOException) TraceException(dr.inference.trace.TraceException) Distribution(dr.math.distributions.Distribution) LogFileTraces(dr.inference.trace.LogFileTraces) List(java.util.List) File(java.io.File)

Example 5 with TraceException

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

the class EBSPAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    try {
        // 10% is brun-in default
        final double burnin = xo.getAttribute(BURN_IN, 0.1);
        if (burnin < 0) {
            throw new XMLParseException("burnIn should be either between 0 and 1 or a positive number");
        }
        final double[] hpdLevels = xo.hasAttribute(HPD_LEVELS) ? xo.getDoubleArrayAttribute(HPD_LEVELS) : null;
        final File log = FileHelpers.getFile(getElementText(xo, LOG_FILE_NAME));
        final XMLObject treeFileNames = xo.getChild(TREE_FILE_NAMES);
        final int nTrees = treeFileNames.getChildCount();
        File[] treeFiles = new File[nTrees];
        for (int k = 0; k < nTrees; ++k) {
            treeFiles[k] = FileHelpers.getFile(((XMLObject) treeFileNames.getChild(k)).getStringChild(0));
        }
        String modelTypeName = getElementText(xo, MODEL_TYPE).trim().toUpperCase();
        String populationFirstColumn = getElementText(xo, POPULATION_FIRST_COLUMN);
        String indicatorsFirstColumn = getElementText(xo, INDICATORS_FIRST_COLUMN);
        VariableDemographicModel.Type modelType = VariableDemographicModel.Type.valueOf(modelTypeName);
        String rootHeightColumn = null;
        int nBins = -1;
        if (xo.hasAttribute(NBINS)) {
            if (xo.getChild(ROOTHEIGHT_COLUMN) != null) {
                rootHeightColumn = getElementText(xo, ROOTHEIGHT_COLUMN);
                nBins = xo.getIntegerAttribute(NBINS);
            }
        }
        PrintWriter allDemoWriter = null;
        if (xo.getChild(ALLDEMO_COLUMN) != null) {
            String fName = getElementText(xo, ALLDEMO_COLUMN);
            allDemoWriter = new PrintWriter(new FileWriter(fName));
        }
        final boolean quantiles = xo.getAttribute(QUANTILES, false);
        final boolean logSpace = xo.getAttribute(LOG_SPACE, false);
        final boolean useMid = xo.getAttribute(USE_MIDDLE, false);
        final int onlyNchanges = xo.getAttribute(N_CHANGES, -1);
        return new EBSPAnalysis(log, treeFiles, modelType, populationFirstColumn, indicatorsFirstColumn, rootHeightColumn, nBins, burnin, hpdLevels, quantiles, logSpace, useMid, onlyNchanges, allDemoWriter);
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (Importer.ImportException e) {
        throw new XMLParseException(e.toString());
    } catch (TraceException e) {
        throw new XMLParseException(e.toString());
    }
}
Also used : EBSPAnalysis(dr.evomodel.coalescent.EBSPAnalysis) VariableDemographicModel(dr.evomodel.coalescent.VariableDemographicModel) FileWriter(java.io.FileWriter) TraceException(dr.inference.trace.TraceException) File(java.io.File) PrintWriter(java.io.PrintWriter) Importer(dr.evolution.io.Importer)

Aggregations

TraceException (dr.inference.trace.TraceException)11 File (java.io.File)8 LogFileTraces (dr.inference.trace.LogFileTraces)7 MarginalLikelihoodAnalysis (dr.inference.trace.MarginalLikelihoodAnalysis)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)3 Importer (dr.evolution.io.Importer)2 ImportException (jebl.evolution.io.ImportException)2 Arguments (dr.app.util.Arguments)1 EBSPAnalysis (dr.evomodel.coalescent.EBSPAnalysis)1 GPSkytrackAnalysis (dr.evomodel.coalescent.GPSkytrackAnalysis)1 VariableDemographicModel (dr.evomodel.coalescent.VariableDemographicModel)1 Parameter (dr.inference.model.Parameter)1 Distribution (dr.math.distributions.Distribution)1 Variate (dr.stats.Variate)1 XMLObject (dr.xml.XMLObject)1 XMLParseException (dr.xml.XMLParseException)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1