Search in sources :

Example 1 with ParameterEstimationTaskSimulatorIDA

use of org.vcell.optimization.ParameterEstimationTaskSimulatorIDA in project vcell by virtualcell.

the class ParameterEstimationRunTaskPanel method plot.

private void plot() {
    try {
        java.util.Vector<DataSource> dataSourceList = new java.util.Vector<DataSource>();
        java.util.Vector<String> nameVector = new java.util.Vector<String>();
        ModelOptimizationSpec modelOptimizationSpec = parameterEstimationTask.getModelOptimizationSpec();
        final ReferenceDataMappingSpec[] mappingSpecs = modelOptimizationSpec.getReferenceDataMappingSpecs();
        int timeIndex = modelOptimizationSpec.getReferenceDataTimeColumnIndex();
        ReferenceData referenceData = modelOptimizationSpec.getReferenceData();
        if (referenceData != null) {
            dataSourceList.add(new DataSource.DataSourceReferenceData("EXPT", timeIndex, referenceData));
            String[] refColumnNames = referenceData.getColumnNames();
            for (int i = 0; i < refColumnNames.length; i++) {
                if (i == timeIndex) {
                    continue;
                }
                nameVector.add(refColumnNames[i]);
                break;
            }
        }
        ParameterEstimationTaskSimulatorIDA parestSimulator = new ParameterEstimationTaskSimulatorIDA();
        ODESolverResultSet odeSolverResultSet = parestSimulator.getOdeSolverResultSet(parameterEstimationTask);
        if (odeSolverResultSet != null) {
            dataSourceList.add(new DataSource.DataSourceRowColumnResultSet("EST", odeSolverResultSet));
            if (mappingSpecs != null) {
                for (int i = 0; i < mappingSpecs.length; i++) {
                    if (i == timeIndex) {
                        continue;
                    }
                    Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(mappingSpecs[i].getModelObject());
                    nameVector.add(var.getName());
                    break;
                }
            }
        }
        DataSource[] dataSources = (DataSource[]) BeanUtils.getArray(dataSourceList, DataSource.class);
        MultisourcePlotPane multisourcePlotPane = new MultisourcePlotPane();
        multisourcePlotPane.setGroupingListSorter(new Comparator<SortDataReferenceHelper>() {

            @Override
            public int compare(SortDataReferenceHelper o1, SortDataReferenceHelper o2) {
                DataSource ds01 = o1.dataReference.getDataSource();
                DataSource ds02 = o2.dataReference.getDataSource();
                // System.out.println(ds01.getClass().getSimpleName()+" "+o1.dataReference.getIdentifier()+" "+ds02.getClass().getSimpleName()+" "+o2.dataReference.getIdentifier());
                if (ds01 instanceof DataSource.DataSourceReferenceData) {
                    if (ds02 instanceof DataSource.DataSourceReferenceData) {
                        // both reference data, sort names
                        ReferenceDataMappingSpec mspec01 = null;
                        ReferenceDataMappingSpec mspec02 = null;
                        for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
                            // Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
                            if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
                                continue;
                            }
                            if (o1.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
                                mspec01 = rdMappingSpec;
                                o1.setReferenceDataMappingSpec(rdMappingSpec);
                            } else if (o2.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
                                mspec02 = rdMappingSpec;
                                o2.setReferenceDataMappingSpec(rdMappingSpec);
                            }
                        }
                        if (mspec01 == null && mspec02 == null) {
                            return o1.dataReference.getIdentifier().compareToIgnoreCase(o2.dataReference.getIdentifier());
                        } else if (mspec01 != null && mspec02 != null) {
                            return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
                        } else if (mspec01 != null && mspec02 == null) {
                            return -1;
                        } else {
                            return 1;
                        }
                    } else {
                        // compare ref to ode
                        ReferenceDataMappingSpec mspec01 = null;
                        ReferenceDataMappingSpec mspec02 = null;
                        for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
                            Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
                            if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
                                continue;
                            }
                            if (o1.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
                                mspec01 = rdMappingSpec;
                                o1.setReferenceDataMappingSpec(rdMappingSpec);
                            } else if (o2.dataReference.getIdentifier().equals(var.getName())) {
                                mspec02 = rdMappingSpec;
                                o2.setReferenceDataMappingSpec(rdMappingSpec);
                            }
                        }
                        if (mspec01 == null && mspec02 == null) {
                            return -1;
                        } else if (mspec01 != null && mspec02 == null) {
                            return -1;
                        } else if (mspec02 != null && mspec01 == null) {
                            return 1;
                        } else {
                            // BeanUtils.forceStringSize(mspec02.getReferenceDataColumnName(), 25, " ", true));
                            return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
                        }
                    }
                } else {
                    if (ds02 instanceof DataSource.DataSourceRowColumnResultSet) {
                        // both OdeSolverResultSet data, sort names
                        ReferenceDataMappingSpec mspec01 = null;
                        ReferenceDataMappingSpec mspec02 = null;
                        for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
                            Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
                            if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
                                continue;
                            }
                            if (o1.dataReference.getIdentifier().equals(var.getName())) {
                                mspec01 = rdMappingSpec;
                                o1.setReferenceDataMappingSpec(rdMappingSpec);
                            } else if (o2.dataReference.getIdentifier().equals(var.getName())) {
                                mspec02 = rdMappingSpec;
                                o2.setReferenceDataMappingSpec(rdMappingSpec);
                            }
                        }
                        if (mspec01 == null && mspec02 == null) {
                            return o1.dataReference.getIdentifier().compareToIgnoreCase(o2.dataReference.getIdentifier());
                        } else if (mspec01 != null && mspec02 != null) {
                            return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
                        } else if (mspec01 != null && mspec02 == null) {
                            return -1;
                        } else {
                            return 1;
                        }
                    } else {
                        // compare ode to ref
                        ReferenceDataMappingSpec mspec01 = null;
                        ReferenceDataMappingSpec mspec02 = null;
                        for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
                            Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
                            if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
                                continue;
                            }
                            if (o2.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
                                mspec02 = rdMappingSpec;
                                o2.setReferenceDataMappingSpec(rdMappingSpec);
                            } else if (o1.dataReference.getIdentifier().equals(var.getName())) {
                                mspec01 = rdMappingSpec;
                                o1.setReferenceDataMappingSpec(rdMappingSpec);
                            }
                        }
                        if (mspec01 == null && mspec02 == null) {
                            return 1;
                        } else if (mspec01 != null && mspec02 == null) {
                            return -1;
                        } else if (mspec02 != null && mspec01 == null) {
                            return 1;
                        } else {
                            // BeanUtils.forceStringSize(mspec02.getReferenceDataColumnName(), 25, " ", true));
                            return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
                        }
                    }
                }
            }
        });
        multisourcePlotPane.setDataSources(dataSources);
        String[] nameArray = new String[nameVector.size()];
        nameArray = (String[]) BeanUtils.getArray(nameVector, String.class);
        multisourcePlotPane.select(nameArray);
        DialogUtils.showComponentCloseDialog(JOptionPane.getFrameForComponent(this), multisourcePlotPane, "Data Plot");
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}
Also used : Variable(cbit.vcell.math.Variable) MultisourcePlotPane(cbit.vcell.modelopt.gui.MultisourcePlotPane) ReferenceDataMappingSpec(cbit.vcell.modelopt.ReferenceDataMappingSpec) ReservedSymbol(cbit.vcell.model.Model.ReservedSymbol) ModelOptimizationSpec(cbit.vcell.modelopt.ModelOptimizationSpec) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ParameterEstimationTaskSimulatorIDA(org.vcell.optimization.ParameterEstimationTaskSimulatorIDA) OptimizationException(cbit.vcell.opt.OptimizationException) UtilCancelException(org.vcell.util.UtilCancelException) ExpressionException(cbit.vcell.parser.ExpressionException) DataSource(cbit.vcell.modelopt.DataSource) SortDataReferenceHelper(cbit.vcell.modelopt.gui.MultisourcePlotListModel.SortDataReferenceHelper) ReferenceData(cbit.vcell.opt.ReferenceData)

