Search in sources :

Example 1 with NetworkGenerationRequirements

use of cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements in project vcell by virtualcell.

the class StandaloneRuleBasedTest method checkNonspatialStochasticSimContext.

private static void checkNonspatialStochasticSimContext(SimulationContext srcSimContext, File baseDirectory, int numTrials, long bngTimeoutDuration) throws Exception {
    if (!srcSimContext.getApplicationType().equals(Application.NETWORK_STOCHASTIC) || srcSimContext.getGeometry().getDimension() != 0) {
        throw new RuntimeException("simContext is of type " + srcSimContext.getApplicationType() + " and geometry dimension of " + srcSimContext.getGeometry().getDimension() + ", expecting nonspatial stochastic");
    }
    BioModel origBioModel = srcSimContext.getBioModel();
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(XmlHelper.bioModelToXML(origBioModel)));
    bioModel.refreshDependencies();
    // create ODE and RuleBased
    SimulationContext newODEApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewODEApp", false, Application.NETWORK_DETERMINISTIC);
    SimulationContext newRuleBasedApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewRuleBasedApp", false, Application.RULE_BASED_STOCHASTIC);
    newODEApp.setBioModel(bioModel);
    newRuleBasedApp.setBioModel(bioModel);
    ArrayList<AnnotatedFunction> outputFunctionsList = srcSimContext.getOutputFunctionContext().getOutputFunctionsList();
    // OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
    newODEApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    newRuleBasedApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    NetworkGenerationRequirements networkGenRequirements = NetworkGenerationRequirements.getComputeFull(bngTimeoutDuration);
    bioModel.addSimulationContext(newODEApp);
    newODEApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    bioModel.addSimulationContext(newRuleBasedApp);
    newRuleBasedApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    srcSimContext.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    // Create non-spatialStoch, ODE and RuleBased sims
    Simulation nonspatialStochAppNewSim = srcSimContext.addNewSimulation(STOCH_SIM_NAME, /*SimulationOwner.DEFAULT_SIM_NAME_PREFIX*/
    new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    Simulation newODEAppNewSim = newODEApp.addNewSimulation(ODE_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    Simulation newRuleBasedAppNewSim = newRuleBasedApp.addNewSimulation(NFS_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    nonspatialStochAppNewSim.setSimulationOwner(srcSimContext);
    newODEAppNewSim.setSimulationOwner(newODEApp);
    newRuleBasedAppNewSim.setSimulationOwner(newRuleBasedApp);
    try {
        bioModel.getModel().getSpeciesContexts();
        ArrayList<String> varNameList = new ArrayList<String>();
        for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
            varNameList.add(scs.getSpeciesContext().getName());
        }
        String[] varNames = varNameList.toArray(new String[0]);
        OutputTimeSpec outputTimeSpec = nonspatialStochAppNewSim.getSolverTaskDescription().getOutputTimeSpec();
        ArrayList<Double> sampleTimeList = new ArrayList<Double>();
        if (outputTimeSpec instanceof UniformOutputTimeSpec) {
            double endingTime = nonspatialStochAppNewSim.getSolverTaskDescription().getTimeBounds().getEndingTime();
            double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
            int currTimeIndex = 0;
            while (currTimeIndex * dT <= (endingTime + 1e-8)) {
                sampleTimeList.add(currTimeIndex * dT);
                currTimeIndex++;
            }
        }
        double[] sampleTimes = new double[sampleTimeList.size()];
        for (int i = 0; i < sampleTimes.length; i++) {
            sampleTimes[i] = sampleTimeList.get(i);
        }
        TimeSeriesMultitrialData sampleDataStoch1 = new TimeSeriesMultitrialData("stochastic1", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataStoch2 = new TimeSeriesMultitrialData("stochastic2", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataDeterministic = new TimeSeriesMultitrialData("determinstic", varNames, sampleTimes, 1);
        runsolver(nonspatialStochAppNewSim, baseDirectory, numTrials, sampleDataStoch1);
        runsolver(newODEAppNewSim, baseDirectory, 1, sampleDataDeterministic);
        runsolver(newRuleBasedAppNewSim, baseDirectory, numTrials, sampleDataStoch2);
        StochtestFileUtils.writeVarDiffData(new File(baseDirectory, VARDIFF_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeKolmogorovSmirnovTest(new File(baseDirectory, KS_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeChiSquareTest(new File(baseDirectory, ChiSquared_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeData(sampleDataStoch1, new File(baseDirectory, "data." + sampleDataStoch1.datasetName + ".json"));
        StochtestFileUtils.writeData(sampleDataStoch2, new File(baseDirectory, "data." + sampleDataStoch2.datasetName + ".json"));
        StochtestFileUtils.writeData(sampleDataDeterministic, new File(baseDirectory, "data." + sampleDataDeterministic.datasetName + ".json"));
    } finally {
        srcSimContext.removeSimulation(nonspatialStochAppNewSim);
        newODEApp.removeSimulation(newODEAppNewSim);
        newRuleBasedApp.removeSimulation(newRuleBasedAppNewSim);
    }
}
Also used : MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) TimeSeriesMultitrialData(org.vcell.stochtest.TimeSeriesMultitrialData) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements) XMLSource(cbit.vcell.xml.XMLSource) File(java.io.File) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 2 with NetworkGenerationRequirements

use of cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements in project vcell by virtualcell.

the class StochtestRunService method computeTrials.

private void computeTrials(SimulationContext simContext, StochtestRun stochtestRun, File baseDirectory, OutputTimeSpec outputTimeSpec, double endTime, int numTrials) throws PropertyVetoException, IOException {
    // 
    // make simulation
    // 
    MathMappingCallback callback = new MathMappingCallback() {

        @Override
        public void setProgressFraction(float fractionDone) {
        }

        @Override
        public void setMessage(String message) {
        }

        @Override
        public boolean isInterrupted() {
            return false;
        }
    };
    NetworkGenerationRequirements networkGenerationRequirements = NetworkGenerationRequirements.getComputeFull(bngTimeoutMS);
    simContext.refreshMathDescription(callback, networkGenerationRequirements);
    Simulation sim = simContext.addNewSimulation("stochtestrun_" + stochtestRun.key, callback, networkGenerationRequirements);
    sim.setSimulationOwner(simContext);
    // 
    // get variables to save
    // 
    simContext.getModel().getSpeciesContexts();
    ArrayList<String> varNameList = new ArrayList<String>();
    for (SpeciesContextSpec scs : simContext.getReactionContext().getSpeciesContextSpecs()) {
        varNameList.add(scs.getSpeciesContext().getName());
    }
    String[] varNames = varNameList.toArray(new String[0]);
    // 
    // get time points to save
    // 
    ArrayList<Double> sampleTimeList = new ArrayList<Double>();
    if (outputTimeSpec instanceof UniformOutputTimeSpec) {
        double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
        int currTimeIndex = 0;
        while (currTimeIndex * dT <= (endTime + 1e-8)) {
            sampleTimeList.add(currTimeIndex * dT);
            currTimeIndex++;
        }
    }
    double[] sampleTimes = new double[sampleTimeList.size()];
    for (int i = 0; i < sampleTimes.length; i++) {
        sampleTimes[i] = sampleTimeList.get(i);
    }
    // 
    // run N trials and save data
    // 
    TimeSeriesMultitrialData sampleData = new TimeSeriesMultitrialData(sim.getName(), varNames, sampleTimes, numTrials);
    runsolver(sim, baseDirectory, numTrials, sampleData);
    StochtestFileUtils.writeData(sampleData, StochtestFileUtils.getStochtestRunDataFile(baseDir, stochtestRun));
}
Also used : MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) ArrayList(java.util.ArrayList) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements)

Example 3 with NetworkGenerationRequirements

use of cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements in project vcell by virtualcell.

the class RulebasedTransformer method generateNetwork.

private void generateNetwork(SimulationContext simContext, Set<ReactionRule> fromReactions, MathMappingCallback mathMappingCallback) throws ClassNotFoundException, IOException {
    TaskCallbackMessage tcm;
    BNGOutputSpec outputSpec;
    speciesEquivalenceMap.clear();
    kineticsParameterMap.clear();
    NetworkGenerationRequirements networkGenerationRequirements = NetworkGenerationRequirements.ComputeFullNoTimeout;
    String input = convertToBngl(simContext, true, mathMappingCallback, networkGenerationRequirements);
    // System.out.println(input);		// TODO: uncomment to see the xml string
    for (Map.Entry<FakeSeedSpeciesInitialConditionsParameter, Pair<SpeciesContext, Expression>> entry : speciesEquivalenceMap.entrySet()) {
        FakeSeedSpeciesInitialConditionsParameter key = entry.getKey();
        Pair<SpeciesContext, Expression> value = entry.getValue();
        SpeciesContext sc = value.one;
        Expression initial = value.two;
        System.out.println("key: " + key.fakeParameterName + ",   species: " + sc.getName() + ", initial: " + initial.infix());
    }
    BNGInput bngInput = new BNGInput(input);
    BNGOutput bngOutput = null;
    try {
        // for the writeXML command we don't want to run iteration by iteration - it wouldn't even make sense since we don't flatten anything
        // so we run bionetgen the "old" way
        final BNGExecutorService bngService = BNGExecutorService.getInstanceOld(bngInput, networkGenerationRequirements.timeoutDurationMS);
        bngOutput = bngService.executeBNG();
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.out);
        // rethrow without losing context
        throw ex;
    } catch (Exception ex) {
        ex.printStackTrace(System.out);
        throw new RuntimeException(ex.getMessage());
    }
    simContext.setInsufficientIterations(false);
    simContext.setInsufficientMaxMolecules(false);
    String bngConsoleString = bngOutput.getConsoleOutput();
    tcm = new TaskCallbackMessage(TaskCallbackStatus.DetailBatch, bngConsoleString);
    // simContext.appendToConsole(tcm);
    // String bngNetString = bngOutput.getNetFileContent();
    // outputSpec = BNGOutputFileParser.createBngOutputSpec(bngNetString);
    // //BNGOutputFileParser.printBNGNetOutput(outputSpec);			// prints all output to console
    // 
    // if (mathMappingCallback.isInterrupted()){
    // String msg = "Canceled by user.";
    // //			tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, msg);
    // //			simContext.appendToConsole(tcm);
    // //			simContext.setMd5hash(null);					// clean the cache if the user interrupts
    // throw new UserCancelException(msg);
    // }
    // if(outputSpec.getBNGSpecies().length > SimulationConsolePanel.speciesLimit) {
    // String message = SimulationConsolePanel.getSpeciesLimitExceededMessage(outputSpec);
    // //			tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, message);
    // //			simContext.appendToConsole(tcm);
    // //			simContext.setMd5hash(null);
    // throw new RuntimeException(message);
    // }
    // if(outputSpec.getBNGReactions().length > SimulationConsolePanel.reactionsLimit) {
    // String message = SimulationConsolePanel.getReactionsLimitExceededMessage(outputSpec);
    // //			tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, message);
    // //			simContext.appendToConsole(tcm);
    // //			simContext.setMd5hash(null);
    // throw new RuntimeException(message);
    // }
    // TODO: uncomment here to parse the xml file!!!
    parseBngOutput(simContext, fromReactions, bngOutput);
// 
// Saving the observables, as produced by bionetgen
// in debug configurations add to command line   -Ddebug.user=danv
// 
// String debugUser = PropertyLoader.getProperty("debug.user", "not_defined");
// if (debugUser.equals("danv") || debugUser.equals("mblinov")){
// System.out.println("Saving their observables");
// parseObservablesBngOutput(simContext, bngOutput);
// }
// compareOutputs(simContext);
}
Also used : SpeciesContext(cbit.vcell.model.SpeciesContext) BNGExecutorService(cbit.vcell.server.bionetgen.BNGExecutorService) BNGOutput(cbit.vcell.server.bionetgen.BNGOutput) FakeSeedSpeciesInitialConditionsParameter(org.vcell.model.rbm.FakeSeedSpeciesInitialConditionsParameter) BNGOutputSpec(cbit.vcell.bionetgen.BNGOutputSpec) PropertyVetoException(java.beans.PropertyVetoException) ModelException(cbit.vcell.model.ModelException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) Expression(cbit.vcell.parser.Expression) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements) BNGInput(cbit.vcell.server.bionetgen.BNGInput) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Pair(org.vcell.util.Pair)

Example 4 with NetworkGenerationRequirements

use of cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements in project vcell by virtualcell.

the class RuleBasedTest method checkNonspatialStochasticSimContext.

private static void checkNonspatialStochasticSimContext(SimulationContext srcSimContext, File baseDirectory, int numTrials) throws Exception {
    if (!srcSimContext.getApplicationType().equals(Application.NETWORK_STOCHASTIC) || srcSimContext.getGeometry().getDimension() != 0) {
        throw new RuntimeException("simContext is of type " + srcSimContext.getApplicationType() + " and geometry dimension of " + srcSimContext.getGeometry().getDimension() + ", expecting nonspatial stochastic");
    }
    BioModel origBioModel = srcSimContext.getBioModel();
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(XmlHelper.bioModelToXML(origBioModel)));
    bioModel.refreshDependencies();
    // create ODE and RuleBased
    SimulationContext newODEApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewODEApp", false, Application.NETWORK_DETERMINISTIC);
    SimulationContext newRuleBasedApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewRuleBasedApp", false, Application.RULE_BASED_STOCHASTIC);
    newODEApp.setBioModel(bioModel);
    newRuleBasedApp.setBioModel(bioModel);
    ArrayList<AnnotatedFunction> outputFunctionsList = srcSimContext.getOutputFunctionContext().getOutputFunctionsList();
    // OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
    newODEApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    newRuleBasedApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    NetworkGenerationRequirements networkGenerationRequirements = NetworkGenerationRequirements.AllowTruncatedStandardTimeout;
    bioModel.addSimulationContext(newODEApp);
    newODEApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    bioModel.addSimulationContext(newRuleBasedApp);
    newRuleBasedApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    srcSimContext.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    // Create non-spatialStoch, ODE and RuleBased sims
    Simulation nonspatialStochAppNewSim = srcSimContext.addNewSimulation(STOCH_SIM_NAME, /*SimulationOwner.DEFAULT_SIM_NAME_PREFIX*/
    new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    Simulation newODEAppNewSim = newODEApp.addNewSimulation(ODE_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    Simulation newRuleBasedAppNewSim = newRuleBasedApp.addNewSimulation(NFS_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
    nonspatialStochAppNewSim.setSimulationOwner(srcSimContext);
    newODEAppNewSim.setSimulationOwner(newODEApp);
    newRuleBasedAppNewSim.setSimulationOwner(newRuleBasedApp);
    try {
        bioModel.getModel().getSpeciesContexts();
        ArrayList<String> varNameList = new ArrayList<String>();
        for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
            varNameList.add(scs.getSpeciesContext().getName());
        }
        String[] varNames = varNameList.toArray(new String[0]);
        OutputTimeSpec outputTimeSpec = nonspatialStochAppNewSim.getSolverTaskDescription().getOutputTimeSpec();
        ArrayList<Double> sampleTimeList = new ArrayList<Double>();
        if (outputTimeSpec instanceof UniformOutputTimeSpec) {
            double endingTime = nonspatialStochAppNewSim.getSolverTaskDescription().getTimeBounds().getEndingTime();
            double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
            int currTimeIndex = 0;
            while (currTimeIndex * dT <= (endingTime + 1e-8)) {
                sampleTimeList.add(currTimeIndex * dT);
                currTimeIndex++;
            }
        }
        double[] sampleTimes = new double[sampleTimeList.size()];
        for (int i = 0; i < sampleTimes.length; i++) {
            sampleTimes[i] = sampleTimeList.get(i);
        }
        TimeSeriesMultitrialData sampleDataStoch1 = new TimeSeriesMultitrialData("stochastic1", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataStoch2 = new TimeSeriesMultitrialData("stochastic2", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataDeterministic = new TimeSeriesMultitrialData("determinstic", varNames, sampleTimes, 1);
        runsolver(nonspatialStochAppNewSim, baseDirectory, numTrials, sampleDataStoch1);
        runsolver(newODEAppNewSim, baseDirectory, 1, sampleDataDeterministic);
        runsolver(newRuleBasedAppNewSim, baseDirectory, numTrials, sampleDataStoch2);
        writeVarDiffData(baseDirectory, sampleDataStoch1, sampleDataStoch2);
        writeKolmogorovSmirnovTest(baseDirectory, sampleDataStoch1, sampleDataStoch2);
        writeChiSquareTest(baseDirectory, sampleDataStoch1, sampleDataStoch2);
        writeData(baseDirectory, sampleDataStoch1);
        writeData(baseDirectory, sampleDataStoch2);
        writeData(baseDirectory, sampleDataDeterministic);
    } finally {
        srcSimContext.removeSimulation(nonspatialStochAppNewSim);
        newODEApp.removeSimulation(newODEAppNewSim);
        newRuleBasedApp.removeSimulation(newRuleBasedAppNewSim);
    }
}
Also used : MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements) XMLSource(cbit.vcell.xml.XMLSource) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Aggregations

NetworkGenerationRequirements (cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements)4 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)3 Simulation (cbit.vcell.solver.Simulation)3 TempSimulation (cbit.vcell.solver.TempSimulation)3 UniformOutputTimeSpec (cbit.vcell.solver.UniformOutputTimeSpec)3 ArrayList (java.util.ArrayList)3 BioModel (cbit.vcell.biomodel.BioModel)2 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)2 SimulationContext (cbit.vcell.mapping.SimulationContext)2 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)2 OutputTimeSpec (cbit.vcell.solver.OutputTimeSpec)2 XMLSource (cbit.vcell.xml.XMLSource)2 BNGOutputSpec (cbit.vcell.bionetgen.BNGOutputSpec)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 ModelException (cbit.vcell.model.ModelException)1 SpeciesContext (cbit.vcell.model.SpeciesContext)1 Expression (cbit.vcell.parser.Expression)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 BNGExecutorService (cbit.vcell.server.bionetgen.BNGExecutorService)1 BNGInput (cbit.vcell.server.bionetgen.BNGInput)1