Search in sources :

Example 1 with ColtDataSet

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

the class LoadTimeSeriesDataAction method loadTimeSeriesDataSet.

/**
 * Method loadDataSet_TabDelim
 */
private void loadTimeSeriesDataSet() {
    // select a file to load using the file chooser
    JFileChooser chooser = getJFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showOpenDialog(this.editor);
    // get the file
    File file = chooser.getSelectedFile();
    Preferences.userRoot().put("fileSaveLocation", file.getParent());
    try {
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line;
        StringTokenizer st;
        // read in variable name and set up DataSet.
        List<Node> variables = new LinkedList<>();
        st = new StringTokenizer(in.readLine());
        while (st.hasMoreTokens()) {
            String name = st.nextToken();
            ContinuousVariable var = new ContinuousVariable(name);
            variables.add(var);
        }
        DataSet dataSet = new ColtDataSet(0, variables);
        int row = -1;
        while ((line = in.readLine()) != null) {
            int col = -1;
            st = new StringTokenizer(line);
            while (st.hasMoreTokens()) {
                String literal = st.nextToken();
                if (literal.length() == 0) {
                    continue;
                }
                dataSet.setObject(row, ++col, literal);
            }
        }
        TimeSeriesData dataSet3 = new TimeSeriesData(dataSet.getDoubleData(), dataSet.getVariableNames());
        editor.getDataWrapper().setDataModel(dataSet3);
        firePropertyChange("modelChanged", null, null);
        editor.reset();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) StringTokenizer(java.util.StringTokenizer) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) TimeSeriesData(edu.cmu.tetrad.data.TimeSeriesData)

Example 2 with ColtDataSet

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

the class ScatterPlotDisplayPanelOld method paintComponent.

/**
 * Paints the histogram and related items.
 */
