Resource Management

This module provides a tool to manage dependencies on resources within a vivarium simulation. These resources take the form of things that can be created and utilized by components, for example columns in the state table or named value pipelines.

Because these resources need to be created before they can be used, they are sensitive to ordering. The intent behind this tool is to provide an interface that allows other managers to register resources with the resource manager and in turn ask for ordered sequences of these resources according to their dependencies or raise exceptions if this is not possible.

exception vivarium.framework.resource.ResourceError[source]

Error raised when a dependency requirement is violated.

class vivarium.framework.resource.ResourceGroup(resource_type, resource_names, producer, dependencies)[source]

Resource groups are the nodes in the resource dependency graph.

A resource group represents the pool of resources produced by a single callable and all the dependencies necessary to produce that resource. When thinking of the dependency graph, this represents a vertex and all in-edges. This is a local-information representation that can be used to construct the entire dependency graph once all resources are specified.

type

The type of resource produced by this resource group’s producer.

Must be one of RESOURCE_TYPES.

Return type:str
names

The long names (including type) of all resources in this group.

Return type:List[str]
producer

The method or object that produces this group of resources.

Return type:Any
dependencies

The long names (including type) of dependencies for this group.

Return type:List[str]
class vivarium.framework.resource.ResourceManager[source]

Manages all the resources needed for population initialization.

name

The name of this manager.

Return type:str
graph

The networkx graph representation of the resource pool.

Return type:DiGraph
sorted_nodes

Returns a topological sort of the resource graph.

Notes

Topological sorts are not stable. Be wary of depending on order where you shouldn’t.

add_resources(resource_type, resource_names, producer, dependencies)[source]

Adds managed resources to the resource pool.

Parameters:
  • resource_type (str) – The type of the resources being added. Must be one of RESOURCE_TYPES.
  • resource_names (List[str]) – A list of names of the resources being added.
  • producer (Any) – A method or object that will produce the resources.
  • dependencies (List[str]) – A list of resource names formatted as resource_type.resource_name that the producer requires.
Raises:

ResourceError – If either the resource type is invalid, a component has multiple resource producers for the column resource type, or there are multiple producers of the same resource.

class vivarium.framework.resource.ResourceInterface(manager)[source]

The resource management system.

A resource in vivarium is something like a state table column or a randomness stream. These resources are used to initialize or alter the state of the simulation. Many of these resources might depend on each other and therefore need to be created or updated in a particular order. These dependency chains can be quite long and complex.

Placing the ordering responsibility on end users makes simulations very fragile and difficult to understand. Instead, the resource management system allows users to only specify local dependencies. The system then uses the local dependency information to construct a full dependency graph, validate that there are no cyclic dependencies, and return resources and their producers in an order that makes sense.

add_resources(resource_type, resource_names, producer, dependencies)[source]

Adds managed resources to the resource pool.

Parameters:
  • resource_type (str) – The type of the resources being added. Must be one of RESOURCE_TYPES.
  • resource_names (List[str]) – A list of names of the resources being added.
  • producer (Any) – A method or object that will produce the resources.
  • dependencies (List[str]) – A list of resource names formatted as resource_type.resource_name that the producer requires.
Raises:

ResourceError – If either the resource type is invalid, a component has multiple resource producers for the column resource type, or there are multiple producers of the same resource.