Search in sources :

Example 1 with DataModelList

use of edu.cmu.tetrad.data.DataModelList in project tetrad by cmu-phil.

the class RandomSamplerAction method actionPerformed.

/**
 * Performs the action of loading a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    DataModel dataModel = getDataEditor().getSelectedDataModel();
    if (dataModel instanceof DataSet) {
        DataSet dataSet = (DataSet) dataModel;
        if (dataSet.getNumRows() == 0) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Data set is empty.");
            return;
        }
        JComponent editor = editor();
        int selection = JOptionPane.showOptionDialog(JOptionUtils.centeringComp(), editor, "Sample Size", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Done", "Cancel" }, "Done");
        if (selection != 0) {
            return;
        }
        try {
            DataSet newDataSet = RandomSampler.sample(dataSet, getSampleSize());
            DataModelList list = new DataModelList();
            list.add(newDataSet);
            getDataEditor().reset(list);
            getDataEditor().selectFirstTab();
        } catch (Exception e1) {
            String s = e1.getMessage();
            if (s == null || "".equals(s)) {
                s = "Could not construct random sample.";
            }
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), s);
        }
    } else {
        JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Must be a tabular data set.");
    }
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) DataModelList(edu.cmu.tetrad.data.DataModelList) DataModel(edu.cmu.tetrad.data.DataModel)

Example 2 with DataModelList

use of edu.cmu.tetrad.data.DataModelList in project tetrad by cmu-phil.

the class PatternFitEditor method setup.

// ============================ Private Methods =========================//
private void setup() {
    JTabbedPane pane = new JTabbedPane(JTabbedPane.LEFT);
    DataModelList data = comparison.getDataModelList();
    List<BayesIm> bayesIms = comparison.getBayesIms();
    List<SemPm> semPms = comparison.getSemPms();
    if (bayesIms != null && semPms != null) {
        throw new IllegalArgumentException("That's weird; both Bayes and SEM estimations were done. Please complain.");
    }
    if (bayesIms != null) {
        for (int i = 0; i < bayesIms.size(); i++) {
            BayesEstimatorEditor editor = new BayesEstimatorEditor(bayesIms.get(i), (DataSet) data.get(i));
            JPanel panel = new JPanel();
            JScrollPane scroll = new JScrollPane(editor);
            scroll.setPreferredSize(new Dimension(900, 600));
            panel.add(Box.createVerticalStrut(10));
            Box box = Box.createHorizontalBox();
            panel.add(box);
            panel.add(Box.createVerticalStrut(10));
            Box box1 = Box.createHorizontalBox();
            box1.add(new JLabel("Graph Comparison: "));
            box1.add(Box.createHorizontalGlue());
            add(box1);
            setLayout(new BorderLayout());
            pane.add("" + (i + 1), scroll);
        }
    }
    if (semPms != null) {
        for (int i = 0; i < semPms.size(); i++) {
            SemEstimatorEditor editor = new SemEstimatorEditor(semPms.get(i), (DataSet) data.get(i));
            pane.add("" + (i + 1), editor);
        }
    }
    add(pane);
}
Also used : DataModelList(edu.cmu.tetrad.data.DataModelList) BayesIm(edu.cmu.tetrad.bayes.BayesIm) SemPm(edu.cmu.tetrad.sem.SemPm)

Example 3 with DataModelList

use of edu.cmu.tetrad.data.DataModelList 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)

Example 4 with DataModelList

use of edu.cmu.tetrad.data.DataModelList in project tetrad by cmu-phil.

the class LoadDataAction method actionPerformed.

/**
 * Performs the action of loading a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    // first warn user about other datasets being removed.
    // if (!isDataEmpty()) {
    // String message = "Loading data from a file will remove all existing data in the data editor. " +
    // "Do you want to continue?";
    // int option = JOptionPane.showOptionDialog(this.dataEditor, message, "Data Removal Warning",
    // JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
    // // if not yes, cancelAll action.
    // if (option != JOptionPane.YES_OPTION) {
    // return;
    // }
    // }
    JFileChooser chooser = getJFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // Sets the file chooser to allow multiple file selections
    chooser.setMultiSelectionEnabled(true);
    // Customize dialog title bar text
    chooser.setDialogTitle("Choose data files (choose multiple files with Ctrl or Shift key)");
    // The second argument sets both the title for the dialog window and the label for the approve button
    int _ret = chooser.showDialog(this.dataEditor, "Choose");
    if (_ret == JFileChooser.CANCEL_OPTION) {
        return;
    }
    // Files array
    File[] files = chooser.getSelectedFiles();
    Preferences.userRoot().put("fileSaveLocation", files[0].getParent());
    DataModelList dataModelList;
    // Show the data loader dialog to preview data ata and set their parameters
    LoadDataDialog loadData = new LoadDataDialog(files);
    loadData.showDataLoaderDialog();
    boolean keepData = false;
    if (!isDataEmpty()) {
        String message = "Would you like to replace the model data?";
        int option = JOptionPane.showOptionDialog(this.dataEditor, message, "Data Replacement", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Replace", "Keep" }, "Replace");
        keepData = option == 1;
    }
    DataModelList _dataModelList = loadData.getDataModels();
    if (_dataModelList.isEmpty()) {
        // "No files were loaded.");
        return;
    }
    if (keepData) {
        dataModelList = dataEditor.getDataModelList();
    } else {
        dataModelList = new DataModelList();
    }
    dataModelList.addAll(_dataModelList);
    dataEditor.replace(dataModelList);
    dataEditor.selectFirstTab();
    firePropertyChange("modelChanged", null, null);
}
Also used : DataModelList(edu.cmu.tetrad.data.DataModelList) File(java.io.File)

Example 5 with DataModelList

use of edu.cmu.tetrad.data.DataModelList in project tetrad by cmu-phil.

the class LoadDataAction method isDataEmpty.

// ======================= private methods =========================//
/**
 * States whether the data is empty.
 */
private boolean isDataEmpty() {
    DataWrapper wrapper = this.dataEditor.getDataWrapper();
    DataModelList dataModels = wrapper.getDataModelList();
    for (DataModel model : dataModels) {
        if (model instanceof DataSet) {
            return ((DataSet) model).getNumRows() == 0;
        } else {
            // how do you know in this case? Just say false
            return false;
        }
    }
    return true;
}
Also used : DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DataModelList(edu.cmu.tetrad.data.DataModelList) DataSet(edu.cmu.tetrad.data.DataSet) DataModel(edu.cmu.tetrad.data.DataModel)

Aggregations

DataModelList (edu.cmu.tetrad.data.DataModelList)11 DataSet (edu.cmu.tetrad.data.DataSet)7 DataModel (edu.cmu.tetrad.data.DataModel)6 DataWrapper (edu.cmu.tetradapp.model.DataWrapper)3 ArrayList (java.util.ArrayList)3 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 File (java.io.File)2 List (java.util.List)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