use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.
the class LofsSearchEditorNew method addSpecialMenus.
protected void addSpecialMenus(JMenuBar menuBar) {
if (!(getAlgorithmRunner() instanceof IGesRunner)) {
JMenu test = new JMenu("Independence");
menuBar.add(test);
IndTestMenuItems.addIndependenceTestChoices(test, this);
// test.addSeparator();
//
// AlgorithmRunner algorithmRunner = getAlgorithmRunner();
// if (algorithmRunner instanceof IndTestProducer) {
// IndTestProducer p = (IndTestProducer) algorithmRunner;
// IndependenceFactsAction action =
// new IndependenceFactsAction(this, p, "Independence Facts...");
// test.add(action);
// }
}
JMenu graph = new JMenu("Graph");
JMenuItem showDags = new JMenuItem("Show DAGs in forbid_latent_common_causes");
// JMenuItem meekOrient = new JMenuItem("Meek Orientation");
JMenuItem dagInPattern = new JMenuItem("Choose DAG in forbid_latent_common_causes");
JMenuItem gesOrient = new JMenuItem("Global Score-based Reorientation");
JMenuItem nextGraph = new JMenuItem("Next Graph");
JMenuItem previousGraph = new JMenuItem("Previous Graph");
graph.add(new GraphPropertiesAction(getWorkbench()));
graph.add(new PathsAction(getWorkbench()));
graph.add(new TriplesAction(getWorkbench().getGraph(), getAlgorithmRunner()));
graph.addSeparator();
// graph.add(meekOrient);
graph.add(dagInPattern);
graph.add(gesOrient);
graph.addSeparator();
graph.add(previousGraph);
graph.add(nextGraph);
graph.addSeparator();
graph.add(showDags);
graph.addSeparator();
graph.add(new JMenuItem(new SelectBidirectedAction(getWorkbench())));
graph.add(new JMenuItem(new SelectUndirectedAction(getWorkbench())));
menuBar.add(graph);
showDags.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window owner = (Window) getTopLevelAncestor();
new WatchedProcess(owner) {
public void watch() {
// Needs to be a pattern search; this isn't checked
// before running the algorithm because of allowable
// "slop"--e.g. bidirected edges.
AlgorithmRunner runner = getAlgorithmRunner();
Graph graph = runner.getGraph();
if (graph == null) {
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "No result gaph.");
return;
}
PatternDisplay display = new PatternDisplay(graph);
GraphWorkbench workbench = getWorkbench();
EditorWindow editorWindow = new EditorWindow(display, "Independence Facts", "Close", false, workbench);
DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
editorWindow.setVisible(true);
}
};
}
});
// meekOrient.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// ImpliedOrientation rules = getAlgorithmRunner().getMeekRules();
// rules.setKnowledge((IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2()));
// rules.orientImplied(getGraph());
// getGraphHistory().add(getGraph());
// getWorkbench().setGraph(getGraph());
// firePropertyChange("modelChanged", null, null);
// }
// });
dagInPattern.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph graph = new EdgeListGraph(getGraph());
// Removing bidirected edges from the pattern before selecting a DAG. 4
for (Edge edge : graph.getEdges()) {
if (Edges.isBidirectedEdge(edge)) {
graph.removeEdge(edge);
}
}
PatternToDag search = new PatternToDag(new EdgeListGraphSingleConnections(graph));
Graph dag = search.patternToDagMeek();
getGraphHistory().add(dag);
getWorkbench().setGraph(dag);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(dag);
firePropertyChange("modelChanged", null, null);
}
});
gesOrient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DataModel dataModel = getAlgorithmRunner().getDataModel();
final Graph graph = SearchGraphUtils.reorient(getGraph(), dataModel, getKnowledge());
getGraphHistory().add(graph);
getWorkbench().setGraph(graph);
firePropertyChange("modelChanged", null, null);
}
});
nextGraph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph next = getGraphHistory().next();
getWorkbench().setGraph(next);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(next);
firePropertyChange("modelChanged", null, null);
}
});
previousGraph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph previous = getGraphHistory().previous();
getWorkbench().setGraph(previous);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(previous);
firePropertyChange("modelChanged", null, null);
}
});
menuBar.add(new LayoutMenu(this));
}
use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.
the class PcGesSearchEditor method getToolbar.
/**
* Construct the toolbar panel.
*/
protected JPanel getToolbar() {
JPanel toolbar = new JPanel();
getExecuteButton().setText("Execute*");
getExecuteButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeStatsTabs();
execute();
}
});
JButton statsButton = new JButton("Calc Stats");
statsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window owner = (Window) getTopLevelAncestor();
new WatchedProcess(owner) {
public void watch() {
calcStats();
}
};
}
});
Box b1 = Box.createVerticalBox();
b1.add(getParamsPanel());
b1.add(Box.createVerticalStrut(10));
Box b2 = Box.createHorizontalBox();
b2.add(Box.createGlue());
b2.add(getExecuteButton());
b1.add(b2);
b1.add(Box.createVerticalStrut(10));
if (getAlgorithmRunner().getDataModel() instanceof DataSet || getAlgorithmRunner().getDataModel() instanceof ICovarianceMatrix) {
Box b3 = Box.createHorizontalBox();
b3.add(Box.createGlue());
b3.add(statsButton);
b1.add(b3);
}
if (getAlgorithmRunner().getParams() instanceof Parameters) {
Parameters params = getAlgorithmRunner().getParams();
JCheckBox preventCycles = new JCheckBox("Aggressively Prevent Cycles");
preventCycles.setHorizontalTextPosition(AbstractButton.RIGHT);
preventCycles.setSelected(params.getBoolean("aggressivelyPreventCycles", false));
preventCycles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBox box = (JCheckBox) e.getSource();
Parameters params = getAlgorithmRunner().getParams();
params.set("aggressivelyPreventCycles", box.isSelected());
}
});
b1.add(Box.createVerticalStrut(5));
Box hBox = Box.createHorizontalBox();
hBox.add(Box.createHorizontalGlue());
hBox.add(preventCycles);
b1.add(hBox);
b1.add(Box.createVerticalStrut(5));
}
Box b4 = Box.createHorizontalBox();
JLabel label = new JLabel("<html>" + "*Please note that some" + "<br>searches may take a" + "<br>long time to complete." + "</html>");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
label.setBorder(new TitledBorder(""));
b4.add(label);
b1.add(Box.createVerticalStrut(10));
b1.add(b4);
toolbar.add(b1);
return toolbar;
}
use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.
the class PcGesSearchEditor method addSpecialMenus.
protected void addSpecialMenus(JMenuBar menuBar) {
if (!(getAlgorithmRunner() instanceof IGesRunner)) {
JMenu test = new JMenu("Independence");
menuBar.add(test);
IndTestMenuItems.addIndependenceTestChoices(test, this);
// test.addSeparator();
//
// AlgorithmRunner algorithmRunner = getAlgorithmRunner();
// if (algorithmRunner instanceof IndTestProducer) {
// IndTestProducer p = (IndTestProducer) algorithmRunner;
// IndependenceFactsAction action =
// new IndependenceFactsAction(this, p, "Independence Facts...");
// test.add(action);
// }
}
JMenu graph = new JMenu("Graph");
JMenuItem showDags = new JMenuItem("Show DAGs in forbid_latent_common_causes");
// JMenuItem meekOrient = new JMenuItem("Meek Orientation");
JMenuItem dagInPattern = new JMenuItem("Choose DAG in forbid_latent_common_causes");
JMenuItem gesOrient = new JMenuItem("Global Score-based Reorientation");
JMenuItem nextGraph = new JMenuItem("Next Graph");
JMenuItem previousGraph = new JMenuItem("Previous Graph");
// graph.add(new LayoutMenu(this));
graph.add(new GraphPropertiesAction(getWorkbench()));
graph.add(new PathsAction(getWorkbench()));
// graph.add(new DirectedPathsAction(getWorkbench()));
// graph.add(new TreksAction(getWorkbench()));
// graph.add(new AllPathsAction(getWorkbench()));
// graph.add(new NeighborhoodsAction(getWorkbench()));
graph.add(new TriplesAction(getWorkbench().getGraph(), getAlgorithmRunner()));
graph.addSeparator();
// graph.add(meekOrient);
graph.add(dagInPattern);
graph.add(gesOrient);
graph.addSeparator();
graph.add(previousGraph);
graph.add(nextGraph);
graph.addSeparator();
graph.add(showDags);
graph.addSeparator();
graph.add(new JMenuItem(new SelectBidirectedAction(getWorkbench())));
graph.add(new JMenuItem(new SelectUndirectedAction(getWorkbench())));
menuBar.add(graph);
showDags.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window owner = (Window) getTopLevelAncestor();
new WatchedProcess(owner) {
public void watch() {
// Needs to be a pattern search; this isn't checked
// before running the algorithm because of allowable
// "slop"--e.g. bidirected edges.
AlgorithmRunner runner = getAlgorithmRunner();
Graph graph = runner.getGraph();
if (graph == null) {
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "No result gaph.");
return;
}
// if (runner instanceof ImagesRunner) {
// GraphScorer scorer = ((ImagesRunner) runner).getGraphScorer();
// Graph _graph = ((ImagesRunner) runner).getTopGraphs().get(getIndex()).getGraph();
//
// ScoredGraphsDisplay display = new ScoredGraphsDisplay(_graph, scorer);
// GraphWorkbench workbench = getWorkbench();
//
// EditorWindow editorWindow =
// new EditorWindow(display, "Independence Facts",
// "Close", false, workbench);
// DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
// editorWindow.setVisible(true);
// }
// else {
PatternDisplay display = new PatternDisplay(graph);
GraphWorkbench workbench = getWorkbench();
EditorWindow editorWindow = new EditorWindow(display, "Independence Facts", "Close", false, workbench);
DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
editorWindow.setVisible(true);
// }
}
};
}
});
// meekOrient.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// ImpliedOrientation rules = getAlgorithmRunner().getMeekRules();
// rules.setKnowledge((IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2()));
// rules.orientImplied(getGraph());
// getGraphHistory().add(getGraph());
// getWorkbench().setGraph(getGraph());
// firePropertyChange("modelChanged", null, null);
// }
// });
dagInPattern.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph graph = new EdgeListGraph(getGraph());
// // Removing bidirected edges from the pattern before selecting a DAG. 4
// for (Edge edge : graph.getEdges()) {
// if (Edges.isBidirectedEdge(edge)) {
// graph.removeEdge(edge);
// }
// }
Graph dag = SearchGraphUtils.dagFromPattern(graph);
// PatternToDag search = new PatternToDag(new forbid_latent_common_causes(graph));
// Graph dag = search.patternToDagMeek();
getGraphHistory().add(dag);
getWorkbench().setGraph(dag);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(dag);
firePropertyChange("modelChanged", null, null);
}
});
gesOrient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DataModel dataModel = getAlgorithmRunner().getDataModel();
final Graph graph = SearchGraphUtils.reorient(getGraph(), dataModel, getKnowledge());
getGraphHistory().add(graph);
getWorkbench().setGraph(graph);
firePropertyChange("modelChanged", null, null);
}
});
nextGraph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph next = getGraphHistory().next();
getWorkbench().setGraph(next);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(next);
firePropertyChange("modelChanged", null, null);
}
});
previousGraph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graph previous = getGraphHistory().previous();
getWorkbench().setGraph(previous);
((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(previous);
firePropertyChange("modelChanged", null, null);
}
});
// if (getAlgorithmRunner().supportsKnowledge()) {
// menuBar.add(new Knowledge2Menu(this));
// }
menuBar.add(new LayoutMenu(this));
}
use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.
the class BayesUpdaterClassifierEditor method getToolbar.
private Component getToolbar() {
JButton classifyButton = new JButton("Classify");
classifyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window owner = (Window) getTopLevelAncestor();
new WatchedProcess(owner) {
public void watch() {
doClassify();
showClassification();
showRocCurve();
showConfusionMatrix();
}
};
}
});
List<Node> nodes = getClassifier().getBayesImVars();
Node[] variables = nodes.toArray(new Node[0]);
this.variableDropdown = new JComboBox(variables);
getVariableDropdown().setBackground(Color.WHITE);
getVariableDropdown().setMaximumSize(new Dimension(200, 50));
DiscreteVariable variable = (DiscreteVariable) getVariableDropdown().getSelectedItem();
this.categoryDropdown = new JComboBox(variable.getCategories().toArray(new String[0]));
getCategoryDropdown().setBackground(Color.WHITE);
getCategoryDropdown().setMaximumSize(new Dimension(200, 50));
this.variableDropdown.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Object selectedItem = comboBox.getSelectedItem();
DiscreteVariable variable = (DiscreteVariable) selectedItem;
List<String> categories = variable.getCategories();
DefaultComboBoxModel newModel = new DefaultComboBoxModel(categories.toArray(new String[0]));
getCategoryDropdown().setModel(newModel);
// if (categories.size() == 2) {
// getBinaryCutoffField().setEnabled(true);
// getBinaryCutoffField().setEditable(true);
// }
// else {
// getBinaryCutoffField().setEnabled(false);
// getBinaryCutoffField().setEditable(false);
// }
}
});
this.categoryDropdown.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
showRocCurve();
}
});
// this.binaryCutoffField = new DoubleTextField(getBinaryCutoff(), 5,
// NumberFormatUtil.getInstance().getNumberFormat());
// this.binaryCutoffField.setFilter(new DoubleTextField.Filter() {
// public double filter(double value, double oldValue) {
// if (value >= 0.0 && value <= 1.0) {
// setBinaryCutoff(value);
// return value;
// }
//
// return oldValue;
// }
// });
// DiscreteVariable selectedVar =
// (DiscreteVariable) this.variableDropdown.getSelectedItem();
// List<String> categories = selectedVar.getCategories();
//
// if (categories.size() == 2) {
// getBinaryCutoffField().setEnabled(true);
// getBinaryCutoffField().setEditable(true);
// }
// else {
// getBinaryCutoffField().setEnabled(false);
// getBinaryCutoffField().setEditable(false);
// }
Box toolbar = Box.createVerticalBox();
Box row1 = Box.createHorizontalBox();
row1.add(Box.createHorizontalStrut(5));
row1.add(new JLabel("Target = "));
row1.add(getVariableDropdown());
row1.add(Box.createHorizontalStrut(5));
row1.add(new JLabel("Category for ROC ="));
row1.add(getCategoryDropdown());
row1.add(Box.createHorizontalStrut(10));
row1.add(classifyButton);
row1.add(Box.createHorizontalGlue());
toolbar.add(row1);
toolbar.add(Box.createVerticalStrut(5));
// Box row2 = Box.createHorizontalBox();
// row2.add(Box.createHorizontalStrut(5));
// row2.add(new JLabel("(Cutoff for binary target = "));
// row2.add(getBinaryCutoffField());
// row2.add(new JLabel(" )"));
// row2.add(Box.createHorizontalGlue());
// toolbar.add(row2);
toolbar.setBorder(new EmptyBorder(2, 2, 2, 2));
return toolbar;
}
use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.
the class AbstractSearchEditor method execute.
/**
* Executes the algorithm. The execution takes place inside a thread, so one
* cannot count on a result graph having been found when the method
*/
void execute() {
Window owner = (Window) getTopLevelAncestor();
final WatchedProcess process = new WatchedProcess(owner) {
public void watch() {
try {
getExecuteButton().setEnabled(false);
} catch (Exception e) {
throw new RuntimeException(e);
}
setErrorMessage(null);
if (!knowledgeMessageShown) {
Parameters searchParams = getAlgorithmRunner().getParams();
if (searchParams != null) {
IKnowledge knowledge = (IKnowledge) searchParams.get("knowledge", new Knowledge2());
if (!knowledge.isEmpty()) {
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Using previously set knowledge. (To edit, use " + "the Knowledge menu.)");
knowledgeMessageShown = true;
}
}
}
try {
storeLatestWorkbenchGraph();
getAlgorithmRunner().execute();
} catch (Exception e) {
CharArrayWriter writer1 = new CharArrayWriter();
PrintWriter writer2 = new PrintWriter(writer1);
e.printStackTrace(writer2);
String message = writer1.toString();
writer2.close();
e.printStackTrace(System.out);
TetradLogger.getInstance().error(message);
String messageString = e.getMessage();
if (e.getCause() != null) {
messageString = e.getCause().getMessage();
}
if (messageString == null) {
messageString = message;
}
setErrorMessage(messageString);
TetradLogger.getInstance().error("************Algorithm stopped!");
getExecuteButton().setEnabled(true);
throw new RuntimeException(e);
}
// getWorkbenchScroll().setBorder(
// new TitledBorder(getResultLabel()));
Graph resultGraph = resultGraph();
doDefaultArrangement(resultGraph);
getWorkbench().setBackground(Color.WHITE);
getWorkbench().setGraph(resultGraph);
getGraphHistory().clear();
getGraphHistory().add(resultGraph);
getWorkbench().repaint();
// For Mimbuild, e.g., that need to do a second stage.
firePropertyChange("algorithmFinished", null, null);
getExecuteButton().setEnabled(true);
firePropertyChange("modelChanged", null, null);
}
};
Thread watcher = new Thread() {
public void run() {
while (true) {
try {
sleep(300);
if (!process.isAlive()) {
getExecuteButton().setEnabled(true);
return;
}
} catch (InterruptedException e) {
getExecuteButton().setEnabled(true);
return;
}
}
}
};
watcher.start();
}
Aggregations