Skip to content

Make synthetic weather deterministic (#43) - #99

Open
Srikar-Kolli wants to merge 2 commits into
mainfrom
43/deterministic-synthetic-weather
Open

Srikar-Kolli wants to merge 2 commits into
mainfrom
43/deterministic-synthetic-weather

Conversation

@Srikar-Kolli

Copy link
Copy Markdown

Summary

Makes WeatherService.generate_synthetic_data() deterministic so tests and comparisons get the same weather every run.

  • Uses a local np.random.RandomState(seed) instead of the global np.random. The default seed is 7, the same as the sweeps used, so existing sweep output is unchanged.
  • Adds synthetic_weather_seed to LapsRaceSimulator and passes it through run_fixed_speed_sweep and run_strategy_sweep. The sweeps no longer call np.random.seed.
  • Edge-case fixes:
    • A race starting exactly at 13:00 raised ZeroDivisionError.
    • Race windows shorter than the 15/30-minute smoothing kernels returned arrays longer than the time axis. A new _moving_average helper fixes this and gives the same values as before for normal windows.
    • An empty or reversed race window now raises a clear ValueError.
  • Fixes two tests/planning/test_csv.py tests that were already failing on main. They used the old lap_distance_km field and fake args that no longer match main.py.

Testing

  • pytest tests: 143 passed, 0 failed.
  • New tests/test_weather.py (44 tests) covers:
    • Determinism: repeated calls, separate instances, and different seeds.
    • Global random state is neither read nor changed.
    • No network calls.
    • Seed 7 reproduces the old np.random.seed(7) output.
    • Array lengths and value ranges.
    • Edge cases: start at 13:00, start after solar noon, windows of 1 to 31 minutes, empty and reversed windows, fractional hours, and a full-day window.
    • Two end-to-end offline simulator runs that must give identical results.
  • python3 -m compileall sim passes.

Seed a local RandomState in generate_synthetic_data (default seed 7, matching
the sweeps) instead of using global np.random. Thread synthetic_weather_seed
through LapsRaceSimulator and the sweeps. Fix zero-division when the race
starts at 13:00, short-window smoothing length mismatch, and add a clear error
for empty race windows. Add tests/test_weather.py and fix stale test_csv tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Kamal-uva
Kamal-uva self-requested a review October 25, 2026 18:23
@Kamal-uva Kamal-uva self-assigned this Oct 25, 2026
@Kamal-uva

Copy link
Copy Markdown
Contributor

Great work on the deterministic weather implementation and thorough test coverage! The changes look good overall, but there's a merge conflict with the newer time-step functionality on main that we need to resolve before merging.

Changes needed:

  1. sim/weather.py: Preserve resample_to_time_step() from main (including its existing implementation), since the PR branch was created before that functionality was added. Keep the calls to this helper in both fetch_weather_data() and generate_synthetic_data() so API and synthetic weather continue supporting configurable simulation time steps.

  2. Preserve the PR's changes: Keep DEFAULT_SYNTHETIC_SEED, the local np.random.RandomState(seed), the _moving_average() helper, race-window validation, and seed propagation through LapsRaceSimulator and the planning sweeps. The final generate_synthetic_data() should generate deterministic weather, apply the new smoothing helper, and then return the arrays through resample_to_time_step().

  3. tests/test_weather.py: Update the race() helper to include time_step_minutes=1.0, since the merged implementation now accesses that attribute. Add tests for non-default time steps (e.g., 5-minute aggregation and 0.5-minute interpolation), verifying that the returned time, GHI, and cloud-cover arrays have matching lengths and correct time spacing.

  4. Regression testing: Confirm that the existing deterministic weather tests still pass, particularly the seed-7 legacy output comparison at the default 1-minute step. Also verify that the new time-step tests on main still pass and that no existing functionality is removed during conflict resolution.

After updating the branch with main and resolving the conflict, please run python3 -m pytest tests and python3 -m compileall sim to confirm everything works together.

The goal is to integrate the deterministic weather improvements without regressing the recently merged time-step functionality. Once that's resolved and tests pass, this should be ready to merge!

Keep resample_to_time_step() from main alongside the seeded RandomState and
_moving_average(); generate_synthetic_data() now returns through the resampler.
Take main's test_csv fixes, let the stub simulators accept synthetic_weather_seed,
and add time-step tests to tests/test_weather.py.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants