Search in sources :

Example 1 with TimeSeriesJobResults

use of org.vcell.util.document.TimeSeriesJobResults in project vcell by virtualcell.

the class DataSetControllerImpl method getSpecialTimeSeriesValues.

private TimeSeriesJobResults getSpecialTimeSeriesValues(OutputContext outputContext, VCDataIdentifier vcdID, TimeSeriesJobSpec timeSeriesJobSpec, TimeInfo timeInfo) throws Exception {
    String[] variableNames = timeSeriesJobSpec.getVariableNames();
    CartesianMesh mesh = getMesh(vcdID);
    // Automatically 'special' (non-optimized timedata retrieval) if isAllowOptimizedTimeDataRetrieval() == false
    boolean bIsSpecial = !isAllowOptimizedTimeDataRetrieval();
    if (!bIsSpecial) {
        VCData simData = getVCData(vcdID);
        // 
        // Gradient and FieldData functions are special.
        // They have to be evaluated using the 'full data' method using evaluateFunction(...).
        // They cannot be evaluated using the fast findFunctionIndexes(...) method.
        // Also if the 'roi' is a significant fraction of the whole dataset then
        // the 'full data' method of evaluation is more efficient
        // a guess for best efficiency
        final double SIGNIFICANT_ROI_FRACTION = .2;
        final int ABSOLUTE_SIZE_LIMIT = 10000;
        for (int i = 0; i < variableNames.length; i += 1) {
            DataSetIdentifier dsi = (DataSetIdentifier) simData.getEntry(variableNames[i]);
            if (dsi.getVariableType().equals(VariableType.VOLUME)) {
                if (timeSeriesJobSpec.getIndices()[i].length > mesh.getNumVolumeElements() * SIGNIFICANT_ROI_FRACTION) {
                    bIsSpecial = true;
                    break;
                }
            } else if (dsi.getVariableType().equals(VariableType.MEMBRANE)) {
                if (timeSeriesJobSpec.getIndices()[i].length > mesh.getNumMembraneElements() * SIGNIFICANT_ROI_FRACTION) {
                    bIsSpecial = true;
                    break;
                }
            }
            AnnotatedFunction functionFromVarName = getFunction(outputContext, vcdID, variableNames[i]);
            if (functionFromVarName != null) {
                FieldFunctionArguments[] fieldFunctionArgumentsArr = FieldUtilities.getFieldFunctionArguments(functionFromVarName.getExpression());
                if (hasGradient(functionFromVarName.getExpression()) || (fieldFunctionArgumentsArr != null && fieldFunctionArgumentsArr.length > 0)) {
                    bIsSpecial = true;
                    break;
                }
                // check function absolute size limit
                Expression exp = functionFromVarName.getExpression();
                String[] funcSymbols = exp.getSymbols();
                int varCount = 0;
                if (funcSymbols != null) {
                    for (int j = 0; j < funcSymbols.length; j++) {
                        SymbolTableEntry ste = exp.getSymbolBinding(funcSymbols[j]);
                        if (ste instanceof DataSetIdentifier) {
                            varCount += 1;
                        }
                    }
                }
                varCount = Math.max(varCount, 1);
                if (varCount * timeSeriesJobSpec.getIndices()[i].length > ABSOLUTE_SIZE_LIMIT) {
                    bIsSpecial = true;
                    break;
                }
            } else if (timeSeriesJobSpec.getIndices()[i].length > ABSOLUTE_SIZE_LIMIT) {
                bIsSpecial = true;
                break;
            }
        }
    }
    if (!bIsSpecial) {
        return null;
    }
    TimeSeriesJobResults tsjr = null;
    if (timeSeriesJobSpec.isCalcTimeStats()) {
        throw new RuntimeException("Time Stats Not yet implemented for 'special' data");
    } else if (timeSeriesJobSpec.isCalcSpaceStats()) {
        // Get spatial statistics at each time point
        SpatialStatsInfo ssi = calcSpatialStatsInfo(outputContext, timeSeriesJobSpec, vcdID);
        double[][] /*time*/
        argMin = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[][] argMax = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[][] argUnweightedMean = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[][] argWeightedMean = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[][] argUnweightedSum = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[][] argWeightedSum = new double[variableNames.length][timeInfo.desiredTimeValues.length];
        double[] /*varName*/
        argTotalSpace = new double[variableNames.length];
        double[][][] indicesForVarForOneTimepoint = new double[1][][];
        for (int varNameIndex = 0; varNameIndex < variableNames.length; varNameIndex += 1) {
            int[] dataIndices = timeSeriesJobSpec.getIndices()[varNameIndex];
            indicesForVarForOneTimepoint[0] = new double[dataIndices.length + 1][1];
            for (int timeIndex = 0; timeIndex < timeInfo.desiredTimeValues.length; timeIndex += 1) {
                indicesForVarForOneTimepoint[0][0] = new double[] { timeInfo.desiredTimeValues[timeIndex] };
                int num = varNameIndex * timeInfo.desiredTimeValues.length + timeIndex;
                int denom = variableNames.length * timeInfo.desiredTimeValues.length;
                double progressTime = 100.0 * (double) num / (double) denom;
                fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(progressTime), null, null);
                SimDataBlock simDatablock = getSimDataBlock(outputContext, vcdID, variableNames[varNameIndex], timeInfo.desiredTimeValues[timeIndex]);
                double[] data = simDatablock.getData();
                // Put indices in format expected by calcStats (SHOULD BE CHANGED)
                for (int dataIndex = 0; dataIndex < dataIndices.length; dataIndex += 1) {
                    indicesForVarForOneTimepoint[0][dataIndex + 1][0] = data[dataIndices[dataIndex]];
                }
                if (timeSeriesJobSpec.getCrossingMembraneIndices() != null && timeSeriesJobSpec.getCrossingMembraneIndices().length > 0) {
                    adjustMembraneAdjacentVolumeValues(outputContext, indicesForVarForOneTimepoint[0], true, simDatablock, timeSeriesJobSpec.getIndices()[varNameIndex], timeSeriesJobSpec.getCrossingMembraneIndices()[varNameIndex], vcdID, variableNames[varNameIndex], mesh, timeInfo);
                }
                tsjr = calculateStatisticsFromWhole(timeSeriesJobSpec, indicesForVarForOneTimepoint, indicesForVarForOneTimepoint[0][0], ssi);
                argMin[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getMinimums()[0][0];
                argMax[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getMaximums()[0][0];
                argUnweightedMean[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getUnweightedMean()[0][0];
                if (((TSJobResultsSpaceStats) tsjr).getWeightedMean() == null) {
                    argWeightedMean = null;
                } else {
                    argWeightedMean[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getWeightedMean()[0][0];
                }
                argUnweightedSum[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getUnweightedSum()[0][0];
                if (((TSJobResultsSpaceStats) tsjr).getWeightedSum() == null) {
                    argWeightedSum = null;
                } else {
                    argWeightedSum[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getWeightedSum()[0][0];
                }
                if (((TSJobResultsSpaceStats) tsjr).getTotalSpace() == null) {
                    argTotalSpace = null;
                } else {
                    argTotalSpace[varNameIndex] = ((TSJobResultsSpaceStats) tsjr).getTotalSpace()[0];
                }
            }
        }
        tsjr = new TSJobResultsSpaceStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), timeInfo.desiredTimeValues, argMin, argMax, argUnweightedMean, argWeightedMean, argUnweightedSum, argWeightedSum, argTotalSpace);
    } else {
        // Get the values for for all the variables and indices
        double[][][] varIndicesTimesArr = new double[variableNames.length][][];
        for (int varNameIndex = 0; varNameIndex < variableNames.length; varNameIndex += 1) {
            int[] dataIndices = timeSeriesJobSpec.getIndices()[varNameIndex];
            varIndicesTimesArr[varNameIndex] = new double[dataIndices.length + 1][timeInfo.desiredTimeValues.length];
            varIndicesTimesArr[varNameIndex][0] = timeInfo.desiredTimeValues;
            for (int timeIndex = 0; timeIndex < timeInfo.desiredTimeValues.length; timeIndex += 1) {
                int num = varNameIndex * timeInfo.desiredTimeValues.length + timeIndex;
                int denom = variableNames.length * timeInfo.desiredTimeValues.length;
                double progressTime = 100.0 * (double) num / (double) denom;
                fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(progressTime), null, null);
                SimDataBlock simDatablock = getSimDataBlock(outputContext, vcdID, variableNames[varNameIndex], timeInfo.desiredTimeValues[timeIndex]);
                double[] data = simDatablock.getData();
                for (int dataIndex = 0; dataIndex < dataIndices.length; dataIndex += 1) {
                    varIndicesTimesArr[varNameIndex][dataIndex + 1][timeIndex] = data[dataIndices[dataIndex]];
                }
                if (timeSeriesJobSpec.getCrossingMembraneIndices() != null && timeSeriesJobSpec.getCrossingMembraneIndices().length > 0) {
                    adjustMembraneAdjacentVolumeValues(outputContext, varIndicesTimesArr[varNameIndex], true, simDatablock, timeSeriesJobSpec.getIndices()[varNameIndex], timeSeriesJobSpec.getCrossingMembraneIndices()[varNameIndex], vcdID, variableNames[varNameIndex], mesh, timeInfo);
                }
            }
        }
        tsjr = new TSJobResultsNoStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), timeInfo.desiredTimeValues, varIndicesTimesArr);
    }
    return tsjr;
}
Also used : FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) TSJobResultsSpaceStats(org.vcell.util.document.TSJobResultsSpaceStats) CartesianMesh(cbit.vcell.solvers.CartesianMesh) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) TimeSeriesJobResults(org.vcell.util.document.TimeSeriesJobResults) TSJobResultsNoStats(org.vcell.util.document.TSJobResultsNoStats) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 2 with TimeSeriesJobResults

