Skip to content

Commit

Permalink
Merge branch 'refactor-mutation-api' into refactor-position-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jashlu authored Oct 28, 2024
2 parents 65e95f6 + b48da84 commit 5e64e54
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 675 deletions.
8 changes: 5 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"editor.defaultFormatter": "ms-python.black-formatter",
"black-formatter.importStrategy": "fromEnvironment",
"editor.formatOnSave": true,
"black-formatter.importStrategy": "fromEnvironment",
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
20 changes: 10 additions & 10 deletions examples/entering-a-room.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,61 @@
"behavior_library": [
{
"id": "anonymous-sequence",
"display_name": "",
"name": "",
"type": "Sequence",
"show": false
},
{
"id": "approach-door",
"display_name": "Approach the door",
"name": "Approach the door",
"type": "Behavior",
"show": true
},
{
"id": "open-door",
"display_name": "Open the door",
"name": "Open the door",
"type": "Behavior",
"show": true
},
{
"id": "go-through-doorway",
"display_name": "Go through the doorway",
"name": "Go through the doorway",
"type": "Behavior",
"show": true
},
{
"id": "knock-on-the-door",
"display_name": "Knock on the door",
"name": "Knock on the door",
"type": "Behavior",
"show": true
},
{
"id": "sing-a-song",
"display_name": "Sing a song",
"name": "Sing a song",
"type": "Behavior",
"show": true
},
{
"id": "announce-your-presence",
"display_name": "Announce your presence",
"name": "Announce your presence",
"type": "Behavior",
"show": true
},
{
"id": "contact-supervisor-guidance",
"display_name": "Contact supervisor for guidance",
"name": "Contact supervisor for guidance",
"type": "Behavior",
"show": true
},
{
"id": "stop",
"display_name": "Stop",
"name": "Stop",
"type": "Behavior",
"show": true
},
{
"id": "return-to-charging-station",
"display_name": "Return to charging station",
"name": "Return to charging station",
"type": "Behavior",
"show": false
}
Expand Down
1 change: 1 addition & 0 deletions norms/wsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ async def main():
async with serve(echo, "localhost", 8765):
await asyncio.Future() # run forever


if __name__ == "__main__":
asyncio.run(main())
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,3 @@ version.source = "vcs"

[tool.pytest.ini_options]
addopts = "--doctest-modules"

[tool.black]
target-version = ['py311']
39 changes: 15 additions & 24 deletions src/social_norms_trees/atomic_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
BehaviorIdentifier = TypeVar(
"BehaviorIdentifier", bound=Union[ExistingNode, NewNode, CompositeIndex]
)
BehaviorTreeNode = TypeVar(
"BehaviorTreeNode", bound=py_trees.behaviour.Behaviour)
BehaviorTreeNode = TypeVar("BehaviorTreeNode", bound=py_trees.behaviour.Behaviour)
BehaviorTree = TypeVar("BehaviorTree", bound=BehaviorTreeNode)
BehaviorLibrary = TypeVar("BehaviorLibrary", bound=List[BehaviorTreeNode])
TreeOrLibrary = TypeVar(
"TreeOrLibrary", bound=Union[BehaviorTree, BehaviorLibrary])
TreeOrLibrary = TypeVar("TreeOrLibrary", bound=Union[BehaviorTree, BehaviorLibrary])


# =============================================================================
Expand Down Expand Up @@ -452,15 +450,11 @@ def get_library_mapping(library: BehaviorLibrary) -> NodeMappingRepresentation:

mapping = {str(i): n for i, n in enumerate(library)}
labels = list(mapping.keys())
representation = "\n".join(
[f"{i}: {n.name}" for i, n in enumerate(library)])
representation = "\n".join([f"{i}: {n.name}" for i, n in enumerate(library)])
return NodeMappingRepresentation(mapping, labels, representation)


prompt_identify_library_node = partial(
prompt_identify,
function=get_library_mapping
)
prompt_identify_library_node = partial(prompt_identify, function=get_library_mapping)


def get_composite_mapping(tree: BehaviorTree, skip_label="_"):
Expand Down Expand Up @@ -503,10 +497,7 @@ def get_composite_mapping(tree: BehaviorTree, skip_label="_"):
return NodeMappingRepresentation(mapping, allowed_labels, representation)


prompt_identify_composite = partial(
prompt_identify,
function=get_composite_mapping
)
prompt_identify_composite = partial(prompt_identify, function=get_composite_mapping)

class TreeInsertionState:
def __init__(self):
Expand Down Expand Up @@ -730,10 +721,7 @@ def get_child_index_mapping(tree: BehaviorTree, skip_label="_"):
return NodeMappingRepresentation(mapping, allowed_labels, representation)


prompt_identify_child_index = partial(
prompt_identify,
function=get_child_index_mapping
)
prompt_identify_child_index = partial(prompt_identify, function=get_child_index_mapping)


def get_position_mapping(tree):
Expand Down Expand Up @@ -778,8 +766,7 @@ def get_position_mapping(tree):


# Wrapper functions for the atomic operations which give them a UI.
MutationResult = namedtuple(
"MutationResult", ["result", "tree", "function", "kwargs"])
MutationResult = namedtuple("MutationResult", ["result", "tree", "function", "kwargs"])


def mutate_chooser(*fs: Union[Callable], message="Which action?"):
Expand All @@ -804,8 +791,10 @@ def mutate_chooser(*fs: Union[Callable], message="Which action?"):


def mutate_ui(
f,
) -> Callable[[py_trees.behaviour.Behaviour, List[py_trees.behaviour.Behaviour]], Dict]:
f: Callable,
) -> Callable[
[py_trees.behaviour.Behaviour, List[py_trees.behaviour.Behaviour]], MutationResult
]:
"""Factory function for a tree mutator UI.
This creates a version of the atomic function `f`
which prompts the user for the appropriate arguments
Expand All @@ -826,6 +815,7 @@ def f_inner(tree, library):
return_value = MutationResult(
result=inner_result, tree=tree, function=f, kwargs=kwargs
)
_logger.debug(return_value)
return return_value

return f_inner
Expand Down Expand Up @@ -853,7 +843,8 @@ def prompt_get_mutate_arguments(annotation: GenericAlias, tree, library):
elif annotation_ == str(NewNode):
_logger.debug("in NewNode")
new_node = prompt_identify_library_node(
library, message="Which node from the library?")
library, message="Which node from the library?"
)
return new_node
else:
_logger.debug("in 'else'")
Expand All @@ -870,7 +861,7 @@ class QuitException(Exception):
pass


def end_experiment(*args, **kwargs):
def end_experiment():
"""I'm done, end the experiment."""
raise QuitException("User ended the experiment.")

Expand Down
6 changes: 5 additions & 1 deletion src/social_norms_trees/behavior_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class BehaviorLibrary:
def __init__(self, behavior_list):
self.behaviors = behavior_list
self.behavior_from_display_name = {
behavior["display_name"]: behavior for behavior in behavior_list
behavior["name"]: behavior for behavior in behavior_list
}
self.behavior_from_id = {behavior["id"]: behavior for behavior in behavior_list}

def __iter__(self):
for i in self.behaviors:
yield i
Loading

0 comments on commit 5e64e54

Please sign in to comment.