Search in sources :

Example 11 with DataReader

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

the class KnowledgeBoxEditor method menuBar.

private JMenuBar menuBar() {
    JMenuBar menuBar = new JMenuBar();
    JMenu file = new JMenu("File");
    menuBar.add(file);
    JMenuItem load = new JMenuItem("Load");
    JMenuItem save = new JMenuItem("Save");
    file.add(load);
    file.add(save);
    load.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.showOpenDialog(JOptionUtils.centeringComp());
        if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
            return;
        }
        final File selectedFile = chooser.getSelectedFile();
        if (selectedFile == null) {
            return;
        }
        Preferences.userRoot().put("fileSaveLocation", selectedFile.getParent());
        try {
            IKnowledge knowledge = new DataReader().parseKnowledge(selectedFile);
            if (knowledge == null) {
                throw new NullPointerException("No knowledge found in this file. Perhaps the formatting is off");
            }
            setKnowledge(knowledge);
            resetTabbedPane(KnowledgeBoxEditor.this.tabbedPane);
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
            e1.printStackTrace();
        }
    });
    save.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;
        }
        Preferences.userRoot().put("fileSaveLocation", selectedFile.getParent());
        try {
            Knowledge.saveKnowledge(knowledgeBoxModel.getKnowledge(), new FileWriter(selectedFile));
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
        }
    });
    return menuBar;
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) DataReader(edu.cmu.tetrad.data.DataReader) JFileChooser(javax.swing.JFileChooser) FileWriter(java.io.FileWriter) JMenuItem(javax.swing.JMenuItem) File(java.io.File) JMenuBar(javax.swing.JMenuBar) JMenu(javax.swing.JMenu) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Example 12 with DataReader

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

the class KnowledgeBoxEditor method textDisplay.

private Component textDisplay() {
    final JButton testButton = new JButton("Test");
    final JButton loadFromTextPaneButton = new JButton("Load from Text Pane");
    final JButton loadFromFileButton = new JButton("Load from File");
    testButton.addActionListener((e) -> {
        try {
            String text = getTextArea().getText();
            DataReader reader = new DataReader();
            reader.parseKnowledge(text.toCharArray());
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Looks good.");
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
        }
    });
    loadFromTextPaneButton.addActionListener((e) -> {
        try {
            String text = getTextArea().getText();
            DataReader reader = new DataReader();
            IKnowledge knowledge = reader.parseKnowledge(text.toCharArray());
            setKnowledge(knowledge);
            resetTabbedPane(KnowledgeBoxEditor.this.tabbedPane);
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
        }
    });
    loadFromFileButton.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.showOpenDialog(JOptionUtils.centeringComp());
        if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
            return;
        }
        final File file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }
        Preferences.userRoot().put("fileSaveLocation", file.getParent());
        try {
            IKnowledge knowledge = new DataReader().parseKnowledge(file);
            setKnowledge(knowledge);
            resetTabbedPane(KnowledgeBoxEditor.this.tabbedPane);
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
        }
    });
    Box b = Box.createVerticalBox();
    textArea = new JTextArea();
    resetTextDisplay();
    b.add(getTextArea());
    Box b2 = Box.createHorizontalBox();
    b2.add(Box.createHorizontalGlue());
    b2.add(testButton);
    b2.add(loadFromTextPaneButton);
    b2.add(loadFromFileButton);
    b.add(b2);
    return b;
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) DataReader(edu.cmu.tetrad.data.DataReader) JTextArea(javax.swing.JTextArea) JFileChooser(javax.swing.JFileChooser) JButton(javax.swing.JButton) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Example 13 with DataReader

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

the class ExternalAlgorithmBNTPc method search.

/**
 * Reads in the relevant graph from the file (see above) and returns it.
 */
public Graph search(DataModel dataSet, Parameters parameters) {
    int index = getIndex(dataSet);
    File file = new File(path, "/results/" + extDir + "/" + (simIndex + 1) + "/graph." + index + ".txt");
    System.out.println(file.getAbsolutePath());
    try {
        DataReader reader = new DataReader();
        reader.setVariablesSupplied(false);
        DataSet dataSet2 = reader.parseTabular(file);
        System.out.println("Loading graph from " + file.getAbsolutePath());
        Graph graph = GraphUtils.loadGraphBNTPcMatrix(dataSet.getVariables(), dataSet2);
        GraphUtils.circleLayout(graph, 225, 200, 150);
        return graph;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't parse graph.", e);
    }
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) IOException(java.io.IOException) File(java.io.File)

