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;
}
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;
}
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);
}
}
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();
}
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();
}
Aggregations