Debian Codegraph @

src/dr/inferencexml/operators/LoadingsGibbsOperatorParser.java

raw symbols SCIP
1 /*
2 * LoadingsGibbsOperatorParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inferencexml.operators;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.IndependentNormalDistributionModel;
30 import dr.inference.distribution.LatentFactorModelInterface;
31 import dr.inference.distribution.MomentDistributionModel;
32 import dr.inference.model.LatentFactorModel;
33 import dr.inference.model.Likelihood;
34 import dr.inference.model.MatrixParameterInterface;
35 import dr.inference.operators.LoadingsGibbsOperator;
36 import dr.inference.operators.LoadingsGibbsTruncatedOperator;
37 import dr.xml.*;
38
39 /**
40 * Created with IntelliJ IDEA.
41 * User: max
42 * Date: 5/23/14
43 * Time: 1:49 PM
44 * To change this template use File | Settings | File Templates.
45 */
46 public class LoadingsGibbsOperatorParser extends AbstractXMLObjectParser {
47 public static final String LOADINGS_GIBBS_OPERATOR = "loadingsGibbsOperator";
48 public static final String WEIGHT = "weight";
49 private final String RANDOM_SCAN = "randomScan";
50 private final String WORKING_PRIOR = "workingPrior";
51 private final String CUTOFF_PRIOR = "cutoffPrior";
52 private final String MULTI_THREADED = "multiThreaded";
53 private final String NUM_THREADS = "numThreads";
54 private final String PATH_PARAMETER = "pathParameter";
55 private final String UPPER_TRIANGLE = "upperTriangle";
56
57
58 @Override
59 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
60 String weightTemp = (String) xo.getAttribute(WEIGHT);
61 double weight = Double.parseDouble(weightTemp);
62 LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
63 DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
64 MomentDistributionModel prior2 = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class);
65 IndependentNormalDistributionModel prior3 = (IndependentNormalDistributionModel) xo.getChild(IndependentNormalDistributionModel.class);
66 DistributionLikelihood cutoffPrior = null;
67 if(xo.hasChildNamed(CUTOFF_PRIOR)){
68 cutoffPrior = (DistributionLikelihood) xo.getChild(CUTOFF_PRIOR).getChild(DistributionLikelihood.class);
69 }
70 boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
71 int numThreads = xo.getAttribute(NUM_THREADS, 4);
72 MatrixParameterInterface loadings=null;
73 if(xo.getChild(MatrixParameterInterface.class)!=null){
74 loadings=(MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class);
75 }
76 DistributionLikelihood WorkingPrior = null;
77 if(xo.getChild(WORKING_PRIOR) != null){
78 WorkingPrior = (DistributionLikelihood) xo.getChild(WORKING_PRIOR).getChild(DistributionLikelihood.class);
79 }
80 boolean multiThreaded = xo.getAttribute(MULTI_THREADED, false);
81 boolean upperTriangle = xo.getAttribute(UPPER_TRIANGLE, true);
82
83
84 if(prior !=null || prior3 != null){
85 LoadingsGibbsOperator lfmOp = new LoadingsGibbsOperator(LFM, prior, prior3, loadings, weight, randomScan,
86 WorkingPrior, multiThreaded, numThreads, upperTriangle);
87 if(xo.hasAttribute(PATH_PARAMETER)){
88 System.out.println("WARNING: Setting Path Parameter is intended for debugging purposes only!");
89 lfmOp.setPathParameter(xo.getDoubleAttribute(PATH_PARAMETER));
90 }
91 return lfmOp;
92 }
93 else
94 return new LoadingsGibbsTruncatedOperator(LFM, prior2, weight, randomScan, loadings, cutoffPrior);
95 }
96
97 @Override
98 public XMLSyntaxRule[] getSyntaxRules() {
99 return rules;
100 }
101
102 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
103 new ElementRule(LatentFactorModelInterface.class),
104
105 new XORRule(
106 new XORRule(
107 new ElementRule(IndependentNormalDistributionModel.class),
108 new ElementRule(DistributionLikelihood.class)),
109 new AndRule(
110 new ElementRule(MomentDistributionModel.class),
111
112 new ElementRule(CUTOFF_PRIOR, new XMLSyntaxRule[]{new ElementRule(DistributionLikelihood.class)}))
113 ),
114 // new ElementRule(CompoundParameter.class),
115 AttributeRule.newDoubleRule(WEIGHT),
116 AttributeRule.newBooleanRule(MULTI_THREADED, true),
117 AttributeRule.newIntegerRule(NUM_THREADS, true),
118 new ElementRule(WORKING_PRIOR, new XMLSyntaxRule[]{
119 new ElementRule(DistributionLikelihood.class)
120 }, true),
121 AttributeRule.newDoubleRule(PATH_PARAMETER, true),
122 AttributeRule.newBooleanRule(UPPER_TRIANGLE, true)
123 };
124
125 @Override
126 public String getParserDescription() {
127 return "Gibbs sampler for the loadings matrix of a latent factor model"; //To change body of implemented methods use File | Settings | File Templates.
128 }
129
130 @Override
131 public Class getReturnType() {
132 return LoadingsGibbsOperator.class; //To change body of implemented methods use File | Settings | File Templates.
133 }
134
135 @Override
136 public String getParserName() {
137 return LOADINGS_GIBBS_OPERATOR; //To change body of implemented methods use File | Settings | File Templates.
138 }
139 }