Search in sources :

Example 21 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class VCellSBMLSolver method getODESolverResultSet.

public static ODESolverResultSet getODESolverResultSet(SimulationJob argSimJob, String idaFileName) {
    // read .ida file
    ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(idaFileName);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        // Read header
        String line = bufferedReader.readLine();
        if (line == null) {
        // throw exception
        }
        while (line.indexOf(':') > 0) {
            String name = line.substring(0, line.indexOf(':'));
            odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(name));
            line = line.substring(line.indexOf(':') + 1);
        }
        // Read data
        while ((line = bufferedReader.readLine()) != null) {
            line = line + "\t";
            double[] values = new double[odeSolverResultSet.getDataColumnCount()];
            boolean bCompleteRow = true;
            for (int i = 0; i < odeSolverResultSet.getDataColumnCount(); i++) {
                if (line.indexOf('\t') == -1) {
                    bCompleteRow = false;
                    break;
                } else {
                    String value = line.substring(0, line.indexOf('\t')).trim();
                    values[i] = Double.valueOf(value).doubleValue();
                    line = line.substring(line.indexOf('\t') + 1);
                }
            }
            if (bCompleteRow) {
                odeSolverResultSet.addRow(values);
            } else {
                break;
            }
        }
    // 
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    // add appropriate Function columns to result set
    cbit.vcell.math.Function[] functions = argSimJob.getSimulationSymbolTable().getFunctions();
    for (int i = 0; i < functions.length; i++) {
        if (SimulationSymbolTable.isFunctionSaved(functions[i])) {
            Expression exp1 = new Expression(functions[i].getExpression());
            try {
                exp1 = argSimJob.getSimulationSymbolTable().substituteFunctions(exp1);
            } catch (cbit.vcell.math.MathException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + functions[i].getName() + " " + e.getMessage());
            } catch (cbit.vcell.parser.ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + functions[i].getName() + " " + e.getMessage());
            }
            try {
                FunctionColumnDescription cd = new FunctionColumnDescription(exp1.flatten(), functions[i].getName(), null, functions[i].getName(), false);
                odeSolverResultSet.addFunctionColumn(cd);
            } catch (cbit.vcell.parser.ExpressionException e) {
                e.printStackTrace(System.out);
            }
        }
    }
    return odeSolverResultSet;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) ExecutableException(org.vcell.util.exe.ExecutableException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlParseException(cbit.vcell.xml.XmlParseException) SolverException(cbit.vcell.solver.SolverException) SbmlException(org.vcell.sbml.SbmlException) IOException(java.io.IOException) SBMLImportException(org.vcell.sbml.vcell.SBMLImportException) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 22 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class FRAPOptimizationUtils method doubleArrayToSolverResultSet.

// used by opt or sim data, no truncation of time/data. @param startingIndex is used to shift opt/sim times to compare with exp times
public static ODESolverResultSet doubleArrayToSolverResultSet(double[][] origData, double[] timePoints, double timePointOffset, boolean[] selectedROIs) /*throws Exception*/
{
    if (origData != null && timePoints != null && selectedROIs != null) {
        int numSelectedROITypes = 0;
        for (int i = 0; i < selectedROIs.length; i++) {
            if (selectedROIs[i]) {
                numSelectedROITypes++;
            }
        }
        ODESolverResultSet newOdeSolverResultSet = new ODESolverResultSet();
        newOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
        for (int j = 0; j < selectedROIs.length; j++) {
            if (!selectedROIs[j]) {
                continue;
            }
            String currentROIName = FRAPData.VFRAP_ROI_ENUM.values()[j].name();
            String name = currentROIName;
            newOdeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(name));
        }
        // set time
        for (int j = 0; j < timePoints.length; j++) {
            double[] row = new double[numSelectedROITypes + 1];
            row[0] = timePoints[j] + timePointOffset;
            newOdeSolverResultSet.addRow(row);
        }
        // set data
        int columncounter = 0;
        for (int j = 0; j < selectedROIs.length; j++) {
            if (!selectedROIs[j]) {
                continue;
            }
            double[] values = origData[j];
            for (int k = 0; k < values.length; k++) {
                newOdeSolverResultSet.setValue(k, columncounter + 1, values[k]);
            }
            columncounter++;
        }
        return newOdeSolverResultSet;
    }
    return null;
}
Also used : ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription)

