Skip to content

Commit

Permalink
Refactor/context/deprecate init status (#745)
Browse files Browse the repository at this point in the history
* -

* -

* -

* -

* -

* -

* -

* -

* -

* -

* -

* -

* -

* -

* • Log
  LogCondition - subset of ContextFlags used to specified logPref

• Context
  ContextFlags - implemented as common set of context flags for:
  - initialization
  - execution tracking
  - logging
  ContextStatus - aliases to ContextFlags (for backward compatibility)

* • Log
  LogCondition - subset of ContextFlags used to specified logPref

• Context
  ContextFlags - implemented as common set of context flags for:
  - initialization
  - execution tracking
  - logging
  ContextStatus - aliases to ContextFlags (for backward compatibility)

* • Log
  LogCondition - subset of ContextFlags used to specified logPref

• Context
  ContextFlags - implemented as common set of context flags for:
  - initialization
  - execution tracking
  - logging
  ContextStatus - aliases to ContextFlags (for backward compatibility)

* -

* -

* -

* -

* -

* • Context
  context.status replaced with specific field assignments
  (.initialization_phase, .execution_phase, and .source)

* • Context
  context.status replaced with specific field assignments
  (.initialization_phase, .execution_phase, and .source)

* • Context
  context.status replaced with specific field assignments
  (.initialization_phase, .execution_phase, and .source)

* -

* -

* • Context
  context.status replaced with specific field assignments
  (.initialization_phase, .execution_phase, and .source)

* -

* -

* -

* -

* -

* -

* Merge branches 'devel' and 'refactor/context/structured' of https://github.com/PrincetonUniversity/PsyNeuLink into refactor/context/structured

# Conflicts:
#	.idea/runConfigurations/Tests.xml
#	Scripts/McClure.py

* Merge branches 'devel' and 'refactor/context/structured' of https://github.com/PrincetonUniversity/PsyNeuLink into refactor/context/structured

# Conflicts:
#	.idea/runConfigurations/Tests.xml
#	Scripts/McClure.py

* -

* -

* • Context
  - consolidated init_status into context.initialization_status

* • Context
  - consolidated init_status into context.initialization_status

* • Context
  - consolidated init_status into context.initialization_status
  • Loading branch information
jdcpni authored Apr 1, 2018
1 parent c021994 commit 7817392
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 135 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 8 additions & 22 deletions psyneulink/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def __init__(self,
# # MODIFIED 8/14/16 NEW:
# # PROBLEM: variable has different name for different classes; need to standardize name across classes
# try:
# if self.init_status is InitStatus.DEFERRED_INITIALIZATION:
# if self.initialization_status is ContextFlags.DEFERRED_INITIALIZATION:
# defer_init = True
# except AttributeError:
# pass
Expand All @@ -878,8 +878,8 @@ def __init__(self,
self.context.source = ContextFlags.COMPONENT
self.context.string = context + INITIALIZING + ": " + COMPONENT_INIT

self.init_status = InitStatus.UNSET
# self.init_status = InitStatus.INITIALIZING
# self.context.initialization_status = ContextFlags.INITIALIZING
self.context.initialization_status = ContextFlags.UNSET

defaults = self.ClassDefaults.values().copy()
if param_defaults is not None:
Expand Down Expand Up @@ -1001,7 +1001,7 @@ def __init__(self,
# (e.g., instantiate_output_state in Mechanism)
self._instantiate_attributes_after_function(context=context)

self.init_status = InitStatus.INITIALIZED
self.context.initialization_status = ContextFlags.INITIALIZED

def __repr__(self):
return '({0} {1})'.format(type(self).__name__, self.name)
Expand Down Expand Up @@ -1168,12 +1168,12 @@ def checkAndCastInt(x):
def _deferred_init(self, context=None):
"""Use in subclasses that require deferred initialization
"""
if self.init_status is InitStatus.DEFERRED_INITIALIZATION:
if self.context.initialization_status == ContextFlags.DEFERRED_INIT:

# Flag that object is now being initialized
# Note: self.value will be resolved to the object's value as part of initialization
# (usually in _instantiate_function)
self.init_status = InitStatus.INITIALIZING
self.context.initialization_status = ContextFlags.INITIALIZING

del self.init_args['self']

Expand Down Expand Up @@ -1206,7 +1206,7 @@ def _deferred_init(self, context=None):
else:
self._assign_default_name()

self.init_status = InitStatus.INITIALIZED
self.context.initialization_status = ContextFlags.INITIALIZED

def _assign_deferred_init_name(self, name, context):

Expand Down Expand Up @@ -2913,20 +2913,6 @@ def size(self):
s.append(len(v[i]))
return np.array(s)

# @property
# def init_status(self):
# try:
# return self._init_status
# except AttributeError:
# return InitStatus.UNSET
#
# @init_status.setter
# def init_status(self, value):
# if not isinstance(value, InitStatus):
# raise ComponentError("PROGRAM ERROR: Attempt to assign \'init_status\' attribute of {} "
# "a value ({}) other than one of InitStatus".format(self.name, value))
# self._init_status = value

@property
def prefs(self):
# Whenever pref is accessed, use current owner as context (for level checking)
Expand Down Expand Up @@ -3078,7 +3064,7 @@ def log(self):
try:
return self._log
except AttributeError:
if self.init_status is InitStatus.DEFERRED_INITIALIZATION:
if self.context.initialization_status == ContextFlags.DEFERRED_INIT:
raise ComponentError("Initialization of {} is deferred; try assigning {} after it is complete "
"or appropriately configuring a system to which it belongs".
format(self.name, 'log'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
from psyneulink.components.mechanisms.mechanism import Mechanism_Base
from psyneulink.components.states.modulatorysignals.gatingsignal import GatingSignal
from psyneulink.components.states.state import State_Base, _parse_state_spec
from psyneulink.globals.context import ContextFlags
from psyneulink.globals.defaults import defaultGatingPolicy
from psyneulink.globals.keywords import \
GATING, GATING_POLICY, GATING_PROJECTION, GATING_PROJECTIONS, GATING_SIGNAL, GATING_SIGNALS, GATING_SIGNAL_SPECS, \
Expand Down Expand Up @@ -547,7 +548,7 @@ def _assign_as_gating_mechanism(self, context=None):
for state in mech._input_states + mech._output_states:
for projection in state.mod_afferents:
# If projection was deferred for init, initialize it now and instantiate for self
if (projection.init_status is InitStatus.DEFERRED_INITIALIZATION
if (projection.context.initialization_status == ContextFlags.DEFERRED_INIT
and projection.init_args['sender'] is None):
# FIX 5/23/17: MODIFY THIS WHEN (param, GatingProjection) tuple
# FIX: IS REPLACED WITH (param, GatingSignal) tuple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ def __init__(self,
# delete self.init_args[ERROR_SOURCES]

# # Flag for deferred initialization
# self.init_status = InitStatus.DEFERRED_INITIALIZATION
# self.context.initialization_status = ContextFlags.DEFERRED_INIT
# self.initialization_status = ContextFlags.DEFERRED_INIT

self._learning_rate = learning_rate

Expand Down
7 changes: 5 additions & 2 deletions psyneulink/components/mechanisms/mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ def _handle_arg_input_states(self, input_states):
except KeyError:
pass
elif isinstance(parsed_spec, (Projection, Mechanism, State)):
if parsed_spec.init_status is InitStatus.DEFERRED_INITIALIZATION:
if parsed_spec.context.initialization_status == ContextFlags.DEFERRED_INIT:
args = parsed_spec.init_args
# MODIFIED 2/21/18 OLD:
if REFERENCE_VALUE in args and args[REFERENCE_VALUE] is not None:
Expand Down Expand Up @@ -1980,7 +1980,10 @@ def execute(self,
# These need to be set for states to use as context
self.context.string = context
if not INITIALIZING in context: # cxt-set
self.context.initialization_status &= ~ContextFlags.INITIALIZING
# FIX: 3/31/18 - THIS SHOULD BE MOVED TO END OF __init__ METHOD(S)
# self.context.initialization_status &= ~ContextFlags.INITIALIZING
# self.context.initialization_status = ContextFlags.INITIALIZED

if EXECUTING in context:
self.context.execution_phase = ContextFlags.PROCESSING
if LEARNING in context:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
from psyneulink.components.mechanisms.processing.processingmechanism import ProcessingMechanism_Base
from psyneulink.components.states.outputstate import OutputState, PRIMARY, standard_output_states
from psyneulink.components.states.state import _parse_state_spec
from psyneulink.globals.context import ContextFlags
from psyneulink.globals.keywords import PARAMS, PROJECTION, PROJECTIONS, CONTROL, DEFAULT_MATRIX, DEFAULT_VARIABLE, \
EXPONENT, EXPONENTS, FUNCTION, \
INPUT_STATES, LEARNING, MATRIX, NAME, OBJECTIVE_MECHANISM, SENDER, STATE_TYPE, VARIABLE, WEIGHT, WEIGHTS, \
Expand Down Expand Up @@ -657,7 +658,7 @@ def _instantiate_input_states(self, monitored_output_states_specs=None, referenc
"""
from psyneulink.components.states.inputstate import InputState
# If call is for initialization
if self.init_status is InitStatus.UNSET:
if self.context.initialization_status == ContextFlags.UNSET:
# Use self.input_states (containing specs from **input_states** arg of constructor) or default InputState
input_states = self.input_states or [{STATE_TYPE: InputState, VARIABLE: [0]}]
return super()._instantiate_input_states(input_states=input_states, context=context)
Expand Down Expand Up @@ -858,7 +859,7 @@ def _instantiate_monitoring_projections(owner,
if isinstance(sender, OutputState):
# Projection has been specified for receiver and initialization begun, so call deferred_init()
if receiver.path_afferents:
if not receiver.path_afferents[0].init_status is InitStatus.DEFERRED_INITIALIZATION:
if not receiver.path_afferents[0].context.initialization_status == ContextFlags.DEFERRED_INIT:
raise ObjectiveMechanismError("PROGRAM ERROR: {} of {} already has an afferent projection "
"implemented and initialized ({})".
format(receiver.name, owner.name, receiver.path_afferents[0].name))
Expand Down
24 changes: 14 additions & 10 deletions psyneulink/components/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,17 +1449,18 @@ def _parse_and_instantiate_projection_entries(self, pathway, context=None):

# If initialization of MappingProjection has been deferred,
# check sender and receiver, assign them if they have not been assigned, and initialize it
if item.init_status is InitStatus.DEFERRED_INITIALIZATION:
if item.context.initialization_status == ContextFlags.DEFERRED_INIT:
# Check sender arg
try:
sender_arg = item.init_args[SENDER]
except AttributeError:
raise ProcessError("PROGRAM ERROR: init_status of {} is {} but it does not have init_args".
format(item, InitStatus.DEFERRED_INITIALIZATION))
raise ProcessError("PROGRAM ERROR: initialization_status of {} is {} "
"but it does not have init_args".
format(item, ContextFlags.DEFERRED_INIT.name))
except KeyError:
raise ProcessError("PROGRAM ERROR: Value of {} is {} "
"but init_args does not have entry for {}".
format(item.init_args[NAME], InitStatus.DEFERRED_INITIALIZATION, SENDER))
raise ProcessError("PROGRAM ERROR: Value of {} is {} but "
"init_args does not have entry for {}".
format(item.init_args[NAME],ContextFlags.DEFERRED_INIT.name, SENDER))
else:
# If sender is not specified for the Projection,
# assign mechanism that precedes in pathway
Expand All @@ -1475,12 +1476,15 @@ def _parse_and_instantiate_projection_entries(self, pathway, context=None):
try:
receiver_arg = item.init_args[kwReceiverArg]
except AttributeError:
raise ProcessError("PROGRAM ERROR: init_status of {} is {} but it does not have init_args".
format(item, InitStatus.DEFERRED_INITIALIZATION))
raise ProcessError("PROGRAM ERROR: initialization_status of {} is {} "
"but it does not have init_args".
format(item, ContextFlags.DEFERRED_INIT))
except KeyError:
raise ProcessError("PROGRAM ERROR: init_status of {} is {} "
raise ProcessError("PROGRAM ERROR: initialization_status of {} is {} "
"but init_args does not have entry for {}".
format(item.init_args[NAME], InitStatus.DEFERRED_INITIALIZATION, kwReceiverArg))
format(item.init_args[NAME],
ContextFlags.DEFERRED_INIT,
kwReceiverArg))
else:
# If receiver is not specified for the Projection,
# assign mechanism that follows it in the pathway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
from psyneulink.components.projections.modulatory.modulatoryprojection import ModulatoryProjection_Base
from psyneulink.components.projections.projection import ProjectionError, Projection_Base, projection_keywords
from psyneulink.components.shellclasses import Mechanism, Process_Base
from psyneulink.globals.context import ContextFlags
from psyneulink.globals.defaults import defaultControlAllocation
from psyneulink.globals.keywords import CONTROL, CONTROL_PROJECTION, CONTROL_SIGNAL, PARAMETER_STATE, PROJECTION_SENDER
from psyneulink.globals.preferences.componentpreferenceset import is_pref_set
Expand Down Expand Up @@ -298,10 +299,10 @@ def __init__(self,
params=params)

# If receiver has not been assigned, defer init to State.instantiate_projection_to_state()
if (sender is None or sender.init_status is InitStatus.DEFERRED_INITIALIZATION or
if (sender is None or sender.context.initialization_status == ContextFlags.DEFERRED_INIT or
inspect.isclass(receiver) or receiver is None or
receiver.init_status is InitStatus.DEFERRED_INITIALIZATION):
self.init_status = InitStatus.DEFERRED_INITIALIZATION
receiver.context.initialization_status == ContextFlags.DEFERRED_INIT):
self.context.initialization_status = ContextFlags.DEFERRED_INIT

# Validate sender (as variable) and params, and assign to variable and paramInstanceDefaults
# Note: pass name of mechanism (to override assignment of componentName in super.__init__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
from psyneulink.components.projections.modulatory.modulatoryprojection import ModulatoryProjection_Base
from psyneulink.components.projections.projection import ProjectionError, Projection_Base, projection_keywords
from psyneulink.components.shellclasses import Mechanism, Process_Base
from psyneulink.globals.context import ContextFlags
from psyneulink.globals.defaults import defaultGatingPolicy
from psyneulink.globals.keywords import FUNCTION_OUTPUT_TYPE, GATING, GATING_MECHANISM, GATING_PROJECTION, GATING_SIGNAL, INITIALIZING, INPUT_STATE, OUTPUT_STATE, PROJECTION_SENDER
from psyneulink.globals.preferences.componentpreferenceset import is_pref_set
Expand Down Expand Up @@ -288,7 +289,7 @@ def __init__(self,
# If receiver has not been assigned, defer init to State.instantiate_projection_to_state()
if sender is None or receiver is None:
# Flag for deferred initialization
self.init_status = InitStatus.DEFERRED_INITIALIZATION
self.context.initialization_status = ContextFlags.DEFERRED_INIT

# Validate sender (as variable) and params, and assign to variable and paramInstanceDefaults
# Note: pass name of mechanism (to override assignment of componentName in super.__init__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def __init__(self,
# If receiver has not been assigned, defer init to State.instantiate_projection_to_state()
if sender is None or receiver is None:
# Flag for deferred initialization
self.init_status = InitStatus.DEFERRED_INITIALIZATION
self.context.initialization_status = ContextFlags.DEFERRED_INIT
super().__init__(sender=sender,
receiver=receiver,
weight=weight,
Expand Down Expand Up @@ -617,8 +617,8 @@ def _execute(self, variable, runtime_params=None, context=None):
runtime_params = runtime_params or {}

# Pass during initialization (since has not yet been fully initialized
if self.init_status is InitStatus.DEFERRED_INITIALIZATION:
return self.init_status
if self.context.initialization_status == ContextFlags.DEFERRED_INIT:
return self.context.initialization_status

# if self.learning_rate:
# runtime_params.update({SLOPE:self.learning_rate})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,18 @@ def _assign_default_projection_name(self, state=None, sender_name=None, receiver

template = "{} for {}[{}]"

if self.init_status in {InitStatus.INITIALIZED, InitStatus.INITIALIZING, InitStatus.UNSET}:
if self.context.initialization_status & \
(ContextFlags.INITIALIZED | ContextFlags.INITIALIZING | ContextFlags.UNSET):
# If the name is not a default name for the class, return
if not self.className + '-' in self.name:
return self.name
self.name = template.format(self.className, self.receiver.owner.name, self.receiver.name)

elif self.init_status is InitStatus.DEFERRED_INITIALIZATION:
elif self.context.initialization_status == ContextFlags.DEFERRED_INIT:
projection_name = template.format(self.className, state.owner.name, state.name)
# self.init_args[NAME] = self.init_args[NAME] or projection_name
self.name = self.init_args[NAME] or projection_name

else:
raise ModulatoryProjectionError("PROGRAM ERROR: {} has unrecognized InitStatus ({})".
format(self, self.init_status))
format(self, ContextFlags._get_context_string(self.context.initialization_status)))
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def __init__(self,

# If sender or receiver has not been assigned, defer init to State.instantiate_projection_to_state()
if sender is None or receiver is None:
self.init_status = InitStatus.DEFERRED_INITIALIZATION
self.context.initialization_status = ContextFlags.DEFERRED_INIT

# Validate sender (as variable) and params, and assign to variable and paramInstanceDefaults
super().__init__(sender=sender,
Expand Down Expand Up @@ -627,7 +627,8 @@ def _execute(self, variable=None, runtime_params=None, context=None):

# FIX: MOVE THIS TO SUPER
if EXECUTING in context: # cxt-test
self.context.initialization_status &= ~(ContextFlags.VALIDATING | ContextFlags.INITIALIZING)
# self.context.initialization_status &= ~(ContextFlags.VALIDATING | ContextFlags.INITIALIZING)
# self.context.initialization_status = ContextFlags.INITIALIZED
self.context.execution_phase = ContextFlags.PROCESSING
self.context.string = context

Expand Down
Loading

0 comments on commit 7817392

Please sign in to comment.