use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class ModelOptimizationMapping method getRemappedReferenceData.
/**
* Gets the constraintData property (cbit.vcell.opt.ConstraintData) value.
* @return The constraintData property value.
* @see #setConstraintData
*/
private ReferenceData getRemappedReferenceData(MathMapping mathMapping) throws MappingException {
if (modelOptimizationSpec.getReferenceData() == null) {
return null;
}
//
// make sure time is mapped
//
ReferenceData refData = modelOptimizationSpec.getReferenceData();
ReferenceDataMappingSpec[] refDataMappingSpecs = modelOptimizationSpec.getReferenceDataMappingSpecs();
RowColumnResultSet rowColResultSet = new RowColumnResultSet();
Vector<SymbolTableEntry> modelObjectList = new Vector<SymbolTableEntry>();
Vector<double[]> dataList = new Vector<double[]>();
//
// find bound columns, (time is always mapped to the first column)
//
int mappedColumnCount = 0;
for (int i = 0; i < refDataMappingSpecs.length; i++) {
SymbolTableEntry modelObject = refDataMappingSpecs[i].getModelObject();
if (modelObject != null) {
int mappedColumnIndex = mappedColumnCount;
if (modelObject instanceof Model.ReservedSymbol && ((ReservedSymbol) modelObject).isTime()) {
mappedColumnIndex = 0;
}
String origRefDataColumnName = refDataMappingSpecs[i].getReferenceDataColumnName();
int origRefDataColumnIndex = refData.findColumn(origRefDataColumnName);
if (origRefDataColumnIndex < 0) {
throw new RuntimeException("reference data column named '" + origRefDataColumnName + "' not found");
}
double[] columnData = refData.getDataByColumn(origRefDataColumnIndex);
if (modelObjectList.contains(modelObject)) {
throw new RuntimeException("multiple reference data columns mapped to same model object '" + modelObject.getName() + "'");
}
modelObjectList.insertElementAt(modelObject, mappedColumnIndex);
dataList.insertElementAt(columnData, mappedColumnIndex);
mappedColumnCount++;
}
}
//
if (modelObjectList.size() == 0) {
throw new RuntimeException("reference data was not associated with model");
}
if (modelObjectList.size() == 1) {
throw new RuntimeException("reference data was not associated with model, must map time and at least one other column");
}
boolean bFoundTimeVar = false;
for (SymbolTableEntry ste : modelObjectList) {
if (ste instanceof Model.ReservedSymbol && ((ReservedSymbol) ste).isTime()) {
bFoundTimeVar = true;
break;
}
}
if (!bFoundTimeVar) {
throw new RuntimeException("must map time column of reference data to model");
}
//
for (int i = 0; i < modelObjectList.size(); i++) {
SymbolTableEntry modelObject = (SymbolTableEntry) modelObjectList.elementAt(i);
try {
// Find by name because MathSybolMapping has different 'objects' than refDataMapping 'objects'
Variable variable = mathMapping.getMathSymbolMapping().findVariableByName(modelObject.getName());
if (variable != null) {
String symbol = variable.getName();
rowColResultSet.addDataColumn(new ODESolverResultSetColumnDescription(symbol));
} else if (modelObject instanceof Model.ReservedSymbol && ((Model.ReservedSymbol) modelObject).isTime()) {
Model.ReservedSymbol time = (Model.ReservedSymbol) modelObject;
String symbol = time.getName();
rowColResultSet.addDataColumn(new ODESolverResultSetColumnDescription(symbol));
}
} catch (MathException | MatrixException | ExpressionException | ModelException e) {
e.printStackTrace();
throw new MappingException(e.getMessage(), e);
}
}
//
// populate data columns (time and rest)
//
double[] weights = new double[rowColResultSet.getColumnDescriptionsCount()];
weights[0] = 1.0;
int numRows = ((double[]) dataList.elementAt(0)).length;
int numColumns = modelObjectList.size();
for (int j = 0; j < numRows; j++) {
double[] row = new double[numColumns];
for (int i = 0; i < numColumns; i++) {
row[i] = ((double[]) dataList.elementAt(i))[j];
if (i > 0) {
weights[i] += row[i] * row[i];
}
}
rowColResultSet.addRow(row);
}
for (int i = 0; i < numColumns; i++) {
if (weights[i] == 0) {
weights[i] = 1;
} else {
weights[i] = 1 / weights[i];
}
}
SimpleReferenceData remappedRefData = new SimpleReferenceData(rowColResultSet, weights);
return remappedRefData;
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class ReactionRulePropertiesTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object o = getValueAt(rowIndex);
if (!(o instanceof Parameter)) {
return;
}
Parameter parameter = (Parameter) o;
// try {
switch(columnIndex) {
case COLUMN_NAME:
{
try {
if (aValue instanceof String) {
String newName = (String) aValue;
if (!parameter.getName().equals(newName)) {
if (parameter instanceof LocalParameter) {
reactionRule.getKineticLaw().renameParameter(parameter.getName(), newName);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setName(newName);
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
} catch (PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
}
break;
}
case COLUMN_IS_GLOBAL:
{
if (aValue.equals(Boolean.FALSE)) {
// check box has been <unset> (<true> to <false>) : change param from global to local
if ((parameter instanceof LocalProxyParameter) && ((((LocalProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((LocalProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((LocalProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((LocalProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
} else {
try {
reactionRule.getKineticLaw().convertParameterType(parameter, false);
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + e.getMessage());
}
}
} else {
// check box has been <set> (<false> to <true>) : change param from local to global
if ((parameter instanceof LocalParameter) && (((LocalParameter) parameter).getRole() != RbmKineticLaw.RbmKineticLawParameterType.UserDefined)) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a pre-defined kinetics parameter (not user-defined); cannot convert it to a model level (global) parameter.");
} else {
ModelParameter mp = reactionRule.getModel().getModelParameter(parameter.getName());
// model already had the model parameter 'param', but check if 'param' value is different from
// model parameter with same name. If it is, the local value will be overridden by global (model) param
// value, and user should be warned.
String choice = "Ok";
if (mp != null && !(mp.getExpression().compareEqual(parameter.getExpression()))) {
String msgStr = "Model already has a global parameter named : \'" + parameter.getName() + "\'; with value = \'" + mp.getExpression().infix() + "\'; This local parameter \'" + parameter.getName() + "\' with value = \'" + parameter.getExpression().infix() + "\' will be overridden by the global value. \nPress \'Ok' to override " + "local value with global value of \'" + parameter.getName() + "\'. \nPress \'Cancel\' to retain new local value.";
choice = PopupGenerator.showWarningDialog(ownerTable, msgStr, new String[] { "Ok", "Cancel" }, "Ok");
}
if (choice.equals("Ok")) {
try {
// Now 'parameter' is a local kinetic parameter. If it is not numeric, and if its expression
// contains other local kinetic parameters, warn user that 'parameter' cannot be promoted because
// of its expression containing other local parameters.
boolean bPromoteable = true;
if (!parameter.getExpression().isNumeric()) {
String[] symbols = parameter.getExpression().getSymbols();
for (int i = 0; i < symbols.length; i++) {
if (reactionRule.getKineticLaw().getLocalParameter(symbols[i]) != null) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter \'" + parameter.getName() + "\' contains other local kinetic parameters; Cannot convert it to global until the referenced parameters are global.");
bPromoteable = false;
}
}
}
if (bPromoteable) {
reactionRule.getKineticLaw().convertParameterType(parameter, true);
}
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + e.getMessage());
}
}
}
}
fireTableRowsUpdated(rowIndex, rowIndex);
break;
}
case COLUMN_VALUE:
{
try {
if (aValue instanceof ScopedExpression) {
// }
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
if (parameter instanceof LocalParameter) {
LocalParameter localParameter = (LocalParameter) parameter;
reactionRule.getKineticLaw().setParameterValue(localParameter, new Expression(newExpressionString), true);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setExpression(new Expression(newExpressionString));
}
}
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error:\n" + e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_UNITS:
{
try {
if (aValue instanceof String && parameter instanceof LocalParameter && ((LocalParameter) parameter).getRole() == RbmKineticLaw.RbmKineticLawParameterType.UserDefined) {
String newUnitString = (String) aValue;
LocalParameter kineticsParm = (LocalParameter) parameter;
ModelUnitSystem modelUnitSystem = reactionRule.getModel().getUnitSystem();
if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (VCUnitException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter unit:\n" + e.getMessage());
}
break;
}
}
// }catch (java.beans.PropertyVetoException e){
// e.printStackTrace(System.out);
// }
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class GeneralConstraintsTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
System.out.println("GeneralConstraintsTableModel().setValueAt(" + aValue + "," + rowIndex + "," + columnIndex + ")");
if (rowIndex < 0 || rowIndex >= getRowCount()) {
throw new RuntimeException("GeneralConstraintsTableModel.setValueAt(), row = " + rowIndex + " out of range [" + 0 + "," + (getRowCount() - 1) + "]");
}
if (columnIndex < 0 || columnIndex >= NUM_COLUMNS) {
throw new RuntimeException("GeneralConstraintsTableModel.setValueAt(), column = " + columnIndex + " out of range [" + 0 + "," + (NUM_COLUMNS - 1) + "]");
}
GeneralConstraint generalConstraint = getConstraintContainerImpl().getGeneralConstraints(rowIndex);
try {
switch(columnIndex) {
case COLUMN_EXPRESSION:
{
try {
if (aValue instanceof ScopedExpression) {
// generalConstraint.setExpression(exp);
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
generalConstraint.setExpression(new Expression(newExpressionString));
}
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (ExpressionException e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_DESCRIPTION:
{
if (aValue instanceof String) {
generalConstraint.setDescription((String) aValue);
fireTableRowsUpdated(rowIndex, rowIndex);
} else {
System.out.println("ConstraintsTableModel.setValueAt(), unsupported type " + aValue.getClass().getName() + " for COLUMN_DESCRIPTION");
}
break;
}
case COLUMN_INCLUDED:
{
if (aValue instanceof Boolean) {
getConstraintContainerImpl().setActive(generalConstraint, ((Boolean) aValue).booleanValue());
fireTableRowsUpdated(rowIndex, rowIndex);
} else {
System.out.println("ConstraintsTableModel.setValueAt(), unsupported type " + aValue.getClass().getName() + " for COLUMN_INCLUDED");
}
break;
}
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
}
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class SimulationWorkspace method getEstimatedNumTimePointsForStoch.
private static long getEstimatedNumTimePointsForStoch(SimulationSymbolTable simSymbolTable) {
Simulation sim = simSymbolTable.getSimulation();
SolverTaskDescription solverTaskDescription = sim.getSolverTaskDescription();
TimeBounds tb = solverTaskDescription.getTimeBounds();
double startTime = tb.getStartingTime();
double endTime = tb.getEndingTime();
OutputTimeSpec tSpec = solverTaskDescription.getOutputTimeSpec();
// hybrid G_E and G_M are fixed time step methods using uniform output time spec
if (tSpec.isUniform()) {
double outputTimeStep = ((UniformOutputTimeSpec) tSpec).getOutputTimeStep();
return (long) ((endTime - startTime) / outputTimeStep);
}
double maxProbability = 0;
SubDomain subDomain = sim.getMathDescription().getSubDomains().nextElement();
List<VarIniCondition> varInis = subDomain.getVarIniConditions();
// get all the probability expressions
ArrayList<Expression> probList = new ArrayList<Expression>();
for (JumpProcess jp : subDomain.getJumpProcesses()) {
probList.add(jp.getProbabilityRate());
}
// loop through probability expressions
for (int i = 0; i < probList.size(); i++) {
try {
Expression pExp = new Expression(probList.get(i));
pExp.bindExpression(simSymbolTable);
pExp = simSymbolTable.substituteFunctions(pExp);
pExp = pExp.flatten();
String[] symbols = pExp.getSymbols();
// substitute stoch vars with it's initial condition expressions
if (symbols != null) {
for (int j = 0; symbols != null && j < symbols.length; j++) {
for (int k = 0; k < varInis.size(); k++) {
if (symbols[j].equals(varInis.get(k).getVar().getName())) {
pExp.substituteInPlace(new Expression(symbols[j]), new Expression(varInis.get(k).getIniVal()));
break;
}
}
}
}
pExp = simSymbolTable.substituteFunctions(pExp);
pExp = pExp.flatten();
double val = pExp.evaluateConstant();
if (maxProbability < val) {
maxProbability = val;
}
} catch (ExpressionBindingException e) {
System.out.println("Cannot estimate the total time points for stochastic simulation!! Due to the reason below...");
e.printStackTrace();
} catch (ExpressionException ex) {
System.out.println("Cannot estimate the total time points for stochastic simulation!! Due to the reason below...");
ex.printStackTrace();
} catch (MathException e) {
System.out.println("Cannot estimate the total time points for stochastic simulation!! Due to the reason below...");
e.printStackTrace();
}
}
int keepEvery = 1;
if (tSpec.isDefault()) {
keepEvery = ((DefaultOutputTimeSpec) tSpec).getKeepEvery();
}
// points = (endt-startt)/(t*keepEvery) = (endt - startt)/(keepEvery*1/prob)
long estimatedPoints = Math.round((tb.getEndingTime() - tb.getStartingTime()) * maxProbability / keepEvery) + 1;
return estimatedPoints;
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class WorkflowObjectsPanel method displayData.
private void displayData(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) {
RowColumnResultSet rc = (RowColumnResultSet) data;
try {
new DisplayPlotOp().displayPlot(rc, title, listener);
} catch (ExpressionException e) {
e.printStackTrace();
}
} else if (data instanceof ROI) {
ROI roi = (ROI) data;
Image image = roi.getRoiImages()[0];
new DisplayImageOp().displayImage(image, title, listener);
} else if (data instanceof ProfileData[]) {
ProfileData[] profileData = (ProfileData[]) data;
new DisplayProfileLikelihoodPlotsOp().displayProfileLikelihoodPlots(profileData, title, listener);
} else if (data instanceof Image) {
Image image = (Image) data;
new DisplayImageOp().displayImage(image, title, listener);
} else if (data instanceof ImageTimeSeries) {
ImageTimeSeries imageTimeSeries = (ImageTimeSeries) data;
try {
DisplayTimeSeries.displayImageTimeSeries(imageTimeSeries, title, listener);
} catch (ImageException | IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations