use of cbit.vcell.opt.ReferenceData 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.opt.ReferenceData 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.ReferenceData 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.ReferenceData in project vcell by virtualcell.
the class EstParams_CompareResultsDescriptor method aboutToDisplayPanel.
public void aboutToDisplayPanel() {
FRAPStudy fStudy = frapWorkspace.getWorkingFrapStudy();
// create Mean square error for different models under different ROIs
// if(fStudy.getAnalysisMSESummaryData() == null)
// {
fStudy.createAnalysisMSESummaryData();
// }
// auto find best model for user if best model is not selected.
double[][] mseSummaryData = fStudy.getAnalysisMSESummaryData();
// for(int i =0; i<10; i++)
// System.out.print(mseSummaryData[0][i]+" ");
// find best model with significance and has least error
int bestModel = FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT;
if (// best model is saved and there is no model selection change
fStudy.getBestModelIndex() != null) {
bestModel = fStudy.getBestModelIndex().intValue();
} else // need to find the best model
{
// check model significance if more than one model
if (fStudy.getSelectedModels().size() > 1) {
if (getFrapWorkspace().getWorkingFrapStudy().getFrapOptData() != null || getFrapWorkspace().getWorkingFrapStudy().getFrapOptFunc() != null) {
ProfileSummaryData[][] allProfileSumData = FRAPOptimizationUtils.getAllProfileSummaryData(fStudy);
FRAPModel[] frapModels = frapWorkspace.getWorkingFrapStudy().getModels();
int confidenceIdx = ((EstParams_CompareResultsPanel) this.getPanelComponent()).getSelectedConfidenceIndex();
boolean[] modelSignificance = new boolean[FRAPModel.NUM_MODEL_TYPES];
Arrays.fill(modelSignificance, true);
if (frapModels[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] != null && frapModels[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters() != null && allProfileSumData != null && allProfileSumData[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] != null) {
for (int i = 0; i < FRAPModel.NUM_MODEL_PARAMETERS_ONE_DIFF; i++) {
ConfidenceInterval[] intervals = allProfileSumData[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT][i].getConfidenceIntervals();
if (intervals[confidenceIdx].getUpperBound() == frapModels[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters()[i].getUpperBound() && intervals[confidenceIdx].getLowerBound() == frapModels[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT].getModelParameters()[i].getLowerBound()) {
modelSignificance[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] = false;
break;
}
}
}
if (frapModels[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] != null && frapModels[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters() != null && allProfileSumData != null && allProfileSumData[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] != null) {
for (int i = 0; i < FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF; i++) {
ConfidenceInterval[] intervals = allProfileSumData[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS][i].getConfidenceIntervals();
if (intervals[confidenceIdx].getUpperBound() == frapModels[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters()[i].getUpperBound() && intervals[confidenceIdx].getLowerBound() == frapModels[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS].getModelParameters()[i].getLowerBound()) {
modelSignificance[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] = false;
break;
}
}
}
if (frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE] != null && frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters() != null && allProfileSumData != null && allProfileSumData[FRAPModel.IDX_MODEL_REACTION_OFF_RATE] != null) {
for (int i = 0; i < FRAPModel.NUM_MODEL_PARAMETERS_REACTION_OFF_RATE; i++) {
if (i == FRAPModel.INDEX_BLEACH_MONITOR_RATE) {
ConfidenceInterval[] intervals = allProfileSumData[FRAPModel.IDX_MODEL_REACTION_OFF_RATE][FRAPModel.INDEX_BLEACH_MONITOR_RATE].getConfidenceIntervals();
if (intervals[confidenceIdx].getUpperBound() == frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters()[FRAPModel.INDEX_BLEACH_MONITOR_RATE].getUpperBound() && intervals[confidenceIdx].getLowerBound() == frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters()[FRAPModel.INDEX_BLEACH_MONITOR_RATE].getLowerBound()) {
modelSignificance[FRAPModel.IDX_MODEL_REACTION_OFF_RATE] = false;
break;
}
} else if (i == FRAPModel.INDEX_OFF_RATE) {
ConfidenceInterval[] intervals = allProfileSumData[FRAPModel.IDX_MODEL_REACTION_OFF_RATE][FRAPModel.INDEX_OFF_RATE].getConfidenceIntervals();
if (intervals[confidenceIdx].getUpperBound() == frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters()[FRAPModel.INDEX_OFF_RATE].getUpperBound() && intervals[confidenceIdx].getLowerBound() == frapModels[FRAPModel.IDX_MODEL_REACTION_OFF_RATE].getModelParameters()[FRAPModel.INDEX_OFF_RATE].getLowerBound()) {
modelSignificance[FRAPModel.IDX_MODEL_REACTION_OFF_RATE] = false;
break;
}
}
}
}
// check least error model with significance
double minError = 1E8;
if (mseSummaryData != null) {
// exclude cell and bkground ROIs, include sum of error
int secDimLen = FRAPData.VFRAP_ROI_ENUM.values().length - 2 + 1;
if (modelSignificance[FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT] == modelSignificance[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS] && modelSignificance[FRAPModel.IDX_MODEL_REACTION_OFF_RATE] == modelSignificance[FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS]) {
// if all models' significance are the same, find the least error
for (int i = 0; i < FRAPModel.NUM_MODEL_TYPES; i++) {
if ((minError > mseSummaryData[i][secDimLen - 1])) {
minError = mseSummaryData[i][secDimLen - 1];
bestModel = i;
}
}
} else {
// if models' significance are different, find the least error with significance
for (int i = 0; i < FRAPModel.NUM_MODEL_TYPES; i++) {
if (modelSignificance[i] && (minError > mseSummaryData[i][secDimLen - 1])) {
minError = mseSummaryData[i][secDimLen - 1];
bestModel = i;
}
}
}
}
}
} else // only one model is selected and the selected model should be the best model
{
for (int i = 0; i < fStudy.getModels().length; i++) {
if (fStudy.getModels()[i] != null) {
bestModel = i;
break;
}
}
}
}
((EstParams_CompareResultsPanel) this.getPanelComponent()).setBestModelRadioButton(bestModel);
// set data source to multiSourcePlotPane
// length should be fStudy.getSelectedModels().size()+1, however, reaction binding may not have data
ArrayList<DataSource> comparableDataSource = new ArrayList<DataSource>();
// add exp data
ReferenceData expReferenceData = FRAPOptimizationUtils.doubleArrayToSimpleRefData(fStudy.getDimensionReducedExpData(), fStudy.getFrapData().getImageDataset().getImageTimeStamps(), fStudy.getStartingIndexForRecovery(), fStudy.getSelectedROIsForErrorCalculation());
final DataSource expDataSource = new DataSource.DataSourceReferenceData("exp", expReferenceData);
comparableDataSource.add(expDataSource);
// add opt/sim data
// using the same loop, disable the radio button if the model is not included
// adjust radio buttons
((EstParams_CompareResultsPanel) this.getPanelComponent()).disableAllRadioButtons();
ArrayList<Integer> selectedModelIndexes = fStudy.getSelectedModels();
for (int i = 0; i < selectedModelIndexes.size(); i++) {
DataSource newDataSource = null;
double[] timePoints = fStudy.getFrapData().getImageDataset().getImageTimeStamps();
int startingIndex = fStudy.getStartingIndexForRecovery();
double[] truncatedTimes = new double[timePoints.length - startingIndex];
System.arraycopy(timePoints, startingIndex, truncatedTimes, 0, truncatedTimes.length);
if (selectedModelIndexes.get(i).equals(FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT)) {
// adjust radio button
((EstParams_CompareResultsPanel) this.getPanelComponent()).enableRadioButton(FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT);
FRAPModel temModel = fStudy.getFrapModel(FRAPModel.IDX_MODEL_DIFF_ONE_COMPONENT);
ODESolverResultSet temSolverResultSet = FRAPOptimizationUtils.doubleArrayToSolverResultSet(temModel.getData(), truncatedTimes, 0, fStudy.getSelectedROIsForErrorCalculation());
newDataSource = new DataSource.DataSourceRowColumnResultSet("opt_DF1", temSolverResultSet);
} else if (selectedModelIndexes.get(i).equals(FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS)) {
// adjust radio button
((EstParams_CompareResultsPanel) this.getPanelComponent()).enableRadioButton(FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS);
FRAPModel temModel = fStudy.getFrapModel(FRAPModel.IDX_MODEL_DIFF_TWO_COMPONENTS);
ODESolverResultSet temSolverResultSet = FRAPOptimizationUtils.doubleArrayToSolverResultSet(temModel.getData(), truncatedTimes, 0, fStudy.getSelectedROIsForErrorCalculation());
newDataSource = new DataSource.DataSourceRowColumnResultSet("opt_DF2", temSolverResultSet);
} else if (selectedModelIndexes.get(i).equals(FRAPModel.IDX_MODEL_REACTION_OFF_RATE)) {
// adjust radio button
((EstParams_CompareResultsPanel) this.getPanelComponent()).enableRadioButton(FRAPModel.IDX_MODEL_REACTION_OFF_RATE);
FRAPModel temModel = fStudy.getFrapModel(FRAPModel.IDX_MODEL_REACTION_OFF_RATE);
if (temModel.getData() != null) {
ODESolverResultSet temSolverResultSet = FRAPOptimizationUtils.doubleArrayToSolverResultSet(temModel.getData(), truncatedTimes, 0, // for reaction off model, display curve under bleached region only
FRAPStudy.createSelectedROIsForReactionOffRateModel());
newDataSource = new DataSource.DataSourceRowColumnResultSet("sim_Koff", temSolverResultSet);
}
}
if (newDataSource != null) {
comparableDataSource.add(newDataSource);
}
}
// set data to multiSourcePlotPane
((EstParams_CompareResultsPanel) this.getPanelComponent()).setPlotData(comparableDataSource.toArray(new DataSource[comparableDataSource.size()]));
}
use of cbit.vcell.opt.ReferenceData 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;
}
Aggregations