[beast-mcmc] r4804 committed - Branch 1.7: reorganize all validations in one place and make sure only...

0 views
Skip to first unread message

beast...@googlecode.com

unread,
Mar 8, 2012, 7:58:04 PM3/8/12
to beast-...@googlegroups.com
Revision: 4804
Author: dong....@gmail.com
Date: Thu Mar 8 16:56:49 2012
Log: Branch 1.7: reorganize all validations in one place and make sure
only to pop out error message after click OK
http://code.google.com/p/beast-mcmc/source/detail?r=4804

Modified:
/branches/1.7/release/common/VERSION HISTORY.txt
/branches/1.7/src/dr/app/beauti/priorsPanel/HierarchicalPriorDialog.java
/branches/1.7/src/dr/app/beauti/priorsPanel/PriorDialog.java
/branches/1.7/src/dr/app/beauti/priorsPanel/PriorOptionsPanel.java
/branches/1.7/src/dr/app/beauti/priorsPanel/PriorsPanel.java
/branches/1.7/src/dr/app/gui/components/RealNumberField.java
/branches/1.7/src/dr/math/distributions/LaplaceDistribution.java

=======================================
--- /branches/1.7/release/common/VERSION HISTORY.txt Thu Mar 8 06:22:41
2012
+++ /branches/1.7/release/common/VERSION HISTORY.txt Thu Mar 8 16:56:49
2012
@@ -44,6 +44,8 @@
offset prior distribution
Issue 601: Markov Jumps counting of state changes wasn't generating
correct XML
+
+

================================================================================


=======================================
---
/branches/1.7/src/dr/app/beauti/priorsPanel/HierarchicalPriorDialog.java
Sun Nov 6 07:44:00 2011
+++
/branches/1.7/src/dr/app/beauti/priorsPanel/HierarchicalPriorDialog.java
Thu Mar 8 16:56:49 2012
@@ -27,8 +27,8 @@
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
-import java.awt.event.*;
-import java.util.EnumSet;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;

@@ -51,7 +51,7 @@
private JTextField nameField;
private JPanel panel;

-// private final SpecialNumberPanel specialNumberPanel;
+ // private final SpecialNumberPanel specialNumberPanel;
private JChart[] chart;
private JPanel[] quantilePanel;
private JTextArea[] quantileText;
@@ -172,18 +172,18 @@

priorCombo.setSelectedItem(priorType);

- double lower = Double.NEGATIVE_INFINITY;
- double upper = Double.POSITIVE_INFINITY;
-
- if (parameter.isZeroOne) {
- lower = 0.0;
- upper = 1.0;
- } else if (parameter.isNonNegative) {
- lower = 0.0;
- }
-
- initialField.setRange(lower, upper);
- initialField.setValue(parameter.initial);
+ double lower = Double.NEGATIVE_INFINITY;
+ double upper = Double.POSITIVE_INFINITY;
+
+ if (parameter.isZeroOne) {
+ lower = 0.0;
+ upper = 1.0;
+ } else if (parameter.isNonNegative) {
+ lower = 0.0;
+ }
+
+ initialField.setRange(lower, upper);
+ initialField.setValue(parameter.initial);

panel = new JPanel(new GridBagLayout());

@@ -407,6 +407,11 @@
hpmMeanMean = getValue(0);
hpmMeanStDev = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
}

