Skip to content

Commit

Permalink
move addDeltaExchangeOperator to the same class creating all operators
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Jan 17, 2024
1 parent 3d92c80 commit e6b7cce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import beast.base.core.BEASTInterface;
import beast.base.core.BEASTObject;
import beast.base.core.Function;
import beast.base.evolution.operator.kernel.BactrianScaleOperator;
import beast.base.evolution.tree.Tree;
import beast.base.inference.Operator;
Expand All @@ -17,10 +18,12 @@
import com.google.common.collect.Multimap;
import lphy.base.distribution.Dirichlet;
import lphy.base.distribution.RandomComposition;
import lphy.base.distribution.WeightedDirichlet;
import lphy.core.logger.LoggerUtils;
import lphy.core.model.GenerativeDistribution;
import lphy.core.model.GraphicalModelNode;
import lphy.core.model.RandomVariable;
import lphy.core.model.Value;
import lphy.core.vectorization.IID;
import lphybeast.BEASTContext;

Expand Down Expand Up @@ -195,7 +198,7 @@ public static void addUpDownOperator(Tree tree, RealParameter clockRate, BEASTCo
String idStr = clockRate.getID() + "Up" + tree.getID() + "DownOperator";
// avoid to duplicate updown ops from the same pair of rate and tree
if (!context.hasExtraOperator(idStr)) {
BactrianUpDownOperator upDownOperator = new BactrianUpDownOperator();
Operator upDownOperator = new BactrianUpDownOperator();
upDownOperator.setID(idStr);
upDownOperator.setInputValue("up", clockRate);
upDownOperator.setInputValue("down", tree);
Expand All @@ -204,4 +207,18 @@ public static void addUpDownOperator(Tree tree, RealParameter clockRate, BEASTCo
context.addExtraOperator(upDownOperator);
}
}

public static void addDeltaExchangeOperator(Value<Double[]> value, List<Function> args, BEASTContext context) {
WeightedDirichlet weightedDirichlet = (WeightedDirichlet) value.getGenerator();
IntegerParameter weightIntParam = context.getAsIntegerParameter(weightedDirichlet.getWeights());

Operator operator = new BactrianDeltaExchangeOperator();
operator.setInputValue("parameter", args);
operator.setInputValue("weight", BEASTContext.getOperatorWeight(args.size() - 1));
operator.setInputValue("weightvector", weightIntParam);
operator.setInputValue("delta", 1.0 / value.value().length);
operator.initAndValidate();
operator.setID(value.getCanonicalId() + ".deltaExchange");
context.addExtraOperator(operator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import beast.base.core.BEASTInterface;
import beast.base.core.Function;
import beast.base.inference.Operator;
import beast.base.inference.operator.DeltaExchangeOperator;
import beast.base.inference.parameter.IntegerParameter;
import beast.base.inference.parameter.Parameter;
import beast.base.inference.parameter.RealParameter;
import feast.function.Concatenate;
Expand All @@ -14,6 +11,7 @@
import lphy.core.vectorization.VectorUtils;
import lphybeast.BEASTContext;
import lphybeast.ValueToBEAST;
import lphybeast.tobeast.operators.DefaultOperatorStrategy;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,7 +37,7 @@ public BEASTInterface valueToBEAST(Value<Double[]> value, BEASTContext context)

ValueToParameter.setID(concatenatedParameters, value);

addDeltaExchangeOperator(value, args, context);
DefaultOperatorStrategy.addDeltaExchangeOperator(value, args, context);

return concatenatedParameters;
}
Expand Down Expand Up @@ -74,17 +72,4 @@ public Class<BEASTInterface> getBEASTClass() {
return BEASTInterface.class;
}

private void addDeltaExchangeOperator(Value<Double[]> value, List<Function> args, BEASTContext context) {
WeightedDirichlet weightedDirichlet = (WeightedDirichlet) value.getGenerator();
IntegerParameter weightIntParam = context.getAsIntegerParameter(weightedDirichlet.getWeights());

Operator operator = new DeltaExchangeOperator();
operator.setInputValue("parameter", args);
operator.setInputValue("weight", BEASTContext.getOperatorWeight(args.size() - 1));
operator.setInputValue("weightvector", weightIntParam);
operator.setInputValue("delta", 1.0 / value.value().length);
operator.initAndValidate();
operator.setID(value.getCanonicalId() + ".deltaExchange");
context.addExtraOperator(operator);
}
}

0 comments on commit e6b7cce

Please sign in to comment.