Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# .env.docker (container)
CONTAINER_WORKSPACE=/app
PYTHONPATH_CONTAINER=/app/src
NFL_HOME_CONTAINER=/app
PYTHONPATH_CONTAINER=/app/src
14 changes: 6 additions & 8 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ jobs:
version: "latest"

- name: Sync uv
run: |
uv sync --locked
run: uv sync --locked

- name: Create features
run: |
source environment.sh
uv run src/create_features.py
run: uv run src/create_features.py

- name: Train transformer
run: |
source environment.sh
uv run src/train_transformer.py
run: uv run src/train_transformer.py

- name: Make week 9 predictions
run: uv run src/generate_predictions.py

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ output/**/*.txt

# MLflow
mlruns/
mlflow.db

# Ray
log/

# Artifacts
*.pkl
Expand Down
5 changes: 0 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"justMyCode": false,
"subProcess": true,
"env": {
"NFL_HOME": "${env:NFL_HOME}",
"PYTHONPATH": "${workspaceFolder}"
},
"envFile": "${workspaceFolder}/.env",
Expand All @@ -24,14 +23,12 @@
"request": "launch",
"program": "${workspaceFolder}/src/main.py",
"args": [
"--data-root", "${env:NFL_HOME}"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"justMyCode": false,
"subProcess": true,
"env": {
"NFL_HOME": "${env:NFL_HOME}",
"PYTHONPATH": "${workspaceFolder}"
},
"envFile": "${workspaceFolder}/.env",
Expand All @@ -48,7 +45,6 @@
"justMyCode": false,
"subProcess": true,
"env": {
"NFL_HOME": "${env:NFL_HOME}",
"PYTHONPATH": "${workspaceFolder}"
},
"envFile": "${workspaceFolder}/.env",
Expand All @@ -63,7 +59,6 @@
"cwd": "${workspaceFolder}",
"justMyCode": false,
"env": {
"NFL_HOME": "${env:NFL_HOME}",
"PYTHONPATH": "${workspaceFolder}"
},
"envFile": "${workspaceFolder}/.env",
Expand Down
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@ RUN uv sync --frozen --no-dev --no-install-project
# Copy the rest of your repo
COPY . .

# Default envs helpful for your project
ENV NFL_HOME=/app

# Set a sensible default; override in docker-compose.yml
CMD ["bash"]
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,34 @@ curl -LsSf https://astral.sh/uv/install.sh | sh
To create and activate the virtual environment, run the following:
```bash
uv sync --locked
source .venv/bin/activate
```

## Run instructions
```bash
# Source the environmental variables
source environment.sh

# Digest the raw data and create features
uv src/create_features.py
uv run src/create_features.py

# Train the model
uv src/train_transformer.py
uv run src/train_transformer.py

# Run inference on week 9
uv src/generate_predictions.py
uv run src/generate_predictions.py

# From this point you can explore the notebooks/predictions.ipynb, adjust the data path as neccesary and see the animations and plots
```

## Activating MLflow
This project uses MLflow to manage ML models and track experiments from hyper paremter optimization. To start MLflow simply:
```bash
# Make sure your virtual environment is started if you opened a new terminal
# Make sure your virtual environment is sourced so the terminal recognizes mlflow
source .venv/bin/activate

# Start the tracking server
mlflow ui --backend-store-uri ./mlruns
mlflow ui \
--backend-store-uri sqlite:///mlflow.db \
--host 0.0.0.0 \
--port 5000
```

The last command will start the MLflow GUI at your local host loopback on port 5000: http://127.0.0.1:5000
Binary file added data/training/best_model.pth
Binary file not shown.
9 changes: 9 additions & 0 deletions data/training/model_params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"epochs": 50,
"model_dim": 64,
"num_heads": 2,
"num_layers": 4,
"dropout": 0.1,
"batch_size": 64,
"checkpoint_freq": 10
}
4 changes: 0 additions & 4 deletions environment.sh

This file was deleted.

