refactor: Refactor run entrypoint scripts - #51
Conversation
📝 WalkthroughWalkthroughAdds utilities to centralize seeding, brain construction, environment creation, and interrupt handling (exports: Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@packages/quantum-nematode/quantumnematode/utils/config_loader.py`:
- Line 4: Update the bare dict annotation used for kwargs to a precise typing
such as dict[str, Any] or Mapping[str, Any] (and import Any/Mapping) so the
signature and uses around the kwargs variable are fully typed; find the
function(s) that declare or accept kwargs (the bare "kwargs" referenced around
lines 819-855) and replace dict with dict[str, Any] or Mapping[str, Any] (or a
specific TypedDict if a known shape exists), updating imports from typing
accordingly.
In `@packages/quantum-nematode/quantumnematode/utils/interrupt_handler.py`:
- Around line 141-142: The error message for invalid choices is wrong—update the
logger.error call that currently says "Please enter a number between 1 and 4."
(the one next to the interrupt choice handling / the else branch that validates
the user's choice and uses logger.error) to reflect the actual valid range 0–2,
e.g. "Invalid choice. Please enter a number between 0 and 2." Ensure the message
is changed where the code validates the variable choice and calls logger.error.
In `@scripts/run_evolution.py`:
- Around line 459-466: Remove the unused Ruff noqa marker by deleting the
trailing comment "# noqa: NPY002" on the np.random.seed(...) call inside the
episode loop; keep the seeding logic as-is (the loop in run_evolution.py that
calls derive_episode_seed, np.random.seed and random.seed) but remove the
unnecessary noqa token so the linter no longer flags an unused suppression.
🧹 Nitpick comments (1)
scripts/run_evolution.py (1)
248-260: Avoid reloading the config on every episode.
evaluate_fitnessalready loads the simulation config;_create_env_for_evolutionre-reads the file on each call, so each episode pays extra I/O and parsing. Consider passing the pre-loaded config or env_config into the helper and reusing it per evaluation.
| for ep in range(episodes): | ||
| # Seed RNGs for reproducibility when base_seed is provided | ||
| if base_seed is not None: | ||
| episode_seed = _derive_episode_seed(base_seed, gen, candidate_idx, ep) | ||
| episode_seed = derive_episode_seed(base_seed, gen, candidate_idx, ep) | ||
| np.random.seed(episode_seed) # noqa: NPY002 | ||
| random.seed(episode_seed) | ||
|
|
||
| env = create_env_from_config(config_path) | ||
| env = _create_env_for_evolution(config_path) |
There was a problem hiding this comment.
Remove the unused Ruff noqa.
Line 463’s # noqa: NPY002 is flagged as unused; drop it to keep lint clean.
🧹 Proposed fix
- np.random.seed(episode_seed) # noqa: NPY002
+ np.random.seed(episode_seed)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| for ep in range(episodes): | |
| # Seed RNGs for reproducibility when base_seed is provided | |
| if base_seed is not None: | |
| episode_seed = _derive_episode_seed(base_seed, gen, candidate_idx, ep) | |
| episode_seed = derive_episode_seed(base_seed, gen, candidate_idx, ep) | |
| np.random.seed(episode_seed) # noqa: NPY002 | |
| random.seed(episode_seed) | |
| env = create_env_from_config(config_path) | |
| env = _create_env_for_evolution(config_path) | |
| for ep in range(episodes): | |
| # Seed RNGs for reproducibility when base_seed is provided | |
| if base_seed is not None: | |
| episode_seed = derive_episode_seed(base_seed, gen, candidate_idx, ep) | |
| np.random.seed(episode_seed) | |
| random.seed(episode_seed) | |
| env = _create_env_for_evolution(config_path) |
🧰 Tools
🪛 Ruff (0.14.14)
[warning] 463-463: Unused noqa directive (non-enabled: NPY002)
Remove unused noqa directive
(RUF100)
🤖 Prompt for AI Agents
In `@scripts/run_evolution.py` around lines 459 - 466, Remove the unused Ruff noqa
marker by deleting the trailing comment "# noqa: NPY002" on the
np.random.seed(...) call inside the episode loop; keep the seeding logic as-is
(the loop in run_evolution.py that calls derive_episode_seed, np.random.seed and
random.seed) but remove the unnecessary noqa token so the linter no longer flags
an unused suppression.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
scripts/run_evolution.py (1)
433-452:⚠️ Potential issue | 🟠 MajorPass
body_length/episode seed into the env factory to avoid behavior drift.
After the refactor,create_env_from_configis called withoutmax_body_lengthorseed, soSimulationConfig.body_lengthand the per-episode seed are ignored. This can change agent size and reproducibility.🔧 Suggested fix
- for ep in range(episodes): - # Seed RNGs for reproducibility when base_seed is provided - if base_seed is not None: - episode_seed = derive_episode_seed(base_seed, gen, candidate_idx, ep) - np.random.seed(episode_seed) # noqa: NPY002 - random.seed(episode_seed) - - env = create_env_from_config(env_config) + for ep in range(episodes): + episode_seed: int | None = None + # Seed RNGs for reproducibility when base_seed is provided + if base_seed is not None: + episode_seed = derive_episode_seed(base_seed, gen, candidate_idx, ep) + np.random.seed(episode_seed) # noqa: NPY002 + random.seed(episode_seed) + + env = create_env_from_config( + env_config, + seed=episode_seed, + max_body_length=config.body_length, + )
🧹 Nitpick comments (2)
packages/quantum-nematode/quantumnematode/utils/config_loader.py (1)
819-859: Switchcreate_env_from_configdocstring to NumPy style.
The new docstring usesArgs:; please convert to NumPyParameters/Returnssections to match the repo standard.As per coding guidelines, Use NumPy-style docstrings for documentation.♻️ Example docstring update
- Args: - env_config: Parsed environment configuration. - seed: Optional seed for environment RNG. - max_body_length: Optional max body length for the agent. - theme: Optional rendering theme. - - Returns - ------- - Configured DynamicForagingEnvironment instance. + Parameters + ---------- + env_config : EnvironmentConfig + Parsed environment configuration. + seed : int | None + Optional seed for environment RNG. + max_body_length : int | None + Optional max body length for the agent. + theme : Theme | None + Optional rendering theme. + + Returns + ------- + DynamicForagingEnvironment + Configured DynamicForagingEnvironment instance.packages/quantum-nematode/quantumnematode/utils/interrupt_handler.py (1)
32-143: Convert new interrupt-handler docstrings to NumPy style.
Both new docstrings useArgs:; please switch to NumPyParameters/Returnssections.As per coding guidelines, Use NumPy-style docstrings for documentation.♻️ Example update (apply similarly to both functions)
- Args: - max_steps: Maximum number of steps for the simulation. - brain_type: Type of brain architecture used. - qubits: Number of qubits used. - timestamp: Timestamp for the current session. - agent: The simulation agent. - all_results: List of results for each run. - total_runs_done: Total runs completed so far. - tracking_data: Data tracked during the simulation. - plot_dir: Directory where plots will be saved. - plot_results_fn: Optional callable for plotting results. + Parameters + ---------- + max_steps : int + Maximum number of steps for the simulation. + brain_type : BrainType + Type of brain architecture used. + qubits : int + Number of qubits used. + timestamp : str + Timestamp for the current session. + agent : QuantumNematodeAgent + The simulation agent. + all_results : list[SimulationResult] + List of results for each run. + total_runs_done : int + Total runs completed so far. + tracking_data : TrackingData + Data tracked during the simulation. + plot_dir : Path + Directory where plots will be saved. + plot_results_fn : Callable[..., Any] | None + Optional callable for plotting results.Also applies to: 145-167
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/quantum-nematode/quantumnematode/utils/interrupt_handler.py`:
- Around line 84-90: The input() call in interrupt_handler.py that reads into
the variable choice can raise EOFError in non-interactive environments and
currently crashes the halt flow; add an except EOFError handler (alongside the
existing KeyboardInterrupt/ValueError handlers) that treats EOF as an exit
signal — e.g., log/handle via logger (similar to KeyboardInterrupt handling) and
break out or set choice to the exit code so the halt menu cleanly exits instead
of propagating the exception.
…ypoint-scripts refactor: Refactor run entrypoint scripts
Changes:
Summary by CodeRabbit
New Features
Refactor
Chores
✏️ Tip: You can customize this high-level summary in your review settings.