use of cbit.vcell.modelopt.ReferenceDataMappingSpec in project vcell by virtualcell.
the class ParameterEstimationRunTaskPanel method plot.
private void plot() {
try {
java.util.Vector<DataSource> dataSourceList = new java.util.Vector<DataSource>();
java.util.Vector<String> nameVector = new java.util.Vector<String>();
ModelOptimizationSpec modelOptimizationSpec = parameterEstimationTask.getModelOptimizationSpec();
final ReferenceDataMappingSpec[] mappingSpecs = modelOptimizationSpec.getReferenceDataMappingSpecs();
int timeIndex = modelOptimizationSpec.getReferenceDataTimeColumnIndex();
ReferenceData referenceData = modelOptimizationSpec.getReferenceData();
if (referenceData != null) {
dataSourceList.add(new DataSource.DataSourceReferenceData("EXPT", timeIndex, referenceData));
String[] refColumnNames = referenceData.getColumnNames();
for (int i = 0; i < refColumnNames.length; i++) {
if (i == timeIndex) {
continue;
}
nameVector.add(refColumnNames[i]);
break;
}
}
ParameterEstimationTaskSimulatorIDA parestSimulator = new ParameterEstimationTaskSimulatorIDA();
ODESolverResultSet odeSolverResultSet = parestSimulator.getOdeSolverResultSet(parameterEstimationTask);
if (odeSolverResultSet != null) {
dataSourceList.add(new DataSource.DataSourceRowColumnResultSet("EST", odeSolverResultSet));
if (mappingSpecs != null) {
for (int i = 0; i < mappingSpecs.length; i++) {
if (i == timeIndex) {
continue;
}
Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(mappingSpecs[i].getModelObject());
nameVector.add(var.getName());
break;
}
}
}
DataSource[] dataSources = (DataSource[]) BeanUtils.getArray(dataSourceList, DataSource.class);
MultisourcePlotPane multisourcePlotPane = new MultisourcePlotPane();
multisourcePlotPane.setGroupingListSorter(new Comparator<SortDataReferenceHelper>() {
@Override
public int compare(SortDataReferenceHelper o1, SortDataReferenceHelper o2) {
DataSource ds01 = o1.dataReference.getDataSource();
DataSource ds02 = o2.dataReference.getDataSource();
// System.out.println(ds01.getClass().getSimpleName()+" "+o1.dataReference.getIdentifier()+" "+ds02.getClass().getSimpleName()+" "+o2.dataReference.getIdentifier());
if (ds01 instanceof DataSource.DataSourceReferenceData) {
if (ds02 instanceof DataSource.DataSourceReferenceData) {
// both reference data, sort names
ReferenceDataMappingSpec mspec01 = null;
ReferenceDataMappingSpec mspec02 = null;
for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
// Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
continue;
}
if (o1.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
mspec01 = rdMappingSpec;
o1.setReferenceDataMappingSpec(rdMappingSpec);
} else if (o2.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
mspec02 = rdMappingSpec;
o2.setReferenceDataMappingSpec(rdMappingSpec);
}
}
if (mspec01 == null && mspec02 == null) {
return o1.dataReference.getIdentifier().compareToIgnoreCase(o2.dataReference.getIdentifier());
} else if (mspec01 != null && mspec02 != null) {
return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
} else if (mspec01 != null && mspec02 == null) {
return -1;
} else {
return 1;
}
} else {
// compare ref to ode
ReferenceDataMappingSpec mspec01 = null;
ReferenceDataMappingSpec mspec02 = null;
for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
continue;
}
if (o1.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
mspec01 = rdMappingSpec;
o1.setReferenceDataMappingSpec(rdMappingSpec);
} else if (o2.dataReference.getIdentifier().equals(var.getName())) {
mspec02 = rdMappingSpec;
o2.setReferenceDataMappingSpec(rdMappingSpec);
}
}
if (mspec01 == null && mspec02 == null) {
return -1;
} else if (mspec01 != null && mspec02 == null) {
return -1;
} else if (mspec02 != null && mspec01 == null) {
return 1;
} else {
// BeanUtils.forceStringSize(mspec02.getReferenceDataColumnName(), 25, " ", true));
return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
}
}
} else {
if (ds02 instanceof DataSource.DataSourceRowColumnResultSet) {
// both OdeSolverResultSet data, sort names
ReferenceDataMappingSpec mspec01 = null;
ReferenceDataMappingSpec mspec02 = null;
for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
continue;
}
if (o1.dataReference.getIdentifier().equals(var.getName())) {
mspec01 = rdMappingSpec;
o1.setReferenceDataMappingSpec(rdMappingSpec);
} else if (o2.dataReference.getIdentifier().equals(var.getName())) {
mspec02 = rdMappingSpec;
o2.setReferenceDataMappingSpec(rdMappingSpec);
}
}
if (mspec01 == null && mspec02 == null) {
return o1.dataReference.getIdentifier().compareToIgnoreCase(o2.dataReference.getIdentifier());
} else if (mspec01 != null && mspec02 != null) {
return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
} else if (mspec01 != null && mspec02 == null) {
return -1;
} else {
return 1;
}
} else {
// compare ode to ref
ReferenceDataMappingSpec mspec01 = null;
ReferenceDataMappingSpec mspec02 = null;
for (ReferenceDataMappingSpec rdMappingSpec : mappingSpecs) {
Variable var = parameterEstimationTask.getMathSymbolMapping().getVariable(rdMappingSpec.getModelObject());
if (rdMappingSpec.getModelObject() instanceof ReservedSymbol) {
continue;
}
if (o2.dataReference.getIdentifier().equals(rdMappingSpec.getReferenceDataColumnName())) {
mspec02 = rdMappingSpec;
o2.setReferenceDataMappingSpec(rdMappingSpec);
} else if (o1.dataReference.getIdentifier().equals(var.getName())) {
mspec01 = rdMappingSpec;
o1.setReferenceDataMappingSpec(rdMappingSpec);
}
}
if (mspec01 == null && mspec02 == null) {
return 1;
} else if (mspec01 != null && mspec02 == null) {
return -1;
} else if (mspec02 != null && mspec01 == null) {
return 1;
} else {
// BeanUtils.forceStringSize(mspec02.getReferenceDataColumnName(), 25, " ", true));
return mspec01.getReferenceDataColumnName().compareToIgnoreCase(mspec02.getReferenceDataColumnName());
}
}
}
}
});
multisourcePlotPane.setDataSources(dataSources);
String[] nameArray = new String[nameVector.size()];
nameArray = (String[]) BeanUtils.getArray(nameVector, String.class);
multisourcePlotPane.select(nameArray);
DialogUtils.showComponentCloseDialog(JOptionPane.getFrameForComponent(this), multisourcePlotPane, "Data Plot");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
use of cbit.vcell.modelopt.ReferenceDataMappingSpec in project vcell by virtualcell.
the class OptXmlWriter method getCopasiDataXML.
public static Element getCopasiDataXML(ParameterEstimationTask parameterEstimationTask) throws IOException {
ReferenceData refData = parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
Element refDataElement = null;
if (refData instanceof SimpleReferenceData) {
refDataElement = new Element(OptXmlTags.SimpleReferenceData_Tag);
} else if (refData instanceof SpatialReferenceData) {
refDataElement = new Element(OptXmlTags.SpatialReferenceData_Tag);
}
if (refDataElement != null) {
// write variable declarations
// independent variable is t and dimension is 1, these are fixed.
Element timeVarElement = new Element(OptXmlTags.Variable_Tag);
timeVarElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Independent);
timeVarElement.setAttribute(OptXmlTags.VariableName_Attr, ReservedVariable.TIME.getName());
refDataElement.addContent(timeVarElement);
// check if t is at the first column
int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
if (timeIndex != 0) {
throw new RuntimeException("t must be the first column");
}
// add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
File expDataFile = File.createTempFile("expData", ".txt", ResourceUtil.getVcellHome());
PrintWriter pw = new PrintWriter(expDataFile);
pw.print("# Time\t");
for (int i = 1; i < refData.getNumDataColumns(); i++) {
ReferenceDataMappingSpec rdms = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(refData.getColumnNames()[i]);
Element variableElement = new Element(OptXmlTags.Variable_Tag);
variableElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Dependent);
variableElement.setAttribute(OptXmlTags.VariableName_Attr, rdms.getModelObject().getName());
refDataElement.addContent(variableElement);
pw.print(refData.getColumnNames()[i] + "\t");
}
pw.println();
// write data
for (int i = 0; i < refData.getNumDataRows(); i++) {
double[] data = refData.getDataByRow(i);
for (int j = 0; j < data.length; j++) {
pw.print(data[j] + "\t");
}
pw.println();
}
pw.close();
Element dataFileElement = new Element(OptXmlTags.ExperimentalDataFile_Tag);
dataFileElement.setAttribute(OptXmlTags.ExperimentalDataFile_Attr_LastRow, (refData.getNumDataRows() + 1) + "");
dataFileElement.addContent(expDataFile.getAbsolutePath());
refDataElement.addContent(dataFileElement);
}
return refDataElement;
}
use of cbit.vcell.modelopt.ReferenceDataMappingSpec in project vcell by virtualcell.
the class ReferenceDataMappingSpecTableModel method setModelOptimizationSpec.
/**
* Sets the modelOptimizationSpec property (cbit.vcell.modelopt.ModelOptimizationSpec) value.
* @param modelOptimizationSpec The new value for the property.
* @see #getModelOptimizationSpec
*/
public void setModelOptimizationSpec(ModelOptimizationSpec newValue) {
ModelOptimizationSpec oldValue = fieldModelOptimizationSpec;
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
ReferenceDataMappingSpec[] refDataMappingSpecs = oldValue.getReferenceDataMappingSpecs();
for (int i = 0; refDataMappingSpecs != null && i < refDataMappingSpecs.length; i++) {
refDataMappingSpecs[i].removePropertyChangeListener(this);
}
}
fieldModelOptimizationSpec = newValue;
if (newValue != null) {
newValue.addPropertyChangeListener(this);
ReferenceDataMappingSpec[] refDataMappingSpecs = newValue.getReferenceDataMappingSpecs();
for (int i = 0; refDataMappingSpecs != null && i < refDataMappingSpecs.length; i++) {
refDataMappingSpecs[i].addPropertyChangeListener(this);
}
}
fireTableDataChanged();
}
use of cbit.vcell.modelopt.ReferenceDataMappingSpec in project vcell by virtualcell.
the class ReferenceDataPanel method setParameterEstimationTask.
public void setParameterEstimationTask(ParameterEstimationTask newValue) {
ParameterEstimationTask oldValue = fieldParameterEstimationTask;
fieldParameterEstimationTask = newValue;
if (oldValue != null) {
oldValue.getModelOptimizationSpec().removePropertyChangeListener(ivjEventHandler);
ReferenceDataMappingSpec[] refDataMappingSpecs = oldValue.getModelOptimizationSpec().getReferenceDataMappingSpecs();
if (refDataMappingSpecs != null) {
for (ReferenceDataMappingSpec refDataMappingSpec : refDataMappingSpecs) {
refDataMappingSpec.removePropertyChangeListener(ivjEventHandler);
}
}
}
if (fieldParameterEstimationTask != null) {
newValue.getModelOptimizationSpec().addPropertyChangeListener(ivjEventHandler);
ReferenceDataMappingSpec[] refDataMappingSpecs = fieldParameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpecs();
if (refDataMappingSpecs != null) {
for (ReferenceDataMappingSpec refDataMappingSpec : refDataMappingSpecs) {
refDataMappingSpec.addPropertyChangeListener(ivjEventHandler);
}
}
timeIndex = fieldParameterEstimationTask.getModelOptimizationSpec().getReferenceDataTimeColumnIndex();
} else {
// fieldParameterEstimationTask is null, so referenceData cannot have any data, should be null too?
timeIndex = 0;
}
updatePlot();
}
use of cbit.vcell.modelopt.ReferenceDataMappingSpec in project vcell by virtualcell.
the class CopasiServicePython method makeOptProblem.
public static OptProblem makeOptProblem(ParameterEstimationTask parameterEstimationTask) throws IOException, ExpressionException, SBMLException, XMLStreamException {
OptimizationSpec optimizationSpec = parameterEstimationTask.getModelOptimizationMapping().getOptimizationSpec();
SimulationContext simulationContext = parameterEstimationTask.getSimulationContext();
MathMappingCallback callback = new MathMappingCallback() {
@Override
public void setProgressFraction(float fractionDone) {
Thread.dumpStack();
System.out.println("---> stdout mathMapping: progress = " + (fractionDone * 100.0) + "% done");
}
@Override
public void setMessage(String message) {
Thread.dumpStack();
System.out.println("---> stdout mathMapping: message = " + message);
}
@Override
public boolean isInterrupted() {
return false;
}
};
simulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
MathModel vcellMathModel = new MathModel(null);
vcellMathModel.setMathDescription(simulationContext.getMathDescription());
// get math model string
String sbmlString = MathModel_SBMLExporter.getSBMLString(vcellMathModel, 2, 4);
OptProblem optProblem = new OptProblem();
optProblem.setMathModelSbmlContents(sbmlString);
optProblem.setNumberOfOptimizationRuns(parameterEstimationTask.getOptimizationSolverSpec().getNumOfRuns());
for (Parameter p : optimizationSpec.getParameters()) {
ParameterDescription pdesc = new ParameterDescription(p.getName(), p.getScale(), p.getLowerBound(), p.getUpperBound(), p.getInitialGuess());
optProblem.addToParameterDescriptionList(pdesc);
}
SimpleReferenceData refData = (SimpleReferenceData) parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
// check if t is at the first column
int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
if (timeIndex != 0) {
throw new RuntimeException("t must be the first column");
}
DataSet dataset = new DataSet();
for (int rowIndex = 0; rowIndex < refData.getNumDataRows(); rowIndex++) {
DataRow dataRow = new DataRow();
double[] array = refData.getDataByRow(rowIndex);
for (double d : array) {
dataRow.addToData(d);
}
dataset.addToRows(dataRow);
}
optProblem.setExperimentalDataSet(dataset);
optProblem.addToReferenceVariableList(new ReferenceVariable(ReservedVariable.TIME.getName(), ReferenceVariableType.independent));
// add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
for (int i = 1; i < refData.getNumDataColumns(); i++) {
ReferenceDataMappingSpec rdms = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(refData.getColumnNames()[i]);
optProblem.addToReferenceVariableList(new ReferenceVariable(rdms.getModelObject().getName(), ReferenceVariableType.dependent));
}
cbit.vcell.opt.CopasiOptimizationMethod vcellCopasiOptimizationMethod = parameterEstimationTask.getOptimizationSolverSpec().getCopasiOptimizationMethod();
OptimizationMethodType optMethodType = OptimizationMethodType.valueOf(vcellCopasiOptimizationMethod.getType().name());
CopasiOptimizationMethod thriftOptMethod = new CopasiOptimizationMethod();
thriftOptMethod.setOptimizationMethodType(optMethodType);
for (cbit.vcell.opt.CopasiOptimizationParameter optParam : vcellCopasiOptimizationMethod.getParameters()) {
org.vcell.optimization.thrift.CopasiOptimizationParameter p = new org.vcell.optimization.thrift.CopasiOptimizationParameter();
p.setValue(optParam.getValue());
org.vcell.optimization.thrift.OptimizationParameterType optParmType = null;
org.vcell.optimization.thrift.OptimizationParameterDataType optDataType = null;
switch(optParam.getType()) {
case Cooling_Factor:
optParmType = OptimizationParameterType.Cooling_Factor;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case IterationLimit:
optParmType = OptimizationParameterType.IterationLimit;
optDataType = OptimizationParameterDataType.INT;
break;
case Number_of_Generations:
optParmType = OptimizationParameterType.Number_of_Generations;
optDataType = OptimizationParameterDataType.INT;
break;
case Number_of_Iterations:
optParmType = OptimizationParameterType.Number_of_Iterations;
optDataType = OptimizationParameterDataType.INT;
break;
case Pf:
optParmType = OptimizationParameterType.Pf;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Population_Size:
optParmType = OptimizationParameterType.Population_Size;
optDataType = OptimizationParameterDataType.INT;
break;
case Random_Number_Generator:
optParmType = OptimizationParameterType.Random_Number_Generator;
optDataType = OptimizationParameterDataType.INT;
break;
case Rho:
optParmType = OptimizationParameterType.Rho;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Scale:
optParmType = OptimizationParameterType.Scale;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Seed:
optParmType = OptimizationParameterType.Seed;
optDataType = OptimizationParameterDataType.INT;
break;
case Start_Temperature:
optParmType = OptimizationParameterType.Start_Temperature;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Std_Deviation:
optParmType = OptimizationParameterType.Std_Deviation;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Swarm_Size:
optParmType = OptimizationParameterType.Swarm_Size;
optDataType = OptimizationParameterDataType.INT;
break;
case Tolerance:
optParmType = OptimizationParameterType.Tolerance;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
default:
throw new RuntimeException("unsupported parameter type :" + optParam.getType().name() + " in COPASI optimization solver");
}
p.setParamType(optParmType);
p.setDataType(optDataType);
thriftOptMethod.addToOptimizationParameterList(p);
}
optProblem.setOptimizationMethod(thriftOptMethod);
return optProblem;
}
Aggregations