# Compositions¶

## Overview¶

Composition is the base class for objects that combine PsyNeuLink Components into an executable model. It defines a common set of attributes possessed, and methods used by all Composition objects.

## Creating a Composition¶

A generic Composition can be created by calling the constructor, and then adding Components using the Composition’s add methods. However, more commonly, a Composition is created using the constructor for one of its subclasses: System or Process. These automatically create Compositions from lists of Components. Once created, Components can be added or removed from an existing Composition using its add and/or remove methods.

## Execution¶

See System or Process for documentation concerning execution of the corresponding subclass.

## Class Reference¶

class psyneulink.composition.MechanismRole
class psyneulink.composition.Composition
graph

Graph – The full Graph associated with this Composition. Contains both Mechanisms and Projections used in processing or learning.

mechanisms

list[Mechanism] – A list of all Mechanisms contained in this Composition

graph_processing

The Composition’s processing graph (contains only Mechanisms, excluding those used in learning).

Getter: Returns the processing graph, and builds the graph if it needs updating

since the last access.

scheduler_processing

A default Scheduler automatically generated by the Composition, used for the (processing phase of execution.

Getter: Returns the default processing scheduler, and builds it if it needs updating since the last access.
scheduler_learning

A default Scheduler automatically generated by the Composition, used for the learning phase of execution.

Getter: Returns the default learning scheduler, and builds it if it needs updating since the last access.
add_mechanism(mech)

Parameters: mech (Mechanism) – the Mechanism to add
add_projection(sender, projection, receiver)

Parameters: sender (Mechanism) – the sender of projection projection (Projection) – the projection to add receiver (Mechanism) – the receiver of projection
get_mechanisms_by_role(role)

Returns a set of mechanisms in this Composition that have the role role

Parameters: role (MechanismRole) – the set of mechanisms having this role to return set of Mechanisms with MechanismRole role set(Mechanism)
execute(inputs, scheduler_processing=None, scheduler_learning=None, termination_processing=None, termination_learning=None, call_before_time_step=None, call_before_pass=None, call_after_time_step=None, call_after_pass=None, execution_id=None)

Passes inputs to any Mechanisms receiving inputs directly from the user, then coordinates with the Scheduler to receive and execute sets of Mechanisms that are eligible to run until termination conditions are met.

Parameters: inputs ({ Mechanism : list }) – a dictionary containing a key-value pair for each Mechanism in the composition that receives inputs from the user. For each pair, the key is the Mechanism and the value is a list of inputs. scheduler_processing (Scheduler) – the scheduler object that owns the conditions that will instruct the non-learning execution of this Composition. If not specified, the Composition will use its automatically generated scheduler scheduler_learning (Scheduler) – the scheduler object that owns the conditions that will instruct the Learning execution of this Composition. If not specified, the Composition will use its automatically generated scheduler execution_id (UUID) – execution_id will typically be set to none and assigned randomly at runtime call_before_time_step (callable) – will be called before each TIME_STEP is executed call_after_time_step (callable) – will be called after each TIME_STEP is executed call_before_pass (callable) – will be called before each PASS is executed call_after_pass (callable) – will be called after each PASS is executed output value of the final Mechanism executed in the Composition various
run(inputs=None, scheduler_processing=None, scheduler_learning=None, termination_processing=None, termination_learning=None, execution_id=None, num_trials=None, call_before_time_step=None, call_after_time_step=None, call_before_pass=None, call_after_pass=None, call_before_trial=None, call_after_trial=None)

Passes inputs to any mechanisms receiving inputs directly from the user, then coordinates with the scheduler to receive and execute sets of mechanisms that are eligible to run until termination conditions are met.

Parameters: inputs ({ Mechanism : list }) – a dictionary containing a key-value pair for each Mechanism in the composition that receives inputs from the user. For each pair, the key is the Mechanism and the value is a list of inputs. Each input in the list corresponds to a certain TRIAL. scheduler_processing (Scheduler) – the scheduler object that owns the conditions that will instruct the non-learning execution of this Composition. If not specified, the Composition will use its automatically generated scheduler. scheduler_learning (Scheduler) – the scheduler object that owns the conditions that will instruct the Learning execution of this Composition. If not specified, the Composition will use its automatically generated scheduler. execution_id (UUID) – execution_id will typically be set to none and assigned randomly at runtime. num_trials (int) – typically, the composition will infer the number of trials from the length of its input specification. To reuse the same inputs across many trials, you may specify an input dictionary with lists of length 1, or use default inputs, and select a number of trials with num_trials. call_before_time_step (callable) – will be called before each TIME_STEP is executed. call_after_time_step (callable) – will be called after each TIME_STEP is executed. call_before_pass (callable) – will be called before each PASS is executed. call_after_pass (callable) – will be called after each PASS is executed. call_before_trial (callable) – will be called before each TRIAL is executed. call_after_trial (callable) – will be called after each TRIAL is executed. output value of the final Mechanism executed in the composition various