Skip to content

Commit

Permalink
if there is a special Tree Logger, then do not log this tree using no…
Browse files Browse the repository at this point in the history
…rmal Tree Logger #161 LinguaPhylo/linguaPhylo#488
  • Loading branch information
walterxie committed Oct 9, 2024
1 parent fcaf748 commit 259e602
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
8 changes: 8 additions & 0 deletions lphybeast/src/main/java/lphybeast/BEASTContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,10 @@ public List<Loggable> getExtraLoggables() {
return extraLoggables;
}

public List<Loggable> getSkipLoggables() {
return skipLoggables;
}

public List<LoggerHelper> getExtraLoggers() {
return extraLoggers;
}
Expand All @@ -1139,6 +1143,10 @@ public void addExtraLoggable(Loggable loggable) {
extraLoggables.add(loggable);
}

public void addSkipLoggable(Loggable loggable) {
skipLoggables.add(loggable);
}

/**
* {@link LoggerHelper} creates BEAST2 {@link Logger}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@ public UCRelaxedClockModel generatorToBEAST(UCLNMean1 ucln, BEASTInterface value
}
}

GraphicalModelNode branchRates = context.getGraphicalModelNode(value);
if (value instanceof RealParameter rates) {
ucRelaxedClockModel.setInputValue("rates", rates);

// branch rates LogNormal prior, which is same LogNormal as UCLN
Prior ratesPrior = BEASTContext.createPrior(logNormDist, rates);
context.addBEASTObject(ratesPrior, ucln);

// rm rates from log
context.addSkipLoggable(rates);
// replaced by <log id="" ... branchratemodel="@" tree="@"/>
RateStatistic rateStatistic = LoggerUtils.createRateStatistic("RatesStat." + branchRates.getUniqueId(), ucRelaxedClockModel, beastTree);
context.addExtraLoggable(rateStatistic);

} else throw new IllegalArgumentException("Value sampled from LPhy UCLN should be mapped to RealParameter ! " + value);

ucRelaxedClockModel.initAndValidate();
GraphicalModelNode branchRates = context.getGraphicalModelNode(value);
// in case to duplicate with RandomValue id also called "branchRates"
ucRelaxedClockModel.setID(branchRates.getUniqueId() + ".model");

//TODO rm rates from log, replaced by <log id="" ... branchratemodel="@" tree="@"/>
RateStatistic rateStatistic = LoggerUtils.createRateStatistic("RatesStat." + branchRates.getUniqueId(), ucRelaxedClockModel, beastTree);
context.addExtraLoggable(rateStatistic);

// Extra Logger <log ... branchratemodel="@..." tree="@..."/>
MetaDataTreeLogger metaDataTreeLogger = new MetaDataTreeLogger(ucRelaxedClockModel, beastTree, context);
context.addExtraLogger(metaDataTreeLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,23 @@ public List<Logger> createLoggers(long logEvery, String logFileStem) {
List<TreeInterface> trees = getTrees();
boolean multipleTrees = trees.size() > 1;
for (TreeInterface tree : trees) {
TreeLoggerHelper treeLogger = new TreeLoggerCreator(tree);
treeLogger.setFileName(logFileStem, multipleTrees);
boolean create = true;
// if there is already a special Tree Logger, e.g. TreeWithMetaDataLogger, for this tree,
// then do not log it using normal Tree Logger
for (LoggerHelper loggerHelper : context.getExtraLoggers()) {
if (loggerHelper instanceof TreeLoggerHelper treeLoggerHelper) {
TreeInterface extraTree = treeLoggerHelper.getTree();
if (extraTree.equals(tree))
create = false;
}
}

if (create) {
TreeLoggerHelper treeLogger = new TreeLoggerCreator(tree);
treeLogger.setFileName(logFileStem, multipleTrees);

loggers.add(treeLogger.createLogger(logEvery, elements));
loggers.add(treeLogger.createLogger(logEvery, elements));
}
}

// extraLoggers, create a seperated logger each time
Expand Down Expand Up @@ -117,6 +130,11 @@ public List<Loggable> getLoggables() {
nonTrees.addAll(context.getExtraLoggables());
}

// rm loggable if it is in skipLoggables
for (Loggable loggable : context.getSkipLoggables()) {
nonTrees.remove(loggable);
}

// for (Loggable loggable : extraLoggables) {
// // fix StructuredCoalescent log
// if (loggable instanceof Constant) {
Expand Down Expand Up @@ -168,13 +186,14 @@ public TreeLoggerCreator(TreeInterface tree) {
this.tree = tree;
}

@Deprecated
private boolean logMetaData() {
TreeInterface tree = getTree();
Map<BEASTInterface, GraphicalModelNode<?>> BEASTToLPHYMap = context.getBEASTToLPHYMap();
GraphicalModelNode graphicalModelNode = BEASTToLPHYMap.get(tree);
Generator generator = ((RandomVariable) graphicalModelNode).getGenerator();

// TODO more general?
//TODO it'd better to use MetaDataTreeLogger
return generator instanceof SkylineCoalescent ||
generator instanceof StructuredCoalescent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* Log branch rates with tree.
* This is for the scenario that cannot use {@link LoggerFactory.TreeLoggerCreator},
* which relies on the generator of lphy's tree.
* @see TreeWithMetaDataLogger
* @author Walter Xie
*/
Expand All @@ -25,8 +27,14 @@ public class MetaDataTreeLogger implements TreeLoggerHelper {
final TreeInterface tree;
String fileName;

/**
* create Tree Logger, using TreeWithMetaDataLogger
* @param branchRateModel can be null, if so, no branchratemodel=""
* @param tree cannot be null
* @param context central config
*/
public MetaDataTreeLogger(BranchRateModel branchRateModel, TreeInterface tree, BEASTContext context) {
this.branchRateModel = Objects.requireNonNull(branchRateModel);
this.branchRateModel = branchRateModel;
this.tree = Objects.requireNonNull(tree);
this.context = context;
}
Expand All @@ -41,7 +49,8 @@ public Logger createLogger(long logEvery, Multimap<BEASTInterface, GraphicalMode

TreeWithMetaDataLogger treeWithMetaDataLogger = new TreeWithMetaDataLogger();
treeWithMetaDataLogger.setInputValue("tree", tree);
treeWithMetaDataLogger.setInputValue("branchratemodel", branchRateModel);
if (branchRateModel != null)
treeWithMetaDataLogger.setInputValue("branchratemodel", branchRateModel);

logger.setInputValue("log", treeWithMetaDataLogger);
logger.initAndValidate();
Expand Down

0 comments on commit 259e602

Please sign in to comment.