Search in sources :

Example 1 with Simulation

use of edu.cmu.tetradapp.model.Simulation in project tetrad by cmu-phil.

the class EdgewiseComparisonParamsEditor method setup.

public void setup() {
    List<GraphSource> graphSources = new LinkedList<>();
    for (Object parentModel : parentModels) {
        if (parentModel instanceof GraphSource) {
            graphSources.add((GraphSource) parentModel);
        }
    }
    if (graphSources.size() == 1 && graphSources.get(0) instanceof GeneralAlgorithmRunner) {
        model1 = (GeneralAlgorithmRunner) graphSources.get(0);
        model2 = ((GeneralAlgorithmRunner) model1).getDataWrapper();
    } else if (graphSources.size() == 2) {
        model1 = (SessionModel) graphSources.get(0);
        model2 = (SessionModel) graphSources.get(1);
    } else {
        throw new IllegalArgumentException("Expecting 2 graph source.");
    }
    setLayout(new BorderLayout());
    // Reset?
    JRadioButton resetOnExecute = new JRadioButton("Reset");
    JRadioButton dontResetOnExecute = new JRadioButton("Appended to");
    ButtonGroup group1 = new ButtonGroup();
    group1.add(resetOnExecute);
    group1.add(dontResetOnExecute);
    resetOnExecute.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            getParams().set("resetTableOnExecute", true);
        }
    });
    dontResetOnExecute.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            getParams().set("resetTableOnExecute", false);
        }
    });
    if (getParams().getBoolean("resetTableOnExecute", false)) {
        resetOnExecute.setSelected(true);
    } else {
        dontResetOnExecute.setSelected(true);
    }
    // Latents?
    JRadioButton latents = new JRadioButton("Yes");
    JRadioButton noLatents = new JRadioButton("No");
    ButtonGroup group2 = new ButtonGroup();
    group2.add(latents);
    group2.add(noLatents);
    latents.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            getParams().set("keepLatents", true);
        }
    });
    if (getParams().getBoolean("keepLatents", false)) {
        latents.setSelected(true);
    } else {
        noLatents.setSelected(true);
    }
    // True graph?
    JRadioButton graph1 = new JRadioButton(model1.getName());
    JRadioButton graph2 = new JRadioButton(model2.getName());
    graph1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            getParams().set("referenceGraphName", model1.getName());
            getParams().set("targetGraphName", model2.getName());
        }
    });
    graph2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            getParams().set("referenceGraphName", model2.getName());
            getParams().set("targetGraphName", model1.getName());
        }
    });
    ButtonGroup group = new ButtonGroup();
    group.add(graph1);
    group.add(graph2);
    boolean alreadySet = false;
    if (model1 instanceof GeneralAlgorithmRunner) {
        graph1.setSelected(true);
    }
    if (model2 instanceof GeneralAlgorithmRunner) {
        graph2.setSelected(true);
        alreadySet = true;
    }
    if (model2 instanceof Simulation) {
        graph2.setSelected(true);
        alreadySet = true;
    }
    if (!alreadySet) {
        graph1.setSelected(true);
    }
    if (graph1.isSelected()) {
        getParams().set("referenceGraphName", model1.getName());
        getParams().set("targetGraphName", model2.getName());
    } else if (graph2.isSelected()) {
        getParams().set("referenceGraphName", model2.getName());
        getParams().set("targetGraphName", model1.getName());
    }
    Box b1 = Box.createVerticalBox();
    Box b8 = Box.createHorizontalBox();
    b8.add(new JLabel("Which of the two input graphs is the true graph?"));
    b8.add(Box.createHorizontalGlue());
    b1.add(b8);
    Box b9 = Box.createHorizontalBox();
    b9.add(graph1);
    b9.add(Box.createHorizontalGlue());
    b1.add(b9);
    Box b10 = Box.createHorizontalBox();
    b10.add(graph2);
    b10.add(Box.createHorizontalGlue());
    b1.add(b10);
    b1.add(Box.createHorizontalGlue());
    add(b1, BorderLayout.CENTER);
}
Also used : ActionEvent(java.awt.event.ActionEvent) LinkedList(java.util.LinkedList) ActionListener(java.awt.event.ActionListener) Simulation(edu.cmu.tetradapp.model.Simulation) GeneralAlgorithmRunner(edu.cmu.tetradapp.model.GeneralAlgorithmRunner) SessionModel(edu.cmu.tetrad.session.SessionModel) GraphSource(edu.cmu.tetradapp.model.GraphSource)

Example 2 with Simulation

