Search in sources :

Example 1 with DataIndexHelper

use of cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.DataIndexHelper in project vcell by virtualcell.

the class DataSetControllerImpl method getDataProcessingOutput.

public static DataOperationResults getDataProcessingOutput(DataOperation dataOperation, File dataProcessingOutputFileHDF5) throws Exception {
    DataOperationResults dataProcessingOutputResults = null;
    FileFormat hdf5FileFormat = null;
    try {
        if (dataProcessingOutputFileHDF5.exists()) {
            // retrieve an instance of H5File
            FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
            if (fileFormat == null) {
                throw new Exception("Cannot find HDF5 FileFormat.");
            }
            // open the file with read-only access
            hdf5FileFormat = fileFormat.open(dataProcessingOutputFileHDF5.getAbsolutePath(), FileFormat.READ);
            hdf5FileFormat.setMaxMembers(Simulation.MAX_LIMIT_SPATIAL_TIMEPOINTS);
            // open the file and retrieve the file structure
            hdf5FileFormat.open();
            Group root = (Group) ((javax.swing.tree.DefaultMutableTreeNode) hdf5FileFormat.getRootNode()).getUserObject();
            if (dataOperation instanceof DataProcessingOutputInfoOP) {
                DataProcessingHelper dataProcessingHelper = new DataProcessingHelper();
                iterateHDF5(root, "", dataProcessingHelper);
                dataProcessingOutputResults = new DataOperationResults.DataProcessingOutputInfo(dataOperation.getVCDataIdentifier(), dataProcessingHelper.getVarNames(), dataProcessingHelper.getVarISizes(), dataProcessingHelper.times, dataProcessingHelper.getVarUnits(), dataProcessingHelper.getPostProcessDataTypes(), dataProcessingHelper.getVarOrigins(), dataProcessingHelper.getVarExtents(), dataProcessingHelper.getVarStatValues());
                // map function names to PostProcess state variable name
                ArrayList<String> postProcessImageVarNames = new ArrayList<String>();
                for (int i = 0; i < ((DataOperationResults.DataProcessingOutputInfo) dataProcessingOutputResults).getVariableNames().length; i++) {
                    String variableName = ((DataOperationResults.DataProcessingOutputInfo) dataProcessingOutputResults).getVariableNames()[i];
                    if (((DataOperationResults.DataProcessingOutputInfo) dataProcessingOutputResults).getPostProcessDataType(variableName).equals(DataOperationResults.DataProcessingOutputInfo.PostProcessDataType.image)) {
                        postProcessImageVarNames.add(variableName);
                    }
                }
                HashMap<String, String> mapFunctionNameToStateVarName = null;
                if (((DataProcessingOutputInfoOP) dataOperation).getOutputContext() != null) {
                    mapFunctionNameToStateVarName = new HashMap<String, String>();
                    for (int i = 0; i < ((DataProcessingOutputInfoOP) dataOperation).getOutputContext().getOutputFunctions().length; i++) {
                        AnnotatedFunction annotatedFunction = ((DataProcessingOutputInfoOP) dataOperation).getOutputContext().getOutputFunctions()[i];
                        if (annotatedFunction.getFunctionType().equals(VariableType.POSTPROCESSING)) {
                            String[] symbols = annotatedFunction.getExpression().flatten().getSymbols();
                            // Find any PostProcess state var that matches a symbol in the function
                            for (int j = 0; j < symbols.length; j++) {
                                if (postProcessImageVarNames.contains(symbols[j])) {
                                    mapFunctionNameToStateVarName.put(annotatedFunction.getName(), symbols[j]);
                                    break;
                                }
                            }
                        }
                    }
                }
                if (mapFunctionNameToStateVarName != null && mapFunctionNameToStateVarName.size() > 0) {
                    dataProcessingOutputResults = new DataOperationResults.DataProcessingOutputInfo(((DataOperationResults.DataProcessingOutputInfo) dataProcessingOutputResults), mapFunctionNameToStateVarName);
                }
            } else {
                OutputContext outputContext = dataOperation.getOutputContext();
                String[] variableNames = null;
                DataIndexHelper dataIndexHelper = null;
                TimePointHelper timePointHelper = null;
                if (dataOperation instanceof DataOperation.DataProcessingOutputDataValuesOP) {
                    variableNames = new String[] { ((DataOperation.DataProcessingOutputDataValuesOP) dataOperation).getVariableName() };
                    dataIndexHelper = ((DataOperation.DataProcessingOutputDataValuesOP) dataOperation).getDataIndexHelper();
                    timePointHelper = ((DataOperation.DataProcessingOutputDataValuesOP) dataOperation).getTimePointHelper();
                } else if (dataOperation instanceof DataOperation.DataProcessingOutputTimeSeriesOP) {
                    variableNames = ((DataOperation.DataProcessingOutputTimeSeriesOP) dataOperation).getTimeSeriesJobSpec().getVariableNames();
                    TimeSeriesJobSpec timeSeriesJobSpec = ((DataOperation.DataProcessingOutputTimeSeriesOP) dataOperation).getTimeSeriesJobSpec();
                    double[] specificTimepoints = extractTimeRange(((DataOperation.DataProcessingOutputTimeSeriesOP) dataOperation).getAllDatasetTimes(), timeSeriesJobSpec.getStartTime(), timeSeriesJobSpec.getEndTime());
                    timePointHelper = TimePointHelper.createSpecificTimePointHelper(specificTimepoints);
                    timeSeriesJobSpec.initIndices();
                    dataIndexHelper = DataIndexHelper.createSpecificDataIndexHelper(timeSeriesJobSpec.getIndices()[0]);
                } else {
                    throw new Exception("Unknown Dataoperation " + dataOperation.getClass().getName());
                }
                if (variableNames.length != 1) {
                    throw new Exception("Only 1 variable request at a time");
                }
                AnnotatedFunction[] annotatedFunctions = (outputContext == null ? null : outputContext.getOutputFunctions());
                AnnotatedFunction foundFunction = null;
                if (annotatedFunctions != null) {
                    for (int i = 0; i < annotatedFunctions.length; i++) {
                        if (annotatedFunctions[i].getName().equals(variableNames[0])) {
                            foundFunction = annotatedFunctions[i];
                            break;
                        }
                    }
                }
                double[] alltimes = null;
                if (foundFunction != null) {
                    DataOperationResults.DataProcessingOutputInfo dataProcessingOutputInfo = (DataOperationResults.DataProcessingOutputInfo) getDataProcessingOutput(new DataOperation.DataProcessingOutputInfoOP(dataOperation.getVCDataIdentifier(), false, dataOperation.getOutputContext()), dataProcessingOutputFileHDF5);
                    alltimes = dataProcessingOutputInfo.getVariableTimePoints();
                    FunctionHelper functionHelper = getPostProcessStateVariables(foundFunction, dataProcessingOutputInfo);
                    DataProcessingHelper dataProcessingHelper = new DataProcessingHelper(functionHelper.postProcessStateVars, timePointHelper, dataIndexHelper);
                    iterateHDF5(root, "", dataProcessingHelper);
                    dataProcessingOutputResults = evaluatePostProcessFunction(dataProcessingOutputInfo, functionHelper.postProcessStateVars, dataProcessingHelper.specificDataValues, dataIndexHelper, timePointHelper, functionHelper.flattenedBoundExpression, variableNames[0]);
                } else {
                    DataProcessingHelper dataProcessingHelper = new DataProcessingHelper(new String[] { variableNames[0] }, timePointHelper, dataIndexHelper);
                    iterateHDF5(root, "", dataProcessingHelper);
                    alltimes = dataProcessingHelper.times;
                    if (dataProcessingHelper.specificDataValues == null) {
                        throw new Exception("Couldn't find postprocess data as specified for var=" + variableNames[0]);
                    }
                    dataProcessingOutputResults = new DataOperationResults.DataProcessingOutputDataValues(dataOperation.getVCDataIdentifier(), variableNames[0], timePointHelper, dataIndexHelper, dataProcessingHelper.specificDataValues[0]);
                }
                if (dataOperation instanceof DataOperation.DataProcessingOutputTimeSeriesOP) {
                    TimeSeriesJobResults timeSeriesJobResults = null;
                    DataProcessingOutputTimeSeriesOP dataProcessingOutputTimeSeriesOP = (DataOperation.DataProcessingOutputTimeSeriesOP) dataOperation;
                    // [time][data]
                    double[][] dataValues = ((DataOperationResults.DataProcessingOutputDataValues) dataProcessingOutputResults).getDataValues();
                    double[] desiredTimes = (timePointHelper.isAllTimePoints() ? alltimes : timePointHelper.getTimePoints());
                    double[][][] timeSeriesFormatedValuesArr = new double[variableNames.length][dataIndexHelper.getDataIndexes().length + 1][desiredTimes.length];
                    for (int i = 0; i < timeSeriesFormatedValuesArr.length; i++) {
                        // var
                        for (int j = 0; j < timeSeriesFormatedValuesArr[i].length; j++) {
                            // index
                            if (j == 0) {
                                timeSeriesFormatedValuesArr[i][j] = desiredTimes;
                                continue;
                            }
                            for (int k = 0; k < timeSeriesFormatedValuesArr[i][j].length; k++) {
                                // time
                                // assume 1 variable for now
                                timeSeriesFormatedValuesArr[i][j][k] = dataValues[k][j - 1];
                            }
                        }
                    }
                    if (dataProcessingOutputTimeSeriesOP.getTimeSeriesJobSpec().isCalcSpaceStats()) {
                        SpatialStatsInfo spatialStatsInfo = new SpatialStatsInfo();
                        spatialStatsInfo.bWeightsValid = false;
                        timeSeriesJobResults = calculateStatisticsFromWhole(dataProcessingOutputTimeSeriesOP.getTimeSeriesJobSpec(), timeSeriesFormatedValuesArr, timePointHelper.getTimePoints(), spatialStatsInfo);
                    } else {
                        timeSeriesJobResults = new TSJobResultsNoStats(variableNames, new int[][] { dataIndexHelper.getDataIndexes() }, timePointHelper.getTimePoints(), timeSeriesFormatedValuesArr);
                    }
                    dataProcessingOutputResults = new DataOperationResults.DataProcessingOutputTimeSeriesValues(dataOperation.getVCDataIdentifier(), timeSeriesJobResults);
                }
            }
        } else {
            throw new FileNotFoundException("Data Processing Output file '" + dataProcessingOutputFileHDF5.getPath() + "' not found");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (hdf5FileFormat != null) {
            try {
                hdf5FileFormat.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return dataProcessingOutputResults;
}
Also used : Group(ncsa.hdf.object.Group) TimeSeriesJobSpec(org.vcell.util.document.TimeSeriesJobSpec) ArrayList(java.util.ArrayList) DataProcessingOutputDataValues(cbit.vcell.simdata.DataOperationResults.DataProcessingOutputDataValues) FileNotFoundException(java.io.FileNotFoundException) FileFormat(ncsa.hdf.object.FileFormat) DataProcessingOutputInfoOP(cbit.vcell.simdata.DataOperation.DataProcessingOutputInfoOP) DataIndexHelper(cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.DataIndexHelper) DataProcessingOutputTimeSeriesOP(cbit.vcell.simdata.DataOperation.DataProcessingOutputTimeSeriesOP) TimeSeriesJobResults(org.vcell.util.document.TimeSeriesJobResults) TimePointHelper(cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.TimePointHelper) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) DivideByZeroException(cbit.vcell.parser.DivideByZeroException) CacheException(org.vcell.util.CacheException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) FileNotFoundException(java.io.FileNotFoundException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) DataProcessingOutputTimeSeriesOP(cbit.vcell.simdata.DataOperation.DataProcessingOutputTimeSeriesOP) DataProcessingOutputDataValues(cbit.vcell.simdata.DataOperationResults.DataProcessingOutputDataValues) TSJobResultsNoStats(org.vcell.util.document.TSJobResultsNoStats)

Aggregations

MathException (cbit.vcell.math.MathException)1 DivideByZeroException (cbit.vcell.parser.DivideByZeroException)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 DataIndexHelper (cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.DataIndexHelper)1 TimePointHelper (cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.TimePointHelper)1 DataProcessingOutputInfoOP (cbit.vcell.simdata.DataOperation.DataProcessingOutputInfoOP)1 DataProcessingOutputTimeSeriesOP (cbit.vcell.simdata.DataOperation.DataProcessingOutputTimeSeriesOP)1 DataProcessingOutputDataValues (cbit.vcell.simdata.DataOperationResults.DataProcessingOutputDataValues)1 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)1 XmlParseException (cbit.vcell.xml.XmlParseException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FileFormat (ncsa.hdf.object.FileFormat)1 Group (ncsa.hdf.object.Group)1 CacheException (org.vcell.util.CacheException)1 DataAccessException (org.vcell.util.DataAccessException)1 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)1 TSJobResultsNoStats (org.vcell.util.document.TSJobResultsNoStats)1