Example 23 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class MergedData method getODEDataBlock.

/**
 * Insert the method's description here.
 * Creation date: (1/14/00 2:28:47 PM)
 * @return cbit.vcell.simdata.ODEDataBlock
 */
public ODEDataBlock getODEDataBlock() throws DataAccessException {
    ODESolverResultSet combinedODESolverRSet = new ODESolverResultSet();
    ODEDataBlock refDataBlock = dataSetControllerImpl.getODEDataBlock(datasetsIDList[0]);
    ODESimData refSimData = refDataBlock.getODESimData();
    // Can use dataTimes field later (for genuine SimulationData), but for now, obtain it on the fly
    double[] times = null;
    try {
        int independentVarIndex = refSimData.findColumn("t");
        if (independentVarIndex < 0) {
            independentVarIndex = refSimData.findColumn(HISTOGRAM_INDEX_NAME);
        }
        times = refSimData.extractColumn(independentVarIndex);
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
    }
    // Adding data/function columns to new resultSet
    ODESolverResultSet[] resultSetList = new ODESolverResultSet[datasetsIDList.length];
    for (int i = 0; i < datasetsIDList.length; i++) {
        ODEDataBlock dataBlock = dataSetControllerImpl.getODEDataBlock(datasetsIDList[i]);
        ODESimData simData = dataBlock.getODESimData();
        ODESolverResultSet newODErset = new ODESolverResultSet();
        // First resultSet is reference resultSet. From the second onwards, resample the resultSet wrt the reference.
        if (i > 0) {
            try {
                newODErset = resampleODEData(refSimData, simData);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("\n >>>> Could not resample data! <<<<\n");
            }
        } else {
            newODErset = simData;
        }
        resultSetList[i] = newODErset;
        // Add data columns
        String[] newVarNames = new String[newODErset.getDataColumnCount()];
        for (int j = 0; j < newODErset.getDataColumnCount(); j++) {
            // If 'time' column is present in combinedResultSet, continue with next data column
            if ((combinedODESolverRSet.findColumn("t") > -1) && (newODErset.getDataColumnDescriptions()[j].getName().equals("t"))) {
                newVarNames[j] = newODErset.getDataColumnDescriptions()[j].getName();
                continue;
            }
            // Retain 'time' column as 't', without using datasetID as prefix to avoid multiple time columns in combinedODEResultSet.
            // Adding the time column from the first dataset to combinedResultSet, since it is treated as the reference dataset.
            String newColName = null;
            if (j == 0 && newODErset.getDataColumnDescriptions()[j].getName().equals("t")) {
                newColName = newODErset.getDataColumnDescriptions()[j].getName();
            } else {
                newColName = dataSetPrefix[i] + "." + newODErset.getDataColumnDescriptions()[j].getName();
            }
            newVarNames[j] = newColName;
            ColumnDescription cd = newODErset.getDataColumnDescriptions()[j];
            if (cd instanceof ODESolverResultSetColumnDescription) {
                ODESolverResultSetColumnDescription newCD = new ODESolverResultSetColumnDescription(newColName, cd.getParameterName(), newColName);
                combinedODESolverRSet.addDataColumn(newCD);
            }
        }
        // Add function columns
        for (int j = 0; j < newODErset.getFunctionColumnCount(); j++) {
            try {
                String newColName = dataSetPrefix[i] + "." + newODErset.getFunctionColumnDescriptions()[j].getName();
                FunctionColumnDescription fcd = newODErset.getFunctionColumnDescriptions()[j];
                Expression newExp = new Expression(fcd.getExpression());
                String[] symbols = newExp.getSymbols();
                if (symbols != null && (symbols.length > 0)) {
                    for (int jj = 0; jj < symbols.length; jj++) {
                        for (int kk = 0; kk < newVarNames.length; kk++) {
                            if (newVarNames[kk].equals(dataSetPrefix[i] + "." + symbols[jj])) {
                                newExp.substituteInPlace(new Expression(symbols[jj]), new Expression(newVarNames[kk]));
                                break;
                            }
                        }
                    }
                }
                FunctionColumnDescription newFcd = new FunctionColumnDescription(newExp, newColName, fcd.getParameterName(), newColName, fcd.getIsUserDefined());
                combinedODESolverRSet.addFunctionColumn(newFcd);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
            }
        }
    }
    // Populating new dataset
    for (int i = 0; i < times.length; i++) {
        double[] newRow = new double[combinedODESolverRSet.getDataColumnCount()];
        int indx = 0;
        for (int j = 0; j < resultSetList.length; j++) {
            ODESolverResultSet resultSet = resultSetList[j];
            double[] tempRow = resultSet.getRow(i);
            int startIndx = 0;
            int arrayLen = tempRow.length;
            if (j > 0) {
                // From the second dataset onwards, we do not want to copy the time column, hence skip to
                // the next element/col in dataset, that reduces the # of elements in the row by 1.
                startIndx = 1;
                arrayLen = tempRow.length - 1;
            }
            System.arraycopy(tempRow, startIndx, newRow, indx, arrayLen);
            indx += tempRow.length;
        }
        combinedODESolverRSet.addRow(newRow);
    }
    ODEDataInfo odeDataInfo = new ODEDataInfo(getResultsInfoObject().getOwner(), getResultsInfoObject().getID(), 0);
    ODESimData odeSimData = new ODESimData(getResultsInfoObject(), combinedODESolverRSet);
    return new ODEDataBlock(odeDataInfo, odeSimData);
}
Also used : ColumnDescription(cbit.vcell.util.ColumnDescription) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) ODESimData(cbit.vcell.solver.ode.ODESimData) ExpressionException(cbit.vcell.parser.ExpressionException) Expression(cbit.vcell.parser.Expression) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 24 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class MergedData method resampleODEData.

