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

feat: improve test coverage and clear redundant code. #238

Merged
merged 7 commits into from
May 19, 2023

Conversation

Gaiejj
Copy link
Member

@Gaiejj Gaiejj commented May 13, 2023

Description

  • Update the test coverage from 93% to 97%
  • Activate collect offline dataset test file.
  • Clearing model-based redundant code.
  • Support offline dataset downloading test. The test dataset can be found in the link of offline dataset.
  • Some test file to improve whole project test coverage.

Motivation and Context

Close issue #237

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.
  • I have reformatted the code using make format. (required)
  • I have checked the code using make lint. (required)
  • I have ensured make test pass. (required)

@Gaiejj Gaiejj added enhancement New feature or request algorithm Some issues about algorithm labels May 13, 2023
@Gaiejj Gaiejj changed the title Dev test feat: improve test coverage and clear redundant code. May 13, 2023
Comment on lines +163 to +165
if self.bias is not None:
return torch.add(w_times_x, self.bias[:, None, :]) # w times x + b
return w_times_x # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

The original code exists some error.
If self.bias is None, then return torch.add(w_times_x, self.bias[:, None, :]) will take a slice of None. So I use an if else here.
Then, as mypy thinks that the code will not be reached, I wonder line 165 may be used in future model-based code, so I just disable mypy here.

Comment on lines -415 to -431

def _log_when_not_update(self) -> None:
"""Log when not update."""
self._logger.store(
**{
'Loss/Loss_reward_critic': 0.0,
'Loss/Loss_pi': 0.0,
'Value/reward_critic': 0.0,
},
)
if self._cfgs.algo_cfgs.use_cost:
self._logger.store(
**{
'Loss/Loss_cost_critic': 0.0,
'Value/cost_critic': 0.0,
},
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Simply unused, so I delete, the same as others.

Comment on lines +93 to +102
'SafetyPointGoal1-v0_data_test': OfflineMeta(
url='https://drive.google.com/file/d/1JPJ127bWM_Tdej0AEGoFAqFFG9mWtzsN/view?usp=share_link',
sha256sum='417b580cd4ef8f05a66d54c5d996b35a23a0e6c8ff8bae06807313a638df2dc6',
episode_length=1,
),
'SafetyPointGoal1-v0_data_init_test': OfflineMeta(
url='https://drive.google.com/file/d/1WlfkoUvWuFUYVMlGwi_EdGO914oWndpV/view?usp=share_link',
sha256sum='fce6cc1fd0c294a8b66397f2f5276c9e7055821ded1f3a6e58e491eb342b1fbe',
episode_length=1,
),
Copy link
Member Author

Choose a reason for hiding this comment

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

The test dataset to test offline dataset. Each dataset is only 0.1MB.

Comment on lines +87 to +106
with open(
os.path.join(
f'{custom_cfgs["logger_cfgs"]["log_dir"]}',
terminal_log_name,
),
'w',
encoding='utf-8',
)
# pylint: disable-next=consider-using-with
sys.stderr = open( # noqa: SIM115
os.path.join(f'{custom_cfgs["logger_cfgs"]["log_dir"]}', error_log_name),
'w',
encoding='utf-8',
)
agent = omnisafe.Agent(algo, env_id, custom_cfgs=custom_cfgs)
reward, cost, ep_len = agent.learn()
) as f_out:
sys.stdout = f_out
with open(
os.path.join(
f'{custom_cfgs["logger_cfgs"]["log_dir"]}',
error_log_name,
),
'w',
encoding='utf-8',
) as f_error:
sys.stderr = f_error
agent = omnisafe.Agent(algo, env_id, custom_cfgs=custom_cfgs)
reward, cost, ep_len = agent.learn()
Copy link
Member Author

Choose a reason for hiding this comment

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

Use with open instead of previous version to meet pylint requirements.

@@ -225,6 +224,7 @@ def test_cem_based(algo):
}
agent = omnisafe.Agent(algo, env_id, custom_cfgs=custom_cfgs)
agent.learn()
agent.render()
Copy link
Member Author

@Gaiejj Gaiejj May 13, 2023

Choose a reason for hiding this comment

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

As model-based algorithms have many branches when evaluating, I render all of them to make a trust-worthy test.

@Gaiejj Gaiejj marked this pull request as ready for review May 13, 2023 17:29
Comment on lines -250 to -251
except yaml.YAMLError as exc:
raise AssertionError(f'{path} error: {exc}') from exc
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, here will raise FileNotFoundError instead of yaml.YAMLError.

Copy link
Collaborator

@muchvo muchvo left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@zmsn-2077 zmsn-2077 left a comment

Choose a reason for hiding this comment

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

LGTM.

@Gaiejj Gaiejj merged commit dee636e into PKU-Alignment:dev May 19, 2023
4 checks passed
@Gaiejj Gaiejj deleted the dev-test branch July 1, 2023 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Some issues about algorithm enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants