use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class ReferenceDataPanel method updateReferenceDataFromFile.
/**
* Comment
*/
private void updateReferenceDataFromFile() {
try {
VCFileChooser fileChooser = new VCFileChooser();
fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_CSV);
// Set the default file filter...
fileChooser.setFileFilter(FileFilters.FILE_FILTER_CSV);
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
File defaultPath = null;
if (getUserPreferences() != null) {
defaultPath = getUserPreferences().getCurrentDialogPath();
if (defaultPath != null) {
fileChooser.setCurrentDirectory(defaultPath);
}
}
fileChooser.setDialogTitle("Import Data File");
if (fileChooser.showOpenDialog(this) != javax.swing.JFileChooser.APPROVE_OPTION) {
// user didn't choose save
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null) {
// no file selected (no name given)
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
if (getUserPreferences() != null) {
File newPath = selectedFile.getParentFile();
if (!newPath.equals(defaultPath)) {
getUserPreferences().setCurrentDialogPath(newPath);
}
}
CSV csv = new CSV();
RowColumnResultSet rowColumnResultSet = csv.importFrom(new java.io.FileReader(selectedFile));
double[] weights = new double[rowColumnResultSet.getDataColumnCount()];
java.util.Arrays.fill(weights, 1.0);
ReferenceData referenceData = new SimpleReferenceData(rowColumnResultSet, weights);
updateReferenceData(referenceData);
}
}
} catch (UserCancelException e) {
// ignore
} catch (Exception e) {
e.printStackTrace();
if (e instanceof ParseException) {
showHelp((ParseException) e);
} else {
DialogUtils.showErrorDialog(this, e.getMessage(), e);
}
}
}
use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class ReferenceDataPanel method subsample.
/**
* Comment
*/
private ReferenceData subsample() {
ReferenceData refData = fieldParameterEstimationTask.getModelOptimizationSpec().getReferenceData();
if (refData == null) {
return refData;
}
RowColumnResultSet rc = new RowColumnResultSet();
String[] columnNames = refData.getColumnNames();
for (int i = 0; i < columnNames.length; i++) {
rc.addDataColumn(new ODESolverResultSetColumnDescription(i == timeIndex ? ReservedVariable.TIME.getName() : columnNames[i]));
}
for (int i = 0; i < refData.getNumDataRows(); i++) {
rc.addRow((double[]) refData.getDataByRow(i).clone());
}
int desiredNumRows = refData.getNumDataRows() / 2;
if (desiredNumRows < 3) {
return refData;
}
try {
rc.trimRows(desiredNumRows);
double[] weights = null;
if (refData.getColumnWeights() != null) {
weights = (double[]) refData.getColumnWeights().clone();
} else {
weights = new double[refData.getColumnNames().length];
java.util.Arrays.fill(weights, 1.0);
}
SimpleReferenceData srd = new SimpleReferenceData(rc, weights);
return srd;
} catch (Exception e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(this, e.getMessage(), e);
return refData;
}
}
use of cbit.vcell.opt.SimpleReferenceData 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.opt.SimpleReferenceData in project vcell by virtualcell.
the class ParameterEstimationTaskXMLPersistence method getParameterEstimationTask.
/**
* Insert the method's description here.
* Creation date: (5/5/2006 4:50:36 PM)
* @return cbit.vcell.modelopt.ParameterEstimationTask
* @param element org.jdom.Element
* @param simContext cbit.vcell.mapping.SimulationContext
*/
public static ParameterEstimationTask getParameterEstimationTask(Element parameterEstimationTaskElement, SimulationContext simContext) throws ExpressionException, MappingException, MathException, java.beans.PropertyVetoException {
Namespace ns = parameterEstimationTaskElement.getNamespace();
ParameterEstimationTask parameterEstimationTask = new ParameterEstimationTask(simContext);
String name = parameterEstimationTaskElement.getAttributeValue(NameAttribute);
parameterEstimationTask.setName(name);
Element annotationElement = parameterEstimationTaskElement.getChild(AnnotationTag, ns);
if (annotationElement != null) {
String annotationText = annotationElement.getText();
parameterEstimationTask.setAnnotation(annotationText);
}
//
// read ParameterMappingSpecs
//
Element parameterMappingSpecListElement = parameterEstimationTaskElement.getChild(ParameterMappingSpecListTag, ns);
if (parameterMappingSpecListElement != null) {
List<Element> parameterMappingSpecElementList = parameterMappingSpecListElement.getChildren(ParameterMappingSpecTag, ns);
for (Element parameterMappingSpecElement : parameterMappingSpecElementList) {
String parameterName = parameterMappingSpecElement.getAttributeValue(ParameterReferenceAttribute);
SymbolTableEntry ste = getSymbolTableEntry(simContext, parameterName);
if (ste instanceof Parameter) {
Parameter parameter = (Parameter) ste;
ParameterMappingSpec parameterMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getParameterMappingSpec(parameter);
if (parameterMappingSpec != null) {
String lowLimitString = parameterMappingSpecElement.getAttributeValue(LowLimitAttribute);
if (lowLimitString != null) {
parameterMappingSpec.setLow(parseDouble(lowLimitString));
}
String highLimitString = parameterMappingSpecElement.getAttributeValue(HighLimitAttribute);
if (highLimitString != null) {
parameterMappingSpec.setHigh(parseDouble(highLimitString));
}
String currentValueString = parameterMappingSpecElement.getAttributeValue(CurrentValueAttribute);
if (currentValueString != null) {
parameterMappingSpec.setCurrent(Double.parseDouble(currentValueString));
}
String selectedString = parameterMappingSpecElement.getAttributeValue(SelectedAttribute);
if (selectedString != null) {
parameterMappingSpec.setSelected(Boolean.valueOf(selectedString).booleanValue());
}
}
} else {
System.out.println("couldn't read parameterMappingSpec '" + parameterName + "', ste=" + ste);
}
}
}
//
// read ReferenceData
//
Element referenceDataElement = parameterEstimationTaskElement.getChild(ReferenceDataTag, ns);
if (referenceDataElement != null) {
String numRowsText = referenceDataElement.getAttributeValue(NumRowsAttribute);
String numColsText = referenceDataElement.getAttributeValue(NumColumnsAttribute);
// int numRows = Integer.parseInt(numRowsText);
int numCols = Integer.parseInt(numColsText);
//
// read columns
//
String[] columnNames = new String[numCols];
double[] columnWeights = new double[numCols];
int columnCounter = 0;
Element dataColumnListElement = referenceDataElement.getChild(DataColumnListTag, ns);
List<Element> dataColumnList = dataColumnListElement.getChildren(DataColumnTag, ns);
for (Element dataColumnElement : dataColumnList) {
columnNames[columnCounter] = dataColumnElement.getAttributeValue(NameAttribute);
columnWeights[columnCounter] = Double.parseDouble(dataColumnElement.getAttributeValue(WeightAttribute));
columnCounter++;
}
//
// read rows
//
Vector<double[]> rowDataVector = new Vector<double[]>();
Element dataRowListElement = referenceDataElement.getChild(DataRowListTag, ns);
List<Element> dataRowList = dataRowListElement.getChildren(DataRowTag, ns);
for (Element dataRowElement : dataRowList) {
String rowText = dataRowElement.getText();
CommentStringTokenizer tokens = new CommentStringTokenizer(rowText);
double[] rowData = new double[numCols];
for (int j = 0; j < numCols; j++) {
if (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
rowData[j] = Double.parseDouble(token);
} else {
throw new RuntimeException("failed to read row data for ReferenceData");
}
}
rowDataVector.add(rowData);
}
ReferenceData referenceData = new SimpleReferenceData(columnNames, columnWeights, rowDataVector);
parameterEstimationTask.getModelOptimizationSpec().setReferenceData(referenceData);
}
//
// read ReferenceDataMappingSpecs
//
Element referenceDataMappingSpecListElement = parameterEstimationTaskElement.getChild(ReferenceDataMappingSpecListTag, ns);
if (referenceDataMappingSpecListElement != null) {
List<Element> referenceDataMappingSpecList = referenceDataMappingSpecListElement.getChildren(ReferenceDataMappingSpecTag, ns);
for (Element referenceDataMappingSpecElement : referenceDataMappingSpecList) {
String referenceDataColumnName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataColumnNameAttribute);
String referenceDataModelSymbolName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataModelSymbolAttribute);
ReferenceDataMappingSpec referenceDataMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(referenceDataColumnName);
SymbolTableEntry modelSymbolTableEntry = null;
if (referenceDataModelSymbolName != null) {
modelSymbolTableEntry = getSymbolTableEntry(simContext, referenceDataModelSymbolName);
if (referenceDataMappingSpec != null && modelSymbolTableEntry != null) {
referenceDataMappingSpec.setModelObject(modelSymbolTableEntry);
}
}
}
}
//
// read OptimizationSolverSpec
//
Element optimizationSolverSpecElement = parameterEstimationTaskElement.getChild(OptimizationSolverSpecTag, ns);
if (optimizationSolverSpecElement != null) {
OptimizationSolverSpec optSolverSpec = null;
String optimizationSolverTypeName = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverTypeAttribute);
// getting parameters
Element optimizationSolverParameterList = optimizationSolverSpecElement.getChild(OptimizationListOfParametersTag, ns);
if (optimizationSolverParameterList != null) {
List<Element> listOfSolverParams = optimizationSolverParameterList.getChildren(OptimizationParameterTag, ns);
CopasiOptimizationMethod copasiOptMethod = null;
if (listOfSolverParams != null && listOfSolverParams.size() > 0) {
List<CopasiOptimizationParameter> copasiSolverParams = new ArrayList<CopasiOptimizationParameter>();
for (Element solverParam : listOfSolverParams) {
String paramName = solverParam.getAttributeValue(OptimizationParameterNameAttribute);
double paramValue = Double.parseDouble(solverParam.getAttributeValue(OptimizationParameterValueAttribute));
CopasiOptimizationParameter copasiParam = new CopasiOptimizationParameter(getCopasiOptimizationParameterTypeByName(paramName), paramValue);
copasiSolverParams.add(copasiParam);
}
copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), copasiSolverParams.toArray(new CopasiOptimizationParameter[copasiSolverParams.size()]));
} else // no parameters
{
copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), new CopasiOptimizationParameter[0]);
}
optSolverSpec = new OptimizationSolverSpec(copasiOptMethod);
// add number of runs attribute
String numOfRunsStr = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverNumOfRunsAttribute);
if (numOfRunsStr != null) {
int numOfRuns = Integer.parseInt(numOfRunsStr);
optSolverSpec.setNumOfRuns(numOfRuns);
}
}
parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
}
if (// optimization solver spec is null create a default copasi evolutionary programming
optimizationSolverSpecElement == null || parameterEstimationTask.getOptimizationSolverSpec() == null) {
OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(new CopasiOptimizationMethod(CopasiOptimizationMethodType.EvolutionaryProgram));
parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
}
// read optimization solver result set
Element optimizationResultSetElement = parameterEstimationTaskElement.getChild(OptXmlTags.OptimizationResultSet_Tag, ns);
if (optimizationResultSetElement != null) {
OptimizationResultSet optResultSet = null;
// read optsolverResultSet
if (optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns) != null) {
Element optSolverResultSetElement = optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns);
OptSolverResultSet optSolverResultSet = null;
// get best parameters, best func value, number of evaluations and construct an optRunResultSet
Element paramListElement = optSolverResultSetElement.getChild(OptimizationListOfParametersTag, ns);
OptRunResultSet optRunResultSet = null;
List<String> paramNames = new ArrayList<String>();
List<Double> paramValues = new ArrayList<Double>();
if (paramListElement != null && !paramListElement.getChildren().isEmpty()) {
List<Element> paramElements = paramListElement.getChildren(OptimizationParameterTag, ns);
if (paramElements != null) {
for (Element paramElement : paramElements) {
String paramName = paramElement.getAttributeValue(OptimizationParameterNameAttribute);
double paramValue = Double.parseDouble(paramElement.getAttributeValue(OptimizationParameterValueAttribute));
paramNames.add(paramName);
paramValues.add(paramValue);
}
}
}
Element bestFuncValueElement = optSolverResultSetElement.getChild(OptXmlTags.ObjectiveFunction_Tag, ns);
double bestFuncValue = Double.parseDouble(bestFuncValueElement.getAttributeValue(OptimizationParameterValueAttribute));
Element numEvaluationsElement = optSolverResultSetElement.getChild(OptXmlTags.OptSolverResultSetFunctionEvaluations_Tag, ns);
long numEvaluations = Long.parseLong(numEvaluationsElement.getAttributeValue(OptimizationParameterValueAttribute));
// change List<Double> to double[]
double[] values = new double[paramValues.size()];
int index = 0;
for (Double value : paramValues) {
values[index++] = value;
}
optRunResultSet = new OptRunResultSet(values, bestFuncValue, numEvaluations, null);
// create optSolverResultSet
optSolverResultSet = new OptSolverResultSet(paramNames.toArray(new String[paramNames.size()]), optRunResultSet);
// create optimization result set
optResultSet = new OptimizationResultSet(optSolverResultSet, null);
}
parameterEstimationTask.setOptimizationResultSet(optResultSet);
}
return parameterEstimationTask;
}
use of cbit.vcell.opt.SimpleReferenceData 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;
}
Aggregations