# Post-Hoc Analysis

After training, SLM Lab automatically generates graphs and metrics. You can regenerate these anytime with updated styling or new metrics.

## What SLM Lab Generates

Every training run produces:

**Training curves** showing reward over time:

![Example Training Curve](https://huggingface.co/datasets/SLM-Lab/benchmark/resolve/main/data/ppo_cartpole_2026_01_30_221924/ppo_cartpole_t0_trial_graph_mean_returns_vs_frames.png)

**Moving average curves** for smoother visualization:

![Example Moving Average](https://huggingface.co/datasets/SLM-Lab/benchmark/resolve/main/data/ppo_cartpole_2026_01_30_221924/ppo_cartpole_t0_trial_graph_mean_returns_ma_vs_frames.png)

## Regenerating Analysis

Use `retro_analyze` to regenerate graphs and metrics without re-running training:

## Basic Usage

```bash
uv run python -c 'from slm_lab.experiment import retro_analysis; retro_analysis.retro_analyze("data/ppo_lunar_2026_01_30_221924")'
```

This regenerates:

* All graphs (PNG and HTML)
* Trial-level aggregated metrics
* Experiment summary (for search runs)

## What Gets Regenerated

| Artifact          | Location                             | Effect        |
| ----------------- | ------------------------------------ | ------------- |
| Trial graphs      | `*_trial_graph_*.png` (root)         | Overwritten   |
| Trial metrics     | `*_trial_metrics_scalar.json` (root) | Overwritten   |
| Session graphs    | `graph/*_session_graph_*.png`        | Overwritten   |
| Session data      | `info/*_session_df_{train,eval}.csv` | **Preserved** |
| Model checkpoints | `model/*.pt`                         | **Preserved** |
| Spec file         | `*_spec.json` (root)                 | **Preserved** |

{% hint style="success" %}
**Safe to run**: Retro analysis only overwrites derived data. Your raw session data, trained models, and spec files are never modified.
{% endhint %}

## Common Use Cases

### Update Graph Styling

SLM Lab updates Plotly styling periodically. Regenerate graphs to get the latest look:

```bash
# Pull latest code
git pull
uv sync

# Regenerate all experiments in data/
for dir in data/*/; do
    uv run python -c "from slm_lab.experiment import retro_analysis; retro_analysis.retro_analyze('$dir')"
done
```

### Recompute Metrics After Code Changes

If you modify the analysis module (e.g., add a new metric):

```bash
# After modifying slm_lab/experiment/analysis.py
uv run python -c 'from slm_lab.experiment import retro_analysis; retro_analysis.retro_analyze("data/ppo_lunar_2026_01_30_221924")'

# Check updated metrics
cat data/ppo_lunar_2026_01_30_221924/ppo_lunar_t0_trial_metrics_scalar.json
```

### Generate Publication-Quality Graphs

For papers or presentations, you may want higher-resolution or different formats:

```python
from slm_lab.experiment import retro_analysis

# Regenerate with custom settings
retro_analysis.retro_analyze('data/ppo_lunar_2026_01_30_221924')

# The HTML files support interactive exploration
# PNG files are ready for documents
```

## Batch Processing

Process multiple experiments:

```python
import os
from slm_lab.experiment import retro_analysis

data_dirs = [d for d in os.listdir('data') if os.path.isdir(f'data/{d}')]

for data_dir in data_dirs:
    print(f"Processing {data_dir}...")
    retro_analysis.retro_analyze(f'data/{data_dir}')
```

## Troubleshooting

### "Session data not found"

Ensure the data folder contains `*_session_df_train.csv` and `*_session_df_eval.csv` files in the `info/` subfolder. These are required for analysis.

### Graphs look wrong

Check that your SLM Lab version matches the data format. Very old experiments may need manual migration.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slm-lab.gitbook.io/slm-lab/analyzing-results/post-hoc-analysis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
