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