3 changes: 1 addition & 2 deletions notebooks/eda.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "0fe17705",
"metadata": {},
"outputs": [],
Expand All @@ -15,7 +15,6 @@
"\n",
"from load_data import RawDataLoader\n",
"\n",
"os.environ[\"NFL_HOME\"] = \"/home/sam/repos/hobby-repos/nfl/\"\n",
"rawLoader = RawDataLoader()\n",
"games_df, plays_df, players_df, location_data_df = rawLoader.get_data(weeks=[i for i in range(1, 10)])"
]
Expand Down
68 changes: 34 additions & 34 deletions notebooks/predictions.ipynb

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions notebooks/train_lstm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "1df8838e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -54,8 +54,6 @@
"#################################################################\n",
"\n",
"import os\n",
"os.environ[\"NFL_HOME\"] = \"/home/sam/repos/hobby-repos/nfl/\"\n",
"\n",
"from common.data_loader import RawDataLoader\n",
"\n",
"# Get raw data\n",
Expand Down
27 changes: 1 addition & 26 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,17 @@ dependencies = [
"pandas>=2.3.2",
"pip-tools>=7.5.0",
"polars>=1.35.1",
"ray[train,tune]>=2.51.1",
"scikit-learn>=1.7.2",
"seaborn>=0.13.2",
"torch>=2.8.0",
"tqdm>=4.67.1",
"xgboost>=3.0.5",
]

[project.optional-dependencies]
dev = [
"pytest>=7.0",
"black>=24.0",
"flake8>=6.0",
"mypy>=1.0"
]
# you can define other "extras", e.g.:
# docs = ["sphinx", "sphinx-rtd-theme"]

[project.scripts]
# command = "module:function"
myproject-cli = "myproject.cli:main"

[tool.setuptools.packages.find]
where = ["src"]

# Tool-specific configuration examples:

[tool.black]
line-length = 88
target-version = ["py39", "py310", "py311", "py312"]

[tool.isort]
profile = "black"

[tool.flake8]
max-line-length = 88
extend-ignore = ["E203"]

[tool.mypy]
strict = true
21 changes: 21 additions & 0 deletions src/common/args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python

"""
Parses input arguments to then act on in code
"""

import argparse

def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--tune",
action="store_true",
help="Use Ray Tune to search hyperparameters instead of a single training run",
)
parser.add_argument(
"--profile",
action="store_true",
help="Enable @time_fcn timing decorators for profiling",
)
return parser.parse_args()
4 changes: 2 additions & 2 deletions src/common/csv_to_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Transforms raw csv data to raw parquet data

Requires:
- Raw location tracking data in NFL_HOME/data/parquet
- Raw location tracking data in nfl/data/parquet

Outputs parquet data in NFL_HOME/data/parquet that is stored in git LFS
Outputs parquet data in nfl/data/parquet that is stored in git LFS
"""

import sys
Expand Down
36 changes: 28 additions & 8 deletions src/common/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@
Contains common use decorators
"""

def time_fcn(func):
import time
from functools import wraps
import time
import functools
import logging

@wraps(func)
_TIME_DECORATORS_ENABLED = False

def set_time_decorators_enabled(enabled: bool) -> None:
"""
Enable/disable time_fcn decorators globally, called at startup
"""
global _TIME_DECORATORS_ENABLED
_TIME_DECORATORS_ENABLED = enabled

def time_fcn(fn):
"""
Decorator that measures wall-clock time of a function and logs it.
When _TIME_DECORATORS_ENABLED is False, it becomes a simple passthrough.
"""
@functools.wraps(fn)
def wrapper(*args, **kwargs):
# Fast exit when disabled (minimal overhead)
if not _TIME_DECORATORS_ENABLED:
return fn(*args, **kwargs)

start = time.perf_counter()
result = func(*args, **kwargs)
print(f"{func.__name__} took {time.perf_counter() - start:.4f}s")
return result
return wrapper
try:
return fn(*args, **kwargs)
finally:
elapsed = time.perf_counter() - start
logging.info("Function %s took %.4f s", fn.__name__, elapsed)
return wrapper
12 changes: 12 additions & 0 deletions src/common/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pathlib import Path

def project_root() -> Path:
# 2 since this is at src/commmon/path.py
return Path(__file__).resolve().parents[2]

# Set project root
PROJECT_ROOT = project_root()

# Create processing dirs
SAVE_DIR = PROJECT_ROOT / "data" / "processed"
SAVE_DIR.mkdir(parents=True, exist_ok=True)
Loading