Search in sources :

Example 6 with AlgorithmModel

use of edu.cmu.tetradapp.ui.model.AlgorithmModel in project tetrad by cmu-phil.

the class GeneralAlgorithmEditor method refreshTestList.

private void refreshTestList() {
    updatingTestModels = true;
    indTestComboBox.removeAllItems();
    AlgorithmModel algoModel = algorithmList.getSelectedValue();
    if (algoModel != null && algoModel.isRequiredTest()) {
        boolean linear = linearVarChkBox.isSelected();
        boolean gaussian = gaussianVarChkBox.isSelected();
        List<IndependenceTestModel> models = IndependenceTestModels.getInstance().getModels(dataType);
        if (linear && gaussian) {
            models.stream().filter(e -> e.getIndependenceTest().getClazz().isAnnotationPresent(Linear.class)).filter(e -> e.getIndependenceTest().getClazz().isAnnotationPresent(Gaussian.class)).forEach(e -> indTestComboBox.addItem(e));
        } else if (linear) {
            models.stream().filter(e -> e.getIndependenceTest().getClazz().isAnnotationPresent(Linear.class)).filter(e -> !e.getIndependenceTest().getClazz().isAnnotationPresent(Gaussian.class)).forEach(e -> indTestComboBox.addItem(e));
        } else if (gaussian) {
            models.stream().filter(e -> !e.getIndependenceTest().getClazz().isAnnotationPresent(Linear.class)).filter(e -> e.getIndependenceTest().getClazz().isAnnotationPresent(Gaussian.class)).forEach(e -> indTestComboBox.addItem(e));
        } else {
            models.stream().forEach(e -> indTestComboBox.addItem(e));
        }
    }
    updatingTestModels = false;
    if (indTestComboBox.getItemCount() > 0) {
        indTestComboBox.setEnabled(true);
        Map<DataType, IndependenceTestModel> map = defaultIndTestModels.get(algoModel);
        if (map == null) {
            map = new EnumMap<>(DataType.class);
            defaultIndTestModels.put(algoModel, map);
        }
        IndependenceTestModel testModel = map.get(dataType);
        if (testModel == null) {
            testModel = IndependenceTestModels.getInstance().getDefaultModel(dataType);
            if (testModel == null) {
                testModel = indTestComboBox.getItemAt(0);
            }
        }
        indTestComboBox.setSelectedItem(testModel);
    } else {
        indTestComboBox.setEnabled(false);
    }
}
Also used : Enumeration(java.util.Enumeration) JDialog(javax.swing.JDialog) TetradDesktop(edu.cmu.tetradapp.app.TetradDesktop) LoggerFactory(org.slf4j.LoggerFactory) Parameters(edu.cmu.tetrad.util.Parameters) Node(edu.cmu.tetrad.graph.Node) PaddingPanel(edu.cmu.tetradapp.ui.PaddingPanel) StringUtils(org.apache.commons.lang3.StringUtils) Linear(edu.cmu.tetrad.annotation.Linear) AlgorithmModel(edu.cmu.tetradapp.ui.model.AlgorithmModel) Map(java.util.Map) ScoreModel(edu.cmu.tetradapp.ui.model.ScoreModel) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) HpcJobActivityAction(edu.cmu.tetradapp.app.hpc.action.HpcJobActivityAction) BorderLayout(java.awt.BorderLayout) ButtonModel(javax.swing.ButtonModel) JComboBox(javax.swing.JComboBox) GraphSelectionWrapper(edu.cmu.tetradapp.model.GraphSelectionWrapper) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GeneralAlgorithmRunner(edu.cmu.tetradapp.model.GeneralAlgorithmRunner) BdeuScore(edu.cmu.tetrad.algcomparison.score.BdeuScore) HpcJobManager(edu.cmu.tetradapp.app.hpc.manager.HpcJobManager) Frame(java.awt.Frame) HpcAccountManager(edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) EnumMap(java.util.EnumMap) AlgType(edu.cmu.tetrad.annotation.AlgType) HpcParameter(edu.pitt.dbmi.tetrad.db.entity.HpcParameter) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) ComboBoxModel(javax.swing.ComboBoxModel) Gaussian(edu.cmu.tetrad.annotation.Gaussian) Nonexecutable(edu.cmu.tetrad.annotation.Nonexecutable) JsonWebToken(edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken) HpcJobInfo(edu.pitt.dbmi.tetrad.db.entity.HpcJobInfo) JRadioButton(javax.swing.JRadioButton) DataModel(edu.cmu.tetrad.data.DataModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) Box(javax.swing.Box) Dimension(java.awt.Dimension) List(java.util.List) DataSet(edu.cmu.tetrad.data.DataSet) JCheckBox(javax.swing.JCheckBox) Optional(java.util.Optional) MessageDigestHash(edu.pitt.dbmi.ccd.commons.file.MessageDigestHash) JPanel(javax.swing.JPanel) Toolkit(java.awt.Toolkit) CardLayout(java.awt.CardLayout) JOptionUtils(edu.cmu.tetrad.util.JOptionUtils) HashMap(java.util.HashMap) AlgorithmModels(edu.cmu.tetradapp.ui.model.AlgorithmModels) FinalizingEditor(edu.cmu.tetradapp.util.FinalizingEditor) AlgorithmParamRequest(edu.pitt.dbmi.tetrad.db.entity.AlgorithmParamRequest) SwingConstants(javax.swing.SwingConstants) ArrayList(java.util.ArrayList) SingleGraphAlg(edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.SingleGraphAlg) HashSet(java.util.HashSet) HasKnowledge(edu.cmu.tetrad.algcomparison.utils.HasKnowledge) SemBicScore(edu.cmu.tetrad.algcomparison.score.SemBicScore) DataType(edu.cmu.tetrad.data.DataType) JsonUtils(edu.cmu.tetrad.util.JsonUtils) IndependenceTestModel(edu.cmu.tetradapp.ui.model.IndependenceTestModel) IndependenceTestModels(edu.cmu.tetradapp.ui.model.IndependenceTestModels) TsImages(edu.cmu.tetrad.algcomparison.algorithm.oracle.pag.TsImages) LinkedList(java.util.LinkedList) AlgorithmFactory(edu.cmu.tetrad.algcomparison.algorithm.AlgorithmFactory) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) JButton(javax.swing.JButton) Logger(org.slf4j.Logger) AlgorithmParameter(edu.pitt.dbmi.tetrad.db.entity.AlgorithmParameter) Files(java.nio.file.Files) ButtonGroup(javax.swing.ButtonGroup) TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) Window(java.awt.Window) JList(javax.swing.JList) DesktopController(edu.cmu.tetradapp.util.DesktopController) Graph(edu.cmu.tetrad.graph.Graph) HpcAccountUtils(edu.cmu.tetradapp.app.hpc.util.HpcAccountUtils) JvmOptions(edu.pitt.dbmi.tetrad.db.entity.JvmOptions) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) JScrollPane(javax.swing.JScrollPane) LayoutStyle(javax.swing.LayoutStyle) DataModelList(edu.cmu.tetrad.data.DataModelList) ScoreModels(edu.cmu.tetradapp.ui.model.ScoreModels) DefaultListModel(javax.swing.DefaultListModel) JLabel(javax.swing.JLabel) GroupLayout(javax.swing.GroupLayout) HpcAccount(edu.pitt.dbmi.tetrad.db.entity.HpcAccount) JTextArea(javax.swing.JTextArea) Knowledge2(edu.cmu.tetrad.data.Knowledge2) IndependenceTestModel(edu.cmu.tetradapp.ui.model.IndependenceTestModel) DataType(edu.cmu.tetrad.data.DataType) AlgorithmModel(edu.cmu.tetradapp.ui.model.AlgorithmModel) Linear(edu.cmu.tetrad.annotation.Linear)

Example 7 with AlgorithmModel

use of edu.cmu.tetradapp.ui.model.AlgorithmModel 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);
}
Also used : Frame(java.awt.Frame) JTextArea(javax.swing.JTextArea) IndependenceTestModel(edu.cmu.tetradapp.ui.model.IndependenceTestModel) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) BorderLayout(java.awt.BorderLayout) HpcJobManager(edu.cmu.tetradapp.app.hpc.manager.HpcJobManager) HpcAccountManager(edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager) AlgorithmParamRequest(edu.pitt.dbmi.tetrad.db.entity.AlgorithmParamRequest) AlgorithmModel(edu.cmu.tetradapp.ui.model.AlgorithmModel) HashSet(java.util.HashSet) JScrollPane(javax.swing.JScrollPane) Path(java.nio.file.Path) Parameters(edu.cmu.tetrad.util.Parameters) Dimension(java.awt.Dimension) IOException(java.io.IOException) JsonWebToken(edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken) ScoreModel(edu.cmu.tetradapp.ui.model.ScoreModel) HpcParameter(edu.pitt.dbmi.tetrad.db.entity.HpcParameter) HpcJobActivityAction(edu.cmu.tetradapp.app.hpc.action.HpcJobActivityAction) DataModel(edu.cmu.tetrad.data.DataModel) JvmOptions(edu.pitt.dbmi.tetrad.db.entity.JvmOptions) HpcJobInfo(edu.pitt.dbmi.tetrad.db.entity.HpcJobInfo) AlgorithmParameter(edu.pitt.dbmi.tetrad.db.entity.AlgorithmParameter) JDialog(javax.swing.JDialog)

