Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log actions in envs #32

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/pymgrid/envs/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def step(self, action, normalized=True):

"""
action = self.convert_action(action)
self._log_action(action)

obs, reward, done, info = self.run(action, normalized=normalized)
obs = self._get_obs(obs)
self.step_callback(**self._get_step_callback_info(action, obs, reward, done, info))
Expand All @@ -244,6 +246,12 @@ def convert_action(self, action):
"""
pass

def _log_action(self, action):
self._microgrid_logger.log(
**{f'converted_action_{k}': v for k, v in action.items()},
**{f'denormalized_converted_action_{k}': v for k, v in self.microgrid_action_space.denormalize(action).items()}
)

def _get_obs(self, obs):
if self.observation_keys:
obs = self.state_series(normalized=True).loc[pd.IndexSlice[:, :, self.observation_keys]]
Expand Down
6 changes: 3 additions & 3 deletions src/pymgrid/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def flush(self):

def log(self, **log_dict):
for key, value in log_dict.items():
if key not in self:
self[key] = []

try:
self[key].append(value.item())
except AttributeError:
self[key].append(value)
except KeyError:
self[key] = [np.nan]*self._log_length
self[key].append(value)

self._log_length += 1

Expand Down