Search in sources :

Example 16 with FunctionColumnDescription

use of cbit.vcell.math.FunctionColumnDescription 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 17 with FunctionColumnDescription

use of cbit.vcell.math.FunctionColumnDescription 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 18 with FunctionColumnDescription

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

the class ODEDataManager method addOutputFunction.

private void addOutputFunction(AnnotatedFunction function, ODESolverResultSet odeRS) {
    // Get the new name and expression for the function and create a new
    // functioncolumndescription, check is function is valid. If it is, add it to the list of columns
    // in the ODEResultSet. Else, pop-up an error dialog indicating that function cannot be added.
    FunctionColumnDescription fcd = null;
    String funcName = function.getName();
    Expression funcExp = function.getExpression();
    fcd = new FunctionColumnDescription(funcExp, funcName, null, function.getDisplayName(), true);
    try {
        odeRS.checkFunctionValidity(fcd);
    } catch (ExpressionException e) {
        javax.swing.JOptionPane.showMessageDialog(null, e.getMessage() + ". " + funcName + " not added.", "Error Adding Function ", javax.swing.JOptionPane.ERROR_MESSAGE);
        // e.printStackTrace(System.out);
        return;
    }
    try {
        odeRS.addFunctionColumn(fcd);
    } catch (ExpressionException e) {
        javax.swing.JOptionPane.showMessageDialog(null, e.getMessage() + ". " + funcName + " not added.", "Error Adding Function ", javax.swing.JOptionPane.ERROR_MESSAGE);
        e.printStackTrace(System.out);
    }
}
Also used : Expression(cbit.vcell.parser.Expression) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 19 with FunctionColumnDescription

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

the class ODESimData method readNCDataFile.

public static ODESimData readNCDataFile(VCDataIdentifier vcdId, File dataFile, File functionsFile) throws DataAccessException {
    // read ida file
    System.out.println("reading NetCDF file : " + dataFile);
    ODESimData odeSimData = new ODESimData();
    odeSimData.formatID = NETCDF_DATA_FORMAT_ID;
    odeSimData.mathName = vcdId.getID();
    // read .stoch file, this funciton here equals to getODESolverRestultSet()+getStateVariableResultSet()  in ODE.
    try {
        NetCDFEvaluator ncEva = new NetCDFEvaluator();
        NetCDFReader ncReader = null;
        try {
            ncEva.setNetCDFTarget(dataFile.getAbsolutePath());
            ncReader = ncEva.getNetCDFReader();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            throw new RuntimeException("Cannot open simulation result file: " + dataFile.getAbsolutePath() + "!");
        }
        // Read result according to trial number
        if (ncReader.getNumTrials() == 1) {
            // Read header
            String[] varNames = ncReader.getSpeciesNames_val();
            // first column will be time t.
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("t"));
            // following columns are stoch variables
            for (int i = 0; i < varNames.length; i++) {
                odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varNames[i]));
            }
            // Read data
            // data only, no time points
            ArrayDouble data = (ArrayDouble) ncEva.getTimeSeriesData(1);
            double[] timePoints = ncReader.getTimePoints();
            System.out.println("time points length is " + timePoints.length);
            // shape[0]:num of timepoints, shape[1]: num of species
            int[] shape = data.getShape();
            if (// one species
            shape.length == 1) {
                ArrayDouble.D1 temData = (ArrayDouble.D1) data;
                System.out.println("one species in time series data and size is " + temData.getSize());
                for (// rows
                int k = 0; // rows
                k < timePoints.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = timePoints[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k);
                    }
                    odeSimData.addRow(values);
                }
            }
            if (// more than one species
            shape.length == 2) {
                ArrayDouble.D2 temData = (ArrayDouble.D2) data;
                System.out.println("multiple species in time series, the length of time series is :" + data.getShape()[0] + ", and the total number of speceis is: " + data.getShape()[1]);
                for (// rows
                int k = 0; // rows
                k < timePoints.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = timePoints[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k, i - 1);
                    }
                    odeSimData.addRow(values);
                }
            }
        } else if (ncReader.getNumTrials() > 1) {
            // Read header
            String[] varNames = ncReader.getSpeciesNames_val();
            // first column will be time t.
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("TrialNo"));
            // following columns are stoch variables
            for (int i = 0; i < varNames.length; i++) {
                odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varNames[i]));
            }
            // Read data
            // data only, no trial numbers
            ArrayDouble data = (ArrayDouble) ncEva.getDataOverTrials(ncReader.getTimePoints().length - 1);
            int[] trialNum = ncEva.getNetCDFReader().getTrialNumbers();
            // System.out.println("total trials are "+trialNum.length);
            // shape[0]:number of trials, shape[1]: num of species
            int[] shape = data.getShape();
            if (// one species
            shape.length == 1) {
                ArrayDouble.D1 temData = (ArrayDouble.D1) data;
                // System.out.println("one species over trials, size is: "+temData.getSize());
                for (// rows
                int k = 0; // rows
                k < trialNum.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = trialNum[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k);
                    }
                    odeSimData.addRow(values);
                }
            }
            if (// more than one species
            shape.length == 2) {
                ArrayDouble.D2 temData = (ArrayDouble.D2) data;
                // System.out.println("multiple species in multiple trials, the length of trials is :"+data.getShape()[0]+", and the total number of speceis is: "+data.getShape()[1]);
                for (// rows
                int k = 0; // rows
                k < trialNum.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = trialNum[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k, i - 1);
                    }
                    odeSimData.addRow(values);
                }
            }
        } else {
            throw new RuntimeException("Number of trials should be a countable positive value, from 1 to N.");
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Problem encountered in parsing hybrid simulation results.\n" + e.getMessage());
    }
    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;
}
Also used : NetCDFEvaluator(cbit.vcell.solver.stoch.NetCDFEvaluator) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ExpressionException(cbit.vcell.parser.ExpressionException) NetCDFReader(cbit.vcell.solver.stoch.NetCDFReader) ArrayDouble(ucar.ma2.ArrayDouble) Expression(cbit.vcell.parser.Expression) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Aggregations

FunctionColumnDescription (cbit.vcell.math.FunctionColumnDescription)19 ODESolverResultSetColumnDescription (cbit.vcell.math.ODESolverResultSetColumnDescription)16 Expression (cbit.vcell.parser.Expression)16 ExpressionException (cbit.vcell.parser.ExpressionException)12 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)11 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)7 IOException (java.io.IOException)7 Function (cbit.vcell.math.Function)5 MathException (cbit.vcell.math.MathException)5 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)5 FileNotFoundException (java.io.FileNotFoundException)5 BufferedReader (java.io.BufferedReader)4 VolVariable (cbit.vcell.math.VolVariable)3 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)3 Simulation (cbit.vcell.solver.Simulation)3 SolverException (cbit.vcell.solver.SolverException)3 ColumnDescription (cbit.vcell.util.ColumnDescription)3 EOFException (java.io.EOFException)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3