Example 14 with DataReader

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

the class FaskGraphs method loadFiles.

private void loadFiles(String path, Parameters parameters, String... contains) {
    DataReader reader = new DataReader();
    reader.setVariablesSupplied(true);
    reader.setDelimiter(DelimiterType.TAB);
    File dir = new File(path);
    File[] files = dir.listFiles();
    if (files == null) {
        throw new NullPointerException();
    }
    FILE: for (File file : files) {
        String name = file.getName();
        for (String s : contains) {
            if (!name.contains(s))
                continue FILE;
        }
        if (!name.contains("graph")) {
            try {
                if (name.contains("autistic")) {
                    types.add(true);
                    DataSet dataSet = reader.parseTabular(new File(path, name));
                    filenames.add(name);
                    datasets.add(dataSet);
                    Fask fask = new Fask();
                    Graph search = fask.search(dataSet, parameters);
                    graphs.add(search);
                } else if (name.contains("typical")) {
                    types.add(false);
                    DataSet dataSet = reader.parseTabular(new File(path, name));
                    filenames.add(name);
                    datasets.add(dataSet);
                    Fask fask = new Fask();
                    Graph search = fask.search(dataSet, parameters);
                    graphs.add(search);
                }
                System.out.println("Loaded " + name);
            } catch (IOException e) {
                System.out.println("File " + name + " could not be parsed.");
            }
        }
    }
    reconcileNames();
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) Fask(edu.cmu.tetrad.algcomparison.algorithm.multi.Fask) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) IOException(java.io.IOException) File(java.io.File)

Example 15 with DataReader

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

the class LoadMadelynData method createData.

@Override
public void createData(Parameters parameters) {
    this.dataSets = new ArrayList<>();
    for (int run = 1; run <= 10; run++) {
        File file = new File(directory + "/structure_" + structure + "_coeff" + run + "_" + suffix + ".txt");
        System.out.println("Loading data from " + file.getAbsolutePath());
        DataReader reader = new DataReader();
        reader.setVariablesSupplied(true);
        try {
            DataSet dataSet = reader.parseTabular(file);
            dataSets.add(dataSet);
            if (!(dataSet.isContinuous())) {
                throw new IllegalArgumentException("Not a continuous data set: " + dataSet.getName());
            }
        } catch (Exception e) {
            System.out.println("Couldn't parse " + file.getAbsolutePath());
        }
    }
    File parent = new File(new File(directory).getParent());
    File file2 = new File(parent + "/structure_" + structure + "_graph.txt");
    System.out.println("Loading graph from " + file2.getAbsolutePath());
    this.graph = GraphUtils.loadGraphTxt(file2);
    GraphUtils.circleLayout(this.graph, 225, 200, 150);
    if (parameters.get("numRuns") != null) {
        parameters.set("numRuns", parameters.get("numRuns"));
    } else {
        parameters.set("numRuns", dataSets.size());
    }
    System.out.println();
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) DataSet(edu.cmu.tetrad.data.DataSet) File(java.io.File)

Aggregations

DataReader (edu.cmu.tetrad.data.DataReader)15 File (java.io.File)14 IOException (java.io.IOException)10 DataSet (edu.cmu.tetrad.data.DataSet)8 IKnowledge (edu.cmu.tetrad.data.IKnowledge)4 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)3 Graph (edu.cmu.tetrad.graph.Graph)2 FileReader (java.io.FileReader)2 JFileChooser (javax.swing.JFileChooser)2 Fask (edu.cmu.tetrad.algcomparison.algorithm.multi.Fask)1 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 DeltaSextadTest (edu.cmu.tetrad.search.DeltaSextadTest)1 IntSextad (edu.cmu.tetrad.search.IntSextad)1 BufferedReader (java.io.BufferedReader)1 CharArrayWriter (java.io.CharArrayWriter)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Box (javax.swing.Box)1