| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.beauti.priorspanel; |
| 27 |
|
| 28 |
import dr.app.beauti.options.Parameter; |
| 29 |
import dr.app.beauti.types.PriorType; |
| 30 |
import dr.app.gui.chart.Axis; |
| 31 |
import dr.app.gui.chart.JChart; |
| 32 |
import dr.app.gui.chart.LinearAxis; |
| 33 |
import dr.app.gui.chart.PDFPlot; |
| 34 |
import dr.app.util.OSType; |
| 35 |
import dr.math.distributions.Distribution; |
| 36 |
import dr.util.NumberFormatter; |
| 37 |
import jam.panels.OptionsPanel; |
| 38 |
|
| 39 |
import javax.swing.*; |
| 40 |
import java.awt.*; |
| 41 |
import java.awt.event.ItemEvent; |
| 42 |
import java.awt.event.ItemListener; |
| 43 |
import java.util.HashMap; |
| 44 |
import java.util.Map; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class PriorSettingsPanel extends JPanel { |
| 53 |
|
| 54 |
private final JFrame frame; |
| 55 |
private JDialog dialog; |
| 56 |
|
| 57 |
private final Map<PriorType, PriorOptionsPanel> optionsPanels = new HashMap<PriorType, PriorOptionsPanel>(); |
| 58 |
|
| 59 |
private JComboBox priorCombo; |
| 60 |
|
| 61 |
private JLabel citationText; |
| 62 |
private JLabel oneOverXCaution; |
| 63 |
private JLabel improperCaution; |
| 64 |
private JChart chart; |
| 65 |
private JPanel quantilePanel; |
| 66 |
private JTextArea quantileText; |
| 67 |
|
| 68 |
private Parameter parameter; |
| 69 |
|
| 70 |
public PriorSettingsPanel(JFrame frame) { |
| 71 |
this.frame = frame; |
| 72 |
|
| 73 |
optionsPanels.put(PriorType.NONE_FIXED, PriorOptionsPanel.INFINITE_UNIFORM); |
| 74 |
optionsPanels.put(PriorType.NONE_IMPROPER, PriorOptionsPanel.INFINITE_UNIFORM); |
| 75 |
optionsPanels.put(PriorType.UNIFORM_PRIOR, PriorOptionsPanel.UNIFORM); |
| 76 |
optionsPanels.put(PriorType.EXPONENTIAL_PRIOR, PriorOptionsPanel.EXPONENTIAL); |
| 77 |
optionsPanels.put(PriorType.LAPLACE_PRIOR, PriorOptionsPanel.LAPLACE); |
| 78 |
optionsPanels.put(PriorType.NORMAL_PRIOR, PriorOptionsPanel.NORMAL); |
| 79 |
optionsPanels.put(PriorType.LOGNORMAL_PRIOR, PriorOptionsPanel.LOG_NORMAL); |
| 80 |
optionsPanels.put(PriorType.GAMMA_PRIOR, PriorOptionsPanel.GAMMA); |
| 81 |
optionsPanels.put(PriorType.INVERSE_GAMMA_PRIOR, PriorOptionsPanel.INVERSE_GAMMA); |
| 82 |
optionsPanels.put(PriorType.BETA_PRIOR, PriorOptionsPanel.BETA); |
| 83 |
optionsPanels.put(PriorType.CTMC_RATE_REFERENCE_PRIOR, PriorOptionsPanel.CTMC_RATE_REFERENCE); |
| 84 |
optionsPanels.put(PriorType.ONE_OVER_X_PRIOR, PriorOptionsPanel.ONE_OVER_X); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
chart = new JChart(new LinearAxis(Axis.AT_MINOR_TICK, Axis.AT_MINOR_TICK), |
| 90 |
new LinearAxis(Axis.AT_ZERO, Axis.AT_DATA)); |
| 91 |
|
| 92 |
JLabel quantileLabels = new JLabel(); |
| 93 |
quantileLabels.setFont(quantileLabels.getFont().deriveFont(10.0f)); |
| 94 |
quantileLabels.setOpaque(false); |
| 95 |
quantileLabels.setText("<html><p align=\"right\">Quantiles: 2.5%:<br>5%:<br>Median:<br>95%:<br>97.5%:</p></html>"); |
| 96 |
|
| 97 |
quantileText = new JTextArea(0, 5); |
| 98 |
quantileText.setFont(quantileText.getFont().deriveFont(10.0f)); |
| 99 |
quantileText.setOpaque(false); |
| 100 |
quantileText.setEditable(false); |
| 101 |
quantileLabels.setHorizontalAlignment(JLabel.LEFT); |
| 102 |
|
| 103 |
quantilePanel = new JPanel(); |
| 104 |
quantilePanel.add(quantileLabels); |
| 105 |
quantilePanel.add(quantileText); |
| 106 |
|
| 107 |
citationText = new JLabel(); |
| 108 |
citationText.setFont(quantileLabels.getFont().deriveFont(11.0f)); |
| 109 |
citationText.setOpaque(false); |
| 110 |
citationText.setText( |
| 111 |
"<html>Approximate continuous time Markov chain rate <br>" + |
| 112 |
"reference prior developed in Ferreira & Suchard (2008).<br>" + |
| 113 |
"Use when explicit prior information is unavailable</html>"); |
| 114 |
|
| 115 |
oneOverXCaution = new JLabel(); |
| 116 |
oneOverXCaution.setFont(quantileLabels.getFont().deriveFont(11.0f)); |
| 117 |
oneOverXCaution.setOpaque(false); |
| 118 |
oneOverXCaution.setText( |
| 119 |
"<html>This improper distribution often leads to an improper posterior. <br>" + |
| 120 |
"This distribution is likely appropriate when used for the <br>" + |
| 121 |
"constant population size under the Coalescent.</html>"); |
| 122 |
|
| 123 |
improperCaution = new JLabel(); |
| 124 |
improperCaution.setFont(quantileLabels.getFont().deriveFont(11.0f)); |
| 125 |
improperCaution.setOpaque(false); |
| 126 |
improperCaution.setText( |
| 127 |
"<html>A uniform prior with infinite bounds is not a proper prior <br>" + |
| 128 |
"(it doesn't integrate to 1). This choice is not recommended.</html>"); |
| 129 |
|
| 130 |
setLayout(new GridBagLayout()); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void setParameter(final Parameter parameter) { |
| 139 |
this.parameter = parameter; |
| 140 |
|
| 141 |
priorCombo = new JComboBox(); |
| 142 |
for (PriorType priorType : PriorType.getPriorTypes(parameter)) { |
| 143 |
priorCombo.addItem(priorType); |
| 144 |
} |
| 145 |
|
| 146 |
if (parameter.priorType != null) { |
| 147 |
priorCombo.setSelectedItem(parameter.priorType); |
| 148 |
} |
| 149 |
|
| 150 |
setupComponents(); |
| 151 |
|
| 152 |
|
| 153 |
priorCombo.addItemListener(new ItemListener() { |
| 154 |
public void itemStateChanged(ItemEvent e) { |
| 155 |
setupComponents(); |
| 156 |
dialog.pack(); |
| 157 |
dialog.repaint(); |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
for (PriorOptionsPanel optionsPanel : optionsPanels.values()) { |
| 162 |
optionsPanel.removeAllListeners(); |
| 163 |
optionsPanel.addListener(new PriorOptionsPanel.Listener() { |
| 164 |
public void optionsPanelChanged() { |
| 165 |
setupChart(); |
| 166 |
dialog.pack(); |
| 167 |
dialog.repaint(); |
| 168 |
} |
| 169 |
}); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public void getArguments(Parameter parameter) { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
parameter.priorType = (PriorType) priorCombo.getSelectedItem(); |
| 190 |
PriorOptionsPanel panel = optionsPanels.get(parameter.priorType); |
| 191 |
if (panel != null) { |
| 192 |
panel.getArguments(parameter, parameter.priorType); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
private void setupComponents() { |
| 198 |
removeAll(); |
| 199 |
|
| 200 |
OptionsPanel optionsPanel = new OptionsPanel(12, (OSType.isMac() ? 6 : 24)); |
| 201 |
|
| 202 |
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 203 |
panel1.add(optionsPanel); |
| 204 |
|
| 205 |
GridBagConstraints gbc = new GridBagConstraints(); |
| 206 |
gbc.gridx = 0; |
| 207 |
gbc.gridy = 0; |
| 208 |
gbc.gridwidth = 1; |
| 209 |
gbc.weightx = 1.0; |
| 210 |
gbc.weighty = 0.0; |
| 211 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
| 212 |
gbc.anchor = GridBagConstraints.PAGE_START; |
| 213 |
gbc.gridwidth = GridBagConstraints.REMAINDER; |
| 214 |
|
| 215 |
add(panel1, gbc); |
| 216 |
|
| 217 |
PriorType priorType = (PriorType) priorCombo.getSelectedItem(); |
| 218 |
|
| 219 |
if (!parameter.isPriorFixed) { |
| 220 |
optionsPanel.addComponentWithLabel("Prior Distribution: ", priorCombo); |
| 221 |
} else { |
| 222 |
optionsPanel.addComponentWithLabel("Prior Distribution: ", new JLabel(priorType.toString())); |
| 223 |
} |
| 224 |
|
| 225 |
PriorOptionsPanel panel3 = optionsPanels.get(priorType); |
| 226 |
|
| 227 |
if (panel3 != null) { |
| 228 |
panel3.setArguments(parameter, priorType); |
| 229 |
optionsPanel.addSpanningComponent(panel3); |
| 230 |
} |
| 231 |
|
| 232 |
if (priorType == PriorType.CTMC_RATE_REFERENCE_PRIOR) { |
| 233 |
optionsPanel.addSpanningComponent(citationText); |
| 234 |
} |
| 235 |
|
| 236 |
if (priorType == PriorType.ONE_OVER_X_PRIOR) { |
| 237 |
optionsPanel.addSpanningComponent(oneOverXCaution); |
| 238 |
} |
| 239 |
|
| 240 |
if (priorType == PriorType.NONE_IMPROPER) { |
| 241 |
optionsPanel.addSpanningComponent(improperCaution); |
| 242 |
} |
| 243 |
|
| 244 |
if (priorType.isPlottable()) { |
| 245 |
optionsPanel.addSeparator(); |
| 246 |
|
| 247 |
setupChart(); |
| 248 |
chart.setMinimumSize(new Dimension(120, 120)); |
| 249 |
chart.setPreferredSize(new Dimension(300, 200)); |
| 250 |
chart.setFontSize(8); |
| 251 |
|
| 252 |
gbc.gridy = 1; |
| 253 |
gbc.weighty = 1.0; |
| 254 |
gbc.fill = GridBagConstraints.BOTH; |
| 255 |
|
| 256 |
add(chart, gbc); |
| 257 |
|
| 258 |
gbc.gridy = 2; |
| 259 |
gbc.weighty = 0.0; |
| 260 |
gbc.anchor = GridBagConstraints.PAGE_END; |
| 261 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
| 262 |
|
| 263 |
add(quantilePanel, gbc); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
repaint(); |
| 268 |
} |
| 269 |
|
| 270 |
NumberFormatter formatter = new NumberFormatter(4); |
| 271 |
|
| 272 |
void setupChart() { |
| 273 |
chart.removeAllPlots(); |
| 274 |
|
| 275 |
if (hasInvalidInput(false)) { |
| 276 |
quantileText.setText("Invalid input"); |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
PriorType priorType = (PriorType) priorCombo.getSelectedItem(); |
| 281 |
|
| 282 |
if (priorType == null) { |
| 283 |
priorType = parameter.priorType; |
| 284 |
priorCombo.setSelectedItem(priorType); |
| 285 |
} |
| 286 |
|
| 287 |
PriorOptionsPanel priorOptionsPanel = optionsPanels.get(priorType); |
| 288 |
|
| 289 |
double offset = 0.0; |
| 290 |
|
| 291 |
Distribution distribution = priorOptionsPanel.getDistribution(parameter); |
| 292 |
|
| 293 |
chart.addPlot(new PDFPlot(distribution, offset)); |
| 294 |
if (distribution != null) { |
| 295 |
quantileText.setText(formatter.format(distribution.quantile(0.025)) + |
| 296 |
"\n" + formatter.format(distribution.quantile(0.05)) + |
| 297 |
"\n" + formatter.format(distribution.quantile(0.5)) + |
| 298 |
"\n" + formatter.format(distribution.quantile(0.95)) + |
| 299 |
"\n" + formatter.format(distribution.quantile(0.975))); |
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
public boolean hasInvalidInput(boolean showError) { |
| 305 |
PriorType priorType = (PriorType) priorCombo.getSelectedItem(); |
| 306 |
PriorOptionsPanel panel = optionsPanels.get(priorType); |
| 307 |
if (panel != null) { |
| 308 |
if (panel.hasInvalidInput(priorType)) { |
| 309 |
if (showError) |
| 310 |
JOptionPane.showMessageDialog(frame, panel.error, "Invalid input", JOptionPane.ERROR_MESSAGE); |
| 311 |
return true; |
| 312 |
} else { |
| 313 |
return false; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
return false; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
public void setDialog(final JDialog dialog) { |
| 322 |
this.dialog = dialog; |
| 323 |
} |
| 324 |
} |