Skip to content

Add voltage angles to observation vector - #770

Open
Gaurav890 wants to merge 2 commits into
Grid2op:dev_1.12.6from
Gaurav890:codex/fix-observation-theta-vector
Open

Add voltage angles to observation vector#770
Gaurav890 wants to merge 2 commits into
Grid2op:dev_1.12.6from
Gaurav890:codex/fix-observation-theta-vector

Conversation

@Gaurav890

Copy link
Copy Markdown

Summary

  • add theta_or, theta_ex, load_theta, gen_theta, and storage_theta to the complete observation vector
  • append the angle fields so existing observation-vector offsets remain unchanged
  • preserve the pre-1.12.6 vector layout when loading older serialized observations
  • document the unsupported-backend zero convention and update regression expectations

Why

Voltage-angle attributes were only present in attr_list_json, so to_vect() omitted them and from_vect() could not reconstruct them. Backends that do not expose voltage angles remain supported: Grid2Op already fills these arrays with zeros and reports their availability through support_theta.

Fixes #711.

Validation

  • python -m unittest grid2op.tests.test_Observation — 83 passed, 1 skipped
  • python -m unittest grid2op.tests.test_attached_envs_compat — 26 passed
  • python -m unittest grid2op.tests.test_Runner — 19 passed
  • python -m unittest grid2op.tests.test_GymConverter — 20 passed
  • focused attached-environment observation-size tests — 4 passed
  • pre-commit secret scan and mailmap check — passed

Signed-off-by: gaurav890 <gauravchaulagain0@gmail.com>
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@Gaurav890
Gaurav890 marked this pull request as ready for review August 13, 2026 06:17
@BDonnot

BDonnot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Hello,

Sorry for the really long delay in my response.

I see that you took care of pretty much everything: backward compatibility and compatibility with all backends, not just the ones supporting it. That is awesome.

I will have a look ASAP.

Theta is "reference dependant": the same system with all theta shifted by say 10 (rad, deg or whatever unit) will be exactly the same. This was the real reason why I did not implemented it on to_vect().
My intuition was that it was difficult (I would say impossible) for a ML algorithm to learn with input data like this.
In json or in gymnasium dict the problem is different: you can also substract the average or the median (or any statistics) to all the theta values of the same grid to get something "meaningful" (or at least not poluted by the choice of the reference)

Thanks for this work :-)

@BDonnot

BDonnot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

An issue needs to be fixed:

======================================================================
ERROR: test_to_from_gym_obs (test_GymConverter.TestWithoutConverterStorage.test_to_from_gym_obs)

Traceback (most recent call last):
File "/Grid2Op/grid2op/tests/test_GymConverter.py", line 104, in test_to_from_gym_obs
self._aux_test_json(obs_space, gym_obs)
File "/Grid2Op/grid2op/tests/test_GymConverter.py", line 48, in _aux_test_json
assert np.all(np.abs(float(obj[k]) - float(obj2[k])) <= self.tol)
^^^^^^^^^^^^^
TypeError: only 0-dimensional arrays can be converted to Python scalars


If I read that properly it's probably because something has been added (most likely the theta) automatically to the gymnasium API and it is not declared "vector".

@BDonnot BDonnot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change in the CHANGELOG.

Some tests are needed, for example in BaseObservation: to test that the obs converted to vector has the right theta information.

Comment thread CHANGELOG.rst Outdated
Signed-off-by: gaurav890 <gauravchaulagain0@gmail.com>
@Gaurav890

Copy link
Copy Markdown
Author

Thanks for the review. I addressed the feedback in a89de57:

  • moved the changelog item to the "Better handling of the voltages" section;
  • strengthened the observation-vector regression test with distinct nonzero values for all five theta attributes, including storage, and verified both the exact vector slices and from_vect reconstruction;
  • fixed the Gym JSON regression on newer NumPy versions. The theta entries were already declared as vector Box spaces; the failure came from the test helper calling float(...) on every one-element array. Arrays are now compared elementwise;
  • documented that voltage angles depend on the backend reference and that users can subtract a common reference or statistic for reference-invariant inputs.

I kept the raw angle values in the core vector so from_vect(to_vect(obs)) remains lossless. Reference normalization can still be applied in the Gym observation converter or downstream preprocessing.

Local validation:

  • python -m unittest grid2op.tests.test_Observation — 83 passed, 1 skipped
  • python -m unittest grid2op.tests.test_GymConverter — 20 passed
  • python -m unittest grid2op.tests.test_attached_envs_compat — 26 passed
  • pre-commit secret scan and mailmap check — passed

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