DLR-RM / DLR-RM/stable-baselines3

[Bug]: Monitor with override_existing=False writes no header when the file is new

Open Beginner friendly
#2,282 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
13.8k
Forks
2.2k
Avg merge
1h 35m
Merged PRs (30d)
2

Description

### 🐛 Bug

`Monitor(..., override_existing=False)` with a `filename` that does not exist yet creates the file in append mode without writing the `#{json header}` line or the CSV column names. `ResultsWriter.__init__` only writes the header when `override_existing=True`, so the first run of a script that uses the append mode (the use case `override_existing` was added for in #1037 / #1035) produces a `monitor.csv` that `load_results()` and `results_plotter` cannot read.

### To Reproduce

```python
import gymnasium as gym
from stable_baselines3.common.monitor import Monitor, load_results

env = Monitor(gym.make("CartPole-v1"), "logs/run.monitor.csv", override_existing=False) # path does not exist yet
obs, _ = env.reset()
done = False
while not done:
obs, r, terminated, truncated, _ = env.step(env.action_space.sample())
done = terminated or truncated
env.close()

print(open("logs/run.monitor.csv").read()) # first line is a data row, no "#{...}" header, no "r,l,t" line
load_results("logs") # AssertionError
```

### Relevant log output / Error message

```
AssertionError (raised by load_results: the first line is a data row, not "#{...}")
```

### System Info

- stable-baselines3 master (de477a1's parent), Python 3.11, Windows 11; also reproduces on Linux since the logic is platform independent.

### Checklist

- [x] My issue does not relate to a custom gym environment.
- [x] I have checked that there is no similar issue in the repo
- [x] I have read the documentation
- [x] I have provided a minimal and working example to reproduce the bug
- [x] I've used the markdown code blocks for both code and stack traces.

Contributor guide

Open the contributing guide

Research direction

Locate ResultsWriter.__init__ and the load_results entry point, then reproduce the new-file case from the issue's example with override_existing=False. Confirm that the resulting CSV begins with the JSON header and column names and that load_results and results_plotter can read it without an AssertionError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.