use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class FRAPOptFunctions method evaluateParameters.
// profileData array contains only two profile distribution, one for bleachWhileMonitoringRate and another for reaction off rate
public ProfileData[] evaluateParameters(Parameter[] currentParams, ClientTaskStatusSupport clientTaskStatusSupport) throws Exception {
int totalParamLen = currentParams.length;
int resultDataCounter = 0;
ProfileData[] resultData = new ProfileData[NUM_PARAM_ESTIMATED];
FRAPStudy frapStudy = getExpFrapStudy();
for (int j = 0; j < totalParamLen; j++) {
// only bleach while monitoring rate and reaction off rate need to evaluated
if (currentParams[j] != null && (currentParams[j].getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_BLEACH_MONITOR_RATE]) || currentParams[j].getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_OFF_RATE]))) {
ProfileData profileData = new ProfileData();
// add the fixed parameter to profileData, output exp data and opt results
Parameter[] newBestParameters = getBestParamters(frapStudy.getFrapData(), null, true);
double iniError = getOffRateResults().getObjectiveFunctionValue();
// fixed parameter(make sure parameter shall not be smaller than epsilon)
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) {
clientTaskStatusSupport.setMessage("<html>Evaluating confidence intervals of \'" + fixedParam.getName() + "\' <br> of finding reaction off rate 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);
// increase
int iterationCount = 1;
double paramLogVal = Math.log10(fixedParam.getInitialGuess());
double lastError = iniError;
boolean isBoundReached = false;
double incrementStep = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter];
int stepIncreaseCount = 0;
while (true) {
if (// if exceeds the maximum iterations, break;
iterationCount > FRAPOptData.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
Parameter[] newParameters = getBestParamters(frapStudy.getFrapData(), increasedParam, true);
double error = getOffRateResults().getObjectiveFunctionValue();
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) < FRAPOptData.MIN_LIKELIHOOD_CHANGE) {
stepIncreaseCount++;
incrementStep = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter] * Math.pow(2, stepIncreaseCount);
} else {
if (stepIncreaseCount > 1) {
incrementStep = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter] / Math.pow(2, stepIncreaseCount);
stepIncreaseCount--;
}
}
if (clientTaskStatusSupport.isInterrupted()) {
throw UserCancelException.CANCEL_GENERIC;
}
lastError = error;
iterationCount++;
clientTaskStatusSupport.setProgress((int) ((iterationCount * 1.0 / FRAPOptData.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 = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter];
stepIncreaseCount = 0;
while (true) {
if (// if exceeds the maximum iterations, break;
iterationCount > FRAPOptData.MAX_ITERATION) {
break;
}
if (isBoundReached) {
break;
}
paramLogVal = paramLogVal - decrementStep;
double paramVal = Math.pow(10, paramLogVal);
// System.out.println("paramVal:" + paramVal);
if (paramVal < (fixedParam.getLowerBound() + FRAPOptimizationUtils.epsilon)) {
paramVal = fixedParam.getLowerBound();
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
Parameter[] newParameters = getBestParamters(frapStudy.getFrapData(), decreasedParam, true);
double error = getOffRateResults().getObjectiveFunctionValue();
pde = new ProfileDataElement(decreasedParam.getName(), paramLogVal, error, newParameters);
profileData.addElement(0, pde);
if (error > (iniError + 10)) {
break;
}
if (Math.abs((error - lastError) / lastError) < FRAPOptData.MIN_LIKELIHOOD_CHANGE) {
stepIncreaseCount++;
decrementStep = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter] * Math.pow(2, stepIncreaseCount);
} else {
if (stepIncreaseCount > 1) {
incrementStep = FRAPOptData.DEFAULT_CI_STEPS_OFF_RATE[resultDataCounter] / Math.pow(2, stepIncreaseCount);
stepIncreaseCount--;
}
}
if (clientTaskStatusSupport.isInterrupted()) {
throw UserCancelException.CANCEL_GENERIC;
}
lastError = error;
iterationCount++;
clientTaskStatusSupport.setProgress((int) (((iterationCount + FRAPOptData.MAX_ITERATION) * 1.0 / FRAPOptData.MAX_ITERATION) * 0.5 * 100));
}
resultData[resultDataCounter++] = profileData;
// finish evaluation of a parameter
clientTaskStatusSupport.setProgress(100);
} else {
continue;
}
}
// 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 ...");
return resultData;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class MicroscopyXmlReader method getProfileDataList.
// get a list of profile data for either diffusion with one diffusing component/or with two diffusing components
private ProfileData[] getProfileDataList(Element profileDataListElement) {
ProfileData[] profileDataArray = null;
// read the list of profile data
if (profileDataListElement != null) {
@SuppressWarnings("unchecked") List<Element> profileDataList = profileDataListElement.getChildren(MicroscopyXMLTags.ProfileDataTag);
profileDataArray = new ProfileData[profileDataList.size()];
int profileCounter = 0;
for (// loop through each profile data
Element profileElement : // loop through each profile data
profileDataList) {
profileDataArray[profileCounter] = getProfileData(profileElement);
profileCounter++;
}
}
return profileDataArray;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class MicroscopyXmlReader method getProfileData.
private ProfileData getProfileData(Element profileDataElement) {
ProfileData profileData = null;
if (profileDataElement != null) {
profileData = new ProfileData();
@SuppressWarnings("unchecked") List<Element> profileDataElementList = profileDataElement.getChildren(MicroscopyXMLTags.ProfieDataElementTag);
for (// loop through each profile data element
int i = 0; // loop through each profile data element
i < profileDataElementList.size(); // loop through each profile data element
i++) {
profileData.addElement(getProfileDataElement(profileDataElementList.get(i)));
}
}
return profileData;
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class FRAPStudyPanel method showEstimateParamWizard.
private void showEstimateParamWizard() {
// check if frapOpt data is null? if yes, run ref simulation.
// check if parameters of each selected models are there, if not, run analytic solution, get best parameters and store parameters
AsynchClientTask saveTask = new AsynchClientTask("Preparing for parameter estimation ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = getFrapWorkspace().getWorkingFrapStudy();
if (fStudy.hasDiffusionOnlyModel()) {
// create optdata
if (fStudy.getFrapOptData() == null) {
if (fStudy.getStoredRefData() != null) {
getFrapWorkspace().getWorkingFrapStudy().setFrapOptData(new FRAPOptData(fStudy, FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF, getLocalWorkspace(), fStudy.getStoredRefData()));
} else {
// check external data info
if (!FRAPWorkspace.areExternalDataOK(getLocalWorkspace(), 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());
}
// run ref sim
fStudy.setFrapOptData(new FRAPOptData(fStudy, FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF, localWorkspace, this.getClientTaskStatusSupport()));
fStudy.setSaveNeeded(true);
}
}
}
if (fStudy.hasReactionOnlyOffRateModel()) {
if (fStudy.getFrapOptFunc() == null) {
fStudy.setFrapOptFunc(new FRAPOptFunctions(fStudy));
}
}
}
};
AsynchClientTask runOptTask = new AsynchClientTask("Running optimization ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = getFrapWorkspace().getWorkingFrapStudy();
if (fStudy.getSelectedModels() != null && fStudy.getSelectedModels().size() > 0) {
ArrayList<Integer> models = fStudy.getSelectedModels();
for (int i = 0; i < models.size(); i++) {
if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() == null) {
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());
Parameter[] bestParameters = fStudy.getFrapOptData().getBestParamters(initialParams, fStudy.getSelectedROIsForErrorCalculation());
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].setModelParameters(bestParameters);
fStudy.setSaveNeeded(true);
}
} else if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() == null) {
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());
Parameter[] bestParameters = fStudy.getFrapOptData().getBestParamters(initialParams, fStudy.getSelectedROIsForErrorCalculation());
fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].setModelParameters(bestParameters);
fStudy.setSaveNeeded(true);
}
} else if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_REACTION_OFF_RATE) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters() == null) {
Parameter[] bestParameters = fStudy.getFrapOptFunc().getBestParamters(fStudy.getFrapData(), null, true);
fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].setModelParameters(bestParameters);
fStudy.setSaveNeeded(true);
}
}
}
}
}
};
AsynchClientTask evaulateCITask = new AsynchClientTask("Evaluating confidence intervals for model parameters ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FRAPStudy fStudy = getFrapWorkspace().getWorkingFrapStudy();
if (fStudy.getSelectedModels() != null && fStudy.getSelectedModels().size() > 0) {
ArrayList<Integer> models = fStudy.getSelectedModels();
for (int i = 0; i < models.size(); i++) {
if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() != null && fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters().length > 0) {
Parameter[] currentParams = fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters();
fStudy.getFrapOptData().setNumEstimatedParams(currentParams.length);
ProfileData[] profileData = null;
if (fStudy.getProfileData_oneDiffComponent() != null) {
profileData = fStudy.getProfileData_oneDiffComponent();
} else {
profileData = fStudy.getFrapOptData().evaluateParameters(currentParams, this.getClientTaskStatusSupport());
fStudy.setProfileData_oneDiffComponent(profileData);
fStudy.setSaveNeeded(true);
}
}
} else if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() != null && fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters().length > 0) {
Parameter[] currentParams = fStudy.getModels()[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters();
fStudy.getFrapOptData().setNumEstimatedParams(currentParams.length);
ProfileData[] profileData = null;
if (fStudy.getProfileData_twoDiffComponents() != null) {
profileData = fStudy.getProfileData_twoDiffComponents();
} else {
profileData = fStudy.getFrapOptData().evaluateParameters(currentParams, this.getClientTaskStatusSupport());
fStudy.setProfileData_twoDiffComponents(profileData);
fStudy.setSaveNeeded(true);
}
}
} else if ((models.get(i)).intValue() == FRAPModel.IDX_MODEL_REACTION_OFF_RATE) {
if (fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters() != null && fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters().length > 0) {
Parameter[] currentParams = fStudy.getModels()[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters();
ProfileData[] profileData = null;
if (fStudy.getProfileData_reactionOffRate() != null) {
profileData = fStudy.getProfileData_reactionOffRate();
} else {
profileData = fStudy.getFrapOptFunc().evaluateParameters(currentParams, this.getClientTaskStatusSupport());
fStudy.setProfileData_reactionOffRate(profileData);
fStudy.setSaveNeeded(true);
}
}
}
}
}
hashTable.put(FRAPSTUDY_KEY, fStudy);
}
};
AsynchClientTask showDialogTask = new AsynchClientTask("Showing estimation results ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
// get frap models before the wizard
FRAPStudy fStudy = getFrapWorkspace().getWorkingFrapStudy();
FRAPModel[] oldFrapModels = new FRAPModel[FRAPModel.NUM_MODEL_TYPES];
for (int i = 0; i < fStudy.getModels().length; i++) {
if (fStudy.getModels()[i] != null) {
FRAPModel model = fStudy.getModels()[i];
oldFrapModels[i] = new FRAPModel(new String(model.getModelIdentifer()), model.duplicateParameters(), null, model.duplicateTimePoints());
}
}
// get best model index before the wizard
Integer oldBestModelIndex = null;
if (fStudy.getBestModelIndex() != null) {
oldBestModelIndex = new Integer(fStudy.getBestModelIndex().intValue());
// put old best model index into hashtable
hashTable.put(BEST_MODEL_KEY, oldBestModelIndex);
}
// put old frapmodels in hashtable
hashTable.put(FRAPMODELS_KEY, oldFrapModels);
// show wizard
final Wizard estParamWizard = getEstimateParametersWizard();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
estParamWizard.showModalDialog(new Dimension(1000, 750));
}
});
// put wizard in hashtable in order to check return code in next task
hashTable.put(EST_PARAM_WIZARD_KEY, estParamWizard);
}
};
AsynchClientTask updateSaveStatusTask = new AsynchClientTask("...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
Wizard estParamWizard = (Wizard) hashTable.get(EST_PARAM_WIZARD_KEY);
// get old values from hashtable
FRAPModel[] oldFrapModels = (FRAPModel[]) hashTable.get(FRAPMODELS_KEY);
Integer oldBestModelIndex = (Integer) hashTable.get(BEST_MODEL_KEY);
// get new values
Integer newBestModelIndex = getFrapWorkspace().getWorkingFrapStudy().getBestModelIndex();
if (estParamWizard.getReturnCode() == Wizard.FINISH_RETURN_CODE) {
if (!getFrapWorkspace().getWorkingFrapStudy().areFRAPModelsEqual(oldFrapModels) || (oldBestModelIndex == null && newBestModelIndex != null) || (oldBestModelIndex != null && newBestModelIndex == null) || (oldBestModelIndex != null && newBestModelIndex != null && oldBestModelIndex.intValue() != newBestModelIndex.intValue())) {
getFrapWorkspace().getWorkingFrapStudy().setSaveNeeded(true);
}
}
}
};
// dispatch
ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), new AsynchClientTask[] { saveTask, runOptTask, evaulateCITask, showDialogTask, updateSaveStatusTask }, true, true, true, null, true);
}
use of org.vcell.optimization.ProfileData in project vcell by virtualcell.
the class FRAPDiffOneParamPanel method showParameterEvaluation.
public void showParameterEvaluation() {
AsynchClientTask evaluateTask = new AsynchClientTask("Prepare to evaluate parameters ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
String errorStr = checkParameters();
if (!errorStr.equals("")) {
throw new IllegalArgumentException(errorStr);
}
}
};
AsynchClientTask showResultTask = new AsynchClientTask("Showing profile likelihood and confidence intervals ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
ProfileData[] profileData = frapOptData.getExpFrapStudy().getProfileData_oneDiffComponent();
if (profileData != null && profileData.length > 0) {
// put plotpanes of different parameters' profile likelihoods into a base panel
JPanel basePanel = new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
for (int i = 0; i < profileData.length; i++) {
ConfidenceIntervalPlotPanel plotPanel = new ConfidenceIntervalPlotPanel();
plotPanel.setProfileSummaryData(FRAPOptimizationUtils.getSummaryFromProfileData(profileData[i]));
plotPanel.setBorder(new EtchedBorder());
String paramName = "";
if (profileData[i].getProfileDataElements().size() > 0) {
paramName = profileData[i].getProfileDataElements().get(0).getParamName();
}
ProfileDataPanel profileDataPanel = new ProfileDataPanel(plotPanel, paramName);
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(FRAPDiffOneParamPanel.this, scrollPane, "Profile Likelihood of Parameters");
}
}
};
// dispatch
ClientTaskDispatcher.dispatch(FRAPDiffOneParamPanel.this, new Hashtable<String, Object>(), new AsynchClientTask[] { evaluateTask, showResultTask }, false, true, null, true);
}
Aggregations