Search in sources :

Example 6 with DataWrapper

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

the class FciSearchParamEditor method setup.

public void setup() {
    /*
      The variable names from the object being searched over (usually data).
     */
    List varNames = (List<String>) params.get("varNames", null);
    DataModel dataModel1 = null;
    Graph graph = null;
    for (Object parentModel1 : parentModels) {
        if (parentModel1 instanceof DataWrapper) {
            DataWrapper dataWrapper = (DataWrapper) parentModel1;
            dataModel1 = dataWrapper.getSelectedDataModel();
        }
        if (parentModel1 instanceof GraphWrapper) {
            GraphWrapper graphWrapper = (GraphWrapper) parentModel1;
            graph = graphWrapper.getGraph();
        }
        if (parentModel1 instanceof DagWrapper) {
            DagWrapper dagWrapper = (DagWrapper) parentModel1;
            graph = dagWrapper.getDag();
        }
        if (parentModel1 instanceof SemGraphWrapper) {
            SemGraphWrapper semGraphWrapper = (SemGraphWrapper) parentModel1;
            graph = semGraphWrapper.getGraph();
        }
    }
    if (dataModel1 != null) {
        varNames = new ArrayList(dataModel1.getVariableNames());
    } else if (graph != null) {
        Iterator<Node> it = graph.getNodes().iterator();
        varNames = new ArrayList();
        Node temp;
        while (it.hasNext()) {
            temp = it.next();
            if (temp.getNodeType() == NodeType.MEASURED) {
                varNames.add(temp.getName());
            }
        }
    } else {
        throw new NullPointerException("Null model (no graph or data model " + "passed to the search).");
    }
    params.set("varNames", varNames);
    IntTextField depthField = new IntTextField(params.getInt("depth", -1), 4);
    depthField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                params.set("depth", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    double alpha = params.getDouble("alpha", 0.001);
    if (!Double.isNaN(alpha)) {
        alphaField = new DoubleTextField(alpha, 4, NumberFormatUtil.getInstance().getNumberFormat());
        alphaField.setFilter(new DoubleTextField.Filter() {

            public double filter(double value, double oldValue) {
                try {
                    params.set("alpha", 0.001);
                    Preferences.userRoot().putDouble("alpha", value);
                    return value;
                } catch (Exception e) {
                    return oldValue;
                }
            }
        });
    }
    setBorder(new MatteBorder(10, 10, 10, 10, super.getBackground()));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Knowledge:"));
    b1.add(Box.createGlue());
    add(b1);
    add(Box.createVerticalStrut(10));
    if (!Double.isNaN(alpha)) {
        Box b2 = Box.createHorizontalBox();
        b2.add(new JLabel("Alpha Value:"));
        b2.add(Box.createGlue());
        b2.add(alphaField);
        add(b2);
        add(Box.createVerticalStrut(10));
    }
    Box b3 = Box.createHorizontalBox();
    b3.add(new JLabel("Search Depth:"));
    b3.add(Box.createGlue());
    b3.add(depthField);
    add(b3);
    add(Box.createVerticalStrut(10));
}
Also used : DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Node(edu.cmu.tetrad.graph.Node) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper) GraphWrapper(edu.cmu.tetradapp.model.GraphWrapper) ArrayList(java.util.ArrayList) DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DagWrapper(edu.cmu.tetradapp.model.DagWrapper) MatteBorder(javax.swing.border.MatteBorder) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel) IntTextField(edu.cmu.tetradapp.util.IntTextField) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper)

Example 7 with DataWrapper

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

the class TestDataWrapper method testConstruction.

@Test
public void testConstruction() {
    this.dataWrapper = new DataWrapper(new Parameters());
    assertNotNull(dataWrapper);
}
Also used : DataWrapper(edu.cmu.tetradapp.model.DataWrapper) Parameters(edu.cmu.tetrad.util.Parameters) Test(org.junit.Test)

Example 8 with DataWrapper

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

the class ConcatenateDatasetsWrapper method construct.

private void construct(DataWrapper... dataWrappers) {
    for (DataWrapper wrapper : dataWrappers) {
        if (wrapper == null) {
            throw new NullPointerException("The given data must not be null");
        }
    }
    List<DataSet> dataSets = new ArrayList<>();
    for (DataWrapper wrapper : dataWrappers) {
        for (DataModel model : wrapper.getDataModelList()) {
            if (!(model instanceof DataSet)) {
                throw new IllegalArgumentException("Sorry, I am only willing to concatenate tabular datasets.");
            }
            DataSet dataSet = (DataSet) model;
            dataSets.add(dataSet);
        }
    }
    DataSet concatenated = DataUtils.concatenate(dataSets);
    concatenated.setName("Concatenated");
    this.setDataModel(concatenated);
    LogDataUtils.logDataModelList("Parent data in which constant columns have been removed.", getDataModelList());
}
Also used : DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DataSet(edu.cmu.tetrad.data.DataSet) DataModel(edu.cmu.tetrad.data.DataModel) ArrayList(java.util.ArrayList)

Example 9 with DataWrapper

use of edu.cmu.tetradapp.model.DataWrapper 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 10 with DataWrapper

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

the class DiscretizationParamsEditor method setParentModels.

/**
 * The parant model should be a <code>DataWrapper</code>.
 */
public void setParentModels(Object[] parentModels) {
    if (parentModels == null || parentModels.length == 0) {
        throw new IllegalArgumentException("There must be parent model");
    }
    DataWrapper data = null;
    for (Object parent : parentModels) {
        if (parent instanceof DataWrapper) {
            data = (DataWrapper) parent;
        }
    }
    if (data == null) {
        throw new IllegalArgumentException("Should have have a data wrapper as a parent");
    }
    DataModel model = data.getSelectedDataModel();
    if (!(model instanceof DataSet)) {
        throw new IllegalArgumentException("The dataset must be a rectangular dataset");
    }
    this.sourceDataSet = (DataSet) model;
}
Also used : DataWrapper(edu.cmu.tetradapp.model.DataWrapper)

Aggregations

DataWrapper (edu.cmu.tetradapp.model.DataWrapper)16 DataModel (edu.cmu.tetrad.data.DataModel)13 ArrayList (java.util.ArrayList)10 DataSet (edu.cmu.tetrad.data.DataSet)7 Graph (edu.cmu.tetrad.graph.Graph)4 DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)4 List (java.util.List)4 DataModelList (edu.cmu.tetrad.data.DataModelList)3 Node (edu.cmu.tetrad.graph.Node)3 DagWrapper (edu.cmu.tetradapp.model.DagWrapper)3 GraphWrapper (edu.cmu.tetradapp.model.GraphWrapper)3 SemGraphWrapper (edu.cmu.tetradapp.model.SemGraphWrapper)3 IntTextField (edu.cmu.tetradapp.util.IntTextField)3 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 Iterator (java.util.Iterator)3 MatteBorder (javax.swing.border.MatteBorder)3 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)2 Parameters (edu.cmu.tetrad.util.Parameters)2 Comparison (edu.cmu.tetrad.algcomparison.Comparison)1