Ejemplo n.º 1
0
def lattice_experiment(config):
    # configure the experiment
    n_agents = config.get('n_agents')
    emitter = config.get('emitter', {'type': 'timeseries'})

    # make lattice environment
    environment = Lattice(config.get('environment', {}))
    network = environment.generate()
    processes = network['processes']
    topology = network['topology']

    # add the agents
    agent_ids = [str(agent_id) for agent_id in range(n_agents)]
    agent_config = config['agent']
    agent_compartment = agent_config['compartment']
    compartment_config = agent_config['config']
    agent = agent_compartment(compartment_config)
    agents = make_agents(agent_ids, agent, {})
    processes['agents'] = agents['processes']
    topology['agents'] = agents['topology']

    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': emitter,
        'initial_state': config.get('initial_state', {})
    })
Ejemplo n.º 2
0
def mother_machine_experiment(config):
    # configure the experiment
    agent_ids = config.get('agent_ids', [])
    emitter = config.get('emitter', {'type': 'timeseries'})

    # get the environment
    environment = Lattice(config.get('environment', {}))
    network = environment.generate({})
    processes = network['processes']
    topology = network['topology']

    # get the agents
    growth_division = GrowthDivisionMinimal(config.get('growth_division', {}))
    agents = make_agents(agent_ids, growth_division,
                         config.get('growth_division', {}))
    processes['agents'] = agents['processes']
    topology['agents'] = agents['topology']

    # import ipdb;
    # ipdb.set_trace()
    # get location initialize?

    return Experiment({
        'processes': processes,
        'topology': topology,
        'initial_state': config.get('initial_state', {}),
        'emitter': emitter,
    })
Ejemplo n.º 3
0
def process_in_experiment(process, settings={}):
    initial_state = settings.get('initial_state', {})
    emitter = settings.get('emitter', {'type': 'timeseries'})
    emit_step = settings.get('emit_step')
    timeline = settings.get('timeline', [])
    environment = settings.get('environment', {})
    paths = settings.get('topology', {})

    processes = {'process': process}
    topology = {
        'process': {
            port: paths.get(port, (port, ))
            for port in process.ports_schema().keys()
        }
    }

    if timeline:
        # Adding a timeline to a process requires only the timeline
        timeline_process = TimelineProcess({'timeline': timeline['timeline']})
        processes.update({'timeline_process': timeline_process})
        topology.update({
            'timeline_process':
            {port: (port, )
             for port in timeline_process.ports}
        })

    if environment:
        # Environment requires ports for external, fields, dimensions,
        # and global (for location)
        ports = environment.get(
            'ports', {
                'external': ('external', ),
                'fields': ('fields', ),
                'dimensions': ('dimensions', ),
                'global': ('global', ),
            })
        environment_process = NonSpatialEnvironment(environment)
        processes.update({'environment_process': environment_process})
        topology.update({
            'environment_process': {
                'external': ports['external'],
                'fields': ports['fields'],
                'dimensions': ports['dimensions'],
                'global': ports['global'],
            },
        })

    # add derivers
    derivers = generate_derivers(processes, topology)
    processes = deep_merge(processes, derivers['processes'])
    topology = deep_merge(topology, derivers['topology'])

    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': emitter,
        'emit_step': emit_step,
        'initial_state': initial_state
    })
Ejemplo n.º 4
0
def test_experiment(
    initial_biomass=1000,
    total_time=2000,
):
    agent_id = '1'
    parameters = {
        'agent_id': agent_id,
        'inclusion_process': {
            'damage_rate': 1e-4,  # rapid damage
        },
        'growth_rate': {
            'growth_rate': 0.001  # fast growth
        },
    }

    # make the agent
    agent_composer = InclusionBodyGrowth(parameters)
    agent_composite = agent_composer.generate()

    # make the environment
    composer = Lattice(LATTICE_CONFIG)
    composite = composer.generate()

    # merge in the agent
    composite.merge(composite=agent_composite, path=('agents', agent_id))

    # get initial state
    initial_state = composite.initial_state()
    initial_state['agents'][agent_id]['molecules'] = {
        'biomass': initial_biomass
    }

    # make the experiment
    inclusion_experiment = Experiment({
        'processes': composite['processes'],
        'topology': composite['topology'],
        'initial_state': initial_state
    })

    # run the experiment
    inclusion_experiment.update(total_time)

    # get the data
    data = inclusion_experiment.emitter.get_data()

    return data