use of org.vcell.util.document.TimeSeriesJobResults in project vcell by virtualcell.

the class DataSetControllerImpl method getTimeSeriesValues.

public TimeSeriesJobResults getTimeSeriesValues(OutputContext outputContext, final VCDataIdentifier vcdID, final TimeSeriesJobSpec timeSeriesJobSpec) throws DataAccessException {
    fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_START, vcdID, new Double(0), null, null);
    dataCachingEnabled = false;
    Exception failException = null;
    try {
        TimeSeriesJobResults timeSeriesJobResults = getTimeSeriesValues_private(outputContext, vcdID, timeSeriesJobSpec);
        fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_COMPLETE, vcdID, new Double(100), timeSeriesJobResults, null);
        return timeSeriesJobResults;
    } catch (Exception e) {
        failException = e;
        e.printStackTrace();
        if (e instanceof DataAccessException) {
            throw (DataAccessException) e;
        } else {
            throw new DataAccessException(e.getClass().getName() + "\n" + e.getMessage());
        }
    } finally {
        dataCachingEnabled = true;
        if (failException != null) {
            fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_FAILURE, vcdID, new Double(0), null, failException);
        }
    }
}
Also used : TimeSeriesJobResults(org.vcell.util.document.TimeSeriesJobResults) 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) DataAccessException(org.vcell.util.DataAccessException)