Example 8 with AlgorithmModel

use of edu.cmu.tetradapp.ui.model.AlgorithmModel in project tetrad by cmu-phil.

the class GeneralAlgorithmEditor method doSearch.

private void doSearch(final GeneralAlgorithmRunner runner) {
    new WatchedProcess((Window) getTopLevelAncestor()) {

        @Override
        public void watch() {
            AlgorithmModel algoModel = algorithmList.getSelectedValue();
            if (algoModel != null) {
                String title = String.format("Algorithm: %s", algoModel.getAlgorithm().getAnnotation().name());
                algorithmGraphTitle.setText(title);
                HpcAccount hpcAccount = null;
                if (algoModel.getAlgorithm().getAnnotation().algoType() != AlgType.orient_pairwise && runner.getDataModelList().getModelList().size() == 1) {
                    String algoName = algoModel.getAlgorithm().getAnnotation().name();
                    hpcAccount = showRemoteComputingOptions(algoName);
                }
                if (hpcAccount == null) {
                    graphEditor.saveLayout();
                    runner.execute();
                    // Show graph
                    graphEditor.replace(runner.getGraphs());
                    graphEditor.validate();
                    firePropertyChange("modelChanged", null, null);
                    // Update the graphContainer
                    graphContainer.add(graphEditor);
                    changeCard(GRAPH_CARD);
                } else {
                    try {
                        doRemoteCompute(runner, hpcAccount);
                    } catch (Exception exception) {
                        LOGGER.error("Unable to run algorithm.", exception);
                    }
                }
            }
        }
    };
}
Also used : WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) HpcAccount(edu.pitt.dbmi.tetrad.db.entity.HpcAccount) AlgorithmModel(edu.cmu.tetradapp.ui.model.AlgorithmModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Aggregations

AlgorithmModel (edu.cmu.tetradapp.ui.model.AlgorithmModel)8 IndependenceTestModel (edu.cmu.tetradapp.ui.model.IndependenceTestModel)5 ScoreModel (edu.cmu.tetradapp.ui.model.ScoreModel)5 BorderLayout (java.awt.BorderLayout)5 Dimension (java.awt.Dimension)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)4 DataModel (edu.cmu.tetrad.data.DataModel)4 DataSet (edu.cmu.tetrad.data.DataSet)4 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)4 Node (edu.cmu.tetrad.graph.Node)4 Parameters (edu.cmu.tetrad.util.Parameters)4 HpcJobActivityAction (edu.cmu.tetradapp.app.hpc.action.HpcJobActivityAction)4 HpcAccountManager (edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager)4 HpcJobManager (edu.cmu.tetradapp.app.hpc.manager.HpcJobManager)4 AlgorithmFactory (edu.cmu.tetrad.algcomparison.algorithm.AlgorithmFactory)3 TsImages (edu.cmu.tetrad.algcomparison.algorithm.oracle.pag.TsImages)3 SingleGraphAlg (edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.SingleGraphAlg)3 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)3 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)3