class GammaOptionsPanel extends PriorOptionsPanel {
@@ -437,5 +442,10 @@
parameter.shape = hpmPrecShape = getValue(0);
parameter.scale = hpmPrecScale = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
}
}
=======================================
--- /branches/1.7/src/dr/app/beauti/priorsPanel/PriorDialog.java Thu Feb 23
14:31:28 2012
+++ /branches/1.7/src/dr/app/beauti/priorsPanel/PriorDialog.java Thu Mar 8
16:56:49 2012
@@ -108,17 +108,17 @@
citationText.setFont(quantileLabels.getFont().deriveFont(11.0f));
citationText.setOpaque(false);
citationText.setText(
- "<html>Approximate continuous time Markov chain rate <br>" +
- "reference prior developed in Ferreira & Suchard
(2008).<br>" +
- "Use when explicit prior information is
unavailable</html>");
+ "<html>Approximate continuous time Markov chain rate <br>"
+
+ "reference prior developed in Ferreira & Suchard
(2008).<br>" +
+ "Use when explicit prior information is
unavailable</html>");

oneOverXCaution = new JLabel();

oneOverXCaution.setFont(quantileLabels.getFont().deriveFont(11.0f));
oneOverXCaution.setOpaque(false);
oneOverXCaution.setText(
"<html>This improper distribution often leads to an
improper posterior. <br>" +
- "This distribution is likely appropriate when used
for the <br>" +
- "constant population size under the
Coalescent.</html>");
+ "This distribution is likely appropriate when used
for the <br>" +
+ "constant population size under the
Coalescent.</html>");

improperCaution = new JLabel();

improperCaution.setFont(quantileLabels.getFont().deriveFont(11.0f));
@@ -130,6 +130,7 @@

/**
* Set the parameter to be controlled
+ *
* @param parameter
*/
public void setParameter(final Parameter parameter) {
@@ -325,7 +326,8 @@
void setupChart() {
chart.removeAllPlots();

- if (hasInvalidInput()) {
+ if (hasInvalidInput(false)) {
+ quantileText.setText("Invalid input");
return;
}

@@ -339,8 +341,11 @@
// ExponentialDistribution(1.0 / mean)
// if (priorType == PriorType.EXPONENTIAL_PRIOR && parameter.mean
== 0) parameter.mean = 1;

- double offset = 0.0;
- Distribution distribution =
optionsPanels.get(priorType).getDistribution(parameter);
+ PriorOptionsPanel priorOptionsPanel = optionsPanels.get(priorType);
+
+ double offset = 0.0; // TODO is this used or duplicated to
OffsetPositiveDistribution?
+ // this does not refresh dist from parameter truncation
lower/upper, it get them from GUI
+ Distribution distribution =
priorOptionsPanel.getDistribution(parameter);

chart.addPlot(new PDFPlot(distribution, offset));
if (distribution != null) {
@@ -353,16 +358,20 @@

}

-
- public boolean hasInvalidInput() {
+ public boolean hasInvalidInput(boolean showError) {
PriorType priorType = (PriorType) priorCombo.getSelectedItem();
PriorOptionsPanel panel = optionsPanels.get(priorType);
if (panel != null) {
- return panel.hasInvalidInput();
- } else {
- // if there is no panel for this prior type then assume it is
valid
- return false;
-// throw new IllegalComponentStateException("Cannot get
PriorOptionsPanel");
- }
+ if (panel.hasInvalidInput(priorType)) {
+ if (showError)
+ JOptionPane.showMessageDialog(frame,
panel.error, "Invalid input", JOptionPane.ERROR_MESSAGE);
+ return true;
+ } else {
+ return false;
+ }
+ }
+ // if there is no panel for this prior type then assume it is valid
+ return false;
+// throw new IllegalComponentStateException("Cannot get
PriorOptionsPanel");
}
}
=======================================
--- /branches/1.7/src/dr/app/beauti/priorsPanel/PriorOptionsPanel.java Thu
Mar 8 05:22:32 2012
+++ /branches/1.7/src/dr/app/beauti/priorsPanel/PriorOptionsPanel.java Thu
Mar 8 16:56:49 2012
@@ -23,17 +23,41 @@
*/
abstract class PriorOptionsPanel extends OptionsPanel {

- public boolean hasInvalidInput() {
+ // this not throw exception, exception thrown by FocusListener in
RealNumberField
+ public boolean hasInvalidInput(PriorType priorType) { // TODO move all
validation here
for (JComponent component : argumentFields) {
if (component instanceof RealNumberField
&& !((RealNumberField) component).isValueValid()) {
+ error = ((RealNumberField) component).getErrorMessage();
return true;
}
}
+ if (priorType == PriorType.UNIFORM_PRIOR && !isInputValid()) {
+ error = "Invalid uniform bound !";
+ return true;
+ }
if (isTruncatable && isTruncatedCheck.isSelected()) {
- return !(lowerField.isValueValid() &&
upperField.isValueValid());
- }
+ if (!lowerField.isValueValid()) {
+ error = lowerField.getErrorMessage();
+ return true;
+ } else if (!upperField.isValueValid()) {
+ error = upperField.getErrorMessage();
+ return true;
+ } else if (lowerField.getValue() >= upperField.getValue()) {
+ error = "Invalid truncation bound !";
+ return true;
+ } else if (getValue(OFFSET) > -1 && lowerField.getValue() <
getValue(OFFSET)) {
+ error = "Offset cannot be smaller than truncation lower !";
+ return true;
+ } else {
+ error = "";
+ return false;
+ }
+ }
+ error = "";
return false;
}
+
+ public String error = "";

interface Listener {
void optionsPanelChanged();
@@ -103,7 +127,7 @@
for (Listener listener : listeners) {
listener.optionsPanelChanged();
}
- }
+ }
});

KeyListener listener = new KeyAdapter() {
@@ -217,6 +241,12 @@
protected double getValue(int i) {
return ((RealNumberField) argumentFields.get(i)).getValue();
}
+
+ protected double getValue(String fieldName) {
+ int i = argumentNames.indexOf(fieldName);
+ if (i < 0) return -1; // has no offset field
+ return ((RealNumberField) argumentFields.get(i)).getValue();
+ }

protected String getArguName(int i) {
return argumentNames.get(i);
@@ -328,6 +358,10 @@

abstract void getArguments(Parameter parameter);

+ abstract boolean isInputValid();
+
+ private static final String OFFSET = "Offset";
+
static final PriorOptionsPanel UNIFORM = new PriorOptionsPanel(false) {
void setup() {
addField("Upper", 1.0, Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY);
@@ -357,13 +391,18 @@
parameter.uniformUpper = getValue(0);
parameter.uniformLower = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return getValue(0) > getValue(1);
+ }
};

static final PriorOptionsPanel EXPONENTIAL = new
PriorOptionsPanel(true) {

void setup() {
addField("Mean", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
- addField("Offset", 0.0, 0.0, Double.POSITIVE_INFINITY);
+ addField(OFFSET, 0.0, 0.0, Double.POSITIVE_INFINITY);
}

public Distribution getDistribution() {
@@ -384,6 +423,11 @@
parameter.mean = getValue(0);
parameter.offset = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel LAPLACE = new PriorOptionsPanel(true) {
@@ -409,6 +453,11 @@
parameter.mean = getValue(0);
parameter.scale = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel NORMAL = new PriorOptionsPanel(true) {
@@ -434,6 +483,11 @@
parameter.mean = getValue(0);
parameter.stdev = getValue(1);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel LOG_NORMAL = new
PriorOptionsPanel(true) {
@@ -447,7 +501,7 @@
addField("Log(Mean)", 0.0, Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY);
}
addField("Log(Stdev)", 1.0, 0.0, Double.POSITIVE_INFINITY);
- addField("Offset", 0.0, 0.0, Double.POSITIVE_INFINITY);
+ addField(OFFSET, 0.0, 0.0, Double.POSITIVE_INFINITY);
addCheckBox("Mean In Real Space", meanInRealSpaceCheck);

meanInRealSpaceCheck.addItemListener(new ItemListener() {
@@ -500,6 +554,11 @@
parameter.offset = getValue(2);

parameter.setMeanInRealSpace(meanInRealSpaceCheck.isSelected());
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }

};

@@ -508,7 +567,7 @@
void setup() {
addField("Shape", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
addField("Scale", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
- addField("Offset", 0.0, 0.0, Double.POSITIVE_INFINITY);
+ addField(OFFSET, 0.0, 0.0, Double.POSITIVE_INFINITY);
}

public Distribution getDistribution() {
@@ -531,6 +590,11 @@
parameter.scale = getValue(1);
parameter.offset = getValue(2);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel INVERSE_GAMMA = new
PriorOptionsPanel(true) {
@@ -538,7 +602,7 @@
void setup() {
addField("Shape", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
addField("Scale", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
- addField("Offset", 0.0, 0.0, Double.POSITIVE_INFINITY);
+ addField(OFFSET, 0.0, 0.0, Double.POSITIVE_INFINITY);
}

public Distribution getDistribution() {
@@ -561,6 +625,11 @@
parameter.scale = getValue(1);
parameter.offset = getValue(2);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

// class TruncatedNormalOptionsPanel extends PriorOptionsPanel {
@@ -591,7 +660,7 @@
void setup() {
addField("Shape", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
addField("ShapeB", 1.0, Double.MIN_VALUE,
Double.POSITIVE_INFINITY);
- addField("Offset", 0.0, 0.0, Double.POSITIVE_INFINITY);
+ addField(OFFSET, 0.0, 0.0, Double.POSITIVE_INFINITY);
}

public Distribution getDistribution() {
@@ -614,6 +683,11 @@
parameter.shapeB = getValue(1);
parameter.offset = getValue(2);
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel CTMC_RATE_REFERENCE = new
PriorOptionsPanel(false) {
@@ -630,6 +704,11 @@

public void getArguments(Parameter parameter) {
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

static final PriorOptionsPanel ONE_OVER_X = new
PriorOptionsPanel(false) {
@@ -646,6 +725,11 @@

public void getArguments(Parameter parameter) {
}
+
+ @Override
+ boolean isInputValid() {
+ return true;
+ }
};

}
=======================================
--- /branches/1.7/src/dr/app/beauti/priorsPanel/PriorsPanel.java Thu Mar 8
06:16:02 2012
+++ /branches/1.7/src/dr/app/beauti/priorsPanel/PriorsPanel.java Thu Mar 8
16:56:49 2012
@@ -403,7 +403,7 @@
do {
result = priorDialog.showDialog();

- isInvalid = priorDialog.hasInvalidInput();
+ isInvalid = priorDialog.hasInvalidInput(true);
} while (result == JOptionPane.OK_OPTION && isInvalid);

if (result == JOptionPane.OK_OPTION) {
=======================================
--- /branches/1.7/src/dr/app/gui/components/RealNumberField.java Thu Mar 8
05:22:32 2012
+++ /branches/1.7/src/dr/app/gui/components/RealNumberField.java Thu Mar 8
16:56:49 2012
@@ -49,13 +49,14 @@

public RealNumberField(double min, double max) {
this(min, max, "Value");
+ this.addFocusListener(this);
}

- public RealNumberField(double min, double max, String label) {
+ public RealNumberField(double min, double max, String label) { // no
FocusListener
this(min, true, max, true, label);
}

- public RealNumberField(double min, boolean includeMin, double max,
boolean includeMax, String label) {
+ public RealNumberField(double min, boolean includeMin, double max,
boolean includeMax, String label) { // no FocusListener
super();
this.min = min;
this.max = max;
@@ -63,7 +64,6 @@
this.includeMax = includeMax;
setLabel(label);
range_check = true;
- this.addFocusListener(this);
}

public void focusGained(FocusEvent evt) {
@@ -130,7 +130,7 @@
setText(obj.toString()); // where used?
}

- public void displayErrorMessage() {
+ public String getErrorMessage() {
String message = "";
if (min == Double.MIN_VALUE) {
message = " greater than 0";
@@ -146,8 +146,12 @@
message = " less than " + max;
}

+ return label + " must be" + message;
+ }
+
+ private void displayErrorMessage() {
JOptionPane.showMessageDialog(null,
- label + " must be" + message, "Invalid value",
JOptionPane.ERROR_MESSAGE);
+ getErrorMessage(), "Invalid value",
JOptionPane.ERROR_MESSAGE);
}

public void setRange(double min, double max) {
=======================================
--- /branches/1.7/src/dr/math/distributions/LaplaceDistribution.java Tue
Aug 12 02:16:06 2008
+++ /branches/1.7/src/dr/math/distributions/LaplaceDistribution.java Thu
Mar 8 16:56:49 2012
@@ -106,6 +106,20 @@
}

public UnivariateFunction getProbabilityDensityFunction() {
- throw new UnsupportedOperationException();
- }
-}
+ return pdfFunction;
+ }
+
+ private final UnivariateFunction pdfFunction = new
UnivariateFunction() {
+ public final double evaluate(double x) {
+ return pdf(x);
+ }
+
+ public final double getLowerBound() {
+ return Double.NEGATIVE_INFINITY;
+ }
+
+ public final double getUpperBound() {
+ return Double.POSITIVE_INFINITY;
+ }
+ };
+}

Reply all
Reply to author
Forward
0 new messages