use of edu.cmu.tetradapp.model.Simulation in project tetrad by cmu-phil.

the class SimulationEditor method getFileMenu.

private void getFileMenu(final JMenu fileMenu, final Simulation simulation, final GraphSelectionEditor graphEditor, final DataEditor dataEditor, final JTabbedPane tabbedPane, final String[] simulationItems) {
    JMenuItem loadSimulation = new JMenuItem("Load Simulation");
    JMenuItem saveSimulation = new JMenuItem("Save Simulation");
    loadSimulation.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            String sessionSaveLocation = Preferences.userRoot().get("fileSaveLocation", "");
            chooser.setCurrentDirectory(new File(sessionSaveLocation));
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int ret1 = chooser.showOpenDialog(JOptionUtils.centeringComp());
            if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
                return;
            }
            File file = chooser.getSelectedFile();
            if (file == null) {
                return;
            }
            // Check to make sure the directory has the right structure.
            File[] files = file.listFiles();
            if (files == null) {
                JOptionPane.showMessageDialog((SimulationEditor.this), "That wasn't a directory");
                return;
            }
            boolean correctStructure = isCorrectStructure(files);
            if (!correctStructure) {
                int count = 0;
                File thisOne = null;
                for (File _file : files) {
                    File[] _files = _file.listFiles();
                    if (_files == null) {
                        continue;
                    }
                    if (isCorrectStructure(_files)) {
                        count++;
                        thisOne = _file;
                    }
                }
                if (thisOne == null) {
                    JOptionPane.showMessageDialog((SimulationEditor.this), "That file was not a simulation, and none of its subdirectories was either. " + "\nNeed a directory with a 'data' subdirectory, a 'graph' subdirectory, " + "\nand a 'parameters.txt' file.");
                    return;
                }
                if (count > 1) {
                    JOptionPane.showMessageDialog((SimulationEditor.this), "More than one subdirectory of that directory was a simulation; please select " + "\none of the subdirectories.");
                    return;
                }
                file = thisOne;
            }
            edu.cmu.tetrad.algcomparison.simulation.Simulation _simulation = new LoadContinuousDataAndGraphs(file.getPath());
            _simulation.createData(simulation.getParams());
            if (_simulation.getNumDataModels() > 0) {
                Graph trueGraph = _simulation.getTrueGraph(0);
                edu.cmu.tetrad.graph.GraphUtils.circleLayout(trueGraph, 225, 200, 150);
                List<Graph> graphs = new ArrayList<>();
                for (int i = 0; i < _simulation.getNumDataModels(); i++) {
                    graphs.add(_simulation.getTrueGraph(i));
                }
                graphEditor.replace(graphs);
                DataWrapper wrapper = new DataWrapper(new Parameters());
                DataModelList list = new DataModelList();
                for (int i = 0; i < _simulation.getNumDataModels(); i++) {
                    list.add(_simulation.getDataModel(i));
                }
                wrapper.setDataModelList(list);
                tabbedPane.setComponentAt(2, new DataEditor(wrapper, false, JTabbedPane.LEFT));
            }
            String graphPref = null;
            String simPref = null;
            if (_simulation.getParameters().contains("graphsDropdownPreference")) {
                graphPref = (String) simulation.getParams().get("graphsDropdownPreference");
            }
            if (_simulation.getParameters().contains("simulationsDropdownPreference")) {
                simPref = (String) simulation.getParams().get("simulationsDropdownPreference");
            }
            if (graphPref != null) {
                graphsDropdown.setSelectedItem(graphPref);
                System.out.println("Set pre-loaded Graph: " + graphPref);
            }
            if (simPref != null) {
                simulationsDropdown.setSelectedItem(simPref);
                System.out.println("Set pre-loaded sim: " + simPref);
            }
            simulation.setSimulation(_simulation, simulation.getParams());
            resetPanel(simulation, graphItems, simulationItems, tabbedPane);
        }

        private boolean isCorrectStructure(File[] files) {
            boolean hasDataDir = false;
            boolean hasGraphDir = false;
            boolean hasParametersFile = false;
            for (File _file : files) {
                if (_file.isDirectory() && _file.getName().equals("data")) {
                    hasDataDir = true;
                }
                if (_file.isDirectory() && _file.getName().equals("graph")) {
                    hasGraphDir = true;
                }
                if (_file.isFile() && _file.getName().equals("parameters.txt")) {
                    hasParametersFile = true;
                }
            }
            return hasDataDir && hasGraphDir && hasParametersFile;
        }
    });
    saveSimulation.addActionListener((e) -> {
        JFileChooser chooser = new JFileChooser();
        String sessionSaveLocation = Preferences.userRoot().get("fileSaveLocation", "");
        chooser.setCurrentDirectory(new File(sessionSaveLocation));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int ret1 = chooser.showSaveDialog(JOptionUtils.centeringComp());
        if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
            return;
        }
        final File selectedFile = chooser.getSelectedFile();
        if (selectedFile == null) {
            return;
        }
        // if (file.listFiles().length != 0) {
        // JOptionPane.showMessageDialog((SimulationEditor.this),
        // "That wasn't a a new or empty directory; try typing a name for the directory\n" +
        // "or creating an empty directory.");
        // return;
        // }
        new Comparison().saveToFiles(selectedFile.getAbsolutePath(), simulation.getSimulation(), simulation.getParams());
        Preferences.userRoot().put("fileSaveLocation", selectedFile.getParent());
    });
    fileMenu.addSeparator();
    fileMenu.add(loadSimulation);
    fileMenu.add(saveSimulation);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) ActionEvent(java.awt.event.ActionEvent) LoadContinuousDataAndGraphs(edu.cmu.tetrad.algcomparison.simulation.LoadContinuousDataAndGraphs) DataWrapper(edu.cmu.tetradapp.model.DataWrapper) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) SingleGraph(edu.cmu.tetrad.algcomparison.graph.SingleGraph) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) ActionListener(java.awt.event.ActionListener) JFileChooser(javax.swing.JFileChooser) TimeSeriesSemSimulation(edu.cmu.tetrad.algcomparison.simulation.TimeSeriesSemSimulation) GeneralSemSimulation(edu.cmu.tetrad.algcomparison.simulation.GeneralSemSimulation) SemSimulation(edu.cmu.tetrad.algcomparison.simulation.SemSimulation) ConditionalGaussianSimulation(edu.cmu.tetrad.algcomparison.simulation.ConditionalGaussianSimulation) StandardizedSemSimulation(edu.cmu.tetrad.algcomparison.simulation.StandardizedSemSimulation) BooleanGlassSimulation(edu.cmu.tetrad.algcomparison.simulation.BooleanGlassSimulation) Simulation(edu.cmu.tetradapp.model.Simulation) LeeHastieSimulation(edu.cmu.tetrad.algcomparison.simulation.LeeHastieSimulation) BayesNetSimulation(edu.cmu.tetrad.algcomparison.simulation.BayesNetSimulation) DataModelList(edu.cmu.tetrad.data.DataModelList) Comparison(edu.cmu.tetrad.algcomparison.Comparison) List(java.util.List) ArrayList(java.util.ArrayList) DataModelList(edu.cmu.tetrad.data.DataModelList) JMenuItem(javax.swing.JMenuItem) File(java.io.File)

