Skip to content

Commit

Permalink
setParam should throw Exception #493
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Jun 10, 2024
1 parent 4903cb9 commit 05253d7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/birth-death/birthDeathRhoSampling.lphy
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ T ~ BirthDeathSampling(diversification=diversification, turnover=turnover, rho=0
sf ~ Dirichlet(conc=[2.0, 2.0, 2.0, 2.0]);
er ~ Dirichlet(conc=[2.0, 4.0, 2.0, 2.0, 4.0, 2.0]);
Q = gtr(freq=sf, rates=er); // construct the GTR instantaneous rate matrix
D ~ PhyloCTMC(tree=T, Q=Q, mu=0.5, L=1000);
D ~ PhyloCTMC(tree=T, Q=Q, mu=0.5, L=100);
27 changes: 10 additions & 17 deletions lphy-base/src/main/java/lphy/base/distribution/ExpMarkovChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,20 @@ public Map<String, Value> getParams() {
}
}

public void setInitialMean(Value<Double> initialMean) {
this.initialMean = initialMean;
}

public void setFirstValue(Value<Double> firstValue) {
this.firstValue = firstValue;
}

public void setN(Value<Integer> n) {
this.n = n;
//TODO cannot work with Number. Perhaps change to setParam
public void setInitialMean(double initialMean) {
this.initialMean.setValue(initialMean);
constructDistribution(random);
}

public Value<Double> getInitialMean() {
return initialMean;
public void setFirstValue(double firstValue) {
this.firstValue.setValue(firstValue);
constructDistribution(random);
}

public Value<Double> getFirstValue() {
return firstValue;
public void setN(int n) {
this.n.setValue(n);
constructDistribution(random);
}

public Value<Integer> getN() {
return n;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public Map<String, Value> getParams() {
}};
}

public void setLength(Value<Integer> length) {
this.length = length;
//TODO cannot work with Number. Perhaps change to setParam
public void setLength(int length) {
this.length.setValue(length);
}

public void setHammingWeight(Value<Integer> hammingWeight) {
this.hammingWeight = hammingWeight;
public void setHammingWeight(int hammingWeight) {
this.hammingWeight.setValue(hammingWeight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ public Map<String, Value> getParams() {
}};
}

public void setN(Value<Integer> n) {
this.n = n;
//TODO cannot work with Number. Perhaps change to setParam
public void setN(int n) {
this.n.setValue(n);
constructDistribution(random);
}

public void setK(Value<Integer> k) {
this.k = k;
public void setK(int k) {
this.k.setValue(k);
constructDistribution(random);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ public Map<String, Value> getParams() {
}};
}

public void setLower(Value<Integer> lower) {
this.lower = lower;
//TODO change to setParam ?
public void setLower(int lower) {
this.lower.setValue(lower);
constructDistribution(random);
}

public void setUpper(Value<Integer> upper) {
this.upper = upper;
public void setUpper(int upper) {
this.upper.setValue(upper);
constructDistribution(random);
}

public Value<Integer> getLower() {
Expand Down
27 changes: 20 additions & 7 deletions lphy/src/main/java/lphy/core/model/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,47 @@ default String getTypeName() {
* This default code is to find whether any setter methods exist,
* then it would not need to be overwritten,
* otherwise this method must be overwritten to set the values by argument names.
* The setter sets the primary type inside the Value, such as,
* <code>public void setLambda(double p) {</code>
* <code> this.lambda.setValue(p); </code>
* <code>}</code>
* @param paramName parameter (argument) name
* @param value {@link Value}
*/
default void setParam(String paramName, Value<?> value) {
// TODO this does not work for Number
// such as, public void setLambda(double p) {
// this.lambda.setValue(p);
// constructDistribution(random); }
String methodName = "set" + Character.toUpperCase(paramName.charAt(0)) + paramName.substring(1);

try {
// just one setter
Method method = getClass().getMethod(methodName, value.value().getClass());

method.invoke(this, value.value());
} catch (NoSuchMethodException e) {

// multiple setters due to multiple parameters
Method[] methods = getClass().getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
try {
method.invoke(this, value.value());
break;
} catch (IllegalArgumentException mismatch) {
//TODO Cannot handle inheritance, such as Number, but value.getType() is Double
LoggerUtils.log.severe(mismatch.getMessage() + ", where " + methodName +
"(" + method.getGenericParameterTypes()[0] + ") does not match the value type " +
value.getType() + "!\nPlease overwrite setParam() instead of using setters !");
} catch (InvocationTargetException | IllegalAccessException ignored) {
LoggerUtils.log.severe(methodName + " err : " + ignored.getMessage());
//TODO Cannot handle inheritance, such as Number,
// it must specify to primary type, such as double
String msg = mismatch.getMessage() + " in " + this.getClass().getSimpleName() +
", where " + methodName + "(" + method.getGenericParameterTypes()[0] +
") does not match the value type " + value.getType() +
"!\nPlease either overwrite setParam(), or change setter value type to " +
value.getType() + " !";
LoggerUtils.log.severe(msg); // log to studio as well
throw new RuntimeException(msg, e);
} catch (InvocationTargetException | IllegalAccessException err) {
String msg = methodName + " err : " + err.getMessage();
LoggerUtils.log.severe(msg); // log to studio as well
throw new RuntimeException(msg, e);
}
}
}
Expand Down

0 comments on commit 05253d7

Please sign in to comment.