public void paintComponent(Graphics graphics) {
    double least = this.scatterPlot.getMinSample();
    double greatest = this.scatterPlot.getMaxSample();
    String minStr = format.format(least);
    String maxStr = format.format(greatest);
    Graphics2D g2d = (Graphics2D) graphics;
    FontMetrics fontMetrics = g2d.getFontMetrics();
    int widthMinStr = fontMetrics.stringWidth(minStr);
    int widthMaxStr = fontMetrics.stringWidth(maxStr);
    int maxWidth = Math.max(widthMinStr, widthMaxStr);
    PADDINGLEFT = maxWidth;
    PADDINGOTHER = 50;
    HEIGHT = 600 + PADDINGOTHER;
    WIDTH = 600 + PADDINGLEFT;
    SPACE = 2;
    DASH = 10;
    setSize(new Dimension(WIDTH + 2 * SPACE, HEIGHT));
    // set up variables.
    this.rectMap.clear();
    int height = HEIGHT - PADDINGOTHER;
    // draw background/surrounding box.
    g2d.setColor(this.getBackground());
    g2d.fillRect(0, 0, WIDTH + 2 * SPACE, HEIGHT);
    g2d.setColor(Color.WHITE);
    g2d.fillRect(PADDINGLEFT, 0, (WIDTH + SPACE) - PADDINGLEFT, height);
    // border
    g2d.setColor(LINE_COLOR);
    g2d.drawRect(PADDINGLEFT, 0, (WIDTH + SPACE) - PADDINGLEFT, height - 2 * SPACE);
    // draw the buttom line
    g2d.setColor(LINE_COLOR);
    // double least = Math.floor(this.scatterPlot.getMinSample());
    // double greatest = Math.ceil(this.scatterPlot.getMaxSample());
    g2d.drawString(minStr, PADDINGLEFT + 5, height + 15);
    g2d.drawLine(PADDINGLEFT, height + DASH, PADDINGOTHER, height);
    g2d.drawString(maxStr, WIDTH - widthMaxStr, height + 15);
    g2d.drawLine(WIDTH + SPACE, height + DASH, WIDTH + SPACE, height);
    // int size = (WIDTH - PADDINGLEFT) / 4;
    // draw the side line
    g2d.setColor(LINE_COLOR);
    int topY = 0;
    // String top = "" + Math.ceil(this.scatterPlot.getMaxSample());
    g2d.drawString(maxStr, PADDINGLEFT - fontMetrics.stringWidth(maxStr), topY + 10);
    g2d.drawLine(PADDINGLEFT - DASH, topY, PADDINGOTHER, topY);
    g2d.drawString(minStr, PADDINGLEFT - fontMetrics.stringWidth(minStr), height - 2);
    g2d.drawLine(PADDINGLEFT - DASH, height, PADDINGOTHER, height);
    // draw the origin lines if they should go on the screen -- first find out where they exist
    if (this.scatterPlot.getMinSample() < 0 && this.scatterPlot.getMaxSample() > 0) {
        double[] originLeft = plotPoint(least, 0, least, greatest);
        double[] originRight = plotPoint(greatest, 0, least, greatest);
        double[] originTop = plotPoint(0, least, least, greatest);
        double[] originBottom = plotPoint(0, greatest, least, greatest);
        g2d.drawLine((int) originLeft[0] + 2, (int) originLeft[1] + 2, (int) originRight[0] + 2, (int) originRight[1] + 2);
        g2d.drawLine((int) originTop[0] + 2, (int) originTop[1] + 2, (int) originBottom[0] + 2, (int) originBottom[1] + 2);
    }
    g2d.setColor(new Color(255, 0, 0));
    // draw each point in the indexSet from our ScatterPlot
    for (Object o : scatterPlot.getIndexSet()) {
        int i = (Integer) o;
        double x = this.scatterPlot.getxData()[i];
        double y = this.scatterPlot.getyData()[i];
        double[] result = plotPoint(x, y, least, greatest);
        g2d.fill(new Ellipse2D.Double(result[0], result[1], 4, 4));
    }
    // draw the regression line
    if (scatterPlot.isDrawRegLine()) {
        // RegressionRunner regRunner;
        RegressionDataset regData;
        /*
             * In the following code, the complement of the indexSet (for all integers <= (n - 1)) is
             * calculated.  This set of indices is removed from a copy of our original dataSet, such
             * that the ScatterPlot only contains the appropriate points for rendering.
             */
        Parameters params = new Parameters();
        Vector<String> regressors = new Vector();
        regressors.add(scatterPlot.getXVariable().getName());
        params.set("targetName", scatterPlot.getYVariable().getName());
        if (scatterPlot.getIndexSet().size() != scatterPlot.getDataSet().getNumRows()) {
            ColtDataSet newDataSet = new ColtDataSet((ColtDataSet) scatterPlot.getDataSet());
            int[] throwAway = new int[scatterPlot.getComplementIndexSet().size()];
            for (int j = 0; j < throwAway.length; j++) {
                throwAway[j] = (Integer) scatterPlot.getComplementIndexSet().get(j);
            }
            newDataSet.removeRows(throwAway);
            regData = new RegressionDataset(newDataSet);
        // regRunner = new RegressionRunner(new DataWrapper(newDataSet), params);
        } else {
            regData = new RegressionDataset(scatterPlot.getDataSet());
        // regRunner = new RegressionRunner(new DataWrapper(scatterPlot.dataSet), params);
        }
        // regRunner.execute();
        RegressionResult regResult = regData.regress(scatterPlot.getYVariable(), scatterPlot.getXVariable());
        double[] coef = regResult.getCoef();
        double[] regLeft = plotPoint(least, coef[0] + coef[1] * least, least, greatest);
        double[] regRight = plotPoint(greatest, coef[0] + coef[1] * greatest, least, greatest);
        g2d.setColor(LINE_COLOR);
        g2d.drawLine((int) regLeft[0] + 2, (int) regLeft[1] + 2, (int) regRight[0] + 2, (int) regRight[1] + 2);
    }
    // draw the display string.
    g2d.setColor(LINE_COLOR);
// g2d.drawString(getDisplayString(), PADDINGOTHER, HEIGHT - 5);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) Ellipse2D(java.awt.geom.Ellipse2D) RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) Vector(java.util.Vector) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 3 with ColtDataSet

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

the class TestColtDataSet method testMixed.

@Test
public void testMixed() {
    List<Node> variables = new LinkedList<>();
    DiscreteVariable x1 = new DiscreteVariable("X1");
    variables.add(x1);
    ContinuousVariable x2 = new ContinuousVariable("X2");
    variables.add(x2);
    DataSet dataSet = new ColtDataSet(5, variables);
    assertTrue(dataSet.getVariables().get(0) instanceof DiscreteVariable);
    assertTrue(dataSet.getVariables().get(1) instanceof ContinuousVariable);
    assertTrue(dataSet.getInt(0, 0) == -99);
    assertTrue(Double.isNaN(dataSet.getDouble(1, 0)));
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ColtDataSet

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

the class TestCellTable method setUp.

public final void setUp() {
    this.table = new CellTable(dims);
    // // Add data to table.
    List<Node> variables = new LinkedList<>();
    variables.add(new DiscreteVariable("X1", 2));
    variables.add(new DiscreteVariable("X2", 2));
    variables.add(new DiscreteVariable("X3", 2));
    variables.add(new DiscreteVariable("X4", 2));
    DataSet dataSet = new ColtDataSet(data.length, variables);
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[0].length; j++) {
            dataSet.setInt(i, j, data[i][j]);
        }
    }
    int[] indices = new int[] { 0, 1, 2, 3 };
    this.table.addToTable(dataSet, indices);
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) CellTable(edu.cmu.tetrad.data.CellTable) DataSet(edu.cmu.tetrad.data.DataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList)

Example 5 with ColtDataSet

use of edu.cmu.tetrad.data.ColtDataSet 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.");
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) TetradVector(edu.cmu.tetrad.util.TetradVector) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)28 DataSet (edu.cmu.tetrad.data.DataSet)24 Node (edu.cmu.tetrad.graph.Node)21 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)17 LinkedList (java.util.LinkedList)13 Test (org.junit.Test)12 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)9 RandomUtil (edu.cmu.tetrad.util.RandomUtil)6 Parameters (edu.cmu.tetrad.util.Parameters)3 ArrayList (java.util.ArrayList)3 KernelGaussian (edu.cmu.tetrad.search.kernel.KernelGaussian)2 DecimalFormat (java.text.DecimalFormat)2 ParseException (java.text.ParseException)2 KMeans (edu.cmu.tetrad.cluster.KMeans)1 CellTable (edu.cmu.tetrad.data.CellTable)1 DataModelList (edu.cmu.tetrad.data.DataModelList)1 TimeSeriesData (edu.cmu.tetrad.data.TimeSeriesData)1 Dag (edu.cmu.tetrad.graph.Dag)1 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)1 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)1