| 1 | package dr.inferencexml.operators; |
| 2 | |
| 3 | import dr.inference.model.MatrixParameter; |
| 4 | import dr.inference.model.MatrixParameterInterface; |
| 5 | import dr.inference.model.Parameter; |
| 6 | import dr.inference.operators.LFMTargetedSearchOperator; |
| 7 | import dr.util.Transform; |
| 8 | import dr.xml.*; |
| 9 | |
| 10 | import java.util.*; |
| 11 | |
| 12 | public class LFMTargetedSearchOperatorParser extends AbstractXMLObjectParser { |
| 13 | public static final String LFM_TARGETED_SEARCH_OPERATOR = "LFMTargetedSearchOperator"; |
| 14 | public static final String SPARSE_MATRIX = "sparseMatrix"; |
| 15 | public static final String LOADINGS_MATRIX = "loadingsMatrix"; |
| 16 | public static final String FACTORS_MATRIX = "factorsMatrix"; |
| 17 | public static final String SPARSE_TARGET_MATRICES = "sparseTargetMatrices"; |
| 18 | public static final String LOADINGS_TARGET_MATRICES = "loadingsTargetMatrices"; |
| 19 | public static final String FACTORS_TARGET_MATRICES = "factorsTargetMatrices"; |
| 20 | public static final String CUTOFFS = "cutoffs"; |
| 21 | public static final String WEIGHT = "weight"; |
| 22 | |
| 23 | |
| 24 | @Override |
| 25 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 26 | MatrixParameterInterface sparseMatrix = (MatrixParameterInterface) xo.getChild(SPARSE_MATRIX).getChild(MatrixParameterInterface.class); |
| 27 | MatrixParameterInterface loadingsMatrix = (MatrixParameterInterface) xo.getChild(LOADINGS_MATRIX).getChild(MatrixParameterInterface.class); |
| 28 | MatrixParameterInterface factorsMatrix = (MatrixParameterInterface) xo.getChild(FACTORS_MATRIX).getChild(MatrixParameterInterface.class); |
| 29 | MatrixParameterInterface cutoffs = (MatrixParameterInterface) xo.getChild(CUTOFFS).getChild(MatrixParameterInterface.class); |
| 30 | |
| 31 | XMLObject cxo = xo.getChild(SPARSE_TARGET_MATRICES); |
| 32 | ArrayList<MatrixParameterInterface> sparseTargetList = new ArrayList<MatrixParameterInterface>(); |
| 33 | for (int i = 0; i < cxo.getChildCount(); i++) { |
| 34 | sparseTargetList.add((MatrixParameterInterface) cxo.getChild(i)); |
| 35 | } |
| 36 | XMLObject dxo = xo.getChild(LOADINGS_TARGET_MATRICES); |
| 37 | ArrayList<MatrixParameterInterface> loadingsTargetList = new ArrayList<MatrixParameterInterface>(); |
| 38 | for (int i = 0; i < dxo.getChildCount(); i++) { |
| 39 | loadingsTargetList.add((MatrixParameterInterface) dxo.getChild(i)); |
| 40 | } |
| 41 | XMLObject exo = xo.getChild(FACTORS_TARGET_MATRICES); |
| 42 | ArrayList<MatrixParameterInterface> factorsTargetList = new ArrayList<MatrixParameterInterface>(); |
| 43 | for (int i = 0; i < exo.getChildCount(); i++) { |
| 44 | factorsTargetList.add((MatrixParameterInterface) exo.getChild(i)); |
| 45 | } |
| 46 | double weight = xo.getDoubleAttribute(WEIGHT); |
| 47 | |
| 48 | return new LFMTargetedSearchOperator(weight, sparseMatrix, sparseTargetList, factorsMatrix, factorsTargetList, loadingsMatrix, loadingsTargetList, cutoffs); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public XMLSyntaxRule[] getSyntaxRules() { |
| 53 | return rules; |
| 54 | } |
| 55 | XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 56 | new ElementRule(SPARSE_MATRIX, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class)}), |
| 57 | new ElementRule(SPARSE_TARGET_MATRICES, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class, 1, Integer.MAX_VALUE)}), |
| 58 | new ElementRule(FACTORS_MATRIX, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class)}), |
| 59 | new ElementRule(FACTORS_TARGET_MATRICES, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class, 1, Integer.MAX_VALUE)}), |
| 60 | new ElementRule(LOADINGS_MATRIX, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class)}), |
| 61 | new ElementRule(LOADINGS_TARGET_MATRICES, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class, 1, Integer.MAX_VALUE)}), |
| 62 | new ElementRule(CUTOFFS, new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class)}), |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | @Override |
| 67 | public String getParserDescription() { |
| 68 | return "Targeted search for Sparse Latent Factor Model"; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Class getReturnType() { |
| 73 | return LFMTargetedSearchOperator.class; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getParserName() { |
| 78 | return LFM_TARGETED_SEARCH_OPERATOR; |
| 79 | } |
| 80 | } |