Debian Codegraph @

src/dr/inferencexml/model/LatentFactorModelParser.java

raw symbols SCIP
1 /*
2 * LatentFactorModelParser.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.model;
27
28 import dr.inference.model.*;
29 import dr.xml.*;
30
31 /**
32 * @author Max Tolkoff
33 * @author Marc Suchard
34 */
35
36 public class LatentFactorModelParser extends AbstractXMLObjectParser {
37 public final static String LATENT_FACTOR_MODEL = "latentFactorModel";
38 public final static String NUMBER_OF_FACTORS = "factorNumber";
39 public final static String FACTORS = "factors";
40 public final static String DATA = "data";
41 public final static String LOADINGS = "loadings";
42 public static final String ROW_PRECISION = "rowPrecision";
43 public static final String COLUMN_PRECISION = "columnPrecision";
44 public static final String SCALE_DATA="scaleData";
45 public static final String CONTINUOUS="continuous";
46 public static final String COMPUTE_RESIDUALS_FOR_DISCRETE="computeResidualsForDiscrete";
47 public static final String RECOMPUTE_RESIDUALS="recomputeResiduals";
48 public static final String RECOMPUTE_FACTORS="recomputeFactors";
49 public static final String RECOMPUTE_LOADINGS="recomputeLoadings";
50 public static final String MISSING_INDICATOR = "missingIndicator";
51
52
53 public String getParserName() {
54 return LATENT_FACTOR_MODEL;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 MatrixParameterInterface factors;
60 if (xo.getChild(FACTORS).getChild(FastMatrixParameter.class) == null)
61 {
62 CompoundParameter factorsTemp = (CompoundParameter) xo.getChild(FACTORS).getChild(CompoundParameter.class);
63 factors = MatrixParameter.recast(factorsTemp.getParameterName(), factorsTemp);
64 }
65 else {
66 factors = (MatrixParameterInterface) xo.getChild(FACTORS).getChild(MatrixParameterInterface.class);
67 }
68 MatrixParameterInterface dataParameter = (MatrixParameterInterface) xo.getChild(DATA).getChild(MatrixParameterInterface.class);
69 MatrixParameterInterface loadings = (MatrixParameterInterface) xo.getChild(LOADINGS).getChild(MatrixParameterInterface.class);
70 Parameter missingIndicator = null;
71 if(xo.hasChildNamed(MISSING_INDICATOR))
72 missingIndicator = (Parameter) xo.getChild(MISSING_INDICATOR).getChild(Parameter.class);
73 DiagonalMatrix rowPrecision = (DiagonalMatrix) xo.getChild(ROW_PRECISION).getChild(MatrixParameter.class);
74 DiagonalMatrix colPrecision = (DiagonalMatrix) xo.getChild(COLUMN_PRECISION).getChild(MatrixParameter.class);
75 boolean newModel= xo.getAttribute(COMPUTE_RESIDUALS_FOR_DISCRETE, true);
76 boolean computeResiduals= xo.getAttribute(RECOMPUTE_RESIDUALS, true);
77 boolean computeFactors=xo.getAttribute(RECOMPUTE_FACTORS, true);
78 boolean computeLoadings=xo.getAttribute(RECOMPUTE_LOADINGS, true);
79 Parameter continuous=null;
80 if(xo.getChild(CONTINUOUS)!=null)
81 continuous=(Parameter) xo.getChild(CONTINUOUS).getChild(Parameter.class);
82 else
83 continuous=new Parameter.Default(colPrecision.getRowDimension(), 1.0);
84 boolean scaleData=xo.getAttribute(SCALE_DATA, true);
85 // int numFactors = xo.getAttribute(NUMBER_OF_FACTORS, 4);
86 Parameter temp=null;
87 // for(int i=0; i<loadings.getColumnDimension(); i++)
88 // {
89 // if(loadings.getParameterValue(i,i)<0)
90 // {
91 // loadings.setParameterValue(i, i, temp.getParameterValue(i));
92 // }
93 // }
94
95
96 return new LatentFactorModel(dataParameter, factors, loadings, rowPrecision, colPrecision, missingIndicator, scaleData, continuous, newModel, computeResiduals, computeFactors, computeLoadings);
97 }
98
99 private static final XMLSyntaxRule[] rules = {
100 AttributeRule.newIntegerRule(NUMBER_OF_FACTORS),
101 AttributeRule.newBooleanRule(SCALE_DATA, true),
102 AttributeRule.newBooleanRule(COMPUTE_RESIDUALS_FOR_DISCRETE, true),
103 AttributeRule.newBooleanRule(RECOMPUTE_FACTORS, true),
104 AttributeRule.newBooleanRule(RECOMPUTE_RESIDUALS, true),
105 AttributeRule.newBooleanRule(RECOMPUTE_LOADINGS,true),
106 new ElementRule(DATA, new XMLSyntaxRule[]{
107 new ElementRule(MatrixParameterInterface.class),
108 }),
109 new ElementRule(FACTORS, new XMLSyntaxRule[]{
110 new ElementRule(CompoundParameter.class)
111 }),
112 new ElementRule(LOADINGS, new XMLSyntaxRule[]{
113 new ElementRule(MatrixParameterInterface.class)
114 }),
115 new ElementRule(ROW_PRECISION, new XMLSyntaxRule[]{
116 new ElementRule(DiagonalMatrix.class)
117 }),
118 new ElementRule(COLUMN_PRECISION, new XMLSyntaxRule[]{
119 new ElementRule(DiagonalMatrix.class)
120 }),
121 new ElementRule(CONTINUOUS, new XMLSyntaxRule[]{
122 new ElementRule(Parameter.class)
123 }, true),
124 new ElementRule(MISSING_INDICATOR, new XMLSyntaxRule[]{
125 new ElementRule(Parameter.class)
126 }, true),
127 };
128
129 // <latentFactorModel>
130 // <factors>
131 // <parameter idref="factors"/>
132 // </factors>
133 // </latentFactorModel>
134
135
136 public XMLSyntaxRule[] getSyntaxRules() {
137 return rules;
138 }
139
140 @Override
141 public String getParserDescription() {
142 return "Sets up a latent factor model, with starting guesses for the loadings and factor matrices as well as the data for the factor analysis";
143 }
144
145 @Override
146 public Class getReturnType() {
147 return LatentFactorModel.class;
148 }
149 }