use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class TimeoutComparison method configuration.
/**
*/
public void configuration(String path) {
try {
new File(path).mkdirs();
PrintStream out = new PrintStream(new FileOutputStream(new File(path, "Configuration.txt")));
Parameters allParams = new Parameters();
List<Class> algorithms = new ArrayList<>();
List<Class> statistics = new ArrayList<>();
List<Class> independenceWrappers = new ArrayList<>();
List<Class> scoreWrappers = new ArrayList<>();
List<Class> simulations = new ArrayList<>();
algorithms.addAll(getClasses(Algorithm.class));
statistics.addAll(getClasses(Statistic.class));
independenceWrappers.addAll(getClasses(IndependenceWrapper.class));
scoreWrappers.addAll(getClasses(ScoreWrapper.class));
simulations.addAll(getClasses(Simulation.class));
out.println("Available Algorithms:");
out.println();
out.println("Algorithms that take an independence test (using an example independence test):");
out.println();
for (Class clazz : new ArrayList<>(algorithms)) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0] == IndependenceWrapper.class) {
Algorithm algorithm = (Algorithm) constructor.newInstance(FisherZ.class.newInstance());
out.println(clazz.getSimpleName() + ": " + algorithm.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(algorithm.getParameters(), allParams, out);
}
if (TakesInitialGraph.class.isAssignableFrom(clazz)) {
out.println("\t" + clazz.getSimpleName() + " can take an initial graph from some other algorithm as input");
}
}
}
}
out.println();
out.println("Algorithms that take a score (using an example score):");
out.println();
for (Class clazz : new ArrayList<>(algorithms)) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0] == ScoreWrapper.class) {
Algorithm algorithm = (Algorithm) constructor.newInstance(BdeuScore.class.newInstance());
out.println(clazz.getSimpleName() + ": " + algorithm.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(algorithm.getParameters(), allParams, out);
}
}
}
}
out.println();
out.println("Algorithms with blank constructor:");
out.println();
for (Class clazz : new ArrayList<>(algorithms)) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
Algorithm algorithm = (Algorithm) constructor.newInstance();
out.println(clazz.getSimpleName() + ": " + algorithm.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(algorithm.getParameters(), allParams, out);
}
}
}
}
out.println();
out.println("Available Statistics:");
out.println();
for (Class clazz : statistics) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
Statistic statistic = (Statistic) constructor.newInstance();
out.println(clazz.getSimpleName() + ": " + statistic.getDescription());
}
}
}
out.println();
out.println("Available Independence Tests:");
out.println();
for (Class clazz : independenceWrappers) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
IndependenceWrapper independence = (IndependenceWrapper) constructor.newInstance();
out.println(clazz.getSimpleName() + ": " + independence.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(independence.getParameters(), allParams, out);
}
}
}
}
out.println();
out.println("Available Scores:");
out.println();
for (Class clazz : scoreWrappers) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
ScoreWrapper score = (ScoreWrapper) constructor.newInstance();
out.println(clazz.getSimpleName() + ": " + score.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(score.getParameters(), allParams, out);
}
}
}
}
out.println();
out.println("Available Simulations:");
out.println();
for (Class clazz : simulations) {
if (Experimental.class.isAssignableFrom(clazz)) {
continue;
}
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
Simulation simulation = (Simulation) constructor.newInstance();
out.println(clazz.getSimpleName() + ": " + simulation.getDescription());
if (HasParameters.class.isAssignableFrom(clazz)) {
printParameters(simulation.getParameters(), allParams, out);
}
}
}
}
out.println();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class CompareFromFiles method main.
public static void main(String... args) {
Parameters parameters = new Parameters();
// Can leave the simulation parameters out since
// we're loading from file here.
parameters.set("numRuns", 3);
parameters.set("maxDistinctValuesDiscrete", 5);
parameters.set("structurePrior", -0.5);
parameters.set("fDegree", -1);
parameters.set("discretize", 0);
parameters.set("alpha", 1e-3, 1e-4);
Statistics statistics = new Statistics();
statistics.add(new ParameterColumn("avgDegree"));
statistics.add(new ParameterColumn("sampleSize"));
statistics.add(new AdjacencyPrecision());
statistics.add(new AdjacencyRecall());
statistics.add(new ArrowheadPrecision());
statistics.add(new ArrowheadRecall());
statistics.add(new ElapsedTime());
statistics.setWeight("AP", 1.0);
statistics.setWeight("AR", 0.5);
statistics.setWeight("AHP", 1.0);
statistics.setWeight("AHR", 0.5);
Algorithms algorithms = new Algorithms();
// algorithms.add(new Fges(new ConditionalGaussianBicScore()));
// algorithms.add(new Fges(new ConditionalGaussianOtherBicScore()));
algorithms.add(new Fges(new MVPBicScore()));
// algorithms.add(new Fges(new MNLRBicScore()));
// algorithms.add(new Fges(new DiscreteMixedBicScore()));
// algorithms.add(new Cpc(new ConditionalGaussianLRT()));
// algorithms.add(new Cpc(new MVPLRT()));
// algorithms.add(new Cpc(new MNLRLRT(), new Mgm()));
Comparison comparison = new Comparison();
comparison.setShowAlgorithmIndices(true);
comparison.setShowSimulationIndices(true);
comparison.setSortByUtility(false);
comparison.setShowUtilities(false);
comparison.setComparisonGraph(Comparison.ComparisonGraph.true_DAG);
comparison.compareFromFiles("comparison", algorithms, statistics, parameters);
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class StARS2 method search.
@Override
public Graph search(DataModel dataSet, Parameters parameters) {
this._dataSet = (DataSet) dataSet;
// int numVars = Math.min(50, ((DataSet) dataSet).getNumColumns());
// int[] cols = new int[numVars];
// for (int i = 0; i < numVars; i++) cols[i] = i;
// .subsetColumns(cols);
_dataSet = (DataSet) dataSet;
double percentageB = parameters.getDouble("StARS.percentageB");
double tolerance = parameters.getDouble("StARS.tolerance");
double cutoff = parameters.getDouble("StARS.cutoff");
int numSubsamples = parameters.getInt("numSubsamples");
Parameters _parameters = new Parameters(parameters);
// Draw 5 samples without replacement.
List<DataSet> samples = new ArrayList<>();
for (int i = 0; i < numSubsamples; i++) {
BootstrapSampler sampler = new BootstrapSampler();
sampler.setWithoutReplacements(true);
samples.add(sampler.sample(_dataSet, (int) (percentageB * _dataSet.getNumRows())));
}
double pFrom = low;
double pTo = high;
double pMid = high;
double lastD = getD(parameters, parameter, high, samples, samples.size(), algorithm, archive);
while (abs(pFrom - pTo) > tolerance) {
pMid = (pFrom + pTo) / 2.0;
_parameters.set(parameter, getValue(pMid, parameters));
double D = getD(parameters, parameter, pMid, samples, samples.size(), algorithm, archive);
System.out.println("pFrom = " + pFrom + " pTo = " + pTo + " pMid = " + pMid + " D = " + D);
if (D > lastD && D < cutoff) {
pTo = pMid;
} else {
pFrom = pMid;
}
lastD = D;
}
//
// archive = new HashMap<>();
//
// MultivariateOptimizer search = new PowellOptimizer(tolerance, tolerance);
// FittingFunction f = new FittingFunction(samples, numSubsamples, _parameters, algorithm, cutoff, low, high, parameter,
// _dataSet, archive);
// PointValuePair p = search.optimize(
// new InitialGuess(new double[]{initialGuess}),
// new ObjectiveFunction(f),
// GoalType.MAXIMIZE,
// MaxEval.unlimited()
// );
//
// double _p = getValue(p.getPoint()[0], parameters);
double _p = getValue(pMid, parameters);
// _p = Math.round(_p * 10.0) / 10.0;
System.out.println(parameter + " = " + _p);
_parameters.set(parameter, getValue(_p, parameters));
//
return algorithm.search(dataSet, _parameters);
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class FofcSearchEditor method getIndTestParamBox.
/**
* Factory to return the correct param editor for independence test params.
* This will go in a little box in the search editor.
*/
private JComponent getIndTestParamBox(Parameters params) {
if (params == null) {
throw new NullPointerException();
}
if (params instanceof Parameters) {
MimRunner runner = getMimRunner();
params.set("varNames", runner.getParams().get("varNames", null));
return new FofcIndTestParamsEditor(params);
}
throw new IllegalArgumentException("Unrecognized Parameters: " + params.getClass());
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class GeneralAlgorithmEditor method doRemoteCompute.
private void doRemoteCompute(final GeneralAlgorithmRunner runner, final HpcAccount hpcAccount) throws Exception {
// **********************
// Show progress panel *
// **********************
Frame ancestor = (Frame) JOptionUtils.centeringComp().getTopLevelAncestor();
final JDialog progressDialog = new JDialog(ancestor, "HPC Job Submission's Progress...", false);
Dimension progressDim = new Dimension(500, 150);
JTextArea progressTextArea = new JTextArea();
progressTextArea.setPreferredSize(progressDim);
progressTextArea.setEditable(false);
JScrollPane progressScroller = new JScrollPane(progressTextArea);
progressScroller.setAlignmentX(LEFT_ALIGNMENT);
progressDialog.setLayout(new BorderLayout());
progressDialog.getContentPane().add(progressScroller, BorderLayout.CENTER);
progressDialog.pack();
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
progressDialog.setLocation((screenDim.width - progressDim.width) / 2, (screenDim.height - progressDim.height) / 2);
progressDialog.setVisible(true);
int totalProcesses = 4;
String newline = "\n";
String tab = "\t";
int progressTextLength = 0;
DataModel dataModel = runner.getDataModel();
// 1. Generate temp file
Path file = null;
Path prior = null;
try {
// ****************************
// Data Preparation Progress *
// ****************************
String dataMessage = String.format("1/%1$d Data Preparation", totalProcesses);
progressTextArea.append(dataMessage);
progressTextArea.append(tab);
progressTextLength = progressTextArea.getText().length();
progressTextArea.append("Preparing...");
progressTextArea.updateUI();
file = Files.createTempFile("Tetrad-data-", ".txt");
// LOGGER.info(file.toAbsolutePath().toString());
List<String> tempLine = new ArrayList<>();
// Header
List<Node> variables = dataModel.getVariables();
if ((variables == null || variables.isEmpty()) && runner.getSourceGraph() != null) {
variables = runner.getSourceGraph().getNodes();
}
String vars = StringUtils.join(variables.toArray(), tab);
tempLine.add(vars);
// Data
DataSet dataSet = (DataSet) dataModel;
for (int i = 0; i < dataSet.getNumRows(); i++) {
String line = null;
for (int j = 0; j < dataSet.getNumColumns(); j++) {
String cell = null;
if (dataSet.isContinuous()) {
cell = String.valueOf(dataSet.getDouble(i, j));
} else {
cell = String.valueOf(dataSet.getInt(i, j));
}
if (line == null) {
line = cell;
} else {
line = line + "\t" + cell;
}
}
tempLine.add(line);
}
// for (String line : tempLine) {
// LOGGER.info(line);
// }
Files.write(file, tempLine);
// Get file's MD5 hash and use it as its identifier
String datasetMd5 = MessageDigestHash.computeMD5Hash(file);
progressTextArea.replaceRange("Done", progressTextLength, progressTextArea.getText().length());
progressTextArea.append(newline);
progressTextArea.updateUI();
// ***************************************
// Prior Knowledge Preparation Progress *
// ***************************************
String priorMessage = String.format("2/%1$d Prior Knowledge Preparation", totalProcesses);
progressTextArea.append(priorMessage);
progressTextArea.append(tab);
progressTextLength = progressTextArea.getText().length();
progressTextArea.append("Preparing...");
progressTextArea.updateUI();
// 2. Generate temp prior knowledge file
Knowledge2 knowledge = (Knowledge2) dataModel.getKnowledge();
if (knowledge != null && !knowledge.isEmpty()) {
prior = Files.createTempFile(file.getFileName().toString(), ".prior");
knowledge.saveKnowledge(Files.newBufferedWriter(prior));
progressTextArea.replaceRange("Done", progressTextLength, progressTextArea.getText().length());
progressTextArea.append(newline);
progressTextArea.updateUI();
} else {
progressTextArea.replaceRange("Skipped", progressTextLength, progressTextArea.getText().length());
progressTextArea.append(newline);
progressTextArea.updateUI();
}
// Get knowledge file's MD5 hash and use it as its identifier
String priorKnowledgeMd5 = null;
if (prior != null) {
priorKnowledgeMd5 = MessageDigestHash.computeMD5Hash(prior);
}
// *******************************************
// Algorithm Parameter Preparation Progress *
// *******************************************
String algorMessage = String.format("3/%1$d Algorithm Preparation", totalProcesses);
progressTextArea.append(algorMessage);
progressTextArea.append(tab);
progressTextLength = progressTextArea.getText().length();
progressTextArea.append("Preparing...");
progressTextArea.updateUI();
// 3.1 Algorithm Id, Independent Test Id, Score Id
AlgorithmModel algoModel = algorithmList.getSelectedValue();
String algoId = algoModel.getAlgorithm().getAnnotation().command();
// Test
String testId = null;
if (indTestComboBox.isEnabled()) {
IndependenceTestModel indTestModel = indTestComboBox.getItemAt(indTestComboBox.getSelectedIndex());
testId = indTestModel.getIndependenceTest().getAnnotation().command();
}
// Score
String scoreId = null;
if (scoreComboBox.isEnabled()) {
ScoreModel scoreModel = scoreComboBox.getItemAt(scoreComboBox.getSelectedIndex());
scoreId = scoreModel.getScore().getAnnotation().command();
}
// 3.2 Parameters
AlgorithmParamRequest algorithmParamRequest = new AlgorithmParamRequest();
// Test and score
algorithmParamRequest.setTestId(testId);
algorithmParamRequest.setScoreId(scoreId);
// Dataset and Prior paths
String datasetPath = file.toAbsolutePath().toString();
LOGGER.info(datasetPath);
algorithmParamRequest.setDatasetPath(datasetPath);
algorithmParamRequest.setDatasetMd5(datasetMd5);
if (prior != null) {
String priorKnowledgePath = prior.toAbsolutePath().toString();
LOGGER.info(priorKnowledgePath);
algorithmParamRequest.setPriorKnowledgePath(priorKnowledgePath);
algorithmParamRequest.setPriorKnowledgeMd5(priorKnowledgeMd5);
}
// VariableType
if (dataModel.isContinuous()) {
algorithmParamRequest.setVariableType("continuous");
} else if (dataModel.isDiscrete()) {
algorithmParamRequest.setVariableType("discrete");
} else {
algorithmParamRequest.setVariableType("mixed");
}
// FileDelimiter
// Pre-determined
String fileDelimiter = "tab";
algorithmParamRequest.setFileDelimiter(fileDelimiter);
Set<AlgorithmParameter> AlgorithmParameters = new HashSet<>();
Parameters parameters = runner.getParameters();
List<String> parameterNames = runner.getAlgorithm().getParameters();
for (String parameter : parameterNames) {
String value = parameters.get(parameter).toString();
LOGGER.info("parameter: " + parameter + "\tvalue: " + value);
if (value != null) {
AlgorithmParameter algorParam = new AlgorithmParameter();
algorParam.setParameter(parameter);
algorParam.setValue(value);
AlgorithmParameters.add(algorParam);
}
}
algorithmParamRequest.setAlgorithmParameters(AlgorithmParameters);
String maxHeapSize = null;
do {
maxHeapSize = JOptionPane.showInputDialog(progressDialog, "Enter Your Request Java Max Heap Size (GB):", "5");
} while (maxHeapSize != null && !StringUtils.isNumeric(maxHeapSize));
if (maxHeapSize != null) {
JvmOptions jvmOptions = new JvmOptions();
jvmOptions.setMaxHeapSize(Integer.parseInt(maxHeapSize));
algorithmParamRequest.setJvmOptions(jvmOptions);
}
// Hpc parameters
final HpcAccountManager hpcAccountManager = desktop.getHpcAccountManager();
JsonWebToken jsonWebToken = HpcAccountUtils.getJsonWebToken(hpcAccountManager, hpcAccount);
if (jsonWebToken.getWallTime() != null) {
// User allowed to customize the job's wall time
String[] wallTime = jsonWebToken.getWallTime();
Object userwallTime = JOptionPane.showInputDialog(progressDialog, "Wall Time:", "Choose Your Wall Time (in Hour)", JOptionPane.QUESTION_MESSAGE, null, wallTime, wallTime[0]);
if (wallTime != null && userwallTime != null) {
HpcParameter hpcParameter = new HpcParameter();
hpcParameter.setKey("walltime");
hpcParameter.setValue(userwallTime.toString());
LOGGER.info("walltime: " + userwallTime.toString());
Set<HpcParameter> hpcParameters = new HashSet<>();
hpcParameters.add(hpcParameter);
algorithmParamRequest.setHpcParameters(hpcParameters);
}
}
progressTextArea.replaceRange("Done", progressTextLength, progressTextArea.getText().length());
progressTextArea.append(newline);
progressTextArea.updateUI();
// ********************************
// Adding HPC Job Queue Progress *
// ********************************
String dbMessage = String.format("4/%1$d HPC Job Queue Submission", totalProcesses);
progressTextArea.append(dbMessage);
progressTextArea.append(tab);
progressTextLength = progressTextArea.getText().length();
progressTextArea.append("Preparing...");
progressTextArea.updateUI();
HpcJobManager hpcJobManager = desktop.getHpcJobManager();
// 4.1 Save HpcJobInfo
hpcJobInfo = new HpcJobInfo();
hpcJobInfo.setAlgoId(algoId);
hpcJobInfo.setAlgorithmParamRequest(algorithmParamRequest);
hpcJobInfo.setStatus(-1);
hpcJobInfo.setHpcAccount(hpcAccount);
hpcJobManager.submitNewHpcJobToQueue(hpcJobInfo, this);
progressTextArea.replaceRange("Done", progressTextLength, progressTextArea.getText().length());
progressTextArea.append(newline);
progressTextArea.updateUI();
this.jsonResult = null;
JOptionPane.showMessageDialog(ancestor, "The " + hpcJobInfo.getAlgoId() + " job on the " + hpcJobInfo.getHpcAccount().getConnectionName() + " node is in the queue successfully!");
} catch (IOException exception) {
LOGGER.error("", exception);
} finally {
progressDialog.setVisible(false);
progressDialog.dispose();
}
(new HpcJobActivityAction("")).actionPerformed(null);
}
Aggregations