use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class OptXmlWriter method getParameterDescriptionXML.
public static Element getParameterDescriptionXML(OptimizationSpec optimizationSpec) {
Element parameterDescriptionElement = new Element(OptXmlTags.ParameterDescription_Tag);
Parameter[] parameters = optimizationSpec.getParameters();
for (int i = 0; i < parameters.length; i++) {
Element parameterElement = new Element(OptXmlTags.Parameter_Tag);
parameterElement.setAttribute(OptXmlTags.ParameterName_Attr, parameters[i].getName());
parameterElement.setAttribute(OptXmlTags.ParameterLow_Attr, Double.toString(parameters[i].getLowerBound()));
parameterElement.setAttribute(OptXmlTags.ParameterHigh_Attr, Double.toString(parameters[i].getUpperBound()));
parameterElement.setAttribute(OptXmlTags.ParameterInit_Attr, Double.toString(parameters[i].getInitialGuess()));
parameterElement.setAttribute(OptXmlTags.ParameterScale_Attr, Double.toString(parameters[i].getScale()));
parameterDescriptionElement.addContent(parameterElement);
}
return parameterDescriptionElement;
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class FRAPOptData method getBestParamters.
// The best parameters will return a whole set for one diffusing component or two diffusing components (including the fixed parameter)
public Parameter[] getBestParamters(Parameter[] inParams, boolean[] errorOfInterest, Parameter fixedParam, boolean bApplyMeasurementError) throws Exception {
// refresh number of pixels in each roi
if (measurementErrors == null) {
measurementErrors = FRAPOptimizationUtils.refreshNormalizedMeasurementError(getExpFrapStudy());
}
int numFixedParam = (fixedParam == null) ? 0 : 1;
// return to the caller, parameter should be a whole set including the fixed parameter
Parameter[] outputParams = new Parameter[inParams.length + numFixedParam];
// send to optimizer
String[] outParaNames = new String[inParams.length];
// send to optimizer
double[] outParaVals = new double[inParams.length];
// set fixed parameter
setFixedParameter(fixedParam);
// set if apply measurement error
setIsApplyMeasurementError(bApplyMeasurementError);
// optimization
double lestError = FRAPOptimizationUtils.estimate(this, inParams, outParaNames, outParaVals, errorOfInterest);
setLeastError(lestError);
// get results as Parameters (take fixed parameter into account)
for (int i = 0; i < outParaNames.length; i++) {
if (outParaNames[i].equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_PRIMARY_DIFF_RATE])) {
double primaryDiffRate = outParaVals[i];
if (primaryDiffRate > FRAPModel.REF_DIFFUSION_RATE_PARAM.getUpperBound()) {
primaryDiffRate = FRAPModel.REF_DIFFUSION_RATE_PARAM.getUpperBound();
}
if (primaryDiffRate < FRAPModel.REF_DIFFUSION_RATE_PARAM.getLowerBound()) {
primaryDiffRate = FRAPModel.REF_DIFFUSION_RATE_PARAM.getLowerBound();
}
outputParams[FRAPModel.INDEX_PRIMARY_DIFF_RATE] = new Parameter(outParaNames[i], FRAPModel.REF_DIFFUSION_RATE_PARAM.getLowerBound(), FRAPModel.REF_DIFFUSION_RATE_PARAM.getUpperBound(), 1.0, primaryDiffRate);
} else if (outParaNames[i].equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_PRIMARY_FRACTION])) {
double primaryFraction = outParaVals[i];
if (primaryFraction > FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound()) {
primaryFraction = FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound();
}
if (primaryFraction < FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound()) {
primaryFraction = FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound();
}
outputParams[FRAPModel.INDEX_PRIMARY_FRACTION] = new Parameter(outParaNames[i], FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound(), 1.0, primaryFraction);
} else if (outParaNames[i].equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_BLEACH_MONITOR_RATE])) {
double bwmRate = outParaVals[i];
if (bwmRate > FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound()) {
bwmRate = FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound();
}
if (bwmRate < FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound()) {
bwmRate = FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound();
}
outputParams[FRAPModel.INDEX_BLEACH_MONITOR_RATE] = new Parameter(outParaNames[i], FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound(), 1.0, bwmRate);
} else if (outParaNames[i].equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_SECONDARY_DIFF_RATE])) {
double secDiffRate = outParaVals[i];
if (secDiffRate > FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getUpperBound()) {
secDiffRate = FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getUpperBound();
}
if (secDiffRate < FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getLowerBound()) {
secDiffRate = FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getLowerBound();
}
outputParams[FRAPModel.INDEX_SECONDARY_DIFF_RATE] = new Parameter(outParaNames[i], FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getLowerBound(), FRAPModel.REF_SECOND_DIFFUSION_RATE_PARAM.getUpperBound(), 1.0, secDiffRate);
} else if (outParaNames[i].equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_SECONDARY_FRACTION])) {
double secFraction = outParaVals[i];
if (secFraction > FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getUpperBound()) {
secFraction = FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getUpperBound();
}
if (secFraction < FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getLowerBound()) {
secFraction = FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getLowerBound();
}
outputParams[FRAPModel.INDEX_SECONDARY_FRACTION] = new Parameter(outParaNames[i], FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getLowerBound(), FRAPModel.REF_SECOND_MOBILE_FRACTION_PARAM.getUpperBound(), 1.0, secFraction);
}
}
// add fixed parameter to the best parameter output to make a whole set
if (fixedParam != null) {
if (fixedParam.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_PRIMARY_DIFF_RATE])) {
outputParams[FRAPModel.INDEX_PRIMARY_DIFF_RATE] = fixedParam;
} else if (fixedParam.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_PRIMARY_FRACTION])) {
outputParams[FRAPModel.INDEX_PRIMARY_FRACTION] = fixedParam;
} else if (fixedParam.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_BLEACH_MONITOR_RATE])) {
outputParams[FRAPModel.INDEX_BLEACH_MONITOR_RATE] = fixedParam;
} else if (fixedParam.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_SECONDARY_DIFF_RATE])) {
outputParams[FRAPModel.INDEX_SECONDARY_DIFF_RATE] = fixedParam;
} else if (fixedParam.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_SECONDARY_FRACTION])) {
outputParams[FRAPModel.INDEX_SECONDARY_FRACTION] = fixedParam;
}
}
// }
return outputParams;
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class FRAPOptimizationUtils method estimate.
// estimate best parameters and return the least error
public static double estimate(FRAPOptData argOptData, Parameter[] inParams, String[] outParaNames, double[] outParaVals, boolean[] eoi) throws Exception {
/*PowellSolver solver = new PowellSolver();
LookupTableObjectiveFunction func = new LookupTableObjectiveFunction(argOptData);
//best point found. we have only one dimension here.
double[] p = new double[1];
p[0] = iniDiffGuess;
//current direction set
double[][] xi = new double[1][1];
for (int i=0;i<1;i++)
{
for (int j=0;j<1;j++)
{
xi[i][j]=(i == j ? 1.0 : 0.0);
}
}
//run powell with initial guess, initial direction set and objective function
double minError = solver.powell(1, p, xi, FTOL, func);
parameters[FRAPOptData.idxOptDiffRate] = p[0];
parameters[FRAPOptData.idxMinError] = minError;*/
// long startTime =System.currentTimeMillis();
// create optimization solver
PowellOptimizationSolver optSolver = new PowellOptimizationSolver();
// create optimization spec
OptimizationSpec optSpec = new OptimizationSpec();
// add opt function
DefaultScalarFunction scalarFunc = new LookupTableObjectiveFunction(argOptData, eoi);
optSpec.setObjectiveFunction(new ImplicitObjectiveFunction(scalarFunc));
// create solver spec
OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, FRAPOptimizationUtils.FTOL);
// create solver call back
OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
// create optimization result set
OptimizationResultSet optResultSet = null;
for (int i = 0; i < inParams.length; i++) {
// add parameters
optSpec.addParameter(inParams[i]);
}
optResultSet = optSolver.solve(optSpec, optSolverSpec, optSolverCallbacks);
OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
// if the parameters are 5, we have to go over again to see if we get the best answer.
if (// 5 parameters
inParams.length == 5) {
OptimizationSpec optSpec2 = new OptimizationSpec();
optSpec2.setObjectiveFunction(new ImplicitObjectiveFunction(scalarFunc));
Parameter[] inParamsFromResult = generateInParamSet(inParams, optSolverResultSet.getBestEstimates());
for (int i = 0; i < inParamsFromResult.length; i++) {
// add parameters
optSpec2.addParameter(inParamsFromResult[i]);
}
OptimizationResultSet tempOptResultSet = optSolver.solve(optSpec2, optSolverSpec, optSolverCallbacks);
OptSolverResultSet tempOptSolverResultSet = tempOptResultSet.getOptSolverResultSet();
if (optSolverResultSet.getLeastObjectiveFunctionValue() > tempOptSolverResultSet.getLeastObjectiveFunctionValue()) {
optSolverResultSet = tempOptSolverResultSet;
}
}
// System.out.println("obj function value:"+optResultSet.getObjectiveFunctionValue());
// System.out.println("");
// copy results to output parameters
String[] names = optSolverResultSet.getParameterNames();
double[] values = optSolverResultSet.getBestEstimates();
for (int i = 0; i < names.length; i++) {
outParaNames[i] = names[i];
outParaVals[i] = values[i];
}
// System.out.println("total: " + ( endTime - startTime) );
return optSolverResultSet.getLeastObjectiveFunctionValue();
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class CurveFitting method fitRecovery_reacKoffRateOnly.
/*
* @para: time, time points since the first post bleach.
* @para: normalized_data, first dimension index 0:average intensities under cell region, first dimension index 1: average intensities under bleached region
* @para: inputparam, index 0:cellROIAvg at time 0(first post bleach). index 1:bleachedROIAvg at time 0(first post bleach).
* @para: outputParam, results for 2 or 3 parameters, depending on if there is any fixed parameter.
*/
public static double fitRecovery_reacKoffRateOnly(double[] time, double[][] normalized_data, double[] inputparam, double[] outputParam, Parameter fixedParameter, Weights weights) throws ExpressionException, OptimizationException, IOException {
if (normalized_data != null && normalized_data.length > 0) {
for (int i = 0; i < normalized_data.length; i++) {
if (normalized_data[i] != null && time.length != normalized_data[i].length) {
throw new RuntimeException("Fluorecence and time arrays must be the same length");
}
}
}
// normaliztion for time by subtracting the starting time: time[0]
double[] normalized_time = new double[time.length];
for (int i = 0; i < time.length; i++) {
normalized_time[i] = time[i] - time[0];
}
// initiate variables
OptimizationResultSet optResultSet = null;
Expression bwmRateExp = new Expression(FRAPOptFunctions.FUNC_CELL_INTENSITY);
Expression koffRateExp = new Expression(FRAPOptFunctions.FUNC_RECOVERY_BLEACH_REACTION_DOMINANT);
// get parameters to be estimated(use all of the 3 if there is no fixed parameter. Or 2 out of the 3 if there is a fixed parameter)
Parameter bwmParam = new Parameter(FRAPOptFunctions.SYMBOL_BWM_RATE, FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getScale(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getInitialGuess());
Parameter koffParam = new Parameter(FRAPOptFunctions.SYMBOL_KOFF, FRAPModel.REF_REACTION_OFF_RATE.getLowerBound(), FRAPModel.REF_REACTION_OFF_RATE.getUpperBound(), FRAPModel.REF_REACTION_OFF_RATE.getScale(), FRAPModel.REF_REACTION_OFF_RATE.getInitialGuess());
Parameter fittingParamA = new Parameter(FRAPOptFunctions.SYMBOL_A, /*binding site concentration is reused to store fitting parameter A, but the name can not be reused*/
FRAPModel.REF_BS_CONCENTRATION_OR_A.getLowerBound(), FRAPModel.REF_BS_CONCENTRATION_OR_A.getUpperBound(), FRAPModel.REF_BS_CONCENTRATION_OR_A.getScale(), FRAPModel.REF_BS_CONCENTRATION_OR_A.getInitialGuess());
// get column names for reference data to be constructed
String[] columnNames = new String[] { ReservedVariable.TIME.getName(), "cellIntensityAvg", "bleachIntensityAvg" };
if (fixedParameter == null) {
// get fitting parameter array
Parameter[] parameters = new Parameter[] { bwmParam, fittingParamA, koffParam };
// get expression pairs
bwmRateExp = bwmRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inicell), new Expression(inputparam[0]));
bindExpressionToParametersAndTime(bwmRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair bwmRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(bwmRateExp, 1);
koffRateExp = koffRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inibleached), new Expression(inputparam[1]));
bindExpressionToParametersAndTime(koffRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair koffRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(koffRateExp, 2);
ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[] { bwmRateExpDataPair, koffRateExpDataPair };
// solve
optResultSet = solve(expDataPairs, parameters, normalized_time, normalized_data, columnNames, weights);
} else {
if (fixedParameter != null && fixedParameter.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_BLEACH_MONITOR_RATE])) {
// get fitting parameter array
Parameter[] parameters = new Parameter[] { fittingParamA, koffParam };
// get expression pairs
bwmRateExp = bwmRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inicell), new Expression(inputparam[0]));
bwmRateExp = bwmRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_BWM_RATE), new Expression(fixedParameter.getInitialGuess()));
bindExpressionToParametersAndTime(bwmRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair bwmRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(bwmRateExp, 1);
koffRateExp = koffRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inibleached), new Expression(inputparam[1]));
koffRateExp = koffRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_BWM_RATE), new Expression(fixedParameter.getInitialGuess()));
bindExpressionToParametersAndTime(koffRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair koffRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(koffRateExp, 2);
ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[] { bwmRateExpDataPair, koffRateExpDataPair };
// solve
optResultSet = solve(expDataPairs, parameters, normalized_time, normalized_data, columnNames, weights);
} else if (fixedParameter != null && fixedParameter.getName().equals(FRAPModel.MODEL_PARAMETER_NAMES[FRAPModel.INDEX_OFF_RATE])) {
// get fitting parameter array
Parameter[] parameters = new Parameter[] { bwmParam, fittingParamA };
// get expression pairs
bwmRateExp = bwmRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inicell), new Expression(inputparam[0]));
bindExpressionToParametersAndTime(bwmRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair bwmRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(bwmRateExp, 1);
koffRateExp = koffRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_I_inibleached), new Expression(inputparam[1]));
koffRateExp = koffRateExp.getSubstitutedExpression(new Expression(FRAPOptFunctions.SYMBOL_KOFF), new Expression(fixedParameter.getInitialGuess()));
bindExpressionToParametersAndTime(koffRateExp, parameters);
ExplicitFitObjectiveFunction.ExpressionDataPair koffRateExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(koffRateExp, 2);
ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[] { bwmRateExpDataPair, koffRateExpDataPair };
// solve
optResultSet = solve(expDataPairs, parameters, normalized_time, normalized_data, columnNames, weights);
}
}
// get output parameter values
OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
String[] paramNames = optSolverResultSet.getParameterNames();
double[] paramValues = optSolverResultSet.getBestEstimates();
// copy into "output" buffer from parameter values.
for (int i = 0; i < paramValues.length; i++) {
outputParam[i] = paramValues[i];
}
// for debug purpose
for (int i = 0; i < paramNames.length; i++) {
System.out.println("finally: " + paramNames[i] + " = " + paramValues[i]);
}
// investigate the return information
processReturnCode(OptimizationSolverSpec.SOLVERTYPE_POWELL, optSolverResultSet);
// return objective function value
return optSolverResultSet.getLeastObjectiveFunctionValue();
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class CurveFitting method fitBleachWhileMonitoring.
/*
* @para: time, time points since the first post bleach
* @para: flour, average intensities under bleached region according to time points since the first post bleach
* @para: parameterValues, the array which will pass results back
*/
public static Expression fitBleachWhileMonitoring(double[] time, double[] normalized_fluor, double[] outputParam, Weights weights) throws ExpressionException, OptimizationException, IOException {
if (time.length != normalized_fluor.length) {
throw new RuntimeException("Fluorecence and time arrays must be the same length");
}
// normaliztion for time by subtracting the starting time: time[0]
double[] normalized_time = new double[time.length];
for (int i = 0; i < time.length; i++) {
normalized_time[i] = time[i] - time[0];
}
Expression modelExp = null;
OptimizationResultSet optResultSet = null;
String[] paramNames = null;
double[] paramValues = null;
double cellFirstPostBleach = normalized_fluor[0];
modelExp = new Expression(FRAPOptFunctions.FUNC_CELL_INTENSITY);
modelExp.substituteInPlace(new Expression(FRAPOptFunctions.SYMBOL_I_inicell), new Expression(cellFirstPostBleach));
// initialize starting guess, arguments in Parameter are name, Lower Bound, Upper Bound, Scale, Initial Guess
Parameter[] parameters = new Parameter[] { new Parameter(FRAPOptFunctions.SYMBOL_BWM_RATE, FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getScale(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getInitialGuess()) };
bindExpressionToParametersAndTime(modelExp, parameters);
// estimate blech while monitoring rate by minimizing the error between funtion values and reference data
if (weights == null) {
optResultSet = solve(modelExp, parameters, normalized_time, normalized_fluor);
} else {
optResultSet = solve(modelExp, parameters, normalized_time, normalized_fluor, weights);
}
OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
paramNames = optSolverResultSet.getParameterNames();
paramValues = optSolverResultSet.getBestEstimates();
// copy into "output" buffer for parameter values.
for (int i = 0; i < paramValues.length; i++) {
outputParam[i] = paramValues[i];
}
System.out.println(optSolverResultSet.getOptimizationStatus().toString());
for (int i = 0; i < paramNames.length; i++) {
System.out.println("finally: " + paramNames[i] + " = " + paramValues[i]);
}
// investigate the return information
processReturnCode(OptimizationSolverSpec.SOLVERTYPE_POWELL, optSolverResultSet);
//
// construct final equation
//
Expression fit = new Expression(modelExp);
System.out.println("bleach while monitoring fit before subsituting parameters:" + fit.infix());
//
for (int i = 0; i < paramValues.length; i++) {
fit.substituteInPlace(new Expression(paramNames[i]), new Expression(paramValues[i]));
}
//
// undo time shift
//
fit.substituteInPlace(new Expression(ReservedVariable.TIME.getName()), new Expression(ReservedVariable.TIME.getName() + "-" + time[0]));
//
// undo fluorescence normalization
//
System.out.println("bleach while monitoring fit equation after unnorm:" + fit.infix());
return fit;
}
Aggregations