| 1 |
package dr.inferencexml.model; |
| 2 |
|
| 3 |
import dr.inference.model.CompoundParameter; |
| 4 |
import dr.inference.model.MatrixParameter; |
| 5 |
import dr.inference.model.MatrixParameterInterface; |
| 6 |
import dr.inference.model.Parameter; |
| 7 |
import dr.xml.*; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class ScaleData extends AbstractXMLObjectParser{ |
| 13 |
|
| 14 |
public final static String SCALE_DATA = "scaleData"; |
| 15 |
public final static String CONTINUOUS = "continuous"; |
| 16 |
|
| 17 |
@Override |
| 18 |
public String getParserName() { |
| 19 |
return SCALE_DATA; |
| 20 |
} |
| 21 |
|
| 22 |
@Override |
| 23 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 24 |
MatrixParameterInterface scaling = null; |
| 25 |
CompoundParameter temp = null; |
| 26 |
if(xo.getChild(MatrixParameterInterface.class)!=null){ |
| 27 |
scaling = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class); |
| 28 |
} |
| 29 |
else{ |
| 30 |
temp = (CompoundParameter) xo.getChild(CompoundParameter.class); |
| 31 |
scaling = MatrixParameter.recast(temp.getParameterName(), temp); |
| 32 |
} |
| 33 |
Parameter continuous; |
| 34 |
if(xo.hasChildNamed(CONTINUOUS)) |
| 35 |
continuous = (Parameter) xo.getChild(CONTINUOUS).getChild(Parameter.class); |
| 36 |
else |
| 37 |
continuous = new Parameter.Default(scaling.getColumnDimension(), 1); |
| 38 |
|
| 39 |
System.out.println(continuous.getParameterValue(11)); |
| 40 |
|
| 41 |
double[][] aData = scaling.getParameterAsMatrix(); |
| 42 |
double[] meanList = new double[scaling.getRowDimension()]; |
| 43 |
double[] varList = new double[scaling.getRowDimension()]; |
| 44 |
double[] count = new double[scaling.getRowDimension()]; |
| 45 |
for (int i = 0; i < scaling.getColumnDimension(); i++) { |
| 46 |
for (int j = 0; j < scaling.getRowDimension(); j++) { |
| 47 |
if (scaling.getParameterValue(j, i) != 0) { |
| 48 |
meanList[j] += scaling.getParameterValue(j, i); |
| 49 |
count[j]++; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
for (int i = 0; i < scaling.getRowDimension(); i++) { |
| 54 |
if (continuous.getParameterValue(i) == 1) |
| 55 |
meanList[i] = meanList[i] / count[i]; |
| 56 |
else |
| 57 |
meanList[i] = 0; |
| 58 |
} |
| 59 |
|
| 60 |
double[][] answerTemp = new double[scaling.getRowDimension()][scaling.getColumnDimension()]; |
| 61 |
for (int i = 0; i < scaling.getColumnDimension(); i++) { |
| 62 |
for (int j = 0; j < scaling.getRowDimension(); j++) { |
| 63 |
if (aData[j][i] != 0) { |
| 64 |
answerTemp[j][i] = aData[j][i] - meanList[j]; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
for (int i = 0; i < scaling.getColumnDimension(); i++) { |
| 71 |
for (int j = 0; j < scaling.getRowDimension(); j++) { |
| 72 |
varList[j] += answerTemp[j][i] * answerTemp[j][i]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
for (int i = 0; i < scaling.getRowDimension(); i++) { |
| 77 |
if (continuous.getParameterValue(i) == 1) { |
| 78 |
varList[i] = varList[i] / (count[i] - 1); |
| 79 |
varList[i] = StrictMath.sqrt(varList[i]); |
| 80 |
} else { |
| 81 |
varList[i] = 1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
for (int i = 0; i < scaling.getColumnDimension(); i++) { |
| 89 |
for (int j = 0; j < scaling.getRowDimension(); j++) { |
| 90 |
scaling.setParameterValue(j, i, answerTemp[j][i] / varList[j]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 103 |
return rules; |
| 104 |
} |
| 105 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 106 |
new OrRule(new ElementRule(MatrixParameterInterface.class), new ElementRule(CompoundParameter.class)), |
| 107 |
new ElementRule(CONTINUOUS, new XMLSyntaxRule[]{ |
| 108 |
new ElementRule(Parameter.class) |
| 109 |
}, true)}; |
| 110 |
|
| 111 |
@Override |
| 112 |
public String getParserDescription() { |
| 113 |
return "Standardizes the rows of a data matrix"; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public Class getReturnType() { |
| 118 |
return null; |
| 119 |
} |
| 120 |
} |