Search in sources :

Example 6 with FunctionColumnDescription

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

the class SundialsSolver method getODESolverResultSet.

/**
 * This method was created in VisualAge.
 * @return double[]
 * @param vectorIndex int
 */
public ODESolverResultSet getODESolverResultSet() {
    // 
    // read .ida file
    // 
    ODESolverResultSet odeSolverResultSet = getStateVariableResultSet();
    if (odeSolverResultSet == null) {
        return null;
    }
    // 
    // add appropriate Function columns to result set
    // 
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    Function[] functions = simSymbolTable.getFunctions();
    for (int i = 0; i < functions.length; i++) {
        if (SimulationSymbolTable.isFunctionSaved(functions[i])) {
            Expression exp1 = new Expression(functions[i].getExpression());
            try {
                exp1 = simSymbolTable.substituteFunctions(exp1);
            } catch (MathException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + functions[i].getName() + " " + e.getMessage());
            } catch (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 (ExpressionException e) {
                e.printStackTrace(System.out);
            }
        }
    }
    if (getSensitivityParameter() != null) {
        try {
            if (odeSolverResultSet.findColumn(getSensitivityParameter().getName()) == -1) {
                FunctionColumnDescription fcd = new FunctionColumnDescription(new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getName(), null, getSensitivityParameter().getName(), false);
                odeSolverResultSet.addFunctionColumn(fcd);
            }
            Variable[] variables = simSymbolTable.getVariables();
            StateVariable[] stateVars = createStateVariables();
            for (int i = 0; i < variables.length; i++) {
                if (variables[i] instanceof Function && SimulationSymbolTable.isFunctionSaved((Function) variables[i])) {
                    Function depSensFunction = (Function) variables[i];
                    Expression depSensFnExpr = new Expression(depSensFunction.getExpression());
                    depSensFnExpr = simSymbolTable.substituteFunctions(depSensFnExpr);
                    depSensFnExpr = getFunctionSensitivity(depSensFnExpr, getSensitivityParameter(), stateVars);
                    // depSensFnExpr = depSensFnExpr.flatten(); 	// already bound and flattened in getFunctionSensitivity, no need here.....
                    String depSensFnName = new String("sens_" + depSensFunction.getName() + "_wrt_" + getSensitivityParameter().getName());
                    if (depSensFunction != null) {
                        FunctionColumnDescription cd = new FunctionColumnDescription(depSensFnExpr.flatten(), depSensFnName, getSensitivityParameter().getName(), depSensFnName, false);
                        odeSolverResultSet.addFunctionColumn(cd);
                    }
                }
            }
        } catch (MathException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        }
    }
    return odeSolverResultSet;
}
Also used : Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 7 with FunctionColumnDescription

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

the class ODESimData method readIn.

/**
 * JMW : This really should be synchronized...
 */
