Search in sources :

Example 1 with LogFileTraces

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

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

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

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

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

the class LocationsPanel method removeTraceList.

private void removeTraceList() {
    int[] selRows = traceTable.getSelectedRows();
    LogFileTraces[] tls = new LogFileTraces[selRows.length];
    int i = 0;
    for (int row : selRows) {
        tls[i] = traceLists.get(row);
        i++;
    }
    for (LogFileTraces tl : tls) {
        traceLists.remove(tl);
    }
    traceTableModel.fireTableDataChanged();
    if (traceLists.size() == 0) {
        frame.getDeleteItemAction().setEnabled(false);
    //            setAnalysesEnabled(false);
    //            layerTableModel.fireTableDataChanged();
    }
    if (traceLists.size() > 0) {
        int row = selRows[0];
        if (row >= traceLists.size()) {
            row = traceLists.size() - 1;
        }
        traceTable.getSelectionModel().addSelectionInterval(row, row);
    }
//        setupDividerLocation();
}
Also used : LogFileTraces(dr.inference.trace.LogFileTraces)

Aggregations

LogFileTraces (dr.inference.trace.LogFileTraces)11 TraceException (dr.inference.trace.TraceException)7 File (java.io.File)5 MarginalLikelihoodAnalysis (dr.inference.trace.MarginalLikelihoodAnalysis)4 FileNotFoundException (java.io.FileNotFoundException)4 Distribution (dr.math.distributions.Distribution)1 XMLObject (dr.xml.XMLObject)1 XMLParseException (dr.xml.XMLParseException)1 IOException (java.io.IOException)1 List (java.util.List)1 EmpiricalDemographicFunction (jebl.evolution.coalescent.EmpiricalDemographicFunction)1 ImportException (jebl.evolution.io.ImportException)1 NewickExporter (jebl.evolution.io.NewickExporter)1 NexusImporter (jebl.evolution.io.NexusImporter)1 TreeImporter (jebl.evolution.io.TreeImporter)1 RootedTree (jebl.evolution.trees.RootedTree)1 CoalescentIntervalGenerator (jebl.evolution.treesimulation.CoalescentIntervalGenerator)1 IntervalGenerator (jebl.evolution.treesimulation.IntervalGenerator)1 TreeSimulator (jebl.evolution.treesimulation.TreeSimulator)1