Skip to content

Commit

Permalink
better masking parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchard committed Dec 13, 2023
1 parent 1a1949c commit b6145ba
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/dr/app/beast/development_parsers.properties
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ dr.evomodel.treedatalikelihood.continuous.IntegratedFactorAnalysisLikelihoodPars
# Shrinkage
dr.inference.model.MaskFromTree
dr.inferencexml.distribution.shrinkage.JointBayesianBridgeStatisticsParser
dr.inferencexml.model.MaskingParser

# HMC
dr.inferencexml.hmc.CompoundPriorPreconditionerParser
Expand Down
3 changes: 1 addition & 2 deletions src/dr/inferencexml/model/MaskedParameterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public Object parseXMLObject(XMLObject xo) throws XMLParseException {
}
}
Logger.getLogger("dr.inferencexml.model").info("Found in total " + total + " missing values\n");
}
else {
} else {
int from = xo.getAttribute(FROM, 1) - 1;
int to = xo.getAttribute(TO, parameter.getDimension()) - 1;
int every = xo.getAttribute(EVERY, 1);
Expand Down
103 changes: 103 additions & 0 deletions src/dr/inferencexml/model/MaskingParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* MaskingParser.java
*
* Copyright (c) 2002-2023 Alexei Drummond, Andrew Rambaut and Marc Suchard
*
* This file is part of BEAST.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

package dr.inferencexml.model;

import dr.inference.model.Parameter;
import dr.xml.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Marc A. Suchard
*/
public class MaskingParser extends AbstractXMLObjectParser {

private static final String MASKING = "masking";
private static final String DIMENSION = "dimension";
private static final String NAME = "name";
private static final String COMPLEMENT = "complement";

public Object parseXMLObject(XMLObject xo) throws XMLParseException {

Parameter parameter = (Parameter) xo.getChild(Parameter.class);

List<String> names = new ArrayList<>();
for (XMLObject cxo : xo.getAllChildren(DIMENSION)) {
names.add(cxo.getStringAttribute(NAME));
}

Map<String,Integer> dimMap = new HashMap<>();
for (int i = 0; i < parameter.getDimension(); ++i) {
String n = parameter.getDimensionName(i);
dimMap.put(n, i);
}

for (String name : names) {
if (dimMap.containsKey(name)) {
int index = dimMap.get(name);
parameter.setParameterValue(index, 1);
} else {
throw new XMLParseException("Dimension '" + name + "' not found");

}
}

if (xo.getAttribute(COMPLEMENT, false)) {
for (int i = 0; i < parameter.getDimension(); ++i) {
parameter.setParameterValue(i,1 - parameter.getParameterValue(i));
}
}

return parameter;
}

public XMLSyntaxRule[] getSyntaxRules() {
return rules;
}

private final XMLSyntaxRule[] rules = {
new ElementRule(Parameter.class),
AttributeRule.newBooleanRule(COMPLEMENT, true),
new ElementRule(DIMENSION, new XMLSyntaxRule[]{
AttributeRule.newStringRule(NAME),
}, 1, Integer.MAX_VALUE),
};

public String getParserDescription() {
return "A binary masking as a parameter.";
}

public Class getReturnType() {
return Parameter.class;
}

public String getParserName() {
return MASKING;
}
}

0 comments on commit b6145ba

Please sign in to comment.