public void readIn(DataInputStream input) throws IOException {
    formatID = input.readUTF();
    if (formatID.equals(SIMPLE_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        // read data from old format file
        double saveInterval = input.readDouble();
        int savedNumber = input.readInt();
        int variableNumber = input.readInt();
        String[] variableNames = new String[variableNumber];
        double[][] dataValues = new double[savedNumber][variableNumber];
        for (int i = 0; i < variableNumber; i++) {
            int flag = input.readInt();
            variableNames[i] = input.readUTF();
            for (int j = 0; j < savedNumber; j++) {
                dataValues[j][i] = input.readDouble();
            }
        }
        // now put data in new data structure
        int rowCount = savedNumber;
        int columnCount = variableNumber + 1;
        addDataColumn(new ODESolverResultSetColumnDescription("t", "t"));
        for (int c = 1; c < columnCount; c++) {
            addDataColumn(new ODESolverResultSetColumnDescription(variableNames[c - 1], variableNames[c - 1]));
        }
        double[] values = new double[columnCount];
        for (int c = 0; c < columnCount; c++) values[c] = 0.0;
        for (int r = 0; r < rowCount; r++) {
            values[0] = r * saveInterval / 1000.0;
            addRow(values);
        }
        for (int c = 1; c < columnCount; c++) {
            for (int r = 0; r < rowCount; r++) {
                setValue(r, c, dataValues[r][c - 1]);
            }
        }
    } else if (formatID.equals(GENERIC_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        int rowCount = input.readInt();
        int columnCount = input.readInt();
        for (int c = 0; c < columnCount; c++) {
            String columnName = input.readUTF();
            String columnDisplayName = input.readUTF();
            addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnDisplayName));
        }
        double[] values = new double[columnCount];
        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < columnCount; c++) {
                values[c] = input.readDouble();
            }
            addRow(values);
        }
    } else if (formatID.equals(COMPACT_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        int rowCount = input.readInt();
        int columnCount = input.readInt();
        for (int c = 0; c < columnCount; c++) {
            String columnName = input.readUTF();
            String columnDisplayName = input.readUTF();
            String columnParameterName = input.readUTF();
            if (columnParameterName.equals("null")) {
                columnParameterName = null;
            }
            addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnParameterName, columnDisplayName));
        }
        double[] values = new double[columnCount];
        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < columnCount; c++) {
                values[c] = input.readDouble();
            }
            addRow(values);
        }
        try {
            int functionCount = input.readInt();
            for (int c = 0; c < functionCount; c++) {
                String columnName = input.readUTF();
                String columnDisplayName = input.readUTF();
                String columnParameterName = input.readUTF();
                if (columnParameterName.equals("null")) {
                    columnParameterName = null;
                }
                String expressionString = input.readUTF();
                try {
                    Expression expression = new Expression(expressionString);
                    addFunctionColumn(new FunctionColumnDescription(expression, columnName, columnParameterName, columnDisplayName, false));
                } catch (ExpressionBindingException e) {
                    e.printStackTrace(System.out);
                    System.out.println("ODESimData.readIn(): unable to bind expression '" + expressionString + "'");
                } catch (ExpressionException e) {
                    e.printStackTrace(System.out);
                    System.out.println("ODESimData.readIn(): unable to parse expression '" + expressionString + "'");
                }
            }
        } catch (EOFException e) {
        }
    } else {
        throw new IOException("DataInputStream is wrong format '" + formatID + "'");
    }
}
Also used : IOException(java.io.IOException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) Expression(cbit.vcell.parser.Expression) EOFException(java.io.EOFException) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 8 with FunctionColumnDescription

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

the class ODESimData method readIDADataFile.

public static ODESimData readIDADataFile(VCDataIdentifier vcdId, File dataFile, int keepMost, File functionsFile) throws DataAccessException {
    // read ida file
    System.out.println("reading ida file : " + dataFile);
    ODESimData odeSimData = new ODESimData();
    odeSimData.formatID = IDA_DATA_FORMAT_ID;
    odeSimData.mathName = vcdId.getID();
    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile)));
        // Read header
        String line = bufferedReader.readLine();
        if (line == null) {
            // throw exception
            return null;
        }
        StringTokenizer st = new StringTokenizer(line, ":");
        while (st.hasMoreTokens()) {
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(st.nextToken()));
        }
        // Read data
        while ((line = bufferedReader.readLine()) != null) {
            st = new StringTokenizer(line);
            double[] values = new double[odeSimData.getDataColumnCount()];
            int count = 0;
            while (st.hasMoreTokens()) {
                values[count++] = Double.valueOf(st.nextToken()).doubleValue();
            }
            if (count == odeSimData.getDataColumnCount()) {
                odeSimData.addRow(values);
            } else {
                break;
            }
        }
    // 
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    } finally {
        try {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    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());
        }
    }
    if (keepMost > 0) {
        odeSimData.trimRows(keepMost);
    }
    return odeSimData;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) 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) StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 9 with FunctionColumnDescription

use of cbit.vcell.math.FunctionColumnDescription 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;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 10 with FunctionColumnDescription

use of cbit.vcell.math.FunctionColumnDescription 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();
}
Also used : SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ExpressionException(cbit.vcell.parser.ExpressionException) DataSource(cbit.vcell.modelopt.DataSource)

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