Skip to content

Commit

Permalink
Merge pull request #2878 from kmantel/mdf-tests
Browse files Browse the repository at this point in the history
tests: MDF: correct use of shared state
  • Loading branch information
kmantel authored Jan 16, 2024
2 parents 1612d43 + 64caf83 commit a58ff31
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 107 deletions.
59 changes: 37 additions & 22 deletions psyneulink/core/globals/mdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,10 @@ def _generate_composition_string(graph, component_identifiers):
psyneulink.LearningMechanism,
psyneulink.LearningProjection,
)
implicit_roles = (
psyneulink.NodeRole.LEARNING,
)

output = []

comp_identifer = parse_valid_identifier(graph.id)
Expand Down Expand Up @@ -1090,6 +1094,22 @@ def alphabetical_order(items):
control_mechanisms = []
implicit_mechanisms = []

try:
node_roles = {
parse_valid_identifier(node): role for (node, role) in
graph.metadata['required_node_roles']
}
except KeyError:
node_roles = []

try:
excluded_node_roles = {
parse_valid_identifier(node): role for (node, role) in
graph.metadata['excluded_node_roles']
}
except KeyError:
excluded_node_roles = []

# add nested compositions and mechanisms in order they were added
# to this composition
for node in sorted(
Expand All @@ -1104,10 +1124,19 @@ def alphabetical_order(items):
except (AttributeError, KeyError):
component_type = default_node_type
identifier = parse_valid_identifier(node.id)

try:
node_role = eval(_parse_parameter_value(node_roles[identifier]))
except (KeyError, TypeError):
node_role = None

if issubclass(component_type, control_mechanism_types):
control_mechanisms.append(node)
component_identifiers[identifier] = True
elif issubclass(component_type, implicit_types):
elif (
issubclass(component_type, implicit_types)
or node_role in implicit_roles
):
implicit_mechanisms.append(node)
else:
mechanisms.append(node)
Expand Down Expand Up @@ -1166,23 +1195,6 @@ def alphabetical_order(items):
if len(compositions) > 0:
output.append('')

# generate string to add the nodes to this Composition
try:
node_roles = {
parse_valid_identifier(node): role for (node, role) in
graph.metadata['required_node_roles']
}
except KeyError:
node_roles = []

try:
excluded_node_roles = {
parse_valid_identifier(node): role for (node, role) in
graph.metadata['excluded_node_roles']
}
except KeyError:
excluded_node_roles = []

# do not add the controller as a normal node
try:
controller_name = graph.metadata['controller']['id']
Expand Down Expand Up @@ -1383,10 +1395,11 @@ def get_declared_identifiers(model):
for i in range(len(comp_strs)):
# greedy and non-greedy
for cs in comp_strs[i]:
potential_module_names = set([
cs_potential_names = set([
*re.findall(r'([A-Za-z_\.]+)\.', cs),
*re.findall(r'([A-Za-z_\.]+?)\.', cs)
])
potential_module_names.update(cs_potential_names)

for module in potential_module_names:
if module not in component_identifiers:
Expand Down Expand Up @@ -1556,7 +1569,9 @@ def write_mdf_file(compositions, filename: str, path: str = None, fmt: str = Non
not specified then the current directory is used.
fmt : str
specifies file format of output. Current options ('json', 'yml'/'yaml')
specifies file format of output. Auto-detect based on
**filename** extension if None.
Current options: 'json', 'yml'/'yaml'
simple_edge_format : bool
specifies use of
Expand All @@ -1567,8 +1582,8 @@ def write_mdf_file(compositions, filename: str, path: str = None, fmt: str = Non

if fmt is None:
try:
fmt = re.match(r'(.*)\.(.*)$', filename).groups(1)
except AttributeError:
fmt = re.match(r'(.*)\.(.*)$', filename).groups()[1]
except (AttributeError, IndexError):
fmt = 'json'

if path is not None:
Expand Down
10 changes: 5 additions & 5 deletions tests/mdf/model_backprop.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import psyneulink as pnl

a = pnl.TransferMechanism()
b = pnl.TransferMechanism()
c = pnl.TransferMechanism()
A = pnl.TransferMechanism(name='A')
B = pnl.TransferMechanism(name='B')
C = pnl.TransferMechanism(name='C')

p = pnl.Pathway(pathway=[a, b, c])
p = pnl.Pathway(pathway=[A, B, C])

comp = pnl.Composition()
comp = pnl.Composition(name='comp')
comp.add_backpropagation_learning_pathway(pathway=p)
Loading

0 comments on commit a58ff31

Please sign in to comment.