Search in sources :

Example 76 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class GeneralAlgorithmRunner method execute.

// ============================PUBLIC METHODS==========================//
@Override
public void execute() {
    List<Graph> graphList = new ArrayList<>();
    int i = 0;
    if (getDataModelList().isEmpty()) {
        if (getSourceGraph() != null) {
            Algorithm algo = getAlgorithm();
            if (algo instanceof HasKnowledge) {
                ((HasKnowledge) algo).setKnowledge(getKnowledge());
            }
            graphList.add(algo.search(null, parameters));
        } else {
            throw new IllegalArgumentException("The parent boxes did not include any datasets or graphs. Try opening\n" + "the editors for those boxes and loading or simulating them.");
        }
    } else {
        if (getAlgorithm() instanceof MultiDataSetAlgorithm) {
            for (int k = 0; k < parameters.getInt("numRuns"); k++) {
                List<DataSet> dataSets = getDataModelList().stream().map(e -> (DataSet) e).collect(Collectors.toCollection(ArrayList::new));
                if (dataSets.size() < parameters.getInt("randomSelectionSize")) {
                    throw new IllegalArgumentException("Sorry, the 'random selection size' is greater than " + "the number of data sets.");
                }
                Collections.shuffle(dataSets);
                List<DataModel> sub = new ArrayList<>();
                for (int j = 0; j < parameters.getInt("randomSelectionSize"); j++) {
                    sub.add(dataSets.get(j));
                }
                Algorithm algo = getAlgorithm();
                if (algo instanceof HasKnowledge) {
                    ((HasKnowledge) algo).setKnowledge(getKnowledge());
                }
                graphList.add(((MultiDataSetAlgorithm) algo).search(sub, parameters));
            }
        } else if (getAlgorithm() instanceof ClusterAlgorithm) {
            for (int k = 0; k < parameters.getInt("numRuns"); k++) {
                getDataModelList().forEach(dataModel -> {
                    if (dataModel instanceof ICovarianceMatrix) {
                        ICovarianceMatrix dataSet = (ICovarianceMatrix) dataModel;
                        graphList.add(algorithm.search(dataSet, parameters));
                    } else if (dataModel instanceof DataSet) {
                        DataSet dataSet = (DataSet) dataModel;
                        if (!dataSet.isContinuous()) {
                            throw new IllegalArgumentException("Sorry, you need a continuous dataset for a cluster algorithm.");
                        }
                        graphList.add(algorithm.search(dataSet, parameters));
                    }
                });
            }
        } else {
            getDataModelList().forEach(data -> {
                IKnowledge knowledgeFromData = data.getKnowledge();
                if (!(knowledgeFromData == null || knowledgeFromData.getVariables().isEmpty())) {
                    this.knowledge = knowledgeFromData;
                }
                Algorithm algo = getAlgorithm();
                if (algo instanceof HasKnowledge) {
                    ((HasKnowledge) algo).setKnowledge(getKnowledge());
                }
                DataType algDataType = algo.getDataType();
                if (data.isContinuous() && (algDataType == DataType.Continuous || algDataType == DataType.Mixed)) {
                    graphList.add(algo.search(data, parameters));
                } else if (data.isDiscrete() && (algDataType == DataType.Discrete || algDataType == DataType.Mixed)) {
                    graphList.add(algo.search(data, parameters));
                } else if (data.isMixed() && algDataType == DataType.Mixed) {
                    graphList.add(algo.search(data, parameters));
                } else {
                    throw new IllegalArgumentException("The type of data changed; try opening up the search editor and " + "running the algorithm there.");
                }
            });
        }
    }
    if (getKnowledge().getVariablesNotInTiers().size() < getKnowledge().getVariables().size()) {
        for (Graph graph : graphList) {
            SearchGraphUtils.arrangeByKnowledgeTiers(graph, getKnowledge());
        }
    } else {
        for (Graph graph : graphList) {
            GraphUtils.circleLayout(graph, 225, 200, 150);
        }
    }
    this.graphList = graphList;
}
Also used : GraphUtils(edu.cmu.tetrad.graph.GraphUtils) ObjectInputStream(java.io.ObjectInputStream) Parameters(edu.cmu.tetrad.util.Parameters) HashMap(java.util.HashMap) Triple(edu.cmu.tetrad.graph.Triple) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) HasKnowledge(edu.cmu.tetrad.algcomparison.utils.HasKnowledge) DataType(edu.cmu.tetrad.data.DataType) KnowledgeBoxInput(edu.cmu.tetrad.data.KnowledgeBoxInput) Map(java.util.Map) ClusterAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.cluster.ClusterAlgorithm) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Fges(edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges) BdeuScore(edu.cmu.tetrad.algcomparison.score.BdeuScore) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) IKnowledge(edu.cmu.tetrad.data.IKnowledge) Graph(edu.cmu.tetrad.graph.Graph) IOException(java.io.IOException) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) Collectors(java.util.stream.Collectors) DataModel(edu.cmu.tetrad.data.DataModel) DataModelList(edu.cmu.tetrad.data.DataModelList) List(java.util.List) ParamsResettable(edu.cmu.tetrad.session.ParamsResettable) DataSet(edu.cmu.tetrad.data.DataSet) ImpliedOrientation(edu.cmu.tetrad.search.ImpliedOrientation) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) SessionModel(edu.cmu.tetrad.session.SessionModel) IndependenceTest(edu.cmu.tetrad.search.IndependenceTest) SearchGraphUtils(edu.cmu.tetrad.search.SearchGraphUtils) Knowledge2(edu.cmu.tetrad.data.Knowledge2) Unmarshallable(edu.cmu.tetrad.util.Unmarshallable) Collections(java.util.Collections) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) HasKnowledge(edu.cmu.tetrad.algcomparison.utils.HasKnowledge) ClusterAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.cluster.ClusterAlgorithm) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) IKnowledge(edu.cmu.tetrad.data.IKnowledge) ClusterAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.cluster.ClusterAlgorithm) Graph(edu.cmu.tetrad.graph.Graph) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) DataModel(edu.cmu.tetrad.data.DataModel) DataType(edu.cmu.tetrad.data.DataType)

