Search in sources :

Example 1 with DataReader

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

the class TestDeltaSextadTest method testBollenExampleb.

@Test
public void testBollenExampleb() {
    DataSet data = null;
    try {
        String name = "src/test/resources/dataLG.txt";
        DataReader reader = new DataReader();
        data = reader.parseTabular(new File(name));
    } catch (IOException e) {
        e.printStackTrace();
    }
    int m1 = 0;
    int m2 = 1;
    int m3 = 2;
    int m4 = 3;
    int m5 = 4;
    int m6 = 5;
    IntSextad t1 = new IntSextad(m1, m2, m3, m4, m5, m6);
    IntSextad t2 = new IntSextad(m1, m2, m4, m3, m5, m6);
    IntSextad t3 = new IntSextad(m1, m2, m5, m3, m4, m6);
    IntSextad t4 = new IntSextad(m1, m2, m6, m3, m4, m5);
    IntSextad t5 = new IntSextad(m1, m3, m4, m2, m5, m6);
    IntSextad t6 = new IntSextad(m1, m3, m5, m2, m4, m6);
    IntSextad t7 = new IntSextad(m1, m3, m6, m2, m4, m5);
    IntSextad t8 = new IntSextad(m1, m4, m5, m2, m3, m6);
    IntSextad t9 = new IntSextad(m1, m4, m6, m2, m3, m5);
    IntSextad t10 = new IntSextad(m1, m5, m6, m2, m3, m4);
    DeltaSextadTest test = new DeltaSextadTest(data);
    IntSextad[] _sextads = { t2, t5, t10, t3, t6 };
    double p = test.getPValue(_sextads);
    assertEquals(0.21, p, 0.01);
    _sextads = new IntSextad[] { t10 };
    p = test.getPValue(_sextads);
    assertEquals(0.30, p, 0.01);
// This should throw an exception but doesn't.
// MySextad[] _sextads = {t1, t2, t3, t4, t5, t6, t7, t8, t9, t10};
}
Also used : IntSextad(edu.cmu.tetrad.search.IntSextad) DataReader(edu.cmu.tetrad.data.DataReader) DataSet(edu.cmu.tetrad.data.DataSet) IOException(java.io.IOException) File(java.io.File) DeltaSextadTest(edu.cmu.tetrad.search.DeltaSextadTest) Test(org.junit.Test) DeltaSextadTest(edu.cmu.tetrad.search.DeltaSextadTest)

Example 2 with DataReader

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

the class LoadContinuousDataAndSingleGraph method createData.

@Override
public void createData(Parameters parameters) {
    this.dataSets = new ArrayList<>();
    File dir = new File(path + "/" + subdir);
    if (dir.exists()) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (!file.getName().endsWith(".txt"))
                continue;
            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 dir2 = new File(path + "/graph");
    if (dir2.exists()) {
        File[] files = dir2.listFiles();
        if (files.length != 1) {
            throw new IllegalArgumentException("Expecting exactly one graph file.");
        }
        File file = files[0];
        System.out.println("Loading graph from " + file.getAbsolutePath());
        this.graph = GraphUtils.loadGraphTxt(file);
        // if (!graph.isAdjacentTo(graph.getNode("X3"), graph.getNode("X4"))) {
        // graph.addUndirectedEdge(graph.getNode("X3"), graph.getNode("X4"));
        // }
        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)

Example 3 with DataReader

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

the class ExternalAlgorithmPcalgPc 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(true);
        DataSet dataSet2 = reader.parseTabular(file);
        System.out.println("Loading graph from " + file.getAbsolutePath());
        Graph graph = loadGraphPcAlgMatrix(dataSet2);
        GraphUtils.circleLayout(graph, 225, 200, 150);
        return graph;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't parse graph.");
    }
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) DataSet(edu.cmu.tetrad.data.DataSet) IOException(java.io.IOException) File(java.io.File)

Example 4 with DataReader

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

the class LoadContinuousDataAndGraphs method createData.

@Override
public void createData(Parameters parameters) {
    this.dataSets = new ArrayList<>();
    if (new File(path + "/data").exists()) {
        int numDataSets = new File(path + "/data").listFiles().length;
        try {
            for (int i = 0; i < numDataSets; i++) {
                File file2 = new File(path + "/graph/graph." + (i + 1) + ".txt");
                System.out.println("Loading graph from " + file2.getAbsolutePath());
                this.graphs.add(GraphUtils.loadGraphTxt(file2));
                edu.cmu.tetrad.graph.GraphUtils.circleLayout(this.graphs.get(i), 225, 200, 150);
                File file1 = new File(path + "/data/data." + (i + 1) + ".txt");
                System.out.println("Loading data from " + file1.getAbsolutePath());
                DataReader reader = new DataReader();
                reader.setVariablesSupplied(true);
                dataSets.add(reader.parseTabular(file1));
            }
            File paramFile = new File(path, "parameters.txt");
            System.out.println("Loading parameters from " + paramFile.getAbsolutePath());
            BufferedReader r = new BufferedReader(new FileReader(paramFile));
            String line;
            while ((line = r.readLine()) != null) {
                if (line.contains(" = ")) {
                    String[] tokens = line.split(" = ");
                    String key = tokens[0];
                    String value = tokens[1];
                    usedParameters.add(key);
                    try {
                        double _value = Double.parseDouble(value);
                        parameters.set(key, _value);
                    } catch (NumberFormatException e) {
                        if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
                            parameters.set(key, Boolean.valueOf(value));
                        } else {
                            parameters.set(key, value);
                        }
                    }
                    System.out.println(key + " : " + value);
                }
            }
            parameters.set("numRuns", numDataSets);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 5 with DataReader

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

the class LoadDataFromFileWithoutGraph method createData.

@Override
public void createData(Parameters parameters) {
    try {
        File file = new File(path);
        System.out.println("Loading data from " + file.getAbsolutePath());
        DataReader reader = new DataReader();
        reader.setVariablesSupplied(false);
        this.dataSet = reader.parseTabular(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DataReader(edu.cmu.tetrad.data.DataReader) IOException(java.io.IOException) 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