How to use gillespy2 - 10 common examples

To help you get started, we’ve selected a few gillespy2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StochSS / stochss / stochss-pkg / Tests / test_convert_sbml.py View on Github external
def test_sbml_to_gillespy_success_with_stochss_model(self):
        sbml_file = "stochss-pkg/tests/mock_file_sys/sbml_files/test1.sbml"
        gillespy2_model, errors = convert_to_gillespy_model(sbml_file)
        self.assertIsInstance(gillespy2_model, Model)
        self.assertIsInstance(errors, list)
github StochSS / stochss / stochss / handlers / util / run_workflow.py View on Github external
Attributes
    ----------
    wkfl : StochSS Workflow
        The workflow object being worked with.
    verbose : boolean
        Print progress statements.
    '''
    is_ode = wkfl.settings['simulationSettings']['algorithm'] == "ODE"
    # Get the model data from the file and create the model object
    gillespy2_model, stochss_model = get_models(wkfl.wkfl_mdl_path, wkfl.mdl_file.split('.')[0], wkfl.wkfl_path, is_ode)
    # run the workflow
    try:
        wkfl.run(gillespy2_model, verbose)
    except Exception as error:
        # update workflow status to error if GillesPy2 throws an exception
        log.error("Workflow errors: {0}".format(error))
        open(os.path.join(wkfl.wkfl_path, 'ERROR'), 'w').close()
    else:
        open(os.path.join(wkfl.wkfl_path, 'COMPLETE'), 'w').close() # update status to complete
github StochSS / stochss / stochss / handlers / util / run_model.py View on Github external
def build_function_definitions(self, args):
        '''
        Build a GillesPy2 function definition.

        Attributes
        ----------
        args : dict
            A json representation of a function definition.
        '''
        name = args['name']
        variables = args['variables'].split(', ')
        expression = args['expression']

        return FunctionDefinition(name=name, args=variables, function=expression)
github StochSS / stochss / stochss / handlers / util / run_workflow.py View on Github external
Attributes
    ----------
    wkfl : StochSS Workflow
        The workflow object being worked with.
    wkfl_type : str
        The type of workflow to be used.
    is_new : boolean
        Represents whether the workflow is new or not.
    '''
    old_model_path = os.path.join(wkfl.wkfl_path, wkfl.mdl_file) # path to the old model
    os.remove(old_model_path) # remove the old model
    try:
        copyfile(wkfl.mdl_path, wkfl.wkfl_mdl_path) # copy the new model into the workflow directory
    except FileNotFoundError as error:
        log.error("Failed to copy the model into the directory: {0}".format(error))
    # Update the workflow info file
    update_info_file(wkfl, wkfl_type, initialize)
    # Update workflow settings file
    wkfl.save()
    if initialize:
        # Update workflow status to running
        open(os.path.join(wkfl.wkfl_path, 'RUNNING'), 'w').close()
    return "Successfully saved the existing workflow: {0}".format(wkfl.wkfl_path)
github StochSS / stochss / stochss / handlers / util / run_model.py View on Github external
def get_models(full_path, name):
    try:
        with open(full_path, "r") as model_file:
            stochss_model = json.loads(model_file.read())
            stochss_model['name'] = name
            is_ode = stochss_model['defaultMode'] == "continuous"
    except FileNotFoundError as error:
        print(str(error))
        log.critical("Failed to find the model file: {0}".format(error))

    try:
        _model = ModelFactory(stochss_model, is_ode) # build GillesPy2 model
        gillespy2_model = _model.model
    except Exception as error:
        print(str(error))
        log.error(str(error))
        gillespy2_model = None

    return gillespy2_model, stochss_model
github StochSS / stochss / stochss / handlers / util / run_workflow.py View on Github external
def get_models(full_path, name, wkfl_path, is_ode):
    try:
        with open(full_path, "r") as model_file:
            stochss_model = json.loads(model_file.read())
            stochss_model['name'] = name
    except FileNotFoundError as error:
        log.critical("Failed to copy the model into the directory: {0}".format(error))
        open(os.path.join(wkfl_path, 'ERROR'), 'w').close() # update status to error
    
    try:
        _model = ModelFactory(stochss_model, is_ode) # build GillesPy2 model
        gillespy2_model = _model.model
    except Exception as error:
        log.error("GillesPy2 Model Errors: "+str(error))
        gillespy2_model = None
    
    return gillespy2_model, stochss_model