Example 77 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class HitonRunner method getIndependenceTest.

public IndependenceTest getIndependenceTest() {
    Object dataModel = getDataModel();
    if (dataModel == null) {
        dataModel = getSourceGraph();
    }
    Parameters params = getParams();
    IndTestType testType = (IndTestType) params.get("indTestType", IndTestType.FISHER_Z);
    return new IndTestChooser().getTest(dataModel, params, testType);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) IndTestType(edu.cmu.tetrad.search.IndTestType)

Example 78 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class ImagesRunner method execute.

// ============================PUBLIC METHODS==========================//
/**
 * Executes the algorithm, producing (at least) a result workbench. Must be
 * implemented in the extending class.
 */
public void execute() {
    Object model = getDataModel();
    if (model == null && getSourceGraph() != null) {
        model = getSourceGraph();
    }
    if (model == null) {
        throw new RuntimeException("Data source is unspecified. You may need to double click all your data boxes, \n" + "then click Save, and then right click on them and select Propagate Downstream. \n" + "The issue is that we use a seed to simulate from IM's, so your data is not saved to \n" + "file when you save the session. It can, however, be recreated from the saved seed.");
    }
    Parameters params = getParams();
    if (model instanceof Graph) {
        GraphScore gesScore = new GraphScore((Graph) model);
        fges = new Fges(gesScore);
    } else if (model instanceof DataSet) {
        DataSet dataSet = (DataSet) model;
        if (dataSet.isContinuous()) {
            SemBicScore gesScore = new SemBicScore(new CovarianceMatrixOnTheFly((DataSet) model));
            gesScore.setPenaltyDiscount(params.getDouble("penaltyDiscount", 4));
            fges = new Fges(gesScore);
        } else if (dataSet.isDiscrete()) {
            double samplePrior = getParams().getDouble("samplePrior", 1);
            double structurePrior = getParams().getDouble("structurePrior", 1);
            BDeuScore score = new BDeuScore(dataSet);
            score.setSamplePrior(samplePrior);
            score.setStructurePrior(structurePrior);
            fges = new Fges(score);
        } else {
            throw new IllegalStateException("Data set must either be continuous or discrete.");
        }
    } else if (model instanceof ICovarianceMatrix) {
        SemBicScore gesScore = new SemBicScore((ICovarianceMatrix) model);
        gesScore.setPenaltyDiscount(params.getDouble("penaltyDiscount", 4));
        fges = new Fges(gesScore);
    } else if (model instanceof DataModelList) {
        DataModelList list = (DataModelList) model;
        for (DataModel dataModel : list) {
            if (!(dataModel instanceof DataSet || dataModel instanceof ICovarianceMatrix)) {
                throw new IllegalArgumentException("Need a combination of all continuous data sets or " + "covariance matrices, or else all discrete data sets, or else a single graph.");
            }
        }
        if (allContinuous(list)) {
            double penalty = getParams().getDouble("penaltyDiscount", 4);
            if (params.getBoolean("firstNontriangular", false)) {
                SemBicScoreImages fgesScore = new SemBicScoreImages(list);
                fgesScore.setPenaltyDiscount(penalty);
                fges = new Fges(fgesScore);
            } else {
                SemBicScoreImages fgesScore = new SemBicScoreImages(list);
                fgesScore.setPenaltyDiscount(penalty);
                fges = new Fges(fgesScore);
            }
        } else if (allDiscrete(list)) {
            double structurePrior = getParams().getDouble("structurePrior", 1);
            double samplePrior = getParams().getDouble("samplePrior", 1);
            BdeuScoreImages fgesScore = new BdeuScoreImages(list);
            fgesScore.setSamplePrior(samplePrior);
            fgesScore.setStructurePrior(structurePrior);
            if (params.getBoolean("firstNontriangular", false)) {
                fges = new Fges(fgesScore);
            } else {
                fges = new Fges(fgesScore);
            }
        } else {
            throw new IllegalArgumentException("Data must be either all discrete or all continuous.");
        }
    } else {
        System.out.println("No viable input.");
    }
    fges.setKnowledge((IKnowledge) getParams().get("knowledge", new Knowledge2()));
    fges.setNumPatternsToStore(params.getInt("numPatternsToSave", 1));
    fges.setFaithfulnessAssumed(params.getBoolean("faithfulnessAssumed", true));
    fges.setVerbose(true);
    Graph graph = fges.search();
    if (getSourceGraph() != null) {
        GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (((IKnowledge) getParams().get("knowledge", new Knowledge2())).isDefaultToKnowledgeLayout()) {
        SearchGraphUtils.arrangeByKnowledgeTiers(graph, (IKnowledge) getParams().get("knowledge", new Knowledge2()));
    } else {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    setResultGraph(graph);
    this.topGraphs = new ArrayList<>(fges.getTopGraphs());
    if (topGraphs.isEmpty()) {
        topGraphs.add(new ScoredGraph(getResultGraph(), Double.NaN));
    }
    this.topGraphs = new ArrayList<>(fges.getTopGraphs());
    if (this.topGraphs.isEmpty()) {
        this.topGraphs.add(new ScoredGraph(getResultGraph(), Double.NaN));
    }
    setIndex(topGraphs.size() - 1);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) Graph(edu.cmu.tetrad.graph.Graph)

Example 79 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class MbFanSearchRunner method execute.

// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
 * Executes the algorithm, producing (at least) a result workbench. Must be
 * implemented in the extending class.
 */
public void execute() {
    int pcDepth = getParams().getInt("depth", -1);
    Mbfs mbfs = new Mbfs(getIndependenceTest(), pcDepth);
    Parameters params = getParams();
    if (params instanceof Parameters) {
        mbfs.setAggressivelyPreventCycles(params.getBoolean("aggressivelyPreventCycles", false));
    }
    IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2());
    mbfs.setKnowledge(knowledge);
    String targetName = getParams().getString("targetName", null);
    Graph graph = mbfs.search(targetName);
    setResultGraph(graph);
    if (getSourceGraph() != null) {
        GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
        SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    this.mbfs = mbfs;
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) Parameters(edu.cmu.tetrad.util.Parameters) Graph(edu.cmu.tetrad.graph.Graph) Mbfs(edu.cmu.tetrad.search.Mbfs) Knowledge2(edu.cmu.tetrad.data.Knowledge2)

Example 80 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class MbfsRunner method getIndependenceTest.

public IndependenceTest getIndependenceTest() {
    Object dataModel = getDataModel();
    if (dataModel == null) {
        dataModel = getSourceGraph();
    }
    Parameters params = getParams();
    IndTestType testType = (IndTestType) params.get("indTestType", IndTestType.FISHER_Z);
    return new IndTestChooser().getTest(dataModel, params, testType);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters)

Aggregations

Parameters (edu.cmu.tetrad.util.Parameters)134 Comparison (edu.cmu.tetrad.algcomparison.Comparison)30 Graph (edu.cmu.tetrad.graph.Graph)26 Algorithms (edu.cmu.tetrad.algcomparison.algorithm.Algorithms)25 DataSet (edu.cmu.tetrad.data.DataSet)22 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)20 IKnowledge (edu.cmu.tetrad.data.IKnowledge)18 Simulations (edu.cmu.tetrad.algcomparison.simulation.Simulations)17 RandomForward (edu.cmu.tetrad.algcomparison.graph.RandomForward)14 ArrayList (java.util.ArrayList)14 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)13 Test (org.junit.Test)13 Node (edu.cmu.tetrad.graph.Node)11 ActionEvent (java.awt.event.ActionEvent)10 ActionListener (java.awt.event.ActionListener)10 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)8 Fges (edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges)8 DataModel (edu.cmu.tetrad.data.DataModel)8 TitledBorder (javax.swing.border.TitledBorder)8 FisherZ (edu.cmu.tetrad.algcomparison.independence.FisherZ)7