Aggregations

Simulation (edu.cmu.tetradapp.model.Simulation)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 Comparison (edu.cmu.tetrad.algcomparison.Comparison)1 RandomGraph (edu.cmu.tetrad.algcomparison.graph.RandomGraph)1 SingleGraph (edu.cmu.tetrad.algcomparison.graph.SingleGraph)1 BayesNetSimulation (edu.cmu.tetrad.algcomparison.simulation.BayesNetSimulation)1 BooleanGlassSimulation (edu.cmu.tetrad.algcomparison.simulation.BooleanGlassSimulation)1 ConditionalGaussianSimulation (edu.cmu.tetrad.algcomparison.simulation.ConditionalGaussianSimulation)1 GeneralSemSimulation (edu.cmu.tetrad.algcomparison.simulation.GeneralSemSimulation)1 LeeHastieSimulation (edu.cmu.tetrad.algcomparison.simulation.LeeHastieSimulation)1 LoadContinuousDataAndGraphs (edu.cmu.tetrad.algcomparison.simulation.LoadContinuousDataAndGraphs)1 SemSimulation (edu.cmu.tetrad.algcomparison.simulation.SemSimulation)1 StandardizedSemSimulation (edu.cmu.tetrad.algcomparison.simulation.StandardizedSemSimulation)1 TimeSeriesSemSimulation (edu.cmu.tetrad.algcomparison.simulation.TimeSeriesSemSimulation)1 DataModelList (edu.cmu.tetrad.data.DataModelList)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Graph (edu.cmu.tetrad.graph.Graph)1 SessionModel (edu.cmu.tetrad.session.SessionModel)1 Parameters (edu.cmu.tetrad.util.Parameters)1