| 1 |
|
| 2 |
package dr.evomodel.antigenic.phyloclustering.statistics; |
| 3 |
|
| 4 |
import java.util.LinkedList; |
| 5 |
|
| 6 |
import dr.inference.model.*; |
| 7 |
import dr.xml.*; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class ActiveIndicatorsStatistic extends Statistic.Abstract implements VariableListener { |
| 15 |
|
| 16 |
private LinkedList<Double> activeNodes = new LinkedList<Double>(); |
| 17 |
|
| 18 |
private int max_dim; |
| 19 |
|
| 20 |
public static final String ACTIVE_INDICATORS_STATISTIC = "activeIndicatorsStatistic"; |
| 21 |
|
| 22 |
public ActiveIndicatorsStatistic(Parameter indicators, int maxDim_in) { |
| 23 |
this.indicatorsParameter = indicators; |
| 24 |
indicatorsParameter.addParameterListener(this); |
| 25 |
max_dim = maxDim_in; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public int getDimension() { |
| 31 |
return max_dim; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public double getStatisticValue(int dim) { |
| 39 |
|
| 40 |
if(dim ==0){ |
| 41 |
activeNodes = new LinkedList<Double>(); |
| 42 |
|
| 43 |
for(int i=0; i < indicatorsParameter.getDimension(); i++){ |
| 44 |
if( (int) indicatorsParameter.getParameterValue(i) == 1 ){ |
| 45 |
activeNodes.addLast(new Double(i)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
double val = -1; |
| 53 |
if(dim < activeNodes.size()){ |
| 54 |
val = activeNodes.get(dim).doubleValue(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if(dim == (max_dim -1) && ( activeNodes.size() > max_dim) ){ |
| 61 |
val = -9999999; |
| 62 |
} |
| 63 |
|
| 64 |
return val; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
public String getDimensionName(int dim) { |
| 69 |
String name = "on_" + (dim+1); |
| 70 |
return name; |
| 71 |
} |
| 72 |
|
| 73 |
public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 74 |
|
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 79 |
|
| 80 |
public final static String INDICATORS = "indicators"; |
| 81 |
public final static String MAXDIMSTR = "maxDim"; |
| 82 |
|
| 83 |
|
| 84 |
public String getParserName() { |
| 85 |
return ACTIVE_INDICATORS_STATISTIC; |
| 86 |
} |
| 87 |
|
| 88 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 89 |
Parameter indicators = (Parameter) xo.getElementFirstChild(INDICATORS); |
| 90 |
|
| 91 |
int maxDim = 30; |
| 92 |
if(xo.hasAttribute(MAXDIMSTR)){ |
| 93 |
maxDim = xo.getIntegerAttribute(MAXDIMSTR); |
| 94 |
} |
| 95 |
|
| 96 |
return new ActiveIndicatorsStatistic(indicators, maxDim); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public String getParserDescription() { |
| 105 |
return "This element returns a statistic that shifts a matrix of locations by location drift in the first dimension."; |
| 106 |
} |
| 107 |
|
| 108 |
public Class getReturnType() { |
| 109 |
return ActiveIndicatorsStatistic.class; |
| 110 |
} |
| 111 |
|
| 112 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 113 |
return rules; |
| 114 |
} |
| 115 |
|
| 116 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 117 |
new ElementRule(INDICATORS, Parameter.class), |
| 118 |
AttributeRule.newDoubleRule(MAXDIMSTR, true, "the variance of mu"), |
| 119 |
|
| 120 |
}; |
| 121 |
}; |
| 122 |
|
| 123 |
private Parameter indicatorsParameter; |
| 124 |
|
| 125 |
} |