use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class ODESimData method readNFSIMDataFile.
public static ODESimData readNFSIMDataFile(VCDataIdentifier vcdId, File dataFile, File functionsFile) throws DataAccessException, IOException {
System.out.println("reading NetCDF file : " + dataFile);
ODESimData odeSimData = new ODESimData();
odeSimData.formatID = NETCDF_DATA_FORMAT_ID;
odeSimData.mathName = vcdId.getID();
String file = dataFile.getPath();
BufferedReader reader = new BufferedReader(new FileReader(file));
String firstLine = reader.readLine();
StringTokenizer st = new StringTokenizer(firstLine);
// #
st.nextToken();
// time
st.nextToken();
// first column will be time t.
odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("t"));
int count = st.countTokens();
String varName = new String();
for (int i = 0; i < count; i++) {
varName = st.nextToken();
odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varName));
}
// Read data
// String ls = System.getProperty("line.separator");
// StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
double[] values = new double[odeSimData.getDataColumnCount()];
st = new StringTokenizer(line);
count = st.countTokens();
String sData = new String();
for (int i = 0; i < count; i++) {
sData = st.nextToken();
double dData = Double.parseDouble(sData);
values[i] = dData;
}
odeSimData.addRow(values);
}
if (!odeSimData.getColumnDescriptions(0).getName().equals(SimDataConstants.HISTOGRAM_INDEX_NAME)) {
Vector<AnnotatedFunction> funcList;
try {
funcList = FunctionFileGenerator.readFunctionsFile(functionsFile, vcdId.getID());
for (AnnotatedFunction func : funcList) {
try {
Expression expression = new Expression(func.getExpression());
odeSimData.addFunctionColumn(new FunctionColumnDescription(expression, func.getName(), null, func.getName(), false));
} catch (ExpressionException e) {
throw new RuntimeException("Could not add function " + func.getName() + " to annotatedFunctionList");
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace(System.out);
throw new DataAccessException(e1.getMessage());
} catch (IOException e1) {
e1.printStackTrace(System.out);
throw new DataAccessException(e1.getMessage());
}
}
return odeSimData;
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription 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.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class FRAPEstimationPanel_NotUsed method displayFit.
private void displayFit(FrapDataAnalysisResults.DiffusionOnlyAnalysisRestults diffAnalysisResults, double[] frapDataTimeStamps, int startIndexForRecovery) {
if (diffAnalysisResults == null) {
FRAPParameterEstimateEnum.DIFFUSION_RATE.value = null;
FRAPParameterEstimateEnum.MOBILE_FRACTION.value = null;
FRAPParameterEstimateEnum.IMMOBILE_FRATION.value = null;
FRAPParameterEstimateEnum.START_TIME_RECOVERY.value = null;
FRAPParameterEstimateEnum.BLEACH_RATE_MONITOR.value = null;
multisourcePlotPane.setDataSources(null);
} else {
FRAPParameterEstimateEnum.DIFFUSION_RATE.value = (diffAnalysisResults.getRecoveryDiffusionRate() == null ? null : diffAnalysisResults.getRecoveryDiffusionRate());
FRAPParameterEstimateEnum.MOBILE_FRACTION.value = (diffAnalysisResults.getMobilefraction() == null ? null : diffAnalysisResults.getMobilefraction());
FRAPParameterEstimateEnum.IMMOBILE_FRATION.value = (FRAPParameterEstimateEnum.MOBILE_FRACTION.value == null ? null : 1.0 - FRAPParameterEstimateEnum.MOBILE_FRACTION.value);
FRAPParameterEstimateEnum.BLEACH_RATE_MONITOR.value = (diffAnalysisResults.getBleachWhileMonitoringTau() == null ? null : diffAnalysisResults.getBleachWhileMonitoringTau());
// int startIndexForRecovery = FRAPDataAnalysis.getRecoveryIndex(initFRAPData);
//
// Experiment - Cell ROI Average
//
double[] temp_background = initFRAPData.getAvgBackGroundIntensity();
double[] preBleachAvgXYZ = FrapDataUtils.calculatePreBleachAverageXYZ(initFRAPData, startIndexForRecovery);
double[] cellRegionData = FRAPDataAnalysis.getAverageROIIntensity(initFRAPData, initFRAPData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name()), preBleachAvgXYZ, temp_background);
ReferenceData expCellAvgData = new SimpleReferenceData(new String[] { "t", "CellROIAvg" }, new double[] { 1.0, 1.0 }, new double[][] { frapDataTimeStamps, cellRegionData });
DataSource expCellAvgDataSource = new DataSource.DataSourceReferenceData("expCellAvg", expCellAvgData);
//
// Analytic - Cell ROI Average with Bleach while monitor
//
ODESolverResultSet bleachWhileMonitorOdeSolverResultSet = new ODESolverResultSet();
bleachWhileMonitorOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
try {
bleachWhileMonitorOdeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(diffAnalysisResults.getFitBleachWhileMonitorExpression(), "CellROI_BleachWhileMonitor", null, "bleachWhileMonitorFit", true));
} catch (ExpressionException e) {
e.printStackTrace();
}
for (int i = startIndexForRecovery; i < frapDataTimeStamps.length; i++) {
bleachWhileMonitorOdeSolverResultSet.addRow(new double[] { frapDataTimeStamps[i] });
}
//
// extend if necessary to plot theoretical curve to 4*tau
//
{
double T = frapDataTimeStamps[frapDataTimeStamps.length - 1];
double deltaT = frapDataTimeStamps[frapDataTimeStamps.length - 1] - frapDataTimeStamps[frapDataTimeStamps.length - 2];
while (T + deltaT < 6 * diffAnalysisResults.getRecoveryTau()) {
bleachWhileMonitorOdeSolverResultSet.addRow(new double[] { T });
T += deltaT;
}
}
DataSource bleachWhileMonitorDataSource = new DataSource.DataSourceRowColumnResultSet("bleachwm", bleachWhileMonitorOdeSolverResultSet);
// Recovery curve
double[] bleachRegionData = FRAPDataAnalysis.getAverageROIIntensity(initFRAPData, initFRAPData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name()), preBleachAvgXYZ, temp_background);
;
ReferenceData expRefData = new SimpleReferenceData(new String[] { "t", "BleachROIAvg" }, new double[] { 1.0, 1.0 }, new double[][] { frapDataTimeStamps, bleachRegionData });
DataSource expDataSource = new DataSource.DataSourceReferenceData("experiment", expRefData);
ODESolverResultSet fitOdeSolverResultSet = new ODESolverResultSet();
fitOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
try {
fitOdeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(diffAnalysisResults.getDiffFitExpression(), // "('"+FrapDataAnalysisResults.BLEACH_TYPE_NAMES[bleachEstimationComboBox.getSelectedIndex()]+"')",
"BleachROI_Recovery", null, "recoveryFit", true));
} catch (ExpressionException e) {
e.printStackTrace();
}
for (int i = startIndexForRecovery; i < frapDataTimeStamps.length; i++) {
fitOdeSolverResultSet.addRow(new double[] { frapDataTimeStamps[i] });
}
//
// extend if necessary to plot theoretical curve to 4*tau
//
double T = frapDataTimeStamps[frapDataTimeStamps.length - 1];
double deltaT = frapDataTimeStamps[frapDataTimeStamps.length - 1] - frapDataTimeStamps[frapDataTimeStamps.length - 2];
while (T + deltaT < 6 * diffAnalysisResults.getRecoveryTau()) {
fitOdeSolverResultSet.addRow(new double[] { T });
T += deltaT;
}
DataSource fitDataSource = new DataSource.DataSourceRowColumnResultSet("fit", fitOdeSolverResultSet);
multisourcePlotPane.setDataSources(new DataSource[] { expDataSource, fitDataSource, expCellAvgDataSource, bleachWhileMonitorDataSource });
multisourcePlotPane.selectAll();
}
table.repaint();
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class EstParams_ReactionOffRatePanel method displayResults.
private void displayResults(FRAPData frapData, int startIndexRecovery) throws ExpressionException, DivideByZeroException {
Parameter[] currentParams = offRateParamPanel.getCurrentParameters();
if (frapData == null || currentParams == null) {
multisourcePlotPane.setDataSources(null);
} else {
double[] frapDataTimeStamps = frapData.getImageDataset().getImageTimeStamps();
// Experiment - Cell ROI Average
double[] temp_background = frapData.getAvgBackGroundIntensity();
double[] preBleachAvgXYZ = FrapDataUtils.calculatePreBleachAverageXYZ(frapData, startIndexRecovery);
/*double[] cellRegionData = FRAPDataAnalysis.getAverageROIIntensity(frapData, frapData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name()),preBleachAvgXYZ,temp_background);
ReferenceData expCellAvgData = new SimpleReferenceData(new String[] { ReservedSymbol.TIME.getName(), "CellROIAvg" }, new double[] { 1.0, 1.0 }, new double[][] {frapDataTimeStamps, cellRegionData });
DataSource expCellAvgDataSource = new DataSource.DataSourceReferenceData("exp", expCellAvgData);
//Analytic - Cell ROI Average with Bleach while monitor
ODESolverResultSet bleachWhileMonitorOdeSolverResultSet = new ODESolverResultSet();
bleachWhileMonitorOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(ReservedSymbol.TIME.getName()));
Expression cellAvgExp = new Expression(FRAPOptFunctions.FUNC_CELL_INTENSITY);
// substitute parameter values
cellAvgExp.substituteInPlace(new Expression(FRAPOptFunctions.SYMBOL_I_inicell), new Expression(cellRegionData[startIndexRecovery]));
cellAvgExp.substituteInPlace(new Expression(FRAPOptFunctions.SYMBOL_BWM_RATE), new Expression(currentParams[FRAPModel.INDEX_BLEACH_MONITOR_RATE].getInitialGuess()));
// time shift
cellAvgExp.substituteInPlace(new Expression(ReservedSymbol.TIME.getName()), new Expression(ReservedSymbol.TIME.getName()+"-"+frapDataTimeStamps[startIndexRecovery]));
try {
bleachWhileMonitorOdeSolverResultSet.addFunctionColumn(
new FunctionColumnDescription(
cellAvgExp,
"CellROIAvg",
null,"bleachWhileMonitorFit",true));
} catch (ExpressionException e) {
e.printStackTrace();
}
for (int i = startIndexRecovery; i < frapDataTimeStamps.length; i++)
{
bleachWhileMonitorOdeSolverResultSet.addRow(new double[] { frapDataTimeStamps[i] });
}
DataSource bleachWhileMonitorDataSource = new DataSource.DataSourceOdeSolverResultSet("fit", bleachWhileMonitorOdeSolverResultSet);*/
// experimental bleach region average intensity curve
double[] bleachRegionData = FRAPDataAnalysis.getAverageROIIntensity(frapData, frapData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name()), preBleachAvgXYZ, temp_background);
;
ReferenceData expRefData = new SimpleReferenceData(new String[] { ReservedVariable.TIME.getName(), "BleachROIAvg" }, new double[] { 1.0, 1.0 }, new double[][] { frapDataTimeStamps, bleachRegionData });
DataSource expBleachDataSource = new DataSource.DataSourceReferenceData("exp", expRefData);
// Analytic - bleach region average intensity with bleach while monitoring rate
ODESolverResultSet koffFitOdeSolverResultSet = new ODESolverResultSet();
koffFitOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(ReservedVariable.TIME.getName()));
Expression bleachedAvgExp = frapWorkspace.getWorkingFrapStudy().getFrapOptFunc().getRecoveryExpressionWithCurrentParameters(currentParams);
try {
koffFitOdeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(bleachedAvgExp, "BleachROIAvg", null, "recoveryFit", true));
} catch (ExpressionException e) {
e.printStackTrace();
}
double[] truncatedTimes = new double[frapDataTimeStamps.length - startIndexRecovery];
for (int i = startIndexRecovery; i < frapDataTimeStamps.length; i++) {
koffFitOdeSolverResultSet.addRow(new double[] { frapDataTimeStamps[i] });
truncatedTimes[i - startIndexRecovery] = frapDataTimeStamps[i];
}
setCurrentEstimationResults(frapWorkspace.getWorkingFrapStudy().getFrapOptFunc().createData(bleachedAvgExp.flatten(), truncatedTimes));
DataSource koffFitDataSource = new DataSource.DataSourceRowColumnResultSet("fit", koffFitOdeSolverResultSet);
multisourcePlotPane.setDataSources(new DataSource[] { expBleachDataSource, koffFitDataSource /*, expCellAvgDataSource , bleachWhileMonitorDataSource*/
});
multisourcePlotPane.selectAll();
}
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class EstParams_TwoDiffComponentPanel method plotDerivedSimulationResults.
private void plotDerivedSimulationResults(AnalysisParameters[] anaParams) {
try {
String description = null;
int totalROIlen = FRAPData.VFRAP_ROI_ENUM.values().length;
boolean[] wantsROITypes = new boolean[totalROIlen];
System.arraycopy(frapWorkspace.getWorkingFrapStudy().getSelectedROIsForErrorCalculation(), 0, wantsROITypes, 0, totalROIlen);
ODESolverResultSet fitOdeSolverResultSet = new ODESolverResultSet();
fitOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
for (int j = 0; j < totalROIlen; j++) {
if (!wantsROITypes[j]) {
continue;
}
String currentROIName = FRAPData.VFRAP_ROI_ENUM.values()[j].name();
String name = (description == null ? /*"sim D="+diffusionRates[diffusionRateIndex]+"::"*/
"" : description) + currentROIName;
fitOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(name));
}
int totalWantedROIlen = 0;
for (int i = 0; i < wantsROITypes.length; i++) {
if (wantsROITypes[i]) {
totalWantedROIlen++;
}
}
//
// populate time
//
double[] shiftedSimTimes = frapOptData.getReducedExpTimePoints();
int startIndexRecovery = frapOptData.getExpFrapStudy().getStartingIndexForRecovery();
for (int j = 0; j < shiftedSimTimes.length; j++) {
double[] row = new double[totalWantedROIlen + 1];
row[0] = shiftedSimTimes[j] + frapOptData.getExpFrapStudy().getFrapData().getImageDataset().getImageTimeStamps()[startIndexRecovery];
fitOdeSolverResultSet.addRow(row);
}
// populate values
double[][] currentOptFitData = diffTwoPanel.getCurrentFitData();
// store results
setCurrentEstimationResults(currentOptFitData);
if (allDataHash != null && currentOptFitData != null) {
// populate optimization data---convert it to odeSoverResultSet and further put into datasource
int columncounter = 0;
for (int j = 0; j < totalROIlen; j++) {
if (!wantsROITypes[j]) {
continue;
}
// if(!isSimData) //opt data
// {
double[] values = currentOptFitData[j];
for (int k = 0; k < values.length; k++) {
fitOdeSolverResultSet.setValue(k, columncounter + /*j*/
1, values[k]);
}
// }
columncounter++;
}
// anaParams[0] is the key in allDataHash to get the dataSource[]:exp & sim
DataSource[] selectedRowDataSourceArr = allDataHash.get(anaParams[0]);
if (selectedRowDataSourceArr != null) {
// referenceData is the exp data
final DataSource expDataSource = /*new DataSource(referenceData,"exp")*/
selectedRowDataSourceArr[SpatialAnalysisResults.ARRAY_INDEX_EXPDATASOURCE];
DataSource optDataSource = new DataSource.DataSourceRowColumnResultSet("opt", fitOdeSolverResultSet);
//
DataSource[] newDataSourceArr = new DataSource[2];
newDataSourceArr[SpatialAnalysisResults.ARRAY_INDEX_EXPDATASOURCE] = expDataSource;
newDataSourceArr[SpatialAnalysisResults.ARRAY_INDEX_SIMDATASOURCE] = optDataSource;
if (currentOptFitData == null) {
multisourcePlotPane.setDataSources(null);
} else {
// the following paragraph of code is just to get selected color for selected ROIs
// and make them the same as we show on ChooseModel_RoiForErrorPanel/RoiForErrorPanel
// double valid ROI colors (not include cell and background)
int validROISize = FRAPData.VFRAP_ROI_ENUM.values().length - 2;
Color[] fullColors = ColorUtil.generateAutoColor(validROISize * 2, getBackground(), new Integer(0));
boolean[] selectedROIs = frapWorkspace.getWorkingFrapStudy().getSelectedROIsForErrorCalculation();
int selectedROICounter = 0;
for (int i = 0; i < selectedROIs.length; i++) {
if (selectedROIs[i]) {
selectedROICounter++;
}
}
// double the size, each ROI is a comparison of exp and sim
Color[] selectedColors = new Color[selectedROICounter * 2];
int selectedColorIdx = 0;
for (int i = 0; i < selectedROIs.length; i++) {
if (selectedROIs[i] && i == 0) {
selectedColors[selectedColorIdx] = fullColors[i];
selectedColors[selectedColorIdx + selectedROICounter] = fullColors[i + validROISize];
selectedColorIdx++;
}
if (// skip cell and background ROIs
selectedROIs[i] && i > 2) {
selectedColors[selectedColorIdx] = fullColors[i - 2];
selectedColors[selectedColorIdx + selectedROICounter] = fullColors[i - 2 + validROISize];
selectedColorIdx++;
}
}
int[] selectedIndices = multisourcePlotPane.getUnsortedSelectedIndices();
multisourcePlotPane.setDataSources(newDataSourceArr, selectedColors);
if (selectedIndices.length == 0) {
multisourcePlotPane.selectAll();
} else {
multisourcePlotPane.setUnsortedSelectedIndices(selectedIndices);
}
}
}
}
} catch (Exception e2) {
e2.printStackTrace();
DialogUtils.showErrorDialog(this, "Error graphing Optimizer data " + e2.getMessage());
}
}
Aggregations