use of edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader in project tetrad by cmu-phil.
the class DataLoaderSettings method loadDataWithSettings.
/**
* Kevin's fast data reader
*
* @param file
* @return DataModel on success
*/
public DataModel loadDataWithSettings(File file) throws IOException {
DataModel dataModel = null;
Delimiter delimiter = getDelimiterType();
boolean hasHeader = isVarNamesFirstRow();
String commentMarker = getCommentMarker();
String missingValueMarker = getMissingValueMarker();
if (tabularRadioButton.isSelected()) {
TabularDataReader dataReader = null;
// Continuous, discrete, mixed
if (contRadioButton.isSelected()) {
dataReader = new ContinuousTabularDataFileReader(file, delimiter);
} else if (discRadioButton.isSelected()) {
dataReader = new VerticalDiscreteTabularDataReader(file, delimiter);
} else if (mixedRadioButton.isSelected()) {
dataReader = new MixedTabularDataFileReader(getMaxNumOfDiscCategories(), file, delimiter);
} else {
throw new UnsupportedOperationException("Unsupported data type!");
}
// Header in first row or not
dataReader.setHasHeader(hasHeader);
// Set comment marker
dataReader.setCommentMarker(commentMarker);
dataReader.setMissingValueMarker(missingValueMarker);
// Set the quote character
if (doubleQuoteRadioButton.isSelected()) {
dataReader.setQuoteCharacter('"');
}
if (singleQuoteRadioButton.isSelected()) {
dataReader.setQuoteCharacter('\'');
}
Dataset dataset;
// Handle case ID column based on different selections
if (idNoneRadioButton.isSelected()) {
// No column exclusion
dataset = dataReader.readInData();
} else if (idUnlabeledFirstColRadioButton.isSelected()) {
// Exclude the first column
dataset = dataReader.readInData(new int[] { 1 });
} else if (idLabeledColRadioButton.isSelected() && !idStringField.getText().isEmpty()) {
// Exclude the specified labled column
dataset = dataReader.readInData(new HashSet<>(Arrays.asList(new String[] { idStringField.getText() })));
} else {
throw new UnsupportedOperationException("Unexpected 'Case ID column to ignore' selection.");
}
// Box Dataset to DataModel
dataModel = DataConvertUtils.toDataModel(dataset);
} else if (covarianceRadioButton.isSelected()) {
// Covariance data can only be continuous
CovarianceDataReader dataReader = new LowerCovarianceDataReader(file, delimiter);
// Set comment marker
dataReader.setCommentMarker(commentMarker);
// Set the quote character
if (doubleQuoteRadioButton.isSelected()) {
dataReader.setQuoteCharacter('"');
}
if (singleQuoteRadioButton.isSelected()) {
dataReader.setQuoteCharacter('\'');
}
Dataset dataset = dataReader.readInData();
// Box Dataset to DataModel
dataModel = DataConvertUtils.toDataModel(dataset);
} else {
throw new UnsupportedOperationException("Unsupported selection of File Type!");
}
return dataModel;
}
use of edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader in project tetrad by cmu-phil.
the class GdistanceApply method main.
public static void main(String... args) {
double xdist = 2.4;
double ydist = 2.4;
double zdist = 2;
long timestart = System.nanoTime();
System.out.println("Loading first graph");
Graph graph1 = GraphUtils.loadGraphTxt(new File("Motion_Corrected_Graphs/singlesub_motion_graph_025_04.txt"));
long timegraph1 = System.nanoTime();
// System.out.println(graph1);
System.out.println("Done loading first graph. Elapsed time: " + (timegraph1 - timestart) / 1000000000 + "s");
System.out.println("Loading second graph");
Graph graph2 = GraphUtils.loadGraphTxt(new File("Motion_Corrected_Graphs/singlesub_motion_graph_027_04.txt"));
long timegraph2 = System.nanoTime();
System.out.println("Done loading second graph. Elapsed time: " + (timegraph2 - timegraph1) / 1000000000 + "s");
// +++++++++ these steps are specifically for the motion corrected fMRI graphs ++++++++++++
graph1.removeNode(graph1.getNode("Motion_1"));
graph1.removeNode(graph1.getNode("Motion_2"));
graph1.removeNode(graph1.getNode("Motion_3"));
graph1.removeNode(graph1.getNode("Motion_4"));
graph1.removeNode(graph1.getNode("Motion_5"));
graph1.removeNode(graph1.getNode("Motion_6"));
graph2.removeNode(graph2.getNode("Motion_1"));
graph2.removeNode(graph2.getNode("Motion_2"));
graph2.removeNode(graph2.getNode("Motion_3"));
graph2.removeNode(graph2.getNode("Motion_4"));
graph2.removeNode(graph2.getNode("Motion_5"));
graph2.removeNode(graph2.getNode("Motion_6"));
// load the location map
String workingDirectory = System.getProperty("user.dir");
System.out.println(workingDirectory);
Path mapPath = Paths.get("coords.txt");
System.out.println(mapPath);
TabularDataReader dataReaderMap = new ContinuousTabularDataFileReader(mapPath.toFile(), Delimiter.COMMA);
try {
DataSet locationMap = (DataSet) DataConvertUtils.toDataModel(dataReaderMap.readInData());
long timegraph3 = System.nanoTime();
System.out.println("Done loading location map. Elapsed time: " + (timegraph3 - timegraph2) / 1000000000 + "s");
System.out.println("Running Gdistance");
Gdistance gdist = new Gdistance(locationMap, xdist, ydist, zdist);
List<Double> distance = gdist.distances(graph1, graph2);
System.out.println(distance);
System.out.println("Done running Distance. Elapsed time: " + (System.nanoTime() - timegraph3) / 1000000000 + "s");
System.out.println("Total elapsed time: " + (System.nanoTime() - timestart) / 1000000000 + "s");
PrintWriter writer = new PrintWriter("Gdistances.txt", "UTF-8");
writer.println(distance);
writer.close();
} catch (Exception IOException) {
IOException.printStackTrace();
}
}
use of edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader in project tetrad by cmu-phil.
the class GdistanceTest method main.
public static void main(String... args) {
// first generate a couple random graphs
int numVars = 16;
int numEdges = 16;
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable("X" + i));
}
Graph testdag1 = GraphUtils.randomGraphRandomForwardEdges(vars, 0, numEdges, 30, 15, 15, false, true);
Graph testdag2 = GraphUtils.randomGraphRandomForwardEdges(vars, 0, numEdges, 30, 15, 15, false, true);
// System.out.println(testdag1);
// load the location map
String workingDirectory = System.getProperty("user.dir");
System.out.println(workingDirectory);
Path mapPath = Paths.get("locationMap.txt");
System.out.println(mapPath);
TabularDataReader dataReaderMap = new ContinuousTabularDataFileReader(mapPath.toFile(), Delimiter.COMMA);
try {
DataSet locationMap = (DataSet) DataConvertUtils.toDataModel(dataReaderMap.readInData());
// System.out.println(locationMap);
// then compare their distance
double xdist = 2.4;
double ydist = 2.4;
double zdist = 2;
Gdistance gdist = new Gdistance(locationMap, xdist, ydist, zdist);
List<Double> output = gdist.distances(testdag1, testdag2);
System.out.println(output);
PrintWriter writer = new PrintWriter("Gdistances.txt", "UTF-8");
writer.println(output);
writer.close();
} catch (Exception IOException) {
IOException.printStackTrace();
}
}
use of edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader in project tetrad by cmu-phil.
the class HsimEvalFromData method main.
public static void main(String[] args) {
long timestart = System.nanoTime();
System.out.println("Beginning Evaluation");
String nl = System.lineSeparator();
String output = "Simulation edu.cmu.tetrad.study output comparing Fsim and Hsim on predicting graph discovery accuracy" + nl;
int iterations = 100;
int vars = 20;
int cases = 500;
int edgeratio = 3;
List<Integer> hsimRepeat = Arrays.asList(40);
List<Integer> fsimRepeat = Arrays.asList(40);
List<PRAOerrors>[] fsimErrsByPars = new ArrayList[fsimRepeat.size()];
int whichFrepeat = 0;
for (int frepeat : fsimRepeat) {
fsimErrsByPars[whichFrepeat] = new ArrayList<PRAOerrors>();
whichFrepeat++;
}
List<PRAOerrors>[][] hsimErrsByPars = new ArrayList[1][hsimRepeat.size()];
// System.out.println(resimSize.size()+" "+hsimRepeat.size());
int whichHrepeat;
whichHrepeat = 0;
for (int hrepeat : hsimRepeat) {
// System.out.println(whichrsize+" "+whichHrepeat);
hsimErrsByPars[0][whichHrepeat] = new ArrayList<PRAOerrors>();
whichHrepeat++;
}
// !(*%(@!*^!($%!^ START ITERATING HERE !#$%(*$#@!^(*!$*%(!$#
try {
for (int iterate = 0; iterate < iterations; iterate++) {
System.out.println("iteration " + iterate);
// @#$%@$%^@$^@$^@%$%@$#^ LOADING THE DATA AND GRAPH @$#%%*#^##*^$#@%$
DataSet data1;
Graph graph1 = GraphUtils.loadGraphTxt(new File("graph/graph.1.txt"));
Dag odag = new Dag(graph1);
Set<String> eVars = new HashSet<String>();
eVars.add("MULT");
Path dataFile = Paths.get("data/data.1.txt");
TabularDataReader dataReader = new ContinuousTabularDataFileReader(dataFile.toFile(), Delimiter.TAB);
data1 = (DataSet) DataConvertUtils.toDataModel(dataReader.readInData(eVars));
vars = data1.getNumColumns();
cases = data1.getNumRows();
edgeratio = 3;
// !#@^$@&%^!#$!&@^ CALCULATING TARGET ERRORS $%$#@^@!%!#^$!%$#%
ICovarianceMatrix newcov = new CovarianceMatrixOnTheFly(data1);
SemBicScore oscore = new SemBicScore(newcov);
Fges ofgs = new Fges(oscore);
ofgs.setVerbose(false);
ofgs.setNumPatternsToStore(0);
// ***********This is the original FGS output on the data
Graph oFGSGraph = ofgs.search();
PRAOerrors oErrors = new PRAOerrors(HsimUtils.errorEval(oFGSGraph, odag), "target errors");
// **then step 1: full resim. iterate through the combinations of estimator parameters (just repeat num)
for (whichFrepeat = 0; whichFrepeat < fsimRepeat.size(); whichFrepeat++) {
ArrayList<PRAOerrors> errorsList = new ArrayList<PRAOerrors>();
for (int r = 0; r < fsimRepeat.get(whichFrepeat); r++) {
PatternToDag pickdag = new PatternToDag(oFGSGraph);
Graph fgsDag = pickdag.patternToDagMeek();
Dag fgsdag2 = new Dag(fgsDag);
// then fit an IM to this dag and the data. GeneralizedSemEstimator seems to bug out
// GeneralizedSemPm simSemPm = new GeneralizedSemPm(fgsdag2);
// GeneralizedSemEstimator gsemEstimator = new GeneralizedSemEstimator();
// GeneralizedSemIm fittedIM = gsemEstimator.estimate(simSemPm, oData);
SemPm simSemPm = new SemPm(fgsdag2);
// BayesPm simBayesPm = new BayesPm(fgsdag2, bayesPm);
SemEstimator simSemEstimator = new SemEstimator(data1, simSemPm);
SemIm fittedIM = simSemEstimator.estimate();
DataSet simData = fittedIM.simulateData(data1.getNumRows(), false);
// after making the full resim data (simData), run FGS on that
ICovarianceMatrix simcov = new CovarianceMatrixOnTheFly(simData);
SemBicScore simscore = new SemBicScore(simcov);
Fges simfgs = new Fges(simscore);
simfgs.setVerbose(false);
simfgs.setNumPatternsToStore(0);
Graph simGraphOut = simfgs.search();
PRAOerrors simErrors = new PRAOerrors(HsimUtils.errorEval(simGraphOut, fgsdag2), "Fsim errors " + r);
errorsList.add(simErrors);
}
PRAOerrors avErrors = new PRAOerrors(errorsList, "Average errors for Fsim at repeat=" + fsimRepeat.get(whichFrepeat));
// if (verbosity>3) System.out.println(avErrors.allToString());
// ****calculate the squared errors of prediction, store all these errors in a list
double FsimAR2 = (avErrors.getAdjRecall() - oErrors.getAdjRecall()) * (avErrors.getAdjRecall() - oErrors.getAdjRecall());
double FsimAP2 = (avErrors.getAdjPrecision() - oErrors.getAdjPrecision()) * (avErrors.getAdjPrecision() - oErrors.getAdjPrecision());
double FsimOR2 = (avErrors.getOrientRecall() - oErrors.getOrientRecall()) * (avErrors.getOrientRecall() - oErrors.getOrientRecall());
double FsimOP2 = (avErrors.getOrientPrecision() - oErrors.getOrientPrecision()) * (avErrors.getOrientPrecision() - oErrors.getOrientPrecision());
PRAOerrors Fsim2 = new PRAOerrors(new double[] { FsimAR2, FsimAP2, FsimOR2, FsimOP2 }, "squared errors for Fsim at repeat=" + fsimRepeat.get(whichFrepeat));
// add the fsim squared errors to the appropriate list
fsimErrsByPars[whichFrepeat].add(Fsim2);
}
// **then step 2: hybrid sim. iterate through combos of params (repeat num, resimsize)
for (whichHrepeat = 0; whichHrepeat < hsimRepeat.size(); whichHrepeat++) {
HsimRepeatAC study = new HsimRepeatAC(data1);
PRAOerrors HsimErrors = new PRAOerrors(study.run(1, hsimRepeat.get(whichHrepeat)), "Hsim errors" + "at rsize=" + 1 + " repeat=" + hsimRepeat.get(whichHrepeat));
// ****calculate the squared errors of prediction
double HsimAR2 = (HsimErrors.getAdjRecall() - oErrors.getAdjRecall()) * (HsimErrors.getAdjRecall() - oErrors.getAdjRecall());
double HsimAP2 = (HsimErrors.getAdjPrecision() - oErrors.getAdjPrecision()) * (HsimErrors.getAdjPrecision() - oErrors.getAdjPrecision());
double HsimOR2 = (HsimErrors.getOrientRecall() - oErrors.getOrientRecall()) * (HsimErrors.getOrientRecall() - oErrors.getOrientRecall());
double HsimOP2 = (HsimErrors.getOrientPrecision() - oErrors.getOrientPrecision()) * (HsimErrors.getOrientPrecision() - oErrors.getOrientPrecision());
PRAOerrors Hsim2 = new PRAOerrors(new double[] { HsimAR2, HsimAP2, HsimOR2, HsimOP2 }, "squared errors for Hsim, rsize=" + 1 + " repeat=" + hsimRepeat.get(whichHrepeat));
hsimErrsByPars[0][whichHrepeat].add(Hsim2);
}
}
// Average the squared errors for each set of fsim/hsim params across all iterations
PRAOerrors[] fMSE = new PRAOerrors[fsimRepeat.size()];
PRAOerrors[][] hMSE = new PRAOerrors[1][hsimRepeat.size()];
String[][] latexTableArray = new String[1 * hsimRepeat.size() + fsimRepeat.size()][5];
for (int j = 0; j < fMSE.length; j++) {
fMSE[j] = new PRAOerrors(fsimErrsByPars[j], "MSE for Fsim at vars=" + vars + " edgeratio=" + edgeratio + " cases=" + cases + " frepeat=" + fsimRepeat.get(j) + " iterations=" + iterations);
// if(verbosity>0){System.out.println(fMSE[j].allToString());}
output = output + fMSE[j].allToString() + nl;
latexTableArray[j] = prelimToPRAOtable(fMSE[j]);
}
for (int j = 0; j < hMSE.length; j++) {
for (int k = 0; k < hMSE[j].length; k++) {
hMSE[j][k] = new PRAOerrors(hsimErrsByPars[j][k], "MSE for Hsim at vars=" + vars + " edgeratio=" + edgeratio + " cases=" + cases + " rsize=" + 1 + " repeat=" + hsimRepeat.get(k) + " iterations=" + iterations);
// if(verbosity>0){System.out.println(hMSE[j][k].allToString());}
output = output + hMSE[j][k].allToString() + nl;
latexTableArray[fsimRepeat.size() + j * hMSE[j].length + k] = prelimToPRAOtable(hMSE[j][k]);
}
}
// record all the params, the base error values, and the fsim/hsim mean squared errors
String latexTable = HsimUtils.makeLatexTable(latexTableArray);
PrintWriter writer = new PrintWriter("latexTable.txt", "UTF-8");
writer.println(latexTable);
writer.close();
PrintWriter writer2 = new PrintWriter("HvsF-SimulationEvaluation.txt", "UTF-8");
writer2.println(output);
writer2.close();
long timestop = System.nanoTime();
System.out.println("Evaluation Concluded. Duration: " + (timestop - timestart) / 1000000000 + "s");
} catch (Exception IOException) {
IOException.printStackTrace();
}
}
use of edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader in project tetrad by cmu-phil.
the class Comparison2 method compare.
/**
* Simulates data from model parameterizing the given DAG, and runs the
* algorithm on that data, printing out error statistics.
*/
public static ComparisonResult compare(ComparisonParameters params) {
DataSet dataSet = null;
Graph trueDag = null;
IndependenceTest test = null;
Score score = null;
ComparisonResult result = new ComparisonResult(params);
if (params.isDataFromFile()) {
/**
* Set path to the data directory *
*/
String path = "/Users/dmalinsky/Documents/research/data/danexamples";
File dir = new File(path);
File[] files = dir.listFiles();
if (files == null) {
throw new NullPointerException("No files in " + path);
}
for (File file : files) {
if (file.getName().startsWith("graph") && file.getName().contains(String.valueOf(params.getGraphNum())) && file.getName().endsWith(".g.txt")) {
params.setGraphFile(file.getName());
trueDag = GraphUtils.loadGraphTxt(file);
break;
}
}
String trialGraph = String.valueOf(params.getGraphNum()).concat("-").concat(String.valueOf(params.getTrial())).concat(".dat.txt");
for (File file : files) {
if (file.getName().startsWith("graph") && file.getName().endsWith(trialGraph)) {
Path dataFile = Paths.get(path.concat("/").concat(file.getName()));
Delimiter delimiter = Delimiter.TAB;
if (params.getDataType() == ComparisonParameters.DataType.Continuous) {
try {
TabularDataReader dataReader = new ContinuousTabularDataFileReader(dataFile.toFile(), delimiter);
dataSet = (DataSet) DataConvertUtils.toDataModel(dataReader.readInData());
} catch (IOException e) {
e.printStackTrace();
}
params.setDataFile(file.getName());
break;
} else {
try {
TabularDataReader dataReader = new VerticalDiscreteTabularDataReader(dataFile.toFile(), delimiter);
dataSet = (DataSet) DataConvertUtils.toDataModel(dataReader.readInData());
} catch (IOException e) {
e.printStackTrace();
}
params.setDataFile(file.getName());
break;
}
}
}
System.out.println("current graph file = " + params.getGraphFile());
System.out.println("current data set file = " + params.getDataFile());
}
if (params.isNoData()) {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < params.getNumVars(); i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
trueDag = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, params.getNumEdges(), 10, 10, 10, false, true);
/**
* added 5.25.16 for tsFCI *
*/
if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
trueDag = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, params.getNumEdges(), 10, 10, 10, false, true);
trueDag = TimeSeriesUtils.graphToLagGraph(trueDag, 2);
System.out.println("Creating Time Lag Graph : " + trueDag);
}
/**
* ************************
*/
test = new IndTestDSep(trueDag);
score = new GraphScore(trueDag);
if (params.getAlgorithm() == null) {
throw new IllegalArgumentException("Algorithm not set.");
}
long time1 = System.currentTimeMillis();
if (params.getAlgorithm() == ComparisonParameters.Algorithm.PC) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Pc search = new Pc(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.CPC) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Cpc search = new Cpc(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.PCLocal) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
PcLocal search = new PcLocal(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.PCStableMax) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
PcStableMax search = new PcStableMax(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.FGES) {
if (score == null) {
throw new IllegalArgumentException("Score not set.");
}
Fges search = new Fges(score);
// search.setFaithfulnessAssumed(params.isOneEdgeFaithfulnessAssumed());
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.FCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Fci search = new Fci(test);
result.setResultGraph(search.search());
result.setCorrectResult(new DagToPag(trueDag).convert());
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.GFCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
GFci search = new GFci(test, score);
result.setResultGraph(search.search());
result.setCorrectResult(new DagToPag(trueDag).convert());
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
TsFci search = new TsFci(test);
IKnowledge knowledge = getKnowledge(trueDag);
search.setKnowledge(knowledge);
result.setResultGraph(search.search());
result.setCorrectResult(new TsDagToPag(trueDag).convert());
System.out.println("Correct result for trial = " + result.getCorrectResult());
System.out.println("Search result for trial = " + result.getResultGraph());
} else {
throw new IllegalArgumentException("Unrecognized algorithm.");
}
long time2 = System.currentTimeMillis();
long elapsed = time2 - time1;
result.setElapsed(elapsed);
result.setTrueDag(trueDag);
return result;
} else if (params.getDataFile() != null) {
// dataSet = loadDataFile(params.getDataFile());
System.out.println("Using data from file... ");
if (params.getGraphFile() == null) {
throw new IllegalArgumentException("True graph file not set.");
} else {
System.out.println("Using graph from file... ");
// trueDag = GraphUtils.loadGraph(File params.getGraphFile());
}
} else {
if (params.getNumVars() == -1) {
throw new IllegalArgumentException("Number of variables not set.");
}
if (params.getNumEdges() == -1) {
throw new IllegalArgumentException("Number of edges not set.");
}
if (params.getDataType() == ComparisonParameters.DataType.Continuous) {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < params.getNumVars(); i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
trueDag = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, params.getNumEdges(), 10, 10, 10, false, true);
/**
* added 6.08.16 for tsFCI *
*/
if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
trueDag = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, params.getNumEdges(), 10, 10, 10, false, true);
trueDag = TimeSeriesUtils.graphToLagGraph(trueDag, 2);
System.out.println("Creating Time Lag Graph : " + trueDag);
}
if (params.getDataType() == null) {
throw new IllegalArgumentException("Data type not set or inferred.");
}
if (params.getSampleSize() == -1) {
throw new IllegalArgumentException("Sample size not set.");
}
LargeScaleSimulation sim = new LargeScaleSimulation(trueDag);
/**
* added 6.08.16 for tsFCI *
*/
if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
sim.setCoefRange(0.20, 0.50);
}
/**
* added 6.08.16 for tsFCI *
*/
if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
// // System.out.println("Coefs matrix : " + sim.getCoefs());
// System.out.println(MatrixUtils.toString(sim.getCoefficientMatrix()));
// // System.out.println("dim = " + sim.getCoefs()[1][1]);
// boolean isStableTetradMatrix = allEigenvaluesAreSmallerThanOneInModulus(new TetradMatrix(sim.getCoefficientMatrix()));
// //this TetradMatrix needs to be the matrix of coefficients from the SEM!
// if (!isStableTetradMatrix) {
// System.out.println("%%%%%%%%%% WARNING %%%%%%%%% not a stable set of eigenvalues for data generation");
// System.out.println("Skipping this attempt!");
// sim.setCoefRange(0.2, 0.5);
// dataSet = sim.simulateDataAcyclic(params.getSampleSize());
// }
//
// /***************************/
boolean isStableTetradMatrix;
int attempt = 1;
int tierSize = params.getNumVars();
int[] sub = new int[tierSize];
int[] sub2 = new int[tierSize];
for (int i = 0; i < tierSize; i++) {
sub[i] = i;
sub2[i] = tierSize + i;
}
do {
dataSet = sim.simulateDataFisher(params.getSampleSize());
// System.out.println("Variable Nodes : " + sim.getVariableNodes());
// System.out.println(MatrixUtils.toString(sim.getCoefficientMatrix()));
TetradMatrix coefMat = new TetradMatrix(sim.getCoefficientMatrix());
TetradMatrix B = coefMat.getSelection(sub, sub);
TetradMatrix Gamma1 = coefMat.getSelection(sub2, sub);
TetradMatrix Gamma0 = TetradMatrix.identity(tierSize).minus(B);
TetradMatrix A1 = Gamma0.inverse().times(Gamma1);
// TetradMatrix B2 = coefMat.getSelection(sub2, sub2);
// System.out.println("B matrix : " + B);
// System.out.println("B2 matrix : " + B2);
// System.out.println("Gamma1 matrix : " + Gamma1);
// isStableTetradMatrix = allEigenvaluesAreSmallerThanOneInModulus(new TetradMatrix(sim.getCoefficientMatrix()));
isStableTetradMatrix = TimeSeriesUtils.allEigenvaluesAreSmallerThanOneInModulus(A1);
System.out.println("isStableTetradMatrix? : " + isStableTetradMatrix);
attempt++;
} while ((!isStableTetradMatrix) && attempt <= 5);
if (!isStableTetradMatrix) {
System.out.println("%%%%%%%%%% WARNING %%%%%%%% not a stable coefficient matrix, forcing coefs to [0.15,0.3]");
System.out.println("Made " + (attempt - 1) + " attempts to get stable matrix.");
sim.setCoefRange(0.15, 0.3);
dataSet = sim.simulateDataFisher(params.getSampleSize());
} else {
System.out.println("Coefficient matrix is stable.");
}
}
} else if (params.getDataType() == ComparisonParameters.DataType.Discrete) {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < params.getNumVars(); i++) {
nodes.add(new DiscreteVariable("X" + (i + 1), 3));
}
trueDag = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, params.getNumEdges(), 10, 10, 10, false, true);
if (params.getDataType() == null) {
throw new IllegalArgumentException("Data type not set or inferred.");
}
if (params.getSampleSize() == -1) {
throw new IllegalArgumentException("Sample size not set.");
}
int[] tiers = new int[nodes.size()];
for (int i = 0; i < nodes.size(); i++) {
tiers[i] = i;
}
BayesPm pm = new BayesPm(trueDag, 3, 3);
MlBayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
dataSet = im.simulateData(params.getSampleSize(), false, tiers);
} else {
throw new IllegalArgumentException("Unrecognized data type.");
}
if (dataSet == null) {
throw new IllegalArgumentException("No data set.");
}
}
if (params.getIndependenceTest() == ComparisonParameters.IndependenceTestType.FisherZ) {
if (params.getDataType() != null && params.getDataType() != ComparisonParameters.DataType.Continuous) {
throw new IllegalArgumentException("Data type previously set to something other than continuous.");
}
if (Double.isNaN(params.getAlpha())) {
throw new IllegalArgumentException("Alpha not set.");
}
test = new IndTestFisherZ(dataSet, params.getAlpha());
params.setDataType(ComparisonParameters.DataType.Continuous);
} else if (params.getIndependenceTest() == ComparisonParameters.IndependenceTestType.ChiSquare) {
if (params.getDataType() != null && params.getDataType() != ComparisonParameters.DataType.Discrete) {
throw new IllegalArgumentException("Data type previously set to something other than discrete.");
}
if (Double.isNaN(params.getAlpha())) {
throw new IllegalArgumentException("Alpha not set.");
}
test = new IndTestChiSquare(dataSet, params.getAlpha());
params.setDataType(ComparisonParameters.DataType.Discrete);
}
if (params.getScore() == ScoreType.SemBic) {
if (params.getDataType() != null && params.getDataType() != ComparisonParameters.DataType.Continuous) {
throw new IllegalArgumentException("Data type previously set to something other than continuous.");
}
if (Double.isNaN(params.getPenaltyDiscount())) {
throw new IllegalArgumentException("Penalty discount not set.");
}
SemBicScore semBicScore = new SemBicScore(new CovarianceMatrixOnTheFly(dataSet));
semBicScore.setPenaltyDiscount(params.getPenaltyDiscount());
score = semBicScore;
params.setDataType(ComparisonParameters.DataType.Continuous);
} else if (params.getScore() == ScoreType.BDeu) {
if (params.getDataType() != null && params.getDataType() != ComparisonParameters.DataType.Discrete) {
throw new IllegalArgumentException("Data type previously set to something other than discrete.");
}
if (Double.isNaN(params.getSamplePrior())) {
throw new IllegalArgumentException("Sample prior not set.");
}
if (Double.isNaN(params.getStructurePrior())) {
throw new IllegalArgumentException("Structure prior not set.");
}
score = new BDeuScore(dataSet);
((BDeuScore) score).setSamplePrior(params.getSamplePrior());
((BDeuScore) score).setStructurePrior(params.getStructurePrior());
params.setDataType(ComparisonParameters.DataType.Discrete);
params.setDataType(ComparisonParameters.DataType.Discrete);
}
if (params.getAlgorithm() == null) {
throw new IllegalArgumentException("Algorithm not set.");
}
long time1 = System.currentTimeMillis();
if (params.getAlgorithm() == ComparisonParameters.Algorithm.PC) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Pc search = new Pc(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.CPC) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Cpc search = new Cpc(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.PCLocal) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
PcLocal search = new PcLocal(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.PCStableMax) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
PcStableMax search = new PcStableMax(test);
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.FGES) {
if (score == null) {
throw new IllegalArgumentException("Score not set.");
}
Fges search = new Fges(score);
// search.setFaithfulnessAssumed(params.isOneEdgeFaithfulnessAssumed());
result.setResultGraph(search.search());
result.setCorrectResult(SearchGraphUtils.patternForDag(new EdgeListGraph(trueDag)));
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.FCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
Fci search = new Fci(test);
result.setResultGraph(search.search());
result.setCorrectResult(new DagToPag(trueDag).convert());
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.GFCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
GFci search = new GFci(test, score);
result.setResultGraph(search.search());
result.setCorrectResult(new DagToPag(trueDag).convert());
} else if (params.getAlgorithm() == ComparisonParameters.Algorithm.TsFCI) {
if (test == null) {
throw new IllegalArgumentException("Test not set.");
}
TsFci search = new TsFci(test);
IKnowledge knowledge = getKnowledge(trueDag);
search.setKnowledge(knowledge);
result.setResultGraph(search.search());
result.setCorrectResult(new TsDagToPag(trueDag).convert());
} else {
throw new IllegalArgumentException("Unrecognized algorithm.");
}
long time2 = System.currentTimeMillis();
long elapsed = time2 - time1;
result.setElapsed(elapsed);
result.setTrueDag(trueDag);
return result;
}
Aggregations