Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CLI parameters for resource file paths #40

Merged
merged 15 commits into from
Oct 8, 2024
Merged
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ __pycache__/
# due to using tox and pytest
.tox
.cache
.venv/
.venv/

# Project specific files
db.json
81 changes: 43 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example Experiments

This directory includes configuration files for example experiments.
84 changes: 84 additions & 0 deletions examples/entering-a-room.json
Original file line number Diff line number Diff line change
@@ -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
}
]

}
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ classifiers = [ # Optional

dependencies = [ # Optional
"py_trees",
"websockets"
"websockets",
"click",
"typer",
]

# List additional groups of dependencies here (e.g. development
Expand Down Expand Up @@ -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"]
Expand Down
34 changes: 24 additions & 10 deletions src/social_norms_trees/ui_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pathlib
import time
from typing import Annotated
import click
from datetime import datetime
import json
Expand All @@ -7,6 +9,8 @@
import py_trees
import traceback

import typer

from social_norms_trees.mutate_tree import (
move_node,
exchange_nodes,
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -183,4 +197,4 @@ def main():


if __name__ == "__main__":
main()
app()
2 changes: 1 addition & 1 deletion tests/test_stub.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def test_placeholder():
assert True
assert True