Debian Codegraph @

src/dr/app/beauti/priorspanel/PriorSettingsPanel.java

raw symbols SCIP
1 /*
2 * PriorSettingsPanel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Andrew Rambaut
48 * @author Alexei Drummond
49 * @author Walter Xie
50 * @version $Id: PriorDialog.java,v 1.4 2006/09/05 13:29:34 rambaut Exp $
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 // optionsPanels.put(PriorType.NORMAL_HPM_PRIOR, new NormalHPMOptionsPanel());
86 // optionsPanels.put(PriorType.LOGNORMAL_HPM_PRIOR, new LognormalHPMOptionsPanel());
87 // optionsPanels.put(PriorType.GMRF_PRIOR, new GMRFOptionsPanel());
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 * Set the parameter to be controlled
135 * q
136 * @param parameter
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(); // setArguments here
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 // if (parameter.isNodeHeight || parameter.isStatistic) {
175 // parameter.priorType = (PriorType) priorCombo.getSelectedItem();
176 // if (parameter.priorType == PriorType.NONE_TREE_PRIOR || parameter.priorType == PriorType.NONE_STATISTIC) {
177 // parameter.initial = Double.NaN;
178 // return;
179 // }
180 // } else {
181 // parameter.priorType = (PriorType) priorCombo.getSelectedItem();
182 // }
183 //
184 // if (!parameter.isStatistic && initialField.getValue() != null) parameter.initial = initialField.getValue();
185 //
186 // if (parameter.priorType != PriorType.ONE_OVER_X_PRIOR)
187 // optionsPanels.get(parameter.priorType).setParameterPrior(parameter);
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); // important to make init field appear during switch
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; // TODO is this used or duplicated to OffsetPositiveDistribution?
290 // this does not refresh dist from parameter truncation lower/upper, it get them from GUI
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 // if there is no panel for this prior type then assume it is valid
317 return false;
318 // throw new IllegalComponentStateException("Cannot get PriorOptionsPanel");
319 }
320
321 public void setDialog(final JDialog dialog) {
322 this.dialog = dialog;
323 }
324 }