Search in sources :

Example 1 with ASTNumber

use of org.jmathml.ASTNumber in project vcell by virtualcell.

the class SedMLResultsProcesser2 method process.

/**
 * Processes a set of simulation results and processes them according to the
 * output passed into the constructor. There are several reasons that
 * processing may fail, for example:
 * <ul>
 * <li>If cross-references between outputs, variables and data generators
 * are not valid.
 * <li>If mappings between the data column headers in the raw result, and
 * model element identifiers cannot be resolved.
 * <li>If the MathML for the data post-processing is invalid or outside the
 * scope of SED-ML's MathML usage.
 * </ul>
 * Failures such as these will be logged and can be accessed by a call to
 * getProcessReport() after calling this method.
 *
 * @param results
 * @throws IllegalArgumentException
 *             if results is <code>null</code>.
 */
public void process(Map<AbstractTask, IRawSedmlSimulationResults> results) {
    if (results == null) {
        throw new IllegalArgumentException();
    }
    Map<AbstractTask, double[][]> rawTask2Results = new HashMap<AbstractTask, double[][]>();
    int numRows = 0;
    numRows = makeDefensiveCopyOfData(results, rawTask2Results, numRows);
    List<double[]> processed = new ArrayList<double[]>();
    if (output.getAllDataGeneratorReferences().isEmpty()) {
        report.messages.add(new ExecutionStatusElement(null, NO_DG_INOUTPUT_MSG, ExecutionStatusType.ERROR));
        return;
    }
    for (String dgId : output.getAllDataGeneratorReferences()) {
        double[] mutated = new double[numRows];
        processed.add(mutated);
        DataGenerator dg = sedml.getDataGeneratorWithId(dgId);
        if (dg == null) {
            report.messages.add(new ExecutionStatusElement(null, MISSING_DG_MESSAGE + dgId, ExecutionStatusType.ERROR));
            return;
        }
        List<Variable> vars = dg.getListOfVariables();
        List<Parameter> params = dg.getListOfParameters();
        Map<String, String> Var2Model = new HashMap<String, String>();
        Map<String, IRawSedmlSimulationResults> var2Result = new HashMap<String, IRawSedmlSimulationResults>();
        Map<String, double[][]> var2Data = new HashMap<String, double[][]>();
        String timeID = "";
        // map varIds to result, based upon task reference
        for (Variable variable : vars) {
            String modelID;
            if (variable.isVariable()) {
                // get the task from which this result variable was
                // generated.
                modelID = variable2IDResolver.getIdFromXPathIdentifer(variable.getTarget());
                String taskRef = variable.getReference();
                AbstractTask t = sedml.getTaskWithId(taskRef);
                // get results for this task
                IRawSedmlSimulationResults res = results.get(t);
                // set up lookups to results, raw data and model ID
                var2Result.put(variable.getId(), res);
                var2Data.put(variable.getId(), rawTask2Results.get(t));
                Var2Model.put(variable.getId(), modelID);
            // it's a symbol
            } else if (variable.isSymbol() && variable.getSymbol().equals(VariableSymbol.TIME)) {
                timeID = variable.getId();
                var2Data.put(variable.getId(), rawTask2Results.values().iterator().next());
                Var2Model.put(variable.getId(), variable.getId());
            }
        }
        // get Parameter values
        Map<String, Double> Param2Value = new HashMap<String, Double>();
        for (Parameter p : params) {
            Param2Value.put(p.getId(), p.getValue());
        }
        // now parse maths, and replace raw simulation results with
        // processed results.
        ASTNode node = dg.getMath();
        Set<ASTCi> identifiers = node.getIdentifiers();
        for (ASTCi var : identifiers) {
            if (var.isVector()) {
                String varName = var.getName();
                IModel2DataMappings coll = var2Result.get(varName).getMappings();
                int otherVarInx = coll.getColumnIndexFor(Var2Model.get(varName));
                if (otherVarInx < 0 || otherVarInx >= var2Result.get(varName).getNumColumns()) {
                    report.messages.add(new ExecutionStatusElement(null, NO_DATACOLuMN_FORID_MSG + var, ExecutionStatusType.ERROR));
                    return;
                }
                EvaluationContext con = new EvaluationContext();
                Double[] data = var2Result.get(varName).getDataByColumnIndex(otherVarInx);
                con.setValueFor(varName, Arrays.asList(data));
                if (var.getParentNode() == null || var.getParentNode().getParentNode() == null) {
                    report.messages.add(new ExecutionStatusElement(null, "Could not evaluate [" + var + "] as symbol does not have parent element", ExecutionStatusType.ERROR));
                    return;
                }
                if (!var.getParentNode().canEvaluate(con)) {
                    report.messages.add(new ExecutionStatusElement(null, "Could not evaluate [" + var + "] ", ExecutionStatusType.ERROR));
                    return;
                }
                ASTNumber num = var.getParentNode().evaluate(con);
                // replace vector operation with calculated value.
                var.getParentNode().getParentNode().replaceChild(var.getParentNode(), num);
            }
        }
        // identifiers.add(var.getSpId());
        if (identifiersMapToData(identifiers, Var2Model, Param2Value, var2Result, timeID)) {
            for (int i = 0; i < numRows; i++) {
                EvaluationContext con = new EvaluationContext();
                for (String id : Param2Value.keySet()) {
                    con.setValueFor(id, Param2Value.get(id));
                }
                for (ASTCi var : identifiers) {
                    // we've already resolved parameters
                    if (Param2Value.get(var.getName()) != null) {
                        continue;
                    }
                    int otherVarInx = 0;
                    if (!var.getName().equals(timeID)) {
                        IModel2DataMappings coll = var2Result.get(var.getName()).getMappings();
                        otherVarInx = coll.getColumnIndexFor(Var2Model.get(var.getName()));
                        if (otherVarInx < 0 || otherVarInx >= var2Result.get(var.getName()).getNumColumns()) {
                            report.messages.add(new ExecutionStatusElement(null, NO_DATACOLuMN_FORID_MSG + var, ExecutionStatusType.ERROR));
                            return;
                        }
                    }
                    con.setValueFor(var.getName(), var2Data.get(var.getName())[i][otherVarInx]);
                }
                if (node.canEvaluate(con)) {
                    mutated[i] = node.evaluate(con).getValue();
                } else {
                    report.messages.add(new ExecutionStatusElement(null, COULD_NOT_EXECUTE_MATHML_FOR + dgId, ExecutionStatusType.INFO));
                }
            }
        } else {
            report.messages.add(new ExecutionStatusElement(null, COULD_NOT_RESOLVE_MATHML_MSG + dgId, ExecutionStatusType.ERROR));
            return;
        }
    }
    toReturn = createData(processed, numRows);
}
Also used : AbstractTask(org.jlibsedml.AbstractTask) Variable(org.jlibsedml.Variable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ASTCi(org.jmathml.ASTCi) ASTNode(org.jmathml.ASTNode) ASTNumber(org.jmathml.ASTNumber) DataGenerator(org.jlibsedml.DataGenerator) Parameter(org.jlibsedml.Parameter) EvaluationContext(org.jmathml.EvaluationContext)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AbstractTask (org.jlibsedml.AbstractTask)1 DataGenerator (org.jlibsedml.DataGenerator)1 Parameter (org.jlibsedml.Parameter)1 Variable (org.jlibsedml.Variable)1 ASTCi (org.jmathml.ASTCi)1 ASTNode (org.jmathml.ASTNode)1 ASTNumber (org.jmathml.ASTNumber)1 EvaluationContext (org.jmathml.EvaluationContext)1