Download tests/test_rewards.py from npc-worldwide/guac-coder: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/npc-worldwide/guac-coder/resolve/main/tests/test_rewards.py
- Command line
-
hf download hf://npc-worldwide/guac-coder/tests/test_rewards.py
-
curl -L -o test_rewards.py https://huggingface.co/npc-worldwide/guac-coder/resolve/main/tests/test_rewards.py
5.25 kB
| """Tests for guac_coder rewards module.""" | |
| from guac_coder.config import RewardConfig | |
| from guac_coder.rewards import ( | |
| _has_checkpointing, | |
| _has_chroptiks_import, | |
| _has_class_defs, | |
| _has_dataframe_to_csv, | |
| _has_function_defs, | |
| _has_logging, | |
| _has_narrative_comments, | |
| _has_try_except, | |
| _is_bare_script, | |
| compute_static_rewards, | |
| total_static_reward, | |
| ) | |
| class TestStyleChecks: | |
| """Unit tests for static analysis helpers.""" | |
| def test_has_function_defs_true(self): | |
| code = "def foo():\n pass\n" | |
| assert _has_function_defs(code) is True | |
| def test_has_function_defs_false(self): | |
| code = "x = 1\n" | |
| assert _has_function_defs(code) is False | |
| def test_has_class_defs_true(self): | |
| code = "class Foo:\n pass\n" | |
| assert _has_class_defs(code) is True | |
| def test_has_class_defs_false(self): | |
| code = "def foo():\n pass\n" | |
| assert _has_class_defs(code) is False | |
| def test_has_try_except_true(self): | |
| code = "try:\n x = 1\nexcept:\n pass\n" | |
| assert _has_try_except(code) is True | |
| def test_has_try_except_false(self): | |
| code = "x = 1\n" | |
| assert _has_try_except(code) is False | |
| def test_has_logging_true(self): | |
| code = "import logging\nlogging.info('hello')\n" | |
| assert _has_logging(code) is True | |
| def test_has_logging_false(self): | |
| code = "x = 1\n" | |
| assert _has_logging(code) is False | |
| def test_has_checkpointing_true(self): | |
| code = "torch.save(model.state_dict(), 'ckpt.pt')\n" | |
| assert _has_checkpointing(code) is True | |
| def test_has_checkpointing_false(self): | |
| code = "x = 1\n" | |
| assert _has_checkpointing(code) is False | |
| def test_has_dataframe_to_csv_true(self): | |
| code = "df.to_csv('out.csv')\n" | |
| assert _has_dataframe_to_csv(code) is True | |
| def test_has_dataframe_to_csv_false(self): | |
| code = "with open('f') as fh:\n fh.write('x')\n" | |
| assert _has_dataframe_to_csv(code) is False | |
| def test_has_chroptiks_true(self): | |
| code = "import chroptiks\n" | |
| assert _has_chroptiks_import(code) is True | |
| def test_has_chroptiks_false(self): | |
| code = "import matplotlib\n" | |
| assert _has_chroptiks_import(code) is False | |
| def test_has_narrative_comments_true(self): | |
| code = "# here we load the data\n# next we process it\nx = 1\n" | |
| assert _has_narrative_comments(code) is True | |
| def test_has_narrative_comments_false(self): | |
| code = "# Load config\n# Process data\nx = 1\n" | |
| assert _has_narrative_comments(code) is False | |
| def test_is_bare_script_true(self): | |
| code = "x = 1\ny = 2\n" | |
| assert _is_bare_script(code) is True | |
| def test_is_bare_script_false_with_function(self): | |
| code = "def main():\n pass\n" | |
| assert _is_bare_script(code) is False | |
| def test_is_bare_script_false_with_main_guard(self): | |
| code = "if __name__ == '__main__':\n pass\n" | |
| assert _is_bare_script(code) is False | |
| class TestRewardComputation: | |
| """Tests for compute_static_rewards and total_static_reward.""" | |
| def test_good_code_gets_positive_reward(self): | |
| code = ''' | |
| import logging | |
| import chroptiks | |
| import pandas as pd | |
| def process(data_path: str) -> None: | |
| logging.info("Loading %s", data_path) | |
| df = pd.read_csv(data_path) | |
| df.to_csv("out.csv") | |
| def checkpoint(state, path: str) -> None: | |
| pd.to_pickle(state, path) | |
| if __name__ == "__main__": | |
| process("data.csv") | |
| ''' | |
| cfg = RewardConfig() | |
| rewards = compute_static_rewards(code, cfg) | |
| total = sum(rewards.values()) | |
| # Should have positive rewards for logging, chroptiks, df.to_csv, checkpoint, func def | |
| assert "logging_reward" in rewards | |
| assert "chroptiks_reward" in rewards | |
| assert "dataframe_csv_reward" in rewards | |
| assert "checkpoint_reward" in rewards | |
| assert "function_def_reward" in rewards | |
| assert total > 0 | |
| def test_bad_code_gets_negative_reward(self): | |
| code = ''' | |
| import seaborn as sns | |
| import csv | |
| # here we load the data | |
| # next we process it | |
| try: | |
| with open('data.csv', 'w') as fh: | |
| writer = csv.DictWriter(fh, fieldnames=['a']) | |
| writer.writeheader() | |
| for i in range(100): | |
| writer.writerow({'a': i}) | |
| except: | |
| pass | |
| class DataProcessor: | |
| pass | |
| print("done") | |
| sns.barplot(x=[1,2], y=[3,4]) | |
| plt.grid(True) | |
| plt.title("My Plot") | |
| ''' | |
| cfg = RewardConfig() | |
| rewards = compute_static_rewards(code, cfg) | |
| total = sum(rewards.values()) | |
| # Should be heavily negative | |
| assert "seaborn_penalty" in rewards | |
| assert "grid_penalty" in rewards | |
| assert "title_penalty" in rewards | |
| assert "bargraph_penalty" in rewards | |
| assert "narrative_comment_penalty" in rewards | |
| assert "tryexcept_penalty" in rewards | |
| assert "class_penalty" in rewards | |
| assert "print_penalty" in rewards | |
| assert "bare_script_penalty" in rewards | |
| assert total < 0 | |
| def test_total_static_reward_scalar(self): | |
| code = "x = 1\n" | |
| cfg = RewardConfig() | |
| total = total_static_reward(code, cfg) | |
| assert isinstance(total, float) | |