github StochSS / stochss / stochss / handlers / util / run_workflow.py View on Github external
used for it.

    Attributes
    ----------
    wkfl : StochSS Workflow
        The workflow object being worked with.
    wkfl_type : str
        The type of workflow to be used.
    is_new : boolean
        Represents whether the workflow is new or not.
    '''
    os.mkdir(wkfl.res_path) # make the workflow's result directory
    try:
        copyfile(wkfl.mdl_path, wkfl.wkfl_mdl_path) # copy the model into the workflow directory
    except FileNotFoundError as error:
        log.error("Failed to copy the model into the directory: {0}".format(error))
    # Update the workflow info file
    update_info_file(wkfl, wkfl_type, initialize)
    # Update workflow settings file
    wkfl.save()
    if initialize:
        # Update workflow status to running
        open(os.path.join(wkfl.wkfl_path, 'RUNNING'), 'w').close()
    return "Successfully saved the new workflow: {0}".format(wkfl.wkfl_path)
github StochSS / stochss / stochss / handlers / util / run_workflow.py View on Github external
def setup_logger(log_path):
    '''
    Changer the GillesPy2 logger to record only error level logs and higher
    to the console and to log warning level logs and higher to a log file in
    the workflow directory.

    Attributes
    ----------
    workflow_path : str
        Path to the workflow directory
    '''
    formatter = log.handlers[0].formatter # gillespy2 log formatter
    fh_is_needed = True
    for handler in log.handlers:
        if type(handler) is logging.StreamHandler:
            handler.stream = sys.stderr # Reset the stream to stderr
            handler.setLevel(logging.ERROR) # Only log error and critical logs to console 
        elif type(handler) is logging.FileHandler:
            fh_is_needed = False # File Handler was already added to the log
    # Add the file handler if it not in the log already
    if fh_is_needed:
        fh = logging.FileHandler(log_path) # initialize file handler
        fh.setLevel(logging.WARNING) # log warning, error, and critical logs to file
        fh.setFormatter(formatter) # add gillespy2 log formatter
        log.addHandler(fh) # add file handler to log
github StochSS / stochss / stochss / handlers / util / run_model.py View on Github external
def get_models(full_path, name):
    try:
        with open(full_path, "r") as model_file:
            stochss_model = json.loads(model_file.read())
            stochss_model['name'] = name
            is_ode = stochss_model['defaultMode'] == "continuous"
    except FileNotFoundError as error:
        print(str(error))
        log.critical("Failed to find the model file: {0}".format(error))

    try:
        _model = ModelFactory(stochss_model, is_ode) # build GillesPy2 model
        gillespy2_model = _model.model
    except Exception as error:
        print(str(error))
        log.error(str(error))
        gillespy2_model = None

    return gillespy2_model, stochss_model
github StochSS / stochss / stochss / handlers / util / run_model.py View on Github external
#!/usr/bin/env python3

import os
import sys
import json
import argparse
import logging
import pickle
import plotly

from io import StringIO
from gillespy2.core import log
log_stream = StringIO()
for handler in log.handlers:
    if type(handler) is logging.StreamHandler:
        handler.stream = log_stream


from gillespy2 import Species, Parameter, Reaction, RateRule, Model, AssignmentRule, FunctionDefinition

import numpy
import gillespy2.core.gillespySolver
from gillespy2.core.events import EventAssignment, EventTrigger, Event
from gillespy2.core.gillespyError import ModelError, SolverError, DirectoryError, BuildError, ExecutionError
from gillespy2.solvers.numpy.basic_tau_leaping_solver import BasicTauLeapingSolver
from gillespy2.solvers.numpy.basic_tau_hybrid_solver import BasicTauHybridSolver
from gillespy2.solvers.cpp.variable_ssa_c_solver import VariableSSACSolver

import warnings
warnings.simplefilter("ignore")