Search in sources :

Example 16 with DataModel

use of edu.cmu.tetrad.data.DataModel 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 17 with DataModel

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

the class TimeoutComparison method saveToFiles.

/**
 * Saves simulationWrapper data.
 *
 * @param dataPath The path to the directory where the simulationWrapper
 * data should be saved.
 * @param simulation The simulate used to generate the graphs and data.
 * @param parameters The parameters to be used in the simulationWrapper.
 */
public void saveToFiles(String dataPath, Simulation simulation, Parameters parameters) {
    List<SimulationWrapper> simulationWrappers = getSimulationWrappers(simulation, parameters);
    File dir0 = new File(dataPath);
    File dir;
    int i = 0;
    dir = new File(dir0, "save");
    // 
    // do {
    // dir = new File(dir0, "Simulation" + (++i));
    // } while (dir.exists());
    // if (dir.exists()) {
    // JOptionPane.showMessageDialog(JOptionUtils.centeringComp(),
    // "A file already exists named 'Simulation' in directory '" + dir0.getPath() + "'; \n" +
    // "please remove it first or move it out of the way.");
    // }
    deleteFilesThenDirectory(dir);
    try {
        int index = 0;
        for (SimulationWrapper simulationWrapper : simulationWrappers) {
            for (String param : simulationWrapper.getParameters()) {
                parameters.set(param, simulationWrapper.getValue(param));
            }
            simulationWrapper.createData(simulationWrapper.getSimulationSpecificParameters());
            index++;
            File subdir = new File(dir, "" + index);
            subdir.mkdirs();
            File dir1 = new File(subdir, "graph");
            File dir2 = new File(subdir, "data");
            dir1.mkdirs();
            dir2.mkdirs();
            File dir3 = null;
            if (isSavePatterns()) {
                dir3 = new File(subdir, "patterns");
                dir3.mkdirs();
            }
            File dir4 = null;
            if (isSavePags()) {
                dir4 = new File(subdir, "pags");
                dir4.mkdirs();
            }
            for (int j = 0; j < simulationWrapper.getNumDataModels(); j++) {
                File file2 = new File(dir1, "graph." + (j + 1) + ".txt");
                Graph graph = simulationWrapper.getTrueGraph(j);
                GraphUtils.saveGraph(graph, file2, false);
                File file = new File(dir2, "data." + (j + 1) + ".txt");
                Writer out = new FileWriter(file);
                DataModel dataModel = (DataModel) simulationWrapper.getDataModel(j);
                DataWriter.writeRectangularData((DataSet) dataModel, out, '\t');
                out.close();
                if (isSavePatterns()) {
                    File file3 = new File(dir3, "pattern." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(SearchGraphUtils.patternForDag(graph), file3, false);
                }
                if (isSavePags()) {
                    File file4 = new File(dir4, "pag." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(new DagToPag2(graph).convert(), file4, false);
                }
            }
            PrintStream out = new PrintStream(new FileOutputStream(new File(subdir, "parameters.txt")));
            out.println(simulationWrapper.getDescription());
            // out.println();
            out.println(simulationWrapper.getSimulationSpecificParameters());
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) FileWriter(java.io.FileWriter) IOException(java.io.IOException) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel) FileOutputStream(java.io.FileOutputStream) DagToPag2(edu.cmu.tetrad.search.DagToPag2) File(java.io.File) Writer(java.io.Writer) DataWriter(edu.cmu.tetrad.data.DataWriter) FileWriter(java.io.FileWriter)

Example 18 with DataModel

use of edu.cmu.tetrad.data.DataModel 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 19 with DataModel

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

the class LoadDataDialog method loadAllFiles.

/**
 * Add all files to model once all can be loaded successfully
 */
private void loadAllFiles() throws IOException {
    // Try to load each file and store the file name for failed loading
    for (int i = 0; i < loadedFiles.size(); i++) {
        DataModel dataModel = dataLoaderSettings.loadDataWithSettings(loadedFiles.get(i));
        // Add to dataModelList for further use
        if (dataModel != null) {
            // Must setName() here, file names will be used by the spreadsheet - Zhou
            dataModel.setName(loadedFiles.get(i).getName());
            dataModelList.add(dataModel);
            System.out.println("File index = " + i + " has been loaded successfully");
        }
    }
}
Also used : DataModel(edu.cmu.tetrad.data.DataModel)

Example 20 with DataModel

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

the class LofsSearchEditor 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);
                }
            }
            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);
        }
    });
    // if (getAlgorithmRunner().supportsKnowledge()) {
    // menuBar.add(new Knowledge2Menu(this));
    // }
    menuBar.add(new LayoutMenu(this));
}
Also used : LayoutMenu(edu.cmu.tetradapp.workbench.LayoutMenu) ActionEvent(java.awt.event.ActionEvent) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) ActionListener(java.awt.event.ActionListener) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench) DataModel(edu.cmu.tetrad.data.DataModel)

Aggregations

DataModel (edu.cmu.tetrad.data.DataModel)39 DataSet (edu.cmu.tetrad.data.DataSet)22 ArrayList (java.util.ArrayList)15 DataWrapper (edu.cmu.tetradapp.model.DataWrapper)13 Graph (edu.cmu.tetrad.graph.Graph)9 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)8 Parameters (edu.cmu.tetrad.util.Parameters)8 DataModelList (edu.cmu.tetrad.data.DataModelList)7 Node (edu.cmu.tetrad.graph.Node)7 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)7 List (java.util.List)5 LayoutMenu (edu.cmu.tetradapp.workbench.LayoutMenu)4 DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)3 WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)3 GraphWorkbench (edu.cmu.tetradapp.workbench.GraphWorkbench)3 IOException (java.io.IOException)3 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)2 MultiDataSetAlgorithm (edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm)2 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)2