use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class ExploreAutisticsNeurotypicals method printDataTranspose.
private static void printDataTranspose(String path, String prefix, DataSet dataSet) {
List<Node> nodes = dataSet.getVariables();
Node group = dataSet.getVariable("Group");
List<Node> _nodes = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i) == group) {
_nodes.add(group);
} else {
_nodes.add(new ContinuousVariable("X" + (i + 1)));
}
}
TetradMatrix m = dataSet.getDoubleData();
TetradMatrix mt = m.transpose();
List<Node> tvars = new ArrayList<>();
for (int i = 0; i < mt.columns(); i++) tvars.add(new ContinuousVariable("S" + (i + 1)));
dataSet = new BoxDataSet(new DoubleDataBox(mt.toArray()), tvars);
// dataSet = new BoxDataSet(new DoubleDataBox(dataSet.getDoubleData().toArray()), _nodes);
dataSet.setNumberFormat(new DecimalFormat("0"));
File file1 = new File(path, prefix + ".data.txt");
File file2 = new File(path, prefix + ".dict.txt");
try {
PrintStream out1 = new PrintStream(new FileOutputStream(file1));
PrintStream out2 = new PrintStream(new FileOutputStream(file2));
out1.println(dataSet);
out1.close();
for (int i = 0; i < nodes.size(); i++) {
out2.println(_nodes.get(i) + "\t" + nodes.get(i));
}
out2.println("Group");
out2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class TestColtDataSet method testPermuteRows.
@Test
public void testPermuteRows() {
ContinuousVariable x1 = new ContinuousVariable("X1");
ContinuousVariable x2 = new ContinuousVariable("X2");
List<Node> nodes = new ArrayList<>();
nodes.add(x1);
nodes.add(x2);
DataSet dataSet = new ColtDataSet(3, nodes);
RandomUtil randomUtil = RandomUtil.getInstance();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
dataSet.setDouble(i, j, randomUtil.nextDouble());
}
}
ColtDataSet _dataSet = new ColtDataSet((ColtDataSet) dataSet);
dataSet.permuteRows();
I: for (int i = 0; i < dataSet.getNumRows(); i++) {
TetradVector v = _dataSet.getDoubleData().getRow(i);
for (int j = 0; j < dataSet.getNumRows(); j++) {
TetradVector w = dataSet.getDoubleData().getRow(j);
if (v.equals(w)) {
continue I;
}
}
fail("Missing row in permutation.");
}
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class TestColtDataSet method testContinuous.
@Test
public final void testContinuous() {
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 ColtDataSet(rows, _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());
}
}
List<Node> variables = dataSet.getVariables();
List<Node> newVars = new LinkedList<>();
newVars.add(variables.get(2));
newVars.add(variables.get(4));
DataSet _dataSet = dataSet.subsetColumns(newVars);
assertEquals(dataSet.getDoubleData().getColumn(2).get(0), _dataSet.getDoubleData().getColumn(0).get(0), .001);
assertEquals(dataSet.getDoubleData().getColumn(4).get(0), _dataSet.getDoubleData().getColumn(1).get(0), .001);
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class TestColtDataSet method testRemoveRows.
@Test
public void testRemoveRows() {
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 ColtDataSet(rows, 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 numRows = dataSet.getNumRows();
double d = dataSet.getDouble(3, 0);
int[] _rows = new int[2];
_rows[0] = 1;
_rows[1] = 2;
dataSet.removeRows(_rows);
assertEquals(numRows - 2, dataSet.getNumRows());
assertEquals(d, dataSet.getDouble(1, 0), 0.001);
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class TestDag method checkAddRemoveNodes.
private void checkAddRemoveNodes(Dag graph) {
Node x1 = new GraphNode("x1");
Node x2 = new GraphNode("x2");
Node x3 = new GraphNode("x3");
Node x4 = new GraphNode("x4");
Node x5 = new GraphNode("x5");
graph.addNode(x1);
graph.addNode(x2);
graph.addNode(x3);
graph.addNode(x4);
graph.addNode(x5);
graph.addDirectedEdge(x1, x2);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x3, x4);
graph.addDirectedEdge(x5, x4);
List<Node> children = graph.getChildren(x1);
List<Node> parents = graph.getParents(x4);
assertTrue(children.contains(x2));
assertTrue(parents.contains(x3));
assertTrue(parents.contains(x5));
assertTrue(graph.isDConnectedTo(x1, x3, Collections.EMPTY_LIST));
assertTrue(graph.existsDirectedPathFromTo(x1, x4));
assertTrue(!graph.existsDirectedPathFromTo(x1, x5));
assertTrue(graph.isAncestorOf(x2, x4));
assertTrue(!graph.isAncestorOf(x4, x2));
assertTrue(graph.isDescendentOf(x4, x2));
assertTrue(!graph.isDescendentOf(x2, x4));
}
Aggregations