Search in sources :

Example 1 with DagToPag2

use of edu.cmu.tetrad.search.DagToPag2 in project tetrad by cmu-phil.

the class TimeoutComparison method saveToFiles.

/**
 * Saves simulationWrapper data.
 *
 * @param dataPath The path to the directory where the simulationWrapper
 * data should be saved.
 * @param simulation The simulate used to generate the graphs and data.
 * @param parameters The parameters to be used in the simulationWrapper.
 */
public void saveToFiles(String dataPath, Simulation simulation, Parameters parameters) {
    List<SimulationWrapper> simulationWrappers = getSimulationWrappers(simulation, parameters);
    File dir0 = new File(dataPath);
    File dir;
    int i = 0;
    dir = new File(dir0, "save");
    // 
    // do {
    // dir = new File(dir0, "Simulation" + (++i));
    // } while (dir.exists());
    // if (dir.exists()) {
    // JOptionPane.showMessageDialog(JOptionUtils.centeringComp(),
    // "A file already exists named 'Simulation' in directory '" + dir0.getPath() + "'; \n" +
    // "please remove it first or move it out of the way.");
    // }
    deleteFilesThenDirectory(dir);
    try {
        int index = 0;
        for (SimulationWrapper simulationWrapper : simulationWrappers) {
            for (String param : simulationWrapper.getParameters()) {
                parameters.set(param, simulationWrapper.getValue(param));
            }
            simulationWrapper.createData(simulationWrapper.getSimulationSpecificParameters());
            index++;
            File subdir = new File(dir, "" + index);
            subdir.mkdirs();
            File dir1 = new File(subdir, "graph");
            File dir2 = new File(subdir, "data");
            dir1.mkdirs();
            dir2.mkdirs();
            File dir3 = null;
            if (isSavePatterns()) {
                dir3 = new File(subdir, "patterns");
                dir3.mkdirs();
            }
            File dir4 = null;
            if (isSavePags()) {
                dir4 = new File(subdir, "pags");
                dir4.mkdirs();
            }
            for (int j = 0; j < simulationWrapper.getNumDataModels(); j++) {
                File file2 = new File(dir1, "graph." + (j + 1) + ".txt");
                Graph graph = simulationWrapper.getTrueGraph(j);
                GraphUtils.saveGraph(graph, file2, false);
                File file = new File(dir2, "data." + (j + 1) + ".txt");
                Writer out = new FileWriter(file);
                DataModel dataModel = (DataModel) simulationWrapper.getDataModel(j);
                DataWriter.writeRectangularData((DataSet) dataModel, out, '\t');
                out.close();
                if (isSavePatterns()) {
                    File file3 = new File(dir3, "pattern." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(SearchGraphUtils.patternForDag(graph), file3, false);
                }
                if (isSavePags()) {
                    File file4 = new File(dir4, "pag." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(new DagToPag2(graph).convert(), file4, false);
                }
            }
            PrintStream out = new PrintStream(new FileOutputStream(new File(subdir, "parameters.txt")));
            out.println(simulationWrapper.getDescription());
            // out.println();
            out.println(simulationWrapper.getSimulationSpecificParameters());
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) FileWriter(java.io.FileWriter) IOException(java.io.IOException) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel) FileOutputStream(java.io.FileOutputStream) DagToPag2(edu.cmu.tetrad.search.DagToPag2) File(java.io.File) Writer(java.io.Writer) DataWriter(edu.cmu.tetrad.data.DataWriter) FileWriter(java.io.FileWriter)

Example 2 with DagToPag2

use of edu.cmu.tetrad.search.DagToPag2 in project tetrad by cmu-phil.

the class Comparison method saveToFiles.

/**
 * Saves simulationWrapper data.
 *
 * @param dataPath   The path to the directory where the simulationWrapper data should be saved.
 * @param simulation The simulate used to generate the graphs and data.
 * @param parameters The parameters to be used in the simulationWrapper.
 */
public void saveToFiles(String dataPath, Simulation simulation, Parameters parameters) {
    File dir0 = new File(dataPath);
    File dir;
    // int i = 0;
    dir = new File(dir0, "save");
    // 
    // do {
    // dir = new File(dir0, "Simulation" + (++i));
    // } while (dir.exists());
    // if (dir.exists()) {
    // JOptionPane.showMessageDialog(JOptionUtils.centeringComp(),
    // "A file already exists named 'Simulation' in directory '" + dir0.getPath() + "'; \n" +
    // "please remove it first or move it out of the way.");
    // }
    deleteFilesThenDirectory(dir);
    try {
        int numDataSets = simulation.getNumDataModels();
        if (numDataSets <= 0) {
            File dir1 = new File(dir, "graph");
            File dir2 = new File(dir, "data");
            dir1.mkdirs();
            dir2.mkdirs();
            PrintStream out = new PrintStream(new FileOutputStream(new File(dir, "parameters.txt")));
            out.println(simulation.getDescription());
            out.println(parameters);
            out.close();
            return;
        }
        List<SimulationWrapper> simulationWrappers = getSimulationWrappers(simulation, parameters);
        int index = 0;
        for (SimulationWrapper simulationWrapper : simulationWrappers) {
            for (String param : simulationWrapper.getParameters()) {
                parameters.set(param, simulationWrapper.getValue(param));
            }
            simulationWrapper.createData(simulationWrapper.getSimulationSpecificParameters());
            File subdir = dir;
            if (simulationWrappers.size() > 1) {
                index++;
                subdir = new File(dir, "" + index);
                subdir.mkdirs();
            }
            File dir1 = new File(subdir, "graph");
            File dir2 = new File(subdir, "data");
            dir1.mkdirs();
            dir2.mkdirs();
            File dir3 = null;
            if (isSavePatterns()) {
                dir3 = new File(subdir, "patterns");
                dir3.mkdirs();
            }
            File dir4 = null;
            if (isSavePags()) {
                dir4 = new File(subdir, "pags");
                dir4.mkdirs();
            }
            for (int j = 0; j < simulationWrapper.getNumDataModels(); j++) {
                File file2 = new File(dir1, "graph." + (j + 1) + ".txt");
                Graph graph = simulationWrapper.getTrueGraph(j);
                GraphUtils.saveGraph(graph, file2, false);
                File file = new File(dir2, "data." + (j + 1) + ".txt");
                Writer out = new FileWriter(file);
                DataModel dataModel = (DataModel) simulationWrapper.getDataModel(j);
                DataWriter.writeRectangularData((DataSet) dataModel, out, '\t');
                out.close();
                if (isSavePatterns()) {
                    File file3 = new File(dir3, "pattern." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(SearchGraphUtils.patternForDag(graph), file3, false);
                }
                if (isSavePags()) {
                    File file4 = new File(dir4, "pag." + (j + 1) + ".txt");
                    GraphUtils.saveGraph(new DagToPag2(graph).convert(), file4, false);
                }
            }
            PrintStream out = new PrintStream(new FileOutputStream(new File(subdir, "parameters.txt")));
            out.println(simulationWrapper.getDescription());
            out.println(simulationWrapper.getSimulationSpecificParameters());
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) DagToPag2(edu.cmu.tetrad.search.DagToPag2)

Aggregations

TakesInitialGraph (edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph)2 DagToPag2 (edu.cmu.tetrad.search.DagToPag2)2 DataModel (edu.cmu.tetrad.data.DataModel)1 DataWriter (edu.cmu.tetrad.data.DataWriter)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Graph (edu.cmu.tetrad.graph.Graph)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Writer (java.io.Writer)1