Ejemplo n.º 5
0
def compartment_in_experiment(compartment, settings={}):
    compartment_config = settings.get('compartment', {})
    timeline = settings.get('timeline')
    environment = settings.get('environment')
    outer_path = settings.get('outer_path', tuple())
    emit_step = settings.get('emit_step')

    network = compartment.generate(compartment_config, outer_path)
    processes = network['processes']
    topology = network['topology']

    if timeline is not None:
        # Environment requires ports for all states defined in the timeline, and a global port
        ports = timeline['ports']
        timeline_process = TimelineProcess({'timeline': timeline['timeline']})
        processes.update({'timeline_process': timeline_process})
        if 'global' not in ports:
            ports['global'] = ('global', )
        topology.update(
            {'timeline_process': {port: path
                                  for port, path in ports.items()}})

    if environment is not None:
        # Environment requires ports for external, fields, dimensions,
        # and global (for location)
        ports = environment.get(
            'ports', {
                'external': ('external', ),
                'fields': ('fields', ),
                'dimensions': ('dimensions', ),
                'global': ('global', ),
            })
        environment_process = NonSpatialEnvironment(environment)
        processes.update({'environment_process': environment_process})
        topology.update({
            'environment_process': {
                'external': ports['external'],
                'fields': ports['fields'],
                'dimensions': ports['dimensions'],
                'global': ports['global'],
            },
        })

    return Experiment({
        'processes':
        processes,
        'topology':
        topology,
        'emitter':
        settings.get('emitter', {'type': 'timeseries'}),
        'emit_step':
        emit_step,
        'initial_state':
        settings.get('initial_state', {})
    })
Ejemplo n.º 6
0
def make_experiment_from_compartment_dicts(
    environment_dict, agents_dict, emitter_dict, initial_state
):
    # environment_dict comes from environment.generate()
    # agents_dict comes from make_agents
    processes = environment_dict['processes']
    topology = environment_dict['topology']
    processes['agents'] = agents_dict['processes']
    topology['agents'] = agents_dict['topology']
    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': emitter_dict,
        'initial_state': initial_state})
Ejemplo n.º 7
0
def test_inclusion_body(
    total_time=1000,
    initial_biomass=1000,
):
    agent_id = '0'
    parameters = {
        'agent_id': agent_id,
        'inclusion_process': {
            'damage_rate': 1e-4,  # rapid damage
        },
        'growth_rate': {
            'growth_rate': 0.001  # fast growth
        },
    }
    composer = InclusionBodyGrowth(parameters)
    composite = composer.generate(path=('agents', agent_id))

    initial_state = composite.initial_state()
    initial_state['agents'][agent_id]['molecules'] = {
        'biomass': initial_biomass
    }

    # make the experiment
    inclusion_experiment = Experiment({
        'processes': composite['processes'],
        'topology': composite['topology'],
        'initial_state': initial_state
    })

    # run the experiment
    inclusion_experiment.update(total_time)

    # get the data
    data = inclusion_experiment.emitter.get_data()

    return data
Ejemplo n.º 8
0
def compartment_in_experiment(compartment, settings={}):
    compartment_config = settings.get('compartment', {})
    timeline = settings.get('timeline', {})
    environment = settings.get('environment', {})
    outer_path = settings.get('outer_path', tuple())
    emit_step = settings.get('emit_step')

    network = compartment.generate(compartment_config, outer_path)
    processes = network['processes']
    topology = network['topology']

    if timeline:
        '''
        environment requires ports for all states defined in the timeline
        '''
        ports = timeline['ports']
        timeline_process = TimelineProcess({'timeline': timeline['timeline']})
        processes.update({'timeline_process': timeline_process})
        topology.update({
            'timeline_process': {'global': ('global',)}
        })
        topology['timeline_process'].update({
                port_id: ports[port_id]
                for port_id in timeline_process.ports if port_id is not 'global'})

    if environment:
        '''
        environment requires ports for exchange and external
        '''
        ports = environment.get('ports',
            {'external': ('external',), 'exchange': ('exchange',)})
        environment_process = NonSpatialEnvironment(environment)
        processes.update({'environment_process': environment_process})
        topology.update({
            'environment_process': {
                'external': ports['external'],
                'exchange': ports['exchange']}})

    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': settings.get('emitter', {'type': 'timeseries'}),
        'emit_step': emit_step,
        'initial_state': settings.get('initial_state', {})})
