Skip to content

Commit

Permalink
some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEimer committed Dec 11, 2023
1 parent 36f2c3b commit 4eca801
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
2 changes: 1 addition & 1 deletion carl/context/context_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def sample_contexts(

contexts = []
for _ in range(size):
context = {cf.name: cf.sample() for cf in self.context_space.values()}
context = {cf.name: cf.rvs() for cf in self.context_space.values()}
context = self.insert_defaults(context, context_keys)
contexts += [context]

Expand Down
6 changes: 3 additions & 3 deletions carl/envs/gymnasium/carl_gymnasium_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

try:
pygame.display.init()
except:
import os
except: # pragma: no cover
import os # pragma: no cover

os.environ["SDL_VIDEODRIVER"] = "dummy"
os.environ["SDL_VIDEODRIVER"] = "dummy" # pragma: no cover


class CARLGymnasiumEnv(CARLEnv):
Expand Down
1 change: 1 addition & 0 deletions test/test_all_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_init_all_envs(self):
env = ( # noqa: F841 local variable is assigned to but never used
var()
)
env.reset()
except Exception as e:
print(f"Cannot instantiate {var} environment.")
raise e
15 changes: 15 additions & 0 deletions test/test_context_sampler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from omegaconf import DictConfig

from carl.context.context_space import (
ContextSpace,
Expand Down Expand Up @@ -44,11 +45,25 @@ def test_init(self):
name="TestSampler",
)

with self.assertRaises(ValueError):
ContextSampler(
context_distributions=0,
context_space=self.cspace,
seed=0,
name="TestSampler",
)

def test_sample_contexts(self):
contexts = self.sampler.sample_contexts(n_contexts=3)
self.assertEqual(len(contexts), 3)
self.assertEqual(contexts[0]["gravity"], 9.8)

contexts = self.sampler.sample_contexts(n_contexts=1)
self.assertEqual(len(contexts), 1)
self.assertEqual(contexts[0]["gravity"], 9.8)




if __name__ == "__main__":
unittest.main()
19 changes: 19 additions & 0 deletions test/test_context_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ def test_verify_context(self):
is_valid = self.context_space.verify_context(context)
self.assertEqual(is_valid, False)

def test_sample(self):
context = self.context_space.sample_contexts(["gravity"], size=1)
is_valid = self.context_space.verify_context(context)
self.assertEqual(is_valid, True)

contexts = self.context_space.sample_contexts(["gravity"], size=10)
self.assertTrue(len(contexts) == 10)
for context in contexts:
is_valid = self.context_space.verify_context(context)
self.assertEqual(is_valid, True)

contexts = self.context_space.sample_contexts(None, size=10)
self.assertTrue(len(contexts) == 10)
for context in contexts:
is_valid = self.context_space.verify_context(context)
self.assertEqual(is_valid, True)

with self.assertRaises(ValueError):
self.context_space.sample_contexts(["false_feature"], size=0)

if __name__ == "__main__":
unittest.main()
50 changes: 44 additions & 6 deletions test/test_search_space_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,58 @@
from carl.context.search_space_encoding import search_space_to_config_space


dict_space = {
"uniform_integer": (1, 10),
"uniform_float": (1.0, 10.0),
"categorical": ["a", "b", "c"],
"constant": 1337,
}

dict_space_2 = { "hyperparameters": [
{"name": "x0",
"type": "uniform_float",
"log": False,
"lower": -512.0,
"upper": 512.0,
"default": -3.0},
{"name": "x1",
"type": "uniform_float",
"log": False,
"lower": -512.0,
"upper": 512.0,
"default": -4.0}],
"conditions": [],
"forbiddens": [],
"python_module_version": "0.4.17",
"json_format_version": 0.2}

str_space = """{
"uniform_integer": (1, 10),
"uniform_float": (1.0, 10.0),
"categorical": ["a", "b", "c"],
"constant": 1337,
}"""

class TestSearchSpacEncoding(unittest.TestCase):
def setUp(self):
self.test_space = None
self.test_space = ConfigurationSpace(
name="myspace",
space={
"uniform_integer": (1, 10),
"uniform_float": (1.0, 10.0),
"categorical": ["a", "b", "c"],
"constant": 1337,
},
space=dict_space
)
return super().setUp()

def test_init(self):
self.test_space = ConfigurationSpace(
name="myspace",
space=dict_space_2
)

self.test_space = ConfigurationSpace(
name="myspace",
space=str_space
)

def test_config_spaces(self):
try:
search_space_to_config_space(self.test_space)
Expand Down

0 comments on commit 4eca801

Please sign in to comment.