Example 2 with ParameterEstimationTaskSimulatorIDA

use of org.vcell.optimization.ParameterEstimationTaskSimulatorIDA in project vcell by virtualcell.

the class ParameterEstimationRunTaskPanel method solve.

private void solve() throws NumberFormatException {
    CopasiOptimizationMethod com = optimizationMethodParameterTableModel.copasiOptimizationMethod;
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(com);
    // get num runs for stochstic opt mehtods before starting solving...
    if (com.getType().isStochasticMethod()) {
        int numRuns = Integer.parseInt(((String) numberOfRunComboBox.getSelectedItem()));
        optSolverSpec.setNumOfRuns(numRuns);
    }
    parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
    parameterEstimationTask.getModelOptimizationSpec().setComputeProfileDistributions(computeProfileDistributionsCheckBox.isSelected());
    optSolverCallbacks.reset();
    Double endValue = com.getEndValue();
    optSolverCallbacks.setEvaluation(0, Double.POSITIVE_INFINITY, 0, endValue, 0);
    // (endValue != null);
    getRunStatusDialog().showProgressBar(com);
    Collection<AsynchClientTask> taskList = ClientRequestManager.updateMath(this, parameterEstimationTask.getSimulationContext(), false, NetworkGenerationRequirements.ComputeFullStandardTimeout);
    AsynchClientTask task1 = new AsynchClientTask("checking issues", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            StringBuffer issueText = new StringBuffer();
            java.util.Vector<Issue> issueList = new java.util.Vector<Issue>();
            IssueContext issueContext = new IssueContext();
            parameterEstimationTask.gatherIssues(issueContext, issueList);
            boolean bFailed = false;
            for (int i = 0; i < issueList.size(); i++) {
                Issue issue = (Issue) issueList.elementAt(i);
                issueText.append(issue.getMessage() + "\n");
                if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
                    bFailed = true;
                    break;
                }
            }
            if (bFailed) {
                throw new OptimizationException(issueText.toString());
            }
            parameterEstimationTask.refreshMappings();
        }
    };
    taskList.add(task1);
    AsynchClientTask task2 = new AsynchClientTask("solving", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            MathMappingCallback mathMappingCallback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
            // OptimizationResultSet optResultSet = CopasiOptimizationSolver.solveLocalPython(new ParameterEstimationTaskSimulatorIDA(),parameterEstimationTask,optSolverCallbacks,mathMappingCallback);
            OptimizationResultSet optResultSet = CopasiOptimizationSolver.solveRemoteApi(new ParameterEstimationTaskSimulatorIDA(), parameterEstimationTask, optSolverCallbacks, mathMappingCallback);
            hashTable.put(ORS_KEY, optResultSet);
        }
    };
    taskList.add(task2);
    AsynchClientTask setResultTask = new AsynchClientTask("set results", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            OptimizationResultSet optResultSet = (OptimizationResultSet) hashTable.get(ORS_KEY);
            parameterEstimationTask.setOptimizationResultSet(optResultSet);
        }
    };
    taskList.add(setResultTask);
    ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), taskList.toArray(new AsynchClientTask[taskList.size()]), getRunStatusDialog(), true, true, true, null, false);
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Issue(org.vcell.util.Issue) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) Hashtable(java.util.Hashtable) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) CopasiOptimizationMethod(cbit.vcell.opt.CopasiOptimizationMethod) IssueContext(org.vcell.util.IssueContext) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) EventObject(java.util.EventObject) ParameterEstimationTaskSimulatorIDA(org.vcell.optimization.ParameterEstimationTaskSimulatorIDA)