Example 3 with TimeSeriesJobResults

use of org.vcell.util.document.TimeSeriesJobResults 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)

Example 4 with TimeSeriesJobResults

use of org.vcell.util.document.TimeSeriesJobResults in project vcell by virtualcell.

the class DataSetControllerImpl method getTimeSeriesValues_private.

private TimeSeriesJobResults getTimeSeriesValues_private(OutputContext outputContext, final VCDataIdentifier vcdID, final TimeSeriesJobSpec timeSeriesJobSpec) throws DataAccessException {
    double[] dataTimes = null;
    boolean isPostProcessing = false;
    try {
        if (getVCData(vcdID) instanceof SimulationData && ((SimulationData) getVCData(vcdID)).isPostProcessing(outputContext, timeSeriesJobSpec.getVariableNames()[0])) {
            isPostProcessing = true;
            dataTimes = ((SimulationData) getVCData(vcdID)).getDataTimesPostProcess(outputContext);
        }
    } catch (Exception e) {
        // ignore
        e.printStackTrace();
    }
    if (dataTimes == null) {
        dataTimes = getDataSetTimes(vcdID);
    }
    TimeInfo timeInfo = new TimeInfo(vcdID, timeSeriesJobSpec.getStartTime(), timeSeriesJobSpec.getStep(), timeSeriesJobSpec.getEndTime(), dataTimes);
    if (dataTimes.length <= 0) {
        return null;
    }
    boolean[] wantsTheseTimes = new boolean[dataTimes.length];
    double[] desiredTimeValues = null;
    int desiredNumTimes = 0;
    Arrays.fill(wantsTheseTimes, false);
    double[] tempTimes = new double[dataTimes.length];
    int stepCounter = 0;
    for (int i = 0; i < dataTimes.length; i += 1) {
        if (dataTimes[i] > timeSeriesJobSpec.getEndTime()) {
            break;
        }
        if (dataTimes[i] == timeSeriesJobSpec.getStartTime()) {
            tempTimes[desiredNumTimes] = dataTimes[i];
            desiredNumTimes += 1;
            stepCounter = 0;
            wantsTheseTimes[i] = true;
            if (timeSeriesJobSpec.getStep() == 0) {
                break;
            }
        } else if (desiredNumTimes > 0 && stepCounter % timeSeriesJobSpec.getStep() == 0) {
            tempTimes[desiredNumTimes] = dataTimes[i];
            desiredNumTimes += 1;
            wantsTheseTimes[i] = true;
        }
        stepCounter += 1;
    }
    if (desiredNumTimes == 0) {
        throw new IllegalArgumentException("Couldn't find startTime " + timeSeriesJobSpec.getStartTime());
    }
    desiredTimeValues = new double[desiredNumTimes];
    System.arraycopy(tempTimes, 0, desiredTimeValues, 0, desiredNumTimes);
    // Check timeInfo
    if (desiredTimeValues.length != timeInfo.getDesiredTimeValues().length) {
        throw new DataAccessException("timeInfo check failed");
    }
    for (int i = 0; i < desiredTimeValues.length; i++) {
        if (desiredTimeValues[i] != timeInfo.getDesiredTimeValues()[i]) {
            throw new DataAccessException("timeInfo check failed");
        }
    }
    for (int i = 0; i < wantsTheseTimes.length; i++) {
        if (wantsTheseTimes[i] != timeInfo.getWantsTheseTimes()[i]) {
            throw new DataAccessException("timeInfo check failed");
        }
    }
    try {
        timeSeriesJobSpec.initIndices();
        // See if we need special processing
        TimeSeriesJobResults specialTSJR = getSpecialTimeSeriesValues(outputContext, vcdID, timeSeriesJobSpec, timeInfo);
        if (specialTSJR != null) {
            return specialTSJR;
        }
        // 
        VCData vcData = getVCData(vcdID);
        // 
        // Determine Memory Usage for this job to protect server
        // 
        // No TimeSeries jobs larger than this
        final long MAX_MEM_USAGE = 20000000;
        long memUsage = 0;
        // efficient function stats are not yet implemented so check to adjust calculation
        boolean bHasFunctionVars = false;
        for (int i = 0; i < timeSeriesJobSpec.getVariableNames().length; i += 1) {
            bHasFunctionVars = bHasFunctionVars || (getFunction(outputContext, vcdID, timeSeriesJobSpec.getVariableNames()[i]) != null);
        }
        for (int i = 0; i < timeSeriesJobSpec.getIndices().length; i += 1) {
            memUsage += (timeSeriesJobSpec.isCalcSpaceStats() && !bHasFunctionVars ? NUM_STATS : timeSeriesJobSpec.getIndices()[i].length);
        }
        memUsage *= desiredNumTimes * 8 * 2;
        System.out.println("DataSetControllerImpl.getTimeSeriesValues: job memory=" + memUsage);
        if (memUsage > MAX_MEM_USAGE) {
            throw new DataAccessException("DataSetControllerImpl.getTimeSeriesValues: Job too large" + (bHasFunctionVars ? "(has function vars)" : "") + ", requires approx. " + memUsage + " bytes of memory (only " + MAX_MEM_USAGE + " bytes allowed).  Choose fewer datapoints or times.");
        }
        // 
        Vector<double[][]> valuesV = new Vector<double[][]>();
        SpatialStatsInfo spatialStatsInfo = null;
        if (timeSeriesJobSpec.isCalcSpaceStats()) {
            spatialStatsInfo = calcSpatialStatsInfo(outputContext, timeSeriesJobSpec, vcdID);
        }
        final EventRateLimiter eventRateLimiter = new EventRateLimiter();
        for (int k = 0; k < timeSeriesJobSpec.getVariableNames().length; k += 1) {
            double[][] timeSeries = null;
            String varName = timeSeriesJobSpec.getVariableNames()[k];
            int[] indices = timeSeriesJobSpec.getIndices()[k];
            if (timeSeriesJobSpec.isCalcSpaceStats() && !bHasFunctionVars) {
                timeSeries = new double[NUM_STATS + 1][desiredNumTimes];
            } else {
                timeSeries = new double[indices.length + 1][desiredNumTimes];
            }
            timeSeries[0] = desiredTimeValues;
            ProgressListener progressListener = new ProgressListener() {

                public void updateProgress(double progress) {
                    // System.out.println("Considering firing progress event at "+new Date());
                    if (eventRateLimiter.isOkayToFireEventNow()) {
                        // System.out.println("ACTUALLY firing Progress event at "+new Date());
                        fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(progress), null, null);
                    }
                }

                public void updateMessage(String message) {
                // ignore
                }
            };
            AnnotatedFunction function = getFunction(outputContext, vcdID, varName);
            if (function != null) {
                if (vcData instanceof SimulationData) {
                    function = ((SimulationData) vcData).simplifyFunction(function);
                } else {
                    throw new Exception("DataSetControllerImpl::getTimeSeriesValues_private(): has to be SimulationData to get time plot.");
                }
                MultiFunctionIndexes mfi = new MultiFunctionIndexes(vcdID, function, indices, wantsTheseTimes, progressListener, outputContext);
                for (int i = 0; i < desiredTimeValues.length; i++) {
                    fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(NumberUtils.formatNumber(100.0 * (double) (k * desiredTimeValues.length + i) / (double) (timeSeriesJobSpec.getVariableNames().length * desiredTimeValues.length), 3)), null, null);
                    for (int j = 0; j < indices.length; j++) {
                        timeSeries[j + 1][i] = mfi.evaluateTimeFunction(outputContext, i, j);
                    }
                }
            } else {
                double[][][] valuesOverTime = null;
                if (timeSeriesJobSpec.isCalcSpaceStats() && !bHasFunctionVars) {
                    valuesOverTime = vcData.getSimDataTimeSeries(outputContext, new String[] { varName }, new int[][] { indices }, wantsTheseTimes, spatialStatsInfo, progressListener);
                } else {
                    valuesOverTime = vcData.getSimDataTimeSeries(outputContext, new String[] { varName }, new int[][] { indices }, wantsTheseTimes, progressListener);
                }
                for (int i = 0; i < desiredTimeValues.length; i++) {
                    fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(NumberUtils.formatNumber(100.0 * (double) (k * desiredTimeValues.length + i) / (double) (timeSeriesJobSpec.getVariableNames().length * desiredTimeValues.length), 3)), null, null);
                    if (timeSeriesJobSpec.isCalcSpaceStats() && !bHasFunctionVars) {
                        // min
                        timeSeries[MIN_OFFSET + 1][i] = valuesOverTime[i][0][MIN_OFFSET];
                        // max
                        timeSeries[MAX_OFFSET + 1][i] = valuesOverTime[i][0][MAX_OFFSET];
                        // mean
                        timeSeries[MEAN_OFFSET + 1][i] = valuesOverTime[i][0][MEAN_OFFSET];
                        // wmean
                        timeSeries[WMEAN_OFFSET + 1][i] = valuesOverTime[i][0][WMEAN_OFFSET];
                        // sum
                        timeSeries[SUM_OFFSET + 1][i] = valuesOverTime[i][0][SUM_OFFSET];
                        // wsum
                        timeSeries[WSUM_OFFSET + 1][i] = valuesOverTime[i][0][WSUM_OFFSET];
                    } else {
                        for (int j = 0; j < indices.length; j++) {
                            timeSeries[j + 1][i] = valuesOverTime[i][0][j];
                        }
                    }
                }
            }
            valuesV.add(timeSeries);
        }
        if (timeSeriesJobSpec.isCalcSpaceStats() && !bHasFunctionVars) {
            double[][] min = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            double[][] max = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            double[][] mean = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            double[][] wmean = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            double[][] sum = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            double[][] wsum = new double[timeSeriesJobSpec.getVariableNames().length][desiredTimeValues.length];
            for (int i = 0; i < valuesV.size(); i += 1) {
                double[][] timeStat = (double[][]) valuesV.elementAt(i);
                for (int j = 0; j < desiredTimeValues.length; j += 1) {
                    min[i][j] = timeStat[MIN_OFFSET + 1][j];
                    max[i][j] = timeStat[MAX_OFFSET + 1][j];
                    mean[i][j] = timeStat[MEAN_OFFSET + 1][j];
                    wmean[i][j] = timeStat[WMEAN_OFFSET + 1][j];
                    sum[i][j] = timeStat[SUM_OFFSET + 1][j];
                    wsum[i][j] = timeStat[WSUM_OFFSET + 1][j];
                }
            }
            return new TSJobResultsSpaceStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), desiredTimeValues, min, max, mean, (spatialStatsInfo.bWeightsValid ? wmean : null), sum, (spatialStatsInfo.bWeightsValid ? wsum : null), (spatialStatsInfo.bWeightsValid ? spatialStatsInfo.totalSpace : null));
        } else if (timeSeriesJobSpec.isCalcSpaceStats() && bHasFunctionVars) {
            double[][][] timeSeriesFormatedValuesArr = new double[valuesV.size()][][];
            valuesV.copyInto(timeSeriesFormatedValuesArr);
            return calculateStatisticsFromWhole(timeSeriesJobSpec, timeSeriesFormatedValuesArr, desiredTimeValues, spatialStatsInfo);
        } else {
            double[][][] timeSeriesFormatedValuesArr = new double[valuesV.size()][][];
            valuesV.copyInto(timeSeriesFormatedValuesArr);
            TSJobResultsNoStats tsJobResultsNoStats = new TSJobResultsNoStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), desiredTimeValues, timeSeriesFormatedValuesArr);
            if (!isPostProcessing && timeSeriesJobSpec.getCrossingMembraneIndices() != null && timeSeriesJobSpec.getCrossingMembraneIndices().length > 0) {
                adjustMembraneAdjacentVolumeValues(outputContext, tsJobResultsNoStats.getTimesAndValuesForVariable(timeSeriesJobSpec.getVariableNames()[0]), true, null, timeSeriesJobSpec.getIndices()[0], timeSeriesJobSpec.getCrossingMembraneIndices()[0], vcdID, timeSeriesJobSpec.getVariableNames()[0], getMesh(vcdID), timeInfo);
            }
            return tsJobResultsNoStats;
        }
    } catch (DataAccessException e) {
        lg.error(e.getMessage(), e);
        throw e;
    } catch (Throwable e) {
        lg.error(e.getMessage(), e);
        throw new DataAccessException("DataSetControllerImpl.getTimeSeriesValues: " + (e.getMessage() == null ? e.getClass().getName() : e.getMessage()));
    }
}
Also used : TimeSeriesJobResults(org.vcell.util.document.TimeSeriesJobResults) Vector(java.util.Vector) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) EventRateLimiter(cbit.vcell.util.EventRateLimiter) TSJobResultsSpaceStats(org.vcell.util.document.TSJobResultsSpaceStats) 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) TSJobResultsNoStats(org.vcell.util.document.TSJobResultsNoStats)

Aggregations

TimeSeriesJobResults (org.vcell.util.document.TimeSeriesJobResults)4 MathException (cbit.vcell.math.MathException)3 DivideByZeroException (cbit.vcell.parser.DivideByZeroException)3 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)3 ExpressionException (cbit.vcell.parser.ExpressionException)3 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)3 XmlParseException (cbit.vcell.xml.XmlParseException)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 CacheException (org.vcell.util.CacheException)3 DataAccessException (org.vcell.util.DataAccessException)3 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)3 TSJobResultsNoStats (org.vcell.util.document.TSJobResultsNoStats)3 TSJobResultsSpaceStats (org.vcell.util.document.TSJobResultsSpaceStats)2 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)1 Expression (cbit.vcell.parser.Expression)1 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)1 DataIndexHelper (cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.DataIndexHelper)1 TimePointHelper (cbit.vcell.simdata.DataOperation.DataProcessingOutputDataValuesOP.TimePointHelper)1 DataProcessingOutputInfoOP (cbit.vcell.simdata.DataOperation.DataProcessingOutputInfoOP)1