Skip to content

Commit

Permalink
More Experiment/Event fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckierDodge committed Dec 1, 2023
1 parent 785fb71 commit 7e6ea89
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 68 deletions.
13 changes: 12 additions & 1 deletion wei/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Config:
configured = False
workcell_file = Path("tests/test_workcell.yaml")
workcell_name = "test_workcell"
use_kafka = False
kafka_server = ""
reset_locations = True
update_interval = 10.0
Expand Down Expand Up @@ -47,6 +48,10 @@ def load_config(args: Namespace) -> None:
Config.configured = True
Config.test = False

# Kafka Config
Config.use_kafka = args.use_kafka
Config.kafka_server = args.kafka_server

@staticmethod
def parse_args() -> Namespace:
"""Parse engine's command line arguments."""
Expand All @@ -64,10 +69,16 @@ def parse_args() -> Namespace:
help="port for Redis server",
default=6379,
)
parser.add_argument(
"--use_kafka",
type=bool,
help="Whether or not to log events to kafka",
default=False,
)
parser.add_argument(
"--kafka_server",
type=str,
help="url (no port) for Redis server",
help="url (including port, if necessary) for Kafka server",
default="ec2-54-160-200-147.compute-1.amazonaws.com:9092",
)
parser.add_argument(
Expand Down
9 changes: 3 additions & 6 deletions wei/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import requests

from wei.config import Config


class Event:
"""A single event in an experiment"""
Expand All @@ -20,8 +22,6 @@ def __init__(
server_addr: str,
server_port: str,
experiment_id: str,
wei_internal: bool = False,
use_kafka: bool = False,
) -> None:
"""Initializes an Event Logging object
Expand All @@ -42,16 +42,13 @@ def __init__(
experiment_path: Optional[str]
Path for logging the experiment on the server
use_kafka: Optional[bool]
Use diaspora kafka backend
"""
self.server_addr = server_addr
self.server_port = server_port
self.experiment_id = experiment_id
self.url = f"http://{self.server_addr}:{self.server_port}"
self.wei_internal = wei_internal

if use_kafka:
if Config.use_kafka:
from diaspora_event_sdk import KafkaProducer

self.kafka_producer = KafkaProducer()
Expand Down
66 changes: 9 additions & 57 deletions wei/core/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import fnmatch
import os
from pathlib import Path
from typing import Dict, Optional
from typing import Optional

import ulid

from wei.config import Config
from wei.core.events import Events


class Experiment:
Expand Down Expand Up @@ -44,11 +43,14 @@ def __init__(

def get_experiment_name(self) -> str:
"""Returns the name of the experiment using the experiment_id"""
return [
filename
for filename in os.listdir(str(self.experiments_dir))
if fnmatch.fnmatch(filename, f"*{self.experiment_id}*")
][0].split("_id_")[0]
try:
return [
filename
for filename in os.listdir(str(self.experiments_dir))
if fnmatch.fnmatch(filename, f"*{self.experiment_id}*")
][0].split("_id_")[0]
except IndexError:
return "Unnamed"

@property
def experiment_dir(self) -> Path:
Expand All @@ -66,53 +68,3 @@ def experiment_log_file(self) -> Path:
def run_dir(self) -> Path:
"""Path to the result directory"""
return self.experiment_dir / "runs"


def get_experiment_log_directory(
experiment_id: str,
) -> Path:
"""Returns the path to an experiment's log directory"""
return (
Config.data_directory
/ "experiment"
/ (str(Experiment(experiment_id).experiment_name) + "_id_" + experiment_id)
)


def create_experiment(
experiment_name: str,
experiment_id: Optional[str] = None,
) -> Dict[str, Path]:
"""Create the files for logging and results of the system and log the start of the Experiment
Parameters
----------
experiment_name : str
The user-created name of the experiment
value: ulid, str
the auto-created ulid of the experiment
Returns
-------
Dict
A dictionary with the experiment log_dir value"""

experiment = Experiment(
experiment_name=experiment_name, experiment_id=experiment_id
)
experiment.experiment_dir.mkdir(parents=True, exist_ok=True)
experiment.run_dir.mkdir(parents=True, exist_ok=True)

events = Events(
server_addr=Config.server_host,
server_port=Config.server_port,
experiment_id=experiment.experiment_id,
)
events.start_experiment()
return {
"experiment_id": experiment.experiment_id,
"experiment_name": experiment.experiment_name,
"experiment_path": str(experiment.experiment_dir),
}
25 changes: 21 additions & 4 deletions wei/routers/experiments.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""
Router for the "experiments"/"exp" endpoints
"""
from pathlib import Path
from typing import Dict, Optional

from fastapi import APIRouter
from fastapi.responses import FileResponse, JSONResponse

from wei.core.experiment import Experiment, create_experiment
from wei.config import Config
from wei.core.events import Events
from wei.core.experiment import Experiment
from wei.core.loggers import WEI_Logger

router = APIRouter()
Expand Down Expand Up @@ -43,7 +44,7 @@ async def get_file(filepath: str) -> FileResponse:
def get_experiment(
experiment_name: str,
experiment_id: Optional[str] = None,
) -> Dict[str, Path]:
) -> Dict[str, str]:
"""Pulls an experiment and creates the files and logger for it
Parameters
Expand All @@ -59,4 +60,20 @@ def get_experiment(
"""

return create_experiment(experiment_name, experiment_id)
experiment = Experiment(
experiment_name=experiment_name, experiment_id=experiment_id
)
experiment.experiment_dir.mkdir(parents=True, exist_ok=True)
experiment.run_dir.mkdir(parents=True, exist_ok=True)

events = Events(
server_addr=Config.server_host,
server_port=Config.server_port,
experiment_id=experiment.experiment_id,
)
events.start_experiment()
return {
"experiment_id": experiment.experiment_id,
"experiment_name": experiment.experiment_name,
"experiment_path": str(experiment.experiment_dir),
}

0 comments on commit 7e6ea89

Please sign in to comment.