diff --git a/.gitignore b/.gitignore index 9769132..4fead9f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,7 @@ __pycache__/ # due to using tox and pytest .tox .cache -.venv/ \ No newline at end of file +.venv/ + +# Project specific files +db.json \ No newline at end of file diff --git a/README.md b/README.md index d6c10eb..6e036ff 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,44 @@ -# A sample Python project +# Social Norms Trees +Experiments on teaching social norms to autonomous agents. + +## Developer Setup + +Prerequisites: +- Python 3.11+ +- git + +Clone the repository: +```bash +git clone https://github.com/brown-ccv/social-norms-trees +``` + +Enter the repository directory: +```bash +cd social-norms-trees +``` + +Create a virtual environment: +```bash +python3 -m venv .venv +``` + +Activate the environment: +```bash +. .venv/bin/activate +``` + +Install the package in editable mode for development: +```bash +pip install --editable ".[test]" +``` + +Run the tests: +```bash +pytest +``` + +Run the example experiment: +```bash +social-norms-trees examples/entering-a-room.json +``` -![Python Logo](https://www.python.org/static/community_logos/python-logo.png "Sample inline image") - -A sample project that exists as an aid to the [Python Packaging User -Guide][packaging guide]'s [Tutorial on Packaging and Distributing -Projects][distribution tutorial]. - -This project does not aim to cover best practices for Python project -development as a whole. For example, it does not provide guidance or tool -recommendations for version control, documentation, or testing. - -[The source for this project is available here][src]. - -The metadata for a Python project is defined in the `pyproject.toml` file, -an example of which is included in this project. You should edit this file -accordingly to adapt this sample project to your needs. - ----- - -This is the README file for the project. - -The file should use UTF-8 encoding and can be written using -[reStructuredText][rst] or [markdown][md use] with the appropriate [key set][md -use]. It will be used to generate the project webpage on PyPI and will be -displayed as the project homepage on common code-hosting services, and should be -written for that purpose. - -Typical contents for this file would include an overview of the project, basic -usage examples, etc. Generally, including the project changelog in here is not a -good idea, although a simple “What's New” section for the most recent version -may be appropriate. - -[packaging guide]: https://packaging.python.org -[distribution tutorial]: https://packaging.python.org/tutorials/packaging-projects/ -[src]: https://github.com/pypa/sampleproject -[rst]: http://docutils.sourceforge.net/rst.html -[md]: https://tools.ietf.org/html/rfc7764#section-3.5 "CommonMark variant" -[md use]: https://packaging.python.org/specifications/core-metadata/#description-content-type-optional diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..48ca6b6 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,3 @@ +# Example Experiments + +This directory includes configuration files for example experiments. \ No newline at end of file diff --git a/src/social_norms_trees/resources.json b/examples/abstract-experiment.json similarity index 100% rename from src/social_norms_trees/resources.json rename to examples/abstract-experiment.json diff --git a/examples/entering-a-room.json b/examples/entering-a-room.json new file mode 100644 index 0000000..0b9ee06 --- /dev/null +++ b/examples/entering-a-room.json @@ -0,0 +1,84 @@ +{ + "context": "A nurse assistant robot in a hospital is taking medicine to Mrs. Jones during the day. The robot has already collected the medicine and enters the corridor outside Mrs. Jones' room. It needs to enter the room to take Mrs. Jones her medicine.", + "behavior_tree": { + "name": "", + "type": "Sequence", + "children": [ + { + "name": "Approach the door", + "type": "Behavior" + }, + { + "name": "Open the door", + "type": "Behavior" + }, + { + "name": "Go through the doorway", + "type": "Behavior" + } + ] + }, + "behavior_library": [ + { + "id": "anonymous-sequence", + "display_name": "", + "type": "Sequence", + "show": false + }, + { + "id": "approach-door", + "display_name": "Approach the door", + "type": "Behavior", + "show": true + }, + { + "id": "open-door", + "display_name": "Open the door", + "type": "Behavior", + "show": true + }, + { + "id": "go-through-doorway", + "display_name": "Go through the doorway", + "type": "Behavior", + "show": true + }, + { + "id": "knock-on-the-door", + "display_name": "Knock on the door", + "type": "Behavior", + "show": true + }, + { + "id": "sing-a-song", + "display_name": "Sing a song", + "type": "Behavior", + "show": true + }, + { + "id": "announce-your-presence", + "display_name": "Announce your presence", + "type": "Behavior", + "show": true + }, + { + "id": "contact-supervisor-guidance", + "display_name": "Contact supervisor for guidance", + "type": "Behavior", + "show": true + }, + { + "id": "stop", + "display_name": "Stop", + "type": "Behavior", + "show": true + }, + { + "id": "return-to-charging-station", + "display_name": "Return to charging station", + "type": "Behavior", + "show": false + } + ] + +} diff --git a/pyproject.toml b/pyproject.toml index b1d434e..2d2a85d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,7 +75,9 @@ classifiers = [ # Optional dependencies = [ # Optional "py_trees", - "websockets" + "websockets", + "click", + "typer", ] # List additional groups of dependencies here (e.g. development @@ -110,7 +112,7 @@ test = [ "Source" = "https://github.com/pypa/sampleproject/" [project.scripts] # Optional -sample = "sample:main" +social-norms-trees = "social_norms_trees.ui_wrapper:app" [build-system] requires = ["hatchling", "hatch-vcs"] diff --git a/src/social_norms_trees/ui_wrapper.py b/src/social_norms_trees/ui_wrapper.py index 0d04878..e6188b3 100644 --- a/src/social_norms_trees/ui_wrapper.py +++ b/src/social_norms_trees/ui_wrapper.py @@ -1,4 +1,6 @@ +import pathlib import time +from typing import Annotated import click from datetime import datetime import json @@ -7,6 +9,8 @@ import py_trees import traceback +import typer + from social_norms_trees.mutate_tree import ( move_node, exchange_nodes, @@ -17,8 +21,6 @@ from social_norms_trees.behavior_library import BehaviorLibrary -DB_FILE = "db.json" - def load_db(db_file): if os.path.exists(db_file): @@ -155,24 +157,36 @@ def run_experiment(db, origin_tree, experiment_id, behavior_library): return db -def main(): +app = typer.Typer() + + +@app.command() +def main( + resources_file: Annotated[ + pathlib.Path, + typer.Argument( + help="file with the experimental context, behavior tree, and behavior library" + ), + ], + db_file: Annotated[ + pathlib.Path, + typer.Option(help="file where the experimental results will be written"), + ] = "db.json", +): print("AIT Prototype #1 Simulator") # TODO: write up some context, assumptions made in the README - # TODO: user query for files - DB_FILE = "db.json" - db = load_db(DB_FILE) + db = load_db(db_file) # load tree to run experiment on, and behavior library - RESOURCES_FILE = "resources.json" - original_tree, behavior_library, context_paragraph = load_resources(RESOURCES_FILE) + original_tree, behavior_library, context_paragraph = load_resources(resources_file) print(f"\nContext of this experiment: {context_paragraph}") participant_id, experiment_id = experiment_setup(db, original_tree) db = run_experiment(db, original_tree, experiment_id, behavior_library) - save_db(db, DB_FILE) + save_db(db, db_file) # TODO: define export file, that will be where we export the results to @@ -183,4 +197,4 @@ def main(): if __name__ == "__main__": - main() + app()