/**
 * Insert the method's description here.
 * Creation date: (10/11/00 1:28:51 PM)
 * @param function cbit.vcell.math.Function
 */
private ODESolverResultSet resampleODEData(ODESimData refSimdata, ODESimData simData) throws ExpressionException {
    // If simData and refSimdata times are equal, return simData without resampling.
    // Else resampling is necessary.
    double[] refTimeArray = refSimdata.extractColumn(Math.max(refSimdata.findColumn(HISTOGRAM_INDEX_NAME), refSimdata.findColumn("t")));
    double[] timeArray = simData.extractColumn(Math.max(simData.findColumn(HISTOGRAM_INDEX_NAME), simData.findColumn("t")));
    if (refTimeArray.length == timeArray.length) {
        boolean bEqual = true;
        for (int i = 0; i < refTimeArray.length; i++) {
            if (refTimeArray[i] == timeArray[i]) {
                bEqual = bEqual && true;
            } else {
                bEqual = bEqual && false;
            }
        }
        if (bEqual) {
            return simData;
        }
    }
    ODESolverResultSet newODEresultSet = new ODESolverResultSet();
    for (int i = 0; i < simData.getDataColumnCount(); i++) {
        if (simData.getDataColumnDescriptions()[i] instanceof ODESolverResultSetColumnDescription) {
            ODESolverResultSetColumnDescription colDesc = ((ODESolverResultSetColumnDescription) simData.getDataColumnDescriptions()[i]);
            newODEresultSet.addDataColumn(colDesc);
        }
    }
    for (int i = 0; i < simData.getFunctionColumnCount(); i++) {
        FunctionColumnDescription colDesc = simData.getFunctionColumnDescriptions()[i];
        newODEresultSet.addFunctionColumn(colDesc);
    }
    double[][] resampledData = new double[refTimeArray.length][simData.getDataColumnCount()];
    for (int i = 0; i < simData.getDataColumnCount(); i++) {
        ColumnDescription colDesc = simData.getDataColumnDescriptions()[i];
        // If it is the first column (time), set value in new SimData to the timeArray values in refSimData.
        if (i == 0 && colDesc.getName().equals("t")) {
            for (int j = 0; j < refTimeArray.length; j++) {
                resampledData[j][i] = refTimeArray[j];
            }
            continue;
        }
        double[] data = simData.extractColumn(i);
        int k = 0;
        for (int j = 0; j < refTimeArray.length; j++) {
            // CHECK IF refTimeArray or timeArry has to be used here,
            while ((k < timeArray.length - 2) && (refTimeArray[j] > timeArray[k + 1])) {
                k++;
            }
            // apply first order linear basis for reference data interpolation.
            resampledData[j][i] = data[k] + (data[k + 1] - data[k]) * (refTimeArray[j] - timeArray[k]) / (timeArray[k + 1] - timeArray[k]);
        }
    }
    for (int i = 0; i < refTimeArray.length; i++) {
        newODEresultSet.addRow(resampledData[i]);
    }
    return newODEresultSet;
}
Also used : ColumnDescription(cbit.vcell.util.ColumnDescription) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 25 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class SundialsSolver method getStateVariableResultSet.