Example 3 with ParameterEstimationTaskSimulatorIDA

use of org.vcell.optimization.ParameterEstimationTaskSimulatorIDA in project vcell by virtualcell.

the class OptimizationService method optimize.

public static OptimizationResultSet optimize(ParameterEstimationTask parameterEstimationTask) throws Exception {
    // if (OperatingSystemInfo.getInstance().isMac()){
    // throw new RuntimeException("parameter estimation not currently available on Mac\n\n   try Windows or Linux.\n\n   coming soon on Mac.");
    // }
    copasiOptCallbacks.reset();
    updateMath(parameterEstimationTask.getSimulationContext(), NetworkGenerationRequirements.ComputeFullStandardTimeout);
    StringBuffer issueText = new StringBuffer();
    java.util.Vector<Issue> issueList = new java.util.Vector<Issue>();
    IssueContext issueContext = new IssueContext();
    parameterEstimationTask.gatherIssues(issueContext, issueList);
    boolean bFailed = false;
    for (int i = 0; i < issueList.size(); i++) {
        Issue issue = (Issue) issueList.elementAt(i);
        issueText.append(issue.getMessage() + "\n");
        if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
            bFailed = true;
            break;
        }
    }
    if (bFailed) {
        throw new OptimizationException(issueText.toString());
    }
    parameterEstimationTask.refreshMappings();
    OptimizationResultSet optResultSet = CopasiOptimizationSolver.solveLocalPython(new ParameterEstimationTaskSimulatorIDA(), parameterEstimationTask, copasiOptCallbacks, callback);
    return optResultSet;
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) Issue(org.vcell.util.Issue) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) IssueContext(org.vcell.util.IssueContext) ParameterEstimationTaskSimulatorIDA(org.vcell.optimization.ParameterEstimationTaskSimulatorIDA)

Aggregations

OptimizationException (cbit.vcell.opt.OptimizationException)3 ParameterEstimationTaskSimulatorIDA (org.vcell.optimization.ParameterEstimationTaskSimulatorIDA)3 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)2 Issue (org.vcell.util.Issue)2 IssueContext (org.vcell.util.IssueContext)2 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)1 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 Variable (cbit.vcell.math.Variable)1 ReservedSymbol (cbit.vcell.model.Model.ReservedSymbol)1 DataSource (cbit.vcell.modelopt.DataSource)1 ModelOptimizationSpec (cbit.vcell.modelopt.ModelOptimizationSpec)1 ReferenceDataMappingSpec (cbit.vcell.modelopt.ReferenceDataMappingSpec)1 SortDataReferenceHelper (cbit.vcell.modelopt.gui.MultisourcePlotListModel.SortDataReferenceHelper)1 MultisourcePlotPane (cbit.vcell.modelopt.gui.MultisourcePlotPane)1 CopasiOptimizationMethod (cbit.vcell.opt.CopasiOptimizationMethod)1 OptimizationSolverSpec (cbit.vcell.opt.OptimizationSolverSpec)1 ReferenceData (cbit.vcell.opt.ReferenceData)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)1