From e6b7ccea70632a7ae2d2bbb669a90415bb37c635 Mon Sep 17 00:00:00 2001 From: walterxie Date: Wed, 17 Jan 2024 17:47:57 +1300 Subject: [PATCH] move addDeltaExchangeOperator to the same class creating all operators #109 --- .../operators/DefaultOperatorStrategy.java | 19 ++++++++++++++++++- .../values/DoubleArrayValueToBEAST.java | 19 ++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lphybeast/src/main/java/lphybeast/tobeast/operators/DefaultOperatorStrategy.java b/lphybeast/src/main/java/lphybeast/tobeast/operators/DefaultOperatorStrategy.java index 81b56be..9d05d46 100644 --- a/lphybeast/src/main/java/lphybeast/tobeast/operators/DefaultOperatorStrategy.java +++ b/lphybeast/src/main/java/lphybeast/tobeast/operators/DefaultOperatorStrategy.java @@ -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; @@ -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; @@ -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); @@ -204,4 +207,18 @@ public static void addUpDownOperator(Tree tree, RealParameter clockRate, BEASTCo context.addExtraOperator(upDownOperator); } } + + public static void addDeltaExchangeOperator(Value value, List 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); + } } diff --git a/lphybeast/src/main/java/lphybeast/tobeast/values/DoubleArrayValueToBEAST.java b/lphybeast/src/main/java/lphybeast/tobeast/values/DoubleArrayValueToBEAST.java index 5a7750b..bff9d31 100644 --- a/lphybeast/src/main/java/lphybeast/tobeast/values/DoubleArrayValueToBEAST.java +++ b/lphybeast/src/main/java/lphybeast/tobeast/values/DoubleArrayValueToBEAST.java @@ -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; @@ -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; @@ -39,7 +37,7 @@ public BEASTInterface valueToBEAST(Value value, BEASTContext context) ValueToParameter.setID(concatenatedParameters, value); - addDeltaExchangeOperator(value, args, context); + DefaultOperatorStrategy.addDeltaExchangeOperator(value, args, context); return concatenatedParameters; } @@ -74,17 +72,4 @@ public Class getBEASTClass() { return BEASTInterface.class; } - private void addDeltaExchangeOperator(Value value, List 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); - } }