/**
 * This method was created in VisualAge.
 * @return double[]
 * @param vectorIndex int
 */
private ODESolverResultSet getStateVariableResultSet() {
    ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(getBaseName() + IDA_DATA_EXTENSION);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        // Read header
        String line = bufferedReader.readLine();
        if (line == null) {
            // throw exception
            return null;
        }
        while (line.indexOf(':') > 0) {
            String name = line.substring(0, line.indexOf(':'));
            odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(name));
            line = line.substring(line.indexOf(':') + 1);
        }
        // Read data
        while ((line = bufferedReader.readLine()) != null) {
            line = line + "\t";
            double[] values = new double[odeSolverResultSet.getDataColumnCount()];
            boolean bCompleteRow = true;
            for (int i = 0; i < odeSolverResultSet.getDataColumnCount(); i++) {
                if (line.indexOf('\t') == -1) {
                    bCompleteRow = false;
                    break;
                } else {
                    String value = line.substring(0, line.indexOf('\t')).trim();
                    values[i] = Double.valueOf(value).doubleValue();
                    line = line.substring(line.indexOf('\t') + 1);
                }
            }
            if (bCompleteRow) {
                odeSolverResultSet.addRow(values);
            } else {
                break;
            }
        }
    // 
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception ex) {
            lg.error(ex.getMessage(), ex);
        }
    }
    return (odeSolverResultSet);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FileInputStream(java.io.FileInputStream) SolverException(cbit.vcell.solver.SolverException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) FileNotFoundException(java.io.FileNotFoundException) MathException(cbit.vcell.math.MathException)

Aggregations

ODESolverResultSetColumnDescription (cbit.vcell.math.ODESolverResultSetColumnDescription)26 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)18 FunctionColumnDescription (cbit.vcell.math.FunctionColumnDescription)16 Expression (cbit.vcell.parser.Expression)14 ExpressionException (cbit.vcell.parser.ExpressionException)12 IOException (java.io.IOException)8 MathException (cbit.vcell.math.MathException)6 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)6 FileNotFoundException (java.io.FileNotFoundException)6 DataSource (cbit.vcell.modelopt.DataSource)5 BufferedReader (java.io.BufferedReader)5 Function (cbit.vcell.math.Function)4 ReferenceData (cbit.vcell.opt.ReferenceData)4 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)4 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)4 SolverException (cbit.vcell.solver.SolverException)4 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)3 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)3 Simulation (cbit.vcell.solver.Simulation)3 Color (java.awt.Color)3