Skip to content

Commit

Permalink
Create an automator for user sessions
Browse files Browse the repository at this point in the history
i used the serializable script thing woo
  • Loading branch information
fenreese committed Nov 20, 2024
1 parent 5cc1a59 commit ad4c120
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 8 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ Also see: the [character recognition API](https://github.com/vialab/JPHandwritin

# Setup

## Sessions

Sessions for each user are loaded from a .json file. These files can be created via a Python script in the root. Run

```sh
python3 create_session.py -h
```

to see what it does.

The resulting file is saved as `SessionData/[userID].json` and a folder named after the ID is created in `SessionLogs`.

## Program

1. Make sure you have Unity 2022.3.11f1 installed.
2. Clone this repository and the [character recognition API repository](https://github.com/vialab/JPHandwritingModel). (Make sure the API repo is checked out to the `fastapi` branch.)
3. Set up the API (there are instructions on its repo).
4. Open this repository in Unity.
5. Put on your VR headset and press Play.
5. Fill the user's ID in by clicking on the Game object and filling in the "User ID" field in the Inspector.
6. Put on your VR headset and press Play.

# Credits
See [CREDITS.md](CREDITS.md).
1 change: 1 addition & 0 deletions SessionData/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
Empty file added SessionData/.gitkeep
Empty file.
7 changes: 0 additions & 7 deletions SessionData/test.json

This file was deleted.

97 changes: 97 additions & 0 deletions create_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import argparse
import json
import os
import pathlib
import random
from typing import Any

# prompt user for User ID, number of items, and number of items with tracing on
# limitations: only built-in libraries

MAX_ITEMS = 2

class VocabItem:
def __init__(self, name: str, tracing: bool) -> None:
self.Type = name
self.EnableTracing = tracing

def to_json(self) -> dict[str, str]:
return {
"Type": self.Type,
"Data": json.dumps({"EnableTracing": self.EnableTracing})
}

def import_items() -> list[str]:
with open("vocab_items.json", "r") as file:
contents = json.load(file)
return contents["VocabItems"]

def generate_items(num_items: int, num_tracing: int) -> list:
vocab = import_items()

generated_vocab = []
num_traced = 0

for i in range(num_items):
tracing = False

if (num_traced < num_tracing):
tracing = bool(random.getrandbits(1))

if (tracing):
num_traced += 1

vocab_item = vocab.pop(random.randint(0, len(vocab) - 1))
generated_vocab.append(VocabItem(vocab_item, tracing))

return generated_vocab

def generate_json(items: list[VocabItem], id: str) -> dict[str, Any]:
return {
"UserID": id,
"VocabItems": [x.to_json() for x in items]
}

def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description = "Automatically creates sessions for users for user studies.",
)

parser.add_argument(
"--id",
type = str,
required = True,
help = "The user ID to generate a session file for."
)

parser.add_argument(
"--items",
type = int,
default = MAX_ITEMS,
help = "The number of items to generate."
)

parser.add_argument(
"--traced-items",
type = int,
default = 0,
help = "The number of items to enable tracing for."
)

return parser.parse_args()

def main():
args = parse_args()
contents = generate_json(generate_items(args.items, args.traced_items), args.id)

with open(os.path.join("SessionData", f"{args.id}.json"), "w") as file:
json_contents = json.dumps(contents)
file.write(json_contents)

print(f"Session has been generated and saved in the SessionData folder as {args.id}.json")

pathlib.Path(f"SessionLogs/{args.id}").mkdir(parents = True, exist_ok = True)
print(f"Created SessionLogs/{args.id} folder")

if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions vocab_items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"VocabItems": [
"Lunchbox",
"Textbook"
]
}

0 comments on commit ad4c120

Please sign in to comment.