use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class MicroscopyXmlproducer method getXML.
private static Element getXML(ProfileDataElement profileDataElement) {
Element profileDataElementNode = new Element(MicroscopyXMLTags.ProfieDataElementTag);
profileDataElementNode.setAttribute(MicroscopyXMLTags.profileDataElementParameterNameAttrTag, profileDataElement.getParamName());
profileDataElementNode.setAttribute(MicroscopyXMLTags.profileDataElementParameterValueAttrTag, profileDataElement.getParameterValue() + "");
profileDataElementNode.setAttribute(MicroscopyXMLTags.profileDataElementLikelihoodAttrTag, profileDataElement.getLikelihood() + "");
Parameter[] parameters = profileDataElement.getBestParameters();
for (int i = 0; i < parameters.length; i++) {
if (// some of parameters in reaction off rate model are null.
parameters[i] != null) {
profileDataElementNode.addContent(getXML(parameters[i]));
}
}
return profileDataElementNode;
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class NonGUIFRAPTest method runSolver.
/**
* @param args
*/
public static void runSolver(String[] args) throws Exception {
String startingIndexForRecovery = args[0];
String freeDiffusionRateStr = args[1];
String freeMobileFractionStr = args[2];
String complexDiffusionRateStr = args[3];
String complexMobileFractionStr = args[4];
String bleachWhileMonitoringRateString = args[5];
String immobileFractionStr = args[6];
String bindingSiteConcentrationStr = args[7];
String reacOnRateStr = args[8];
String reacOffRateStr = args[9];
String workingDirectoryPath = args[10];
String inputFRAPDataFileName = args[11];
String inputCellROIFileName = args[12];
String inputBleachROIFileName = args[13];
String inputBackgroundROIFileName = args[14];
String outputXMLFileName = args[15];
String commaSepTimeStamps = args[16];
String commaSepExtentXYZ = args[17];
LocalWorkspace localWorkspace = new LocalWorkspace(new File(workingDirectoryPath));
ExternalDataFileContents extDataFileContents = readExternalDataContents(inputFRAPDataFileName, inputCellROIFileName, inputBleachROIFileName, inputBackgroundROIFileName);
ROI cellROI = new ROI(extDataFileContents.cellROIData.getImage(0, 0, 0), FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
ROI bleachROI = new ROI(extDataFileContents.bleachROIData.getImage(0, 0, 0), FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name());
ROI backgroundROI = new ROI(extDataFileContents.backgroundROIData.getImage(0, 0, 0), FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name());
// Insert Time information
double[] timeStamps = new double[extDataFileContents.imageData.getAllImages().length];
StringTokenizer commaStringTokenizer = new StringTokenizer(commaSepTimeStamps, ",");
int timeCount = 0;
while (commaStringTokenizer.hasMoreTokens()) {
timeStamps[timeCount] = Double.parseDouble(commaStringTokenizer.nextToken());
timeCount++;
}
ImageDataset frapDataImageDataSet = new ImageDataset(extDataFileContents.imageData.getAllImages(), timeStamps, 1);
// Insert Extent information
commaStringTokenizer = new StringTokenizer(commaSepExtentXYZ, ",");
Extent extent = new Extent(Double.parseDouble(commaStringTokenizer.nextToken()), Double.parseDouble(commaStringTokenizer.nextToken()), Double.parseDouble(commaStringTokenizer.nextToken()));
frapDataImageDataSet.setExtent(extent);
bleachROI.getRoiImages()[0].setExtent(extent);
cellROI.getRoiImages()[0].setExtent(extent);
backgroundROI.getRoiImages()[0].setExtent(extent);
FRAPData frapData = FrapDataUtils.importFRAPDataFromImageDataSet(frapDataImageDataSet);
frapData.addReplaceRoi(bleachROI);
frapData.addReplaceRoi(cellROI);
frapData.addReplaceRoi(backgroundROI);
FRAPStudy frapStudy = new FRAPStudy();
frapStudy.setFrapData(frapData);
// old model parameters, need to rewrite
// FRAPStudy.FRAPModelParameters frapModelParameters = new FRAPStudy.FRAPModelParameters(
// new FRAPStudy.InitialModelParameters(freeDiffusionRateStr, freeMobileFractionStr, bleachWhileMonitoringRateString, startingIndexForRecovery),
// null,
// null);
// frapStudy.setFrapModelParameters(frapModelParameters);
frapStudy.refreshDependentROIs();
ExternalDataInfo imageDatasetExternalDataInfo = FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.IMAGE_EXTDATA_NAME);
ExternalDataInfo roiExternalDataInfo = FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.ROI_EXTDATA_NAME);
frapStudy.setFrapDataExternalDataInfo(imageDatasetExternalDataInfo);
frapStudy.setRoiExternalDataInfo(roiExternalDataInfo);
frapStudy.saveImageDatasetAsExternalData(localWorkspace, frapStudy.getFrapDataExternalDataInfo().getExternalDataIdentifier(), frapStudy.getStartingIndexForRecovery());
frapStudy.saveROIsAsExternalData(localWorkspace, frapStudy.getRoiExternalDataInfo().getExternalDataIdentifier(), frapStudy.getStartingIndexForRecovery());
// Double bleachWhileMonitoringRate =
// (!bleachWhileMonitoringRateString.equals("-")
// ?Double.parseDouble(bleachWhileMonitoringRateString)
// :null);
double fd, ff, bwmr, cd, cf, imf, bs, on, off;
try {
fd = Double.parseDouble(freeDiffusionRateStr);
ff = Double.parseDouble(freeMobileFractionStr);
bwmr = Double.parseDouble(bleachWhileMonitoringRateString);
cd = Double.parseDouble(complexDiffusionRateStr);
cf = Double.parseDouble(complexMobileFractionStr);
imf = Double.parseDouble(immobileFractionStr);
bs = Double.parseDouble(bindingSiteConcentrationStr);
on = Double.parseDouble(reacOnRateStr);
off = Double.parseDouble(reacOffRateStr);
} catch (NumberFormatException e) {
throw new Exception("Input parameters are not all valid. Check if they are empty or in illegal forms.");
}
BioModel bioModel = FRAPStudy.createNewSimBioModel(frapStudy, createParameterArray(fd, ff, bwmr, cd, cf, imf, bs, on, off), null, LocalWorkspace.createNewKeyValue(), LocalWorkspace.getDefaultOwner(), new Integer(frapStudy.getStartingIndexForRecovery()));
frapStudy.setBioModel(bioModel);
// no progress listener, need to change
MicroscopyXmlproducer.writeXMLFile(frapStudy, new File(outputXMLFileName), true, null, false);
FRAPStudy.runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), bioModel.getSimulation(0), imageDatasetExternalDataInfo.getExternalDataIdentifier(), roiExternalDataInfo.getExternalDataIdentifier(), null);
VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(bioModel.getSimulations()[0].getSimulationInfo().getAuthoritativeVCSimulationIdentifier(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT);
PDEDataManager simulationDataManager = new PDEDataManager(null, localWorkspace.getVCDataManager(), vcSimulationDataIdentifier);
double[] frapDataTimeStamps = frapData.getImageDataset().getImageTimeStamps();
VCDataManager testVCDataManager = localWorkspace.getVCDataManager();
double[] prebleachAverage = testVCDataManager.getSimDataBlock(null, frapStudy.getRoiExternalDataInfo().getExternalDataIdentifier(), "prebleach_avg", 0).getData();
// TODO: need to create parameters here.
Parameter[] parameters = null;
SpatialAnalysisResults spatialAnalysisResults = FRAPStudy.spatialAnalysis(simulationDataManager, new Integer(frapStudy.getStartingIndexForRecovery()), frapDataTimeStamps[new Integer(frapStudy.getStartingIndexForRecovery())], parameters, frapData, prebleachAverage);
dumpSummaryReport(spatialAnalysisResults, frapDataTimeStamps, new Integer(startingIndexForRecovery).intValue(), new File(workingDirectoryPath, "nonguiSpatialResults.txt"));
dumpSpatialResults(spatialAnalysisResults, frapDataTimeStamps, new File(workingDirectoryPath, "nonguiSpatialResults.txt"));
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class NonGUIFRAPTest method createParameterArray.
private static Parameter[] createParameterArray(double freeDiffRate, double freeFraction, double monitorBleachRate, double complexDifffRate, double complexFraction, double immFraction, double bsConc, double onRate, double offRate) {
Parameter[] params = null;
Parameter freeDiff = new cbit.vcell.opt.Parameter(STR_FREE_DIFF_RATE, FRAPModel.REF_DIFFUSION_RATE_PARAM.getLowerBound(), FRAPModel.REF_DIFFUSION_RATE_PARAM.getUpperBound(), FRAPModel.REF_DIFFUSION_RATE_PARAM.getScale(), freeDiffRate);
Parameter freeFrac = new cbit.vcell.opt.Parameter(STR_FREE_FRACTION, FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getScale(), freeFraction);
Parameter bleachWhileMonitoringRate = new cbit.vcell.opt.Parameter(STR_BLEACH_MONITOR_RATE, FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getLowerBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getUpperBound(), FRAPModel.REF_BLEACH_WHILE_MONITOR_PARAM.getScale(), monitorBleachRate);
Parameter complexdiff = new Parameter(STR_COMPLEX_DIFF_RATE, FRAPModel.REF_DIFFUSION_RATE_PARAM.getLowerBound(), FRAPModel.REF_DIFFUSION_RATE_PARAM.getUpperBound(), FRAPModel.REF_DIFFUSION_RATE_PARAM.getScale(), complexDifffRate);
Parameter complexFrac = new Parameter(STR_COMPLEX_FRACTION, FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getScale(), complexFraction);
Parameter immFrac = new Parameter(STR_IMMOBILE_FRACTION, FRAPModel.REF_MOBILE_FRACTION_PARAM.getLowerBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getUpperBound(), FRAPModel.REF_MOBILE_FRACTION_PARAM.getScale(), immFraction);
Parameter bsConcentration = new Parameter(STR_BINDING_SITE_CONCENTRATION, 0, 1, 1, bsConc);
Parameter onReacRate = new Parameter(STR_ON_RATE, 0, 1e6, 1, onRate);
Parameter offReacRate = new Parameter(STR_OFF_RATE, 0, 1e6, 1, offRate);
params = new Parameter[] { freeDiff, freeFrac, bleachWhileMonitoringRate, complexdiff, complexFrac, immFrac, bsConcentration, onReacRate, offReacRate };
return params;
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class ModelOptimizationMapping method computeOptimizationSpec.
/**
* Insert the method's description here.
* Creation date: (8/22/2005 9:26:52 AM)
* @return cbit.vcell.opt.OptimizationSpec
* @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
*/
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
if (getModelOptimizationSpec().getReferenceData() == null) {
System.out.println("no referenced data defined");
return null;
}
OptimizationSpec optSpec = new OptimizationSpec();
optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
parameterMappings = null;
//
// get original MathDescription (later to be substituted for local/global parameters).
//
SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription origMathDesc = null;
mathSymbolMapping = null;
try {
origMathDesc = mathMapping.getMathDescription();
mathSymbolMapping = mathMapping.getMathSymbolMapping();
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ModelException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
// create objective function (mathDesc and data)
//
ReferenceData referenceData = getRemappedReferenceData(mathMapping);
if (referenceData == null) {
throw new RuntimeException("no referenced data defined");
}
//
// get parameter mappings
//
ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
for (int i = 0; i < parameterMappingSpecs.length; i++) {
cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
String mathSymbol = null;
Variable mathVariable = null;
if (mathSymbolMapping != null) {
Variable variable = mathSymbolMapping.getVariable(modelParameter);
if (variable != null) {
mathSymbol = variable.getName();
}
if (mathSymbol != null) {
mathVariable = origMathDesc.getVariable(mathSymbol);
}
}
if (mathVariable != null) {
if (parameterMappingSpecs[i].isSelected()) {
if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
}
if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getLow() < 0) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
}
if (parameterMappingSpecs[i].getHigh() <= 0) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
}
}
double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
double high = parameterMappingSpecs[i].getHigh();
double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
double current = parameterMappingSpecs[i].getCurrent();
low = Math.min(low, current);
high = Math.max(high, current);
Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
if (parameterMappingSpecs[i].isSelected()) {
allVars[j] = new ParameterVariable(origConstant.getName());
} else {
allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
}
break;
}
}
}
//
if (parameterMappingSpecs[i].isSelected()) {
parameterMappingList.add(parameterMapping);
}
}
}
parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
try {
origMathDesc.setAllVariables(allVars);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
for (int i = 0; i < parameterMappings.length; i++) {
optSpec.addParameter(parameterMappings[i].getOptParameter());
}
Vector<Issue> issueList = new Vector<Issue>();
IssueContext issueContext = new IssueContext();
optSpec.gatherIssues(issueContext, issueList);
for (int i = 0; i < issueList.size(); i++) {
Issue issue = issueList.elementAt(i);
if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
throw new RuntimeException(issue.getMessage());
}
}
//
//
//
optimizationSpec = optSpec;
return mathSymbolMapping;
}
use of cbit.vcell.opt.Parameter in project vcell by virtualcell.
the class OptUtils method getAugmentedObjectiveFunction.
/**
* Insert the method's description here.
* Creation date: (8/4/2005 3:26:14 PM)
* @return function.AugmentedObjectiveFunction
* @param optSpec cbit.vcell.opt.OptimizationSpec
* @param power double
* @param mu double
*/
public static AugmentedObjectiveFunction getAugmentedObjectiveFunction(OptimizationSpec optSpec, double power, double mu, OptSolverCallbacks optSolverCallbacks) throws cbit.vcell.parser.ExpressionException {
Parameter[] parameters = optSpec.getParameters();
//
// build symbol list
//
String[] origSymbols = optSpec.getParameterNames();
String[] scaledSymbols = optSpec.getScaledParameterNames();
double[] scaleFactors = optSpec.getScaleFactors();
ScalarFunction objFunction = null;
if (optSpec.getObjectiveFunction() instanceof ExplicitObjectiveFunction) {
ExplicitObjectiveFunction explicitObjectiveFunction = (ExplicitObjectiveFunction) optSpec.getObjectiveFunction();
//
// build objective function
//
objFunction = new DynamicScalarFunction(explicitObjectiveFunction.getScaledExpression(origSymbols, scaledSymbols, scaleFactors), scaledSymbols);
} else if (optSpec.getObjectiveFunction() instanceof ImplicitObjectiveFunction) {
ImplicitObjectiveFunction implicitObjectiveFunction = (ImplicitObjectiveFunction) optSpec.getObjectiveFunction();
//
// build objective function
//
objFunction = implicitObjectiveFunction.getObjectiveFunction();
} else if (optSpec.getObjectiveFunction() instanceof ExplicitFitObjectiveFunction) {
ExplicitFitObjectiveFunction explicitFitObjectiveFunction = (ExplicitFitObjectiveFunction) optSpec.getObjectiveFunction();
objFunction = explicitFitObjectiveFunction.getScalarFunction();
} else {
throw new RuntimeException("unsupported objective function type : " + optSpec.getObjectiveFunction().getClass().getName());
}
//
// build equality and inequality constraints
//
DynamicVectorFunction equalityConstraints = null;
DynamicVectorFunction inequalityConstraints = null;
Constraint[] constraints = optSpec.getConstraints();
Vector equExpList = new Vector();
Vector inequExpList = new Vector();
//
for (int i = 0; i < constraints.length; i++) {
ConstraintType type = constraints[i].getConstraintType();
if (type.equals(ConstraintType.LinearEquality) || type.equals(ConstraintType.NonlinearEquality)) {
equExpList.add(constraints[i].getExpression());
} else {
inequExpList.add(constraints[i].getExpression());
}
}
//
for (int i = 0; i < parameters.length; i++) {
if (!Double.isInfinite(parameters[i].getLowerBound())) {
inequExpList.add(new Expression("(" + parameters[i].getLowerBound() + " - " + parameters[i].getName() + ")/" + scaleFactors[i]));
}
if (!Double.isInfinite(parameters[i].getUpperBound())) {
inequExpList.add(new Expression("(" + parameters[i].getName() + " - " + parameters[i].getUpperBound() + ")/" + scaleFactors[i]));
}
}
if (equExpList.size() > 0) {
Expression[] exps = (Expression[]) org.vcell.util.BeanUtils.getArray(equExpList, Expression.class);
equalityConstraints = new DynamicVectorFunction(exps, origSymbols);
}
if (inequExpList.size() > 0) {
Expression[] exps = (Expression[]) org.vcell.util.BeanUtils.getArray(inequExpList, Expression.class);
inequalityConstraints = new DynamicVectorFunction(exps, origSymbols);
}
AugmentedObjectiveFunction augmentedObjFunc = new AugmentedObjectiveFunction(objFunction, equalityConstraints, inequalityConstraints, power, mu, optSolverCallbacks);
return augmentedObjFunc;
}
Aggregations