use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class WorkflowObjectsPanel method hasDisplayData.
private boolean hasDisplayData(TaskContext taskContext, WorkflowObject workflowObject) {
if (workflowObject instanceof DataInput || workflowObject instanceof DataOutput) {
String title = parametersFunctionsTableModel.getName(workflowObject);
WindowListener listener = new WindowAdapter() {
};
Object data = null;
if (workflowObject instanceof WorkflowDataSource) {
WorkflowDataSource dataHolder = (WorkflowDataSource) workflowObject;
data = taskContext.getRepository().getData(dataHolder);
} else if (workflowObject instanceof DataInput) {
DataInput dataInput = (DataInput) workflowObject;
data = taskContext.getData(dataInput);
}
if (data instanceof RowColumnResultSet || data instanceof ROI || data instanceof ProfileData[] || data instanceof Image || data instanceof ImageTimeSeries) {
return true;
}
}
return false;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class VirtualFrapBatchRunFrame method getBatchRunTasks.
private ArrayList<AsynchClientTask> getBatchRunTasks() {
// to run batch files
ArrayList<AsynchClientTask> batchRunTaskList = new ArrayList<AsynchClientTask>();
for (int i = 0; i < batchRunWorkspace.getFrapStudies().size(); i++) {
final int finalIdx = i;
AsynchClientTask message1Task = new AsynchClientTask("Preparing for parameter estimation ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
appendJobStatus("<html><br>Running " + (batchRunWorkspace.getFrapStudies().get(finalIdx)).getXmlFilename() + "</html>", false);
}
};
AsynchClientTask saveTask = new AsynchClientTask("Preparing for parameter estimation ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
if (fStudy.hasDiffusionOnlyModel()) {
// check external data info. If not existing, we have to run ref simulation.
if (!FRAPWorkspace.areExternalDataOK(localWorkspace, fStudy.getFrapDataExternalDataInfo(), fStudy.getRoiExternalDataInfo())) {
// if external files are missing/currupt or ROIs are changed, create keys and save them
fStudy.setFrapDataExternalDataInfo(FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.IMAGE_EXTDATA_NAME));
fStudy.setRoiExternalDataInfo(FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.ROI_EXTDATA_NAME));
fStudy.saveROIsAsExternalData(localWorkspace, fStudy.getRoiExternalDataInfo().getExternalDataIdentifier(), fStudy.getStartingIndexForRecovery());
fStudy.saveImageDatasetAsExternalData(localWorkspace, fStudy.getFrapDataExternalDataInfo().getExternalDataIdentifier(), fStudy.getStartingIndexForRecovery());
}
}
}
};
AsynchClientTask message2Task = new AsynchClientTask("Running reference simulation ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
if (fStudy.hasDiffusionOnlyModel()) {
MessagePanel msgPanel = appendJobStatus("Running reference simulation ...", true);
hashTable.put("runRefStatus", msgPanel);
} else if (fStudy.hasReactionOnlyOffRateModel()) {
// MessagePanel msgPanel = appendJobStatus("...", true);
// hashTable.put("runRefStatus", msgPanel);
}
}
};
AsynchClientTask runRefSimTask = new AsynchClientTask("Running reference simulation ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
if (fStudy.hasDiffusionOnlyModel()) {
MessagePanel msgPanel = (MessagePanel) hashTable.get("runRefStatus");
// run ref sim
if (// if ref data is stored ,we don't have to re-run
fStudy.getStoredRefData() != null) {
fStudy.setFrapOptData(new FRAPOptData(fStudy, FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF, localWorkspace, fStudy.getStoredRefData()));
} else {
fStudy.setFrapOptData(new FRAPOptData(fStudy, FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF, localWorkspace, msgPanel));
}
} else if (fStudy.hasReactionOnlyOffRateModel()) {
if (fStudy.getFrapOptFunc() == null) {
fStudy.setFrapOptFunc(new FRAPOptFunctions(fStudy));
}
}
}
};
AsynchClientTask message3Task = new AsynchClientTask("Running optimization ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
if (fStudy.hasDiffusionOnlyModel()) {
MessagePanel msgPanel = (MessagePanel) hashTable.get("runRefStatus");
msgPanel.setProgressCompleted();
}
MessagePanel msgPanel1 = appendJobStatus("Running optimization ...", true);
hashTable.put("optimizationStatus", msgPanel1);
}
};
AsynchClientTask runOptTask = new AsynchClientTask("Running optimization ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
ArrayList<Integer> models = fStudy.getSelectedModels();
if (models.size() == 1) {
for (int i = 0; i < models.size(); i++) {
int model = (models.get(i)).intValue();
if (model == batchRunWorkspace.getSelectedModel()) {
if (model == FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT) {
// if(fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() == null)
// {//always run
fStudy.getFrapOptData().setNumEstimatedParams(FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF);
Parameter[] initialParams = FRAPModel.getInitialParameters(fStudy.getFrapData(), FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT], fStudy.getStartingIndexForRecovery());
// set model parameters and data
Parameter[] bestParameters = fStudy.getFrapOptData().getBestParamters(initialParams, fStudy.getSelectedROIsForErrorCalculation());
double[][] fitData = fStudy.getFrapOptData().getFitData(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].setModelParameters(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].setData(fitData);
// }
} else if (model == FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS) {
// if(fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() == null)
// {//always run
fStudy.getFrapOptData().setNumEstimatedParams(FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF);
Parameter[] initialParams = FRAPModel.getInitialParameters(fStudy.getFrapData(), FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS], fStudy.getStartingIndexForRecovery());
// set model parameters and data
Parameter[] bestParameters = fStudy.getFrapOptData().getBestParamters(initialParams, fStudy.getSelectedROIsForErrorCalculation());
double[][] fitData = fStudy.getFrapOptData().getFitData(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].setModelParameters(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].setData(fitData);
// }
} else if (model == FRAPModel.IDX_MODEL_REACTION_OFF_RATE) {
Parameter[] bestParameters = fStudy.getFrapOptFunc().getBestParamters(fStudy.getFrapData(), null, true);
double[][] fitData = fStudy.getFrapOptFunc().getFitData(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].setModelParameters(bestParameters);
fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].setData(fitData);
}
} else {
throw new Exception("Selected model for batch run is not the same as selected model in FRAP doc " + fStudy.getXmlFilename());
}
}
} else {
throw new Exception("Selected model size exceed 1");
}
}
};
AsynchClientTask message4Task = new AsynchClientTask("Evaluating confidence intervals for parameters ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
MessagePanel msgPanel = (MessagePanel) hashTable.get("optimizationStatus");
msgPanel.setProgressCompleted();
MessagePanel msgPanel2 = appendJobStatus("Evaluating confidence intervals for parameters ...", true);
hashTable.put("evaluateCI", msgPanel2);
}
};
AsynchClientTask evaluateCITask = new AsynchClientTask("Evaluating confidence intervals for parameters ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = (batchRunWorkspace.getFrapStudies().get(finalIdx));
MessagePanel msgPanel = (MessagePanel) hashTable.get("evaluateCI");
// evaluate confidence intervals
ArrayList<Integer> models = fStudy.getSelectedModels();
if (models.size() == 1) {
for (int i = 0; i < models.size(); i++) {
int model = (models.get(i)).intValue();
if (model == batchRunWorkspace.getSelectedModel()) {
if (model == FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() != null) {
ProfileData[] profileData = fStudy.getFrapOptData().evaluateParameters(fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters(), msgPanel);
fStudy.setProfileData_oneDiffComponent(profileData);
}
} else if (model == FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() != null) {
ProfileData[] profileData = fStudy.getFrapOptData().evaluateParameters(fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters(), msgPanel);
fStudy.setProfileData_twoDiffComponents(profileData);
}
} else if (model == FRAPModel.IDX_MODEL_REACTION_OFF_RATE) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters() != null) {
ProfileData[] profileData = fStudy.getFrapOptFunc().evaluateParameters(fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters(), msgPanel);
fStudy.setProfileData_reactionOffRate(profileData);
}
}
} else {
throw new Exception("Selected model for batch run is not the same as selected model in FRAP doc " + fStudy.getXmlFilename());
}
}
} else {
throw new Exception("Selected model size exceed 1");
}
}
};
AsynchClientTask message5Task = new AsynchClientTask("Evaluating confidence intervals for parameters ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
MessagePanel msgPanel = (MessagePanel) hashTable.get("evaluateCI");
msgPanel.setProgressCompleted();
}
};
batchRunTaskList.add(message1Task);
batchRunTaskList.add(saveTask);
batchRunTaskList.add(message2Task);
batchRunTaskList.add(runRefSimTask);
batchRunTaskList.add(message3Task);
batchRunTaskList.add(runOptTask);
batchRunTaskList.add(message4Task);
batchRunTaskList.add(evaluateCITask);
batchRunTaskList.add(message5Task);
}
return batchRunTaskList;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class AnalysisResultsTablePanel method propertyChange.
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(FRAPSingleWorkspace.PROPERTY_CHANGE_CONFIDENCEINTERVAL_DETAILS)) {
if (((Integer) evt.getNewValue()).intValue() == AnalysisTableModel.COLUMN_DIFF_ONE_CI_PLOT && getTable().getSelectedRow() < FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF) {
if (frapWorkspace.getWorkingFrapStudy() != null && frapWorkspace.getWorkingFrapStudy().getProfileData_oneDiffComponent() != null) {
int paramIdx = getTable().getSelectedRow();
ProfileData[] profileData = frapWorkspace.getWorkingFrapStudy().getProfileData_oneDiffComponent();
// put plotpanes of different parameters' profile likelihoods into a base panel
JPanel basePanel = new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
ConfidenceIntervalPlotPanel plotPanel = new ConfidenceIntervalPlotPanel();
plotPanel.setProfileSummaryData(FRAPOptimizationUtils.getSummaryFromProfileData(profileData[paramIdx]));
plotPanel.setBorder(new EtchedBorder());
String paramName = "";
if (profileData[paramIdx].getProfileDataElements().size() > 0) {
paramName = profileData[paramIdx].getProfileDataElements().get(0).getParamName();
}
ProfileDataPanel profileDataPanel = new ProfileDataPanel(plotPanel, paramName);
profileDataPanel.setProfileDataPlotDetailsVisible(true);
basePanel.add(profileDataPanel);
JScrollPane scrollPane = new JScrollPane(basePanel);
scrollPane.setAutoscrolls(true);
scrollPane.setPreferredSize(new Dimension(600, 600));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// show plots in a dialog
DialogUtils.showComponentCloseDialog(parent, scrollPane, "Profile Likelihood of Parameters");
} else {
DialogUtils.showErrorDialog(parent, "Confidence Intervals haven't been evaluated.");
}
} else if (((Integer) evt.getNewValue()).intValue() == AnalysisTableModel.COLUMN_DIFF_TWO_CI_PLOT && getTable().getSelectedRow() < FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF) {
if (frapWorkspace.getWorkingFrapStudy() != null && frapWorkspace.getWorkingFrapStudy().getProfileData_twoDiffComponents() != null) {
int paramIdx = getTable().getSelectedRow();
ProfileData[] profileData = frapWorkspace.getWorkingFrapStudy().getProfileData_twoDiffComponents();
// put plotpanes of different parameters' profile likelihoods into a base panel
JPanel basePanel = new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
ConfidenceIntervalPlotPanel plotPanel = new ConfidenceIntervalPlotPanel();
plotPanel.setProfileSummaryData(FRAPOptimizationUtils.getSummaryFromProfileData(profileData[paramIdx]));
plotPanel.setBorder(new EtchedBorder());
String paramName = "";
if (profileData[paramIdx].getProfileDataElements().size() > 0) {
paramName = profileData[paramIdx].getProfileDataElements().get(0).getParamName();
}
ProfileDataPanel profileDataPanel = new ProfileDataPanel(plotPanel, paramName);
profileDataPanel.setProfileDataPlotDetailsVisible(true);
basePanel.add(profileDataPanel);
JScrollPane scrollPane = new JScrollPane(basePanel);
scrollPane.setAutoscrolls(true);
scrollPane.setPreferredSize(new Dimension(620, 600));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// show plots in a dialog
DialogUtils.showComponentCloseDialog(parent, scrollPane, "Profile Likelihood of Parameters");
} else {
DialogUtils.showErrorDialog(parent, "Confidence Intervals haven't been evaluated.");
}
} else if (((Integer) evt.getNewValue()).intValue() == AnalysisTableModel.COLUMN_KOFF_CI_PLOT) {
if (frapWorkspace.getWorkingFrapStudy() != null && frapWorkspace.getWorkingFrapStudy().getProfileData_reactionOffRate() != null) {
ProfileData[] profileData = frapWorkspace.getWorkingFrapStudy().getProfileData_reactionOffRate();
ProfileData tempProfileData = null;
if (getTable().getSelectedRow() == FRAPModel.INDEX_BLEACH_MONITOR_RATE) {
for (ProfileData pData : profileData) {
if (pData.getProfileDataElements().size() > 0 && pData.getProfileDataElements().get(0).getParamName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_BLEACH_MONITOR_RATE])) {
tempProfileData = pData;
}
}
} else if (getTable().getSelectedRow() == FRAPModel.INDEX_OFF_RATE) {
for (ProfileData pData : profileData) {
if (pData.getProfileDataElements().size() > 0 && pData.getProfileDataElements().get(0).getParamName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_OFF_RATE])) {
tempProfileData = pData;
}
}
}
// put plotpanes of different parameters' profile likelihoods into a base panel
JPanel basePanel = new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
ConfidenceIntervalPlotPanel plotPanel = new ConfidenceIntervalPlotPanel();
plotPanel.setProfileSummaryData(FRAPOptimizationUtils.getSummaryFromProfileData(tempProfileData));
plotPanel.setBorder(new EtchedBorder());
String paramName = "";
if (tempProfileData.getProfileDataElements().size() > 0) {
paramName = tempProfileData.getProfileDataElements().get(0).getParamName();
}
ProfileDataPanel profileDataPanel = new ProfileDataPanel(plotPanel, paramName);
profileDataPanel.setProfileDataPlotDetailsVisible(true);
basePanel.add(profileDataPanel);
JScrollPane scrollPane = new JScrollPane(basePanel);
scrollPane.setAutoscrolls(true);
scrollPane.setPreferredSize(new Dimension(600, 600));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// show plots in a dialog
DialogUtils.showComponentCloseDialog(parent, scrollPane, "Profile Likelihood of Parameters");
} else {
DialogUtils.showErrorDialog(parent, "Confidence Intervals haven't been evaluated.");
}
}
}
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class FRAPOptData method evaluateParameters.
public ProfileData[] evaluateParameters(Parameter[] currentParams, ClientTaskStatusSupport clientTaskStatusSupport) throws Exception {
// long startTime =System.currentTimeMillis();
int totalParamLen = currentParams.length;
ProfileData[] resultData = new ProfileData[totalParamLen];
FRAPStudy frapStudy = getExpFrapStudy();
for (int j = 0; j < totalParamLen; j++) {
ProfileData profileData = new ProfileData();
// add the fixed parameter to profileData, output exp data and opt results
setNumEstimatedParams(totalParamLen);
Parameter[] newBestParameters = getBestParamters(currentParams, frapStudy.getSelectedROIsForErrorCalculation(), null, true);
double iniError = getLeastError();
// fixed parameter
Parameter fixedParam = newBestParameters[j];
if (// log function cannot take 0 as parameter
fixedParam.getInitialGuess() == 0) {
fixedParam = new Parameter(fixedParam.getName(), fixedParam.getLowerBound(), fixedParam.getUpperBound(), fixedParam.getScale(), FRAPOptimizationUtils.epsilon);
}
if (clientTaskStatusSupport != null) {
if (totalParamLen == FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF) {
clientTaskStatusSupport.setMessage("<html>Evaluating confidence intervals of \'" + fixedParam.getName() + "\' <br> of diffusion with one diffusing component model.</html>");
} else if (totalParamLen == FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF) {
clientTaskStatusSupport.setMessage("<html>Evaluating confidence intervals of \'" + fixedParam.getName() + "\' <br> of diffusion with two diffusing components model.</html>");
}
// start evaluation of a parameter.
clientTaskStatusSupport.setProgress(0);
}
ProfileDataElement pde = new ProfileDataElement(fixedParam.getName(), Math.log10(fixedParam.getInitialGuess()), iniError, newBestParameters);
profileData.addElement(pde);
Parameter[] unFixedParams = new Parameter[totalParamLen - 1];
int indexCounter = 0;
for (int i = 0; i < totalParamLen; i++) {
if (!newBestParameters[i].getName().equals(fixedParam.getName())) {
unFixedParams[indexCounter] = newBestParameters[i];
indexCounter++;
} else
continue;
}
// increase
int iterationCount = 1;
double paramLogVal = Math.log10(fixedParam.getInitialGuess());
double lastError = iniError;
boolean isBoundReached = false;
double incrementStep = DEFAULT_CI_STEPS[j];
int stepIncreaseCount = 0;
while (true) {
if (// if exceeds the maximum iterations, break;
iterationCount > MAX_ITERATION) {
break;
}
if (isBoundReached) {
break;
}
paramLogVal = paramLogVal + incrementStep;
double paramVal = Math.pow(10, paramLogVal);
if (paramVal > (fixedParam.getUpperBound() - FRAPOptimizationUtils.epsilon)) {
paramVal = fixedParam.getUpperBound();
paramLogVal = Math.log10(fixedParam.getUpperBound());
isBoundReached = true;
}
Parameter increasedParam = new Parameter(fixedParam.getName(), fixedParam.getLowerBound(), fixedParam.getUpperBound(), fixedParam.getScale(), paramVal);
// getBestParameters returns the whole set of parameters including the fixed parameters
setNumEstimatedParams(totalParamLen - 1);
Parameter[] newParameters = getBestParamters(unFixedParams, frapStudy.getSelectedROIsForErrorCalculation(), increasedParam, true);
for (// use last step unfixed parameter values to optimize
int i = 0; // use last step unfixed parameter values to optimize
i < newParameters.length; // use last step unfixed parameter values to optimize
i++) {
for (int k = 0; k < unFixedParams.length; k++) {
if (newParameters[i].getName().equals(unFixedParams[k].getName())) {
Parameter tempParameter = new Parameter(unFixedParams[k].getName(), unFixedParams[k].getLowerBound(), unFixedParams[k].getUpperBound(), unFixedParams[k].getScale(), newParameters[i].getInitialGuess());
unFixedParams[k] = tempParameter;
}
}
}
double error = getLeastError();
pde = new ProfileDataElement(increasedParam.getName(), paramLogVal, error, newParameters);
profileData.addElement(pde);
// check if the we run enough to get confidence intervals(99% @6.635, we plus 10 over the min error)
if (error > (iniError + 10)) {
break;
}
if (Math.abs((error - lastError) / lastError) < MIN_LIKELIHOOD_CHANGE) {
stepIncreaseCount++;
incrementStep = DEFAULT_CI_STEPS[j] * Math.pow(2, stepIncreaseCount);
} else {
if (stepIncreaseCount > 1) {
incrementStep = DEFAULT_CI_STEPS[j] / Math.pow(2, stepIncreaseCount);
stepIncreaseCount--;
}
}
if (clientTaskStatusSupport.isInterrupted()) {
throw UserCancelException.CANCEL_GENERIC;
}
lastError = error;
iterationCount++;
clientTaskStatusSupport.setProgress((int) ((iterationCount * 1.0 / MAX_ITERATION) * 0.5 * 100));
}
// half way through evaluation of a parameter.
clientTaskStatusSupport.setProgress(50);
// decrease
iterationCount = 1;
paramLogVal = Math.log10(fixedParam.getInitialGuess());
;
;
lastError = iniError;
isBoundReached = false;
double decrementStep = DEFAULT_CI_STEPS[j];
stepIncreaseCount = 0;
while (true) {
if (// if exceeds the maximum iterations, break;
iterationCount > MAX_ITERATION) {
break;
}
if (isBoundReached) {
break;
}
paramLogVal = paramLogVal - decrementStep;
double paramVal = Math.pow(10, paramLogVal);
if (paramVal < (fixedParam.getLowerBound() + FRAPOptimizationUtils.epsilon)) {
paramVal = FRAPOptimizationUtils.epsilon;
paramLogVal = Math.log10(FRAPOptimizationUtils.epsilon);
isBoundReached = true;
}
Parameter decreasedParam = new Parameter(fixedParam.getName(), fixedParam.getLowerBound(), fixedParam.getUpperBound(), fixedParam.getScale(), paramVal);
// getBestParameters returns the whole set of parameters including the fixed parameters
setNumEstimatedParams(totalParamLen - 1);
Parameter[] newParameters = getBestParamters(unFixedParams, frapStudy.getSelectedROIsForErrorCalculation(), decreasedParam, true);
for (// use last step unfixed parameter values to optimize
int i = 0; // use last step unfixed parameter values to optimize
i < newParameters.length; // use last step unfixed parameter values to optimize
i++) {
for (int k = 0; k < unFixedParams.length; k++) {
if (newParameters[i].getName().equals(unFixedParams[k].getName())) {
Parameter tempParameter = new Parameter(unFixedParams[k].getName(), unFixedParams[k].getLowerBound(), unFixedParams[k].getUpperBound(), unFixedParams[k].getScale(), newParameters[i].getInitialGuess());
unFixedParams[k] = tempParameter;
}
}
}
double error = getLeastError();
pde = new ProfileDataElement(decreasedParam.getName(), paramLogVal, error, newParameters);
profileData.addElement(0, pde);
if (error > (iniError + 10)) {
break;
}
if (Math.abs((error - lastError) / lastError) < MIN_LIKELIHOOD_CHANGE) {
stepIncreaseCount++;
decrementStep = DEFAULT_CI_STEPS[j] * Math.pow(2, stepIncreaseCount);
} else {
if (stepIncreaseCount > 1) {
incrementStep = DEFAULT_CI_STEPS[j] / Math.pow(2, stepIncreaseCount);
stepIncreaseCount--;
}
}
if (clientTaskStatusSupport.isInterrupted()) {
throw UserCancelException.CANCEL_GENERIC;
}
lastError = error;
iterationCount++;
clientTaskStatusSupport.setProgress((int) (((iterationCount + MAX_ITERATION) * 1.0 / MAX_ITERATION) * 0.5 * 100));
}
resultData[j] = profileData;
// finish evaluation of a parameter
clientTaskStatusSupport.setProgress(100);
}
// this message is specifically set for batchrun, the message will stay in the status panel. It doesn't affect single run,which disappears quickly that user won't notice.
clientTaskStatusSupport.setMessage("Evaluating confidence intervals ...");
// System.out.println("total time used:" + (endTime - startTime));
return resultData;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class FRAPParamTest method runProfileLikelihood.
public void runProfileLikelihood() {
String errorMsg = checkFrapStudyValidity();
if (!errorMsg.equals("")) {
System.out.println("Application terminated due to " + errorMsg);
System.exit(1);
} else {
try {
ClientTaskStatusSupport ctss = new ClientTaskStatusSupport() {
public void setProgress(int progress) {
System.out.println(progress);
}
public void setMessage(String message) {
System.out.println(message);
}
public boolean isInterrupted() {
// TODO Auto-generated method stub
return false;
}
public int getProgress() {
// TODO Auto-generated method stub
return 0;
}
public void addProgressDialogListener(ProgressDialogListener progressDialogListener) {
throw new RuntimeException("not yet implemented");
}
};
// get startign index
if (frapStudy.getStartingIndexForRecovery() == null) {
int index = FRAPDataAnalysis.calculateRecoveryIndex(frapStudy.getFrapData());
frapStudy.setStartingIndexForRecovery(index);
}
// get dependent rois
if (frapStudy.getFrapData().getRois().length < 4) {
frapStudy.refreshDependentROIs();
}
// get selected ROIs
if (frapStudy.getSelectedROIsForErrorCalculation() == null) {
boolean[] selectedROIs = new boolean[FRAPData.VFRAP_ROI_ENUM.values().length];
int counter = 0;
for (FRAPData.VFRAP_ROI_ENUM roiEnum : FRAPData.VFRAP_ROI_ENUM.values()) {
if (roiEnum.name().equals(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name()) || roiEnum.name().equals(FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name())) {
counter++;
continue;
}
if (frapStudy.getFrapData().getRoi(roiEnum.name()).getNonzeroPixelsCount() > 0) {
selectedROIs[counter] = true;
counter++;
}
}
frapStudy.setSelectedROIsForErrorCalculation(selectedROIs);
}
// get frap opt data
if (frapStudy.getFrapOptData() == null) {
if (!FRAPWorkspace.areExternalDataOK(getLocalWorkspace(), frapStudy.getFrapDataExternalDataInfo(), frapStudy.getRoiExternalDataInfo())) {
// if external files are missing/currupt or ROIs are changed, create keys and save them
frapStudy.setFrapDataExternalDataInfo(FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.IMAGE_EXTDATA_NAME));
frapStudy.setRoiExternalDataInfo(FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.ROI_EXTDATA_NAME));
frapStudy.saveROIsAsExternalData(localWorkspace, frapStudy.getRoiExternalDataInfo().getExternalDataIdentifier(), frapStudy.getStartingIndexForRecovery());
frapStudy.saveImageDatasetAsExternalData(localWorkspace, frapStudy.getFrapDataExternalDataInfo().getExternalDataIdentifier(), frapStudy.getStartingIndexForRecovery());
}
// run ref sim
frapStudy.setFrapOptData(new FRAPOptData(frapStudy, FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF, localWorkspace, ctss));
}
FRAPOptData optData = frapStudy.getFrapOptData();
// create frapModels
if (frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] == null) {
frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] = new FRAPModel(FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT], null, null, null);
if (frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() == null) {
frapStudy.getFrapOptData().setNumEstimatedParams(FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF);
Parameter[] initialParams = FRAPModel.getInitialParameters(frapStudy.getFrapData(), FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT], frapStudy.getStartingIndexForRecovery());
Parameter[] bestParameters = frapStudy.getFrapOptData().getBestParamters(initialParams, frapStudy.getSelectedROIsForErrorCalculation());
frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].setModelParameters(bestParameters);
}
}
if (frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] == null) {
frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] = new FRAPModel(FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS], null, null, null);
if (frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() == null) {
frapStudy.getFrapOptData().setNumEstimatedParams(FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF);
Parameter[] initialParams = FRAPModel.getInitialParameters(frapStudy.getFrapData(), FRAPModel.MODEL_TYPE_ARRAY[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS], frapStudy.getStartingIndexForRecovery());
Parameter[] bestParameters = frapStudy.getFrapOptData().getBestParamters(initialParams, frapStudy.getSelectedROIsForErrorCalculation());
frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].setModelParameters(bestParameters);
}
}
// try diffusion with one diffusing component model
System.out.println("Evaluating parameters in diffusion with one diffusing compoent model...");
Parameter[] bestParameters = frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters();
ProfileData[] profileData = optData.evaluateParameters(bestParameters, ctss);
// output profile likelihood
File outputDir_oneComponent = new File(getLocalWorkspace().getDefaultWorkspaceDirectory() + SUB_DIRECTORY + "OneComponent_SAVED_AT_" + BeanUtils.generateDateTimeString() + System.getProperty("file.separator"));
if (!outputDir_oneComponent.exists()) {
outputDir_oneComponent.mkdirs();
}
for (int i = 0; i < profileData.length; i++) {
ProfileDataElement profileDataElement = profileData[i].getProfileDataElements().get(0);
outputProfileLikelihood(profileData[i].getProfileDataElements(), profileDataElement.getParamName(), outputDir_oneComponent);
}
// try diffusion with two diffusing components model
System.out.println("Evaluating parameters in diffusion with two diffusing compoents model...");
bestParameters = frapStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters();
profileData = optData.evaluateParameters(bestParameters, ctss);
// output profile likelihood
File outputDir_twoComponents = new File(getLocalWorkspace().getDefaultWorkspaceDirectory() + SUB_DIRECTORY + "TwoComponents_SAVED_AT_" + BeanUtils.generateDateTimeString() + System.getProperty("file.separator"));
if (!outputDir_twoComponents.exists()) {
outputDir_twoComponents.mkdirs();
}
for (int i = 0; i < profileData.length; i++) {
ProfileDataElement profileDataElement = profileData[i].getProfileDataElements().get(0);
outputProfileLikelihood(profileData[i].getProfileDataElements(), profileDataElement.getParamName(), outputDir_twoComponents);
}
} catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
}
}
Aggregations