Ejemplo n.º 9
0
def agent_environment_experiment(
        agents_config={},
        environment_config={},
        initial_state={},
        settings={}):
    # experiment settings
    emitter = settings.get('emitter', {'type': 'timeseries'})

    # initialize the agents
    if isinstance(agents_config, dict):
        # dict with single agent config
        agent_type = agents_config['type']
        agent_ids = agents_config['ids']
        agent_compartment = agent_type(agents_config['config'])
        agents = make_agents(agent_ids, agent_compartment, agents_config['config'])
    elif isinstance(agents_config, list):
        # list with multiple agent configurations
        agents = {
            'processes': {},
            'topology': {}}
        for config in agents_config:
            agent_type = config['type']
            agent_ids = config['ids']
            agent_compartment = agent_type(config['config'])
            new_agents = make_agents(agent_ids, agent_compartment, config['config'])
            deep_merge(agents['processes'], new_agents['processes'])
            deep_merge(agents['topology'], new_agents['topology'])

    # initialize the environment
    environment_type = environment_config['type']
    environment_compartment = environment_type(environment_config['config'])

    # combine processes and topologies
    network = environment_compartment.generate({})
    processes = network['processes']
    topology = network['topology']
    processes['agents'] = agents['processes']
    topology['agents'] = agents['topology']
    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': emitter,
        'initial_state': initial_state})
def glucose_phosphorylation_experiment(config=None):
    if config is None:
        config = {}
    default_config = {
        'injected_glc_phosphorylation': {},
        'emitter': {
            'type': 'timeseries',
        },
        'initial_state': {},
    }
    default_config.update(config)
    config = default_config
    compartment = InjectedGlcPhosphorylation(
        config['injected_glc_phosphorylation'])
    compartment_dict = compartment.generate()
    experiment = Experiment({
        'processes': compartment_dict['processes'],
        'topology': compartment_dict['topology'],
        'emitter': config['emitter'],
        'initial_state': config['initial_state'],
    })
    return experiment
def colony_metrics_experiment(config):
    '''Run an experiment to calculate colony metrics

    Arguments:
        config (dict): A dictionary of configuration options which can
            contain the following:

            * **n_agents** (:py:class:`int`): Number of agents to create
              initially
            * **emitter** (:py:class:`dict`): Emitter configuration
              dictionary. This gets passed directly to the
              :py:class:`vivarium.core.experiment.Experiment`
              constructor. Defaults to a timeseries emitter.
            * **environment** (:py:class:`dict`): Configuration to pass
              to the :py:class:`vivarium.compartments.lattice.Lattice`
              constructor. Defaults to ``{}``.
            * **agent** (:py:class:`dict`): Dictionary with the
              following keys-value pairs:

                * **compartment**
                  (:py:class:`vivarium.core.experiment.Compartment`): A
                  compartment class to use for each agent.
                * **config** (:py:class:`dict`): Configuration to pass
                  the agent compartment constructor.

            * **locations** (:py:class:`list`): List of sublists. Each
              sublist should have 2 elements, the coordinates at which
              to place an agent. Coordinates range from 0 to 1 and
              represent fractions of environment bounds. Any agents for
              which no location is specified will be placed randomly per
              the default behavior of
              :py:func:`vivarium.processes.multibody.single_agent_config`.

    Returns:
        vivarium.core.experiment.Experiment: An initialized experiment
        object.
    '''
    # configure the experiment
    n_agents = config.get('n_agents')
    emitter = config.get('emitter', {'type': 'timeseries'})

    # make lattice environment
    environment = Lattice(config.get('environment', {}))
    network = environment.generate()
    processes = network['processes']
    topology = network['topology']

    # add the agents
    agent_ids = [str(agent_id) for agent_id in range(n_agents)]
    agent_config = config['agent']
    agent_compartment = agent_config['type']
    compartment_config = agent_config['config']
    agent = agent_compartment(compartment_config)
    agents = make_agents(agent_ids, agent, {})
    processes['agents'] = agents['processes']
    topology['agents'] = agents['topology']

    # initial agent state
    locations = config.get('locations')
    if locations is None:
        locations = [[0.5, 0.5]]
    agent_config_settings = [{
        'bounds':
        environment.config['multibody']['bounds'],
        'location':
        random.choice(locations)
        if len(locations) <= index else locations[index]
    } for index, agent_id in enumerate(agent_ids)]

    initial_state = {
        'agents': {
            agent_id: single_agent_config(agent_config_setting)
            for agent_id, agent_config_setting in zip(agent_ids,
                                                      agent_config_settings)
        }
    }
    initial_state.update(config.get('initial_state', {}))

    return Experiment({
        'processes': processes,
        'topology': topology,
        'emitter': emitter,
        'initial_state': initial_state,
    })
Ejemplo n.º 12
0
def agent_environment_experiment(
    agents_config=None,
    environment_config=None,
    initial_state=None,
    initial_agent_state=None,
    settings=None,
    invoke=None,
):
    """ Make an experiment with agents placed in an environment under an `agents` store.
     Arguments:
        * **agents_config**: the configuration for the agents
        * **environment_config**: the configuration for the environment
        * **initial_state**: the initial state for the hierarchy, with environment at the
          top level.
        * **initial_agent_state**: the initial_state for agents, set under each agent_id.
        * **settings**: settings include **emitter** and **agent_names**.
        * **invoke**: is the invoke object for calling updates.
    """
    if settings is None:
        settings = {}

    # experiment settings
    emitter = settings.get('emitter', {'type': 'timeseries'})

    # initialize the agents
    if isinstance(agents_config, dict):
        # dict with single agent config
        agent_type = agents_config['type']
        agent_ids = agents_config['ids']
        agent_compartment = agent_type(agents_config['config'])
        agents = make_agents(agent_ids, agent_compartment,
                             agents_config['config'])

        if initial_agent_state:
            initial_state['agents'] = {
                agent_id: initial_agent_state
                for agent_id in agent_ids
            }

    elif isinstance(agents_config, list):
        # list with multiple agent configurations
        agents = {'processes': {}, 'topology': {}}
        for config in agents_config:
            agent_type = config['type']
            agent_ids = config['ids']
            agent_compartment = agent_type(config['config'])
            new_agents = make_agents(agent_ids, agent_compartment,
                                     config['config'])
            deep_merge(agents['processes'], new_agents['processes'])
            deep_merge(agents['topology'], new_agents['topology'])

            if initial_agent_state:
                if 'agents' not in initial_state:
                    initial_state['agents'] = {}
                initial_state['agents'].update(
                    {agent_id: initial_agent_state
                     for agent_id in agent_ids})

    if 'agents' in initial_state:
        environment_config['config']['diffusion']['agents'] = initial_state[
            'agents']

    # initialize the environment
    environment_type = environment_config['type']
    environment_compartment = environment_type(environment_config['config'])

    # combine processes and topologies
    network = environment_compartment.generate()
    processes = network['processes']
    topology = network['topology']
    processes['agents'] = agents['processes']
    topology['agents'] = agents['topology']

    if settings.get('agent_names') is True:
        # add an AgentNames processes, which saves the current agent names
        # to as store at the top level of the hierarchy
        processes['agent_names'] = AgentNames({})
        topology['agent_names'] = {
            'agents': ('agents', ),
            'names': ('names', )
        }

    experiment_config = {
        'processes': processes,
        'topology': topology,
        'emitter': emitter,
        'initial_state': initial_state,
    }
    if settings.get('experiment_name'):
        experiment_config['experiment_name'] = settings.get('experiment_name')
    if settings.get('description'):
        experiment_config['description'] = settings.get('description')
    if invoke:
        experiment_config['invoke'] = invoke
    if 'emit_step' in settings:
        experiment_config['emit_step'] = settings['emit_step']
    return Experiment(experiment_config)