Search in sources :

Example 26 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class TestBoxDataSet method testRowSubset.

@Test
public void testRowSubset() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new BoxDataSet(new DoubleDataBox(rows, variables.size()), variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    double d = dataSet.getDouble(2, 0);
    DataSet _dataSet = dataSet.subsetRows(new int[] { 2, 3, 4 });
    assertEquals(3, _dataSet.getNumRows());
    assertEquals(d, _dataSet.getDouble(0, 0), 0.001);
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 27 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class TestBoxDataSet method testRemoveColumn.

@Test
public void testRemoveColumn() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new BoxDataSet(new DoubleDataBox(rows, variables.size()), variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    int[] _cols = new int[2];
    _cols[0] = 1;
    _cols[1] = 2;
    dataSet.removeCols(_cols);
    List<Node> _variables = new LinkedList<>(variables);
    _variables.remove(2);
    _variables.remove(1);
    assertEquals(dataSet.getVariables(), _variables);
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 28 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class TestBoxDataSet method testDiscrete.

@Test
public void testDiscrete() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        DiscreteVariable variable = new DiscreteVariable("X" + (i + 1), 3);
        variables.add(variable);
    }
    DataSet dataSet = new BoxDataSet(new DoubleDataBox(rows, variables.size()), variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setInt(i, j, randomUtil.nextInt(3));
        }
    }
    BoxDataSet _dataSet = new BoxDataSet((BoxDataSet) dataSet);
    assertEquals(dataSet, _dataSet);
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 29 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class TestDataLoadersRoundtrip method testDiscreteRoundtrip.

@Test
public void testDiscreteRoundtrip() {
    setUp();
    try {
        for (int i = 0; i < 1; i++) {
            List<Node> nodes = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                nodes.add(new ContinuousVariable("X" + (j + 1)));
            }
            Graph randomGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 8, 30, 15, 15, false));
            Dag dag = new Dag(randomGraph);
            BayesPm bayesPm1 = new BayesPm(dag);
            MlBayesIm bayesIm1 = new MlBayesIm(bayesPm1, MlBayesIm.RANDOM);
            DataSet dataSet = bayesIm1.simulateData(10, false);
            new File("target/test_data").mkdir();
            FileWriter fileWriter = new FileWriter("target/test_data/roundtrip.dat");
            Writer writer = new PrintWriter(fileWriter);
            DataWriter.writeRectangularData(dataSet, writer, '\t');
            writer.close();
            File file = new File("target/test_data/roundtrip.dat");
            DataReader reader = new DataReader();
            reader.setKnownVariables(dataSet.getVariables());
            DataSet _dataSet = reader.parseTabular(file);
            assertTrue(dataSet.equals(_dataSet));
        }
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Dag(edu.cmu.tetrad.graph.Dag) Graph(edu.cmu.tetrad.graph.Graph) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 30 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class TestDataLoadersRoundtrip method testContinuousRoundtrip.

@Test
public void testContinuousRoundtrip() {
    setUp();
    try {
        List<Node> nodes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            nodes.add(new ContinuousVariable("X" + (i + 1)));
        }
        Graph randomGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
        SemPm semPm1 = new SemPm(randomGraph);
        SemIm semIm1 = new SemIm(semPm1);
        DataSet dataSet = semIm1.simulateData(10, false);
        FileWriter fileWriter = new FileWriter("target/test_data/roundtrip.dat");
        Writer writer = new PrintWriter(fileWriter);
        DataWriter.writeRectangularData(dataSet, writer, ',');
        writer.close();
        // 
        new File("test_data").mkdir();
        File file = new File("target/test_data/roundtrip.dat");
        DataReader reader = new DataReader();
        reader.setDelimiter(DelimiterType.COMMA);
        DataSet _dataSet = reader.parseTabular(file);
        assertTrue(dataSet.equals(_dataSet));
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Dag(edu.cmu.tetrad.graph.Dag) Graph(edu.cmu.tetrad.graph.Graph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Aggregations

Node (edu.cmu.tetrad.graph.Node)674 ArrayList (java.util.ArrayList)129 Graph (edu.cmu.tetrad.graph.Graph)106 GraphNode (edu.cmu.tetrad.graph.GraphNode)64 DataSet (edu.cmu.tetrad.data.DataSet)59 LinkedList (java.util.LinkedList)55 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)48 Test (org.junit.Test)48 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)46 List (java.util.List)45 Dag (edu.cmu.tetrad.graph.Dag)41 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)41 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)40 ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)37 Endpoint (edu.cmu.tetrad.graph.Endpoint)29 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)26 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)25 Edge (edu.cmu.tetrad.graph.Edge)23 SemIm (edu.cmu.tetrad.sem.SemIm)19 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)19