def setUp(self): self.name = "Test Property" self.desc = "This is a simple property." self.random_number_function = np.random.uniform self.min = 0.0 self.max = 1.0 self.seed = 666 self.n = 10 self.mock_generator = RandomGenerator(seed=self.seed, n=self.n, random_number_function=self.random_number_function)
def __init__(self, seed_generator=None, n=None, random_number_function=None, *args, **kwargs): Property.__init__(self, *args, **kwargs) self.seed = None self.seed_generator = seed_generator self.n = n self.random_number_function = random_number_function self.random_generator = None if self.seed_generator is not None: self.update_seed(seed_generator) self.random_generator = RandomGenerator(self.seed, n, random_number_function) self.mean = None
def test_random_generator(self): # mock the random number function to test trivial behavior gen = RandomGenerator(self.seed, self.N, mock_random) self.assertEqual(gen.N, self.N) self.assertEqual(gen.seed, self.seed) self.assertEqual(gen.get_random_number(value=self.value), np.array([self.value])) self.assertListEqual(list(gen.get_n_random_numbers(value=self.value)), list(np.ones(self.N))) # test passing a list of arguments to the random number function gen.random_number_function = np.random.uniform self.assertAlmostEqual(gen.get_random_number(low=0.0, high=1.0), np.array(0.7004371218578347), places=5)
class TestProperty(unittest.TestCase): def setUp(self): self.name = "Test Property" self.desc = "This is a simple property." self.random_number_function = np.random.uniform self.min = 0.0 self.max = 1.0 self.seed = 666 self.n = 10 self.mock_generator = RandomGenerator( seed=self.seed, n=self.n, random_number_function=self.random_number_function) def test_properties(self): prop = Property(name="Test Property", desc="This is a simple property.") self.assertEquals(prop.name, self.name) self.assertEquals(prop.desc, self.desc) self.assertEqual(prop.generate_values(), None) self.assertEqual(prop.update_seed(), None) mock_seed_generator = MockSeedGenerator(self.seed) random_prop = RandomProperty(seed_generator=mock_seed_generator, n=10, random_number_function=np.random.uniform, name="Random Property", desc="Test the random property") random_prop.generate_values(low=self.min, high=self.max) random_prop_values = list(random_prop.values) # reset the mock generator self.mock_generator.set_seed() mock_random_values = list( self.mock_generator.get_n_random_numbers(low=self.min, high=self.max)) self.assertEquals(random_prop_values, mock_random_values) random_prop.calculate_property_statistics() self.assertAlmostEqual(random_prop.mean, np.mean(mock_random_values)) regional_property = RegionalProperty(MockRegion(name="MockRegion")) self.assertEqual(regional_property.region.name, "MockRegion") def tearDown(self): pass
class TestProperty(unittest.TestCase): def setUp(self): self.name = "Test Property" self.desc = "This is a simple property." self.random_number_function = np.random.uniform self.min = 0.0 self.max = 1.0 self.seed = 666 self.n = 10 self.mock_generator = RandomGenerator(seed=self.seed, n=self.n, random_number_function=self.random_number_function) def test_properties(self): prop = Property(name="Test Property", desc="This is a simple property.") self.assertEquals(prop.name, self.name) self.assertEquals(prop.desc, self.desc) self.assertEqual(prop.generate_values(), None) self.assertEqual(prop.update_seed(), None) mock_seed_generator = MockSeedGenerator(self.seed) random_prop = RandomProperty(seed_generator=mock_seed_generator, n=10, random_number_function=np.random.uniform, name="Random Property", desc="Test the random property") random_prop.generate_values(low=self.min, high=self.max) random_prop_values = list(random_prop.values) # reset the mock generator self.mock_generator.set_seed() mock_random_values = list(self.mock_generator.get_n_random_numbers(low=self.min, high=self.max)) self.assertEquals(random_prop_values, mock_random_values) random_prop.calculate_property_statistics() self.assertAlmostEqual(random_prop.mean, np.mean(mock_random_values)) regional_property = RegionalProperty(MockRegion(name="MockRegion")) self.assertEqual(regional_property.region.name, "MockRegion") def tearDown(self): pass
class RandomProperty(Property): def __init__(self, seed_generator=None, n=None, random_number_function=None, *args, **kwargs): Property.__init__(self, *args, **kwargs) self.seed = None self.seed_generator = seed_generator self.n = n self.random_number_function = random_number_function self.random_generator = None if self.seed_generator is not None: self.update_seed(seed_generator) self.random_generator = RandomGenerator(self.seed, n, random_number_function) self.mean = None def update_seed(self, seed_generator): self.seed = seed_generator.request_seed() self.random_generator = RandomGenerator(self.seed, self.n, self.random_number_function) def generate_values(self, *args, **kwargs): self.values = self.random_generator.get_n_random_numbers(*args, **kwargs) def calculate_property_statistics(self): self.mean = np.mean(self.values)
def update_seed(self, seed_generator): self.seed = seed_generator.request_seed() self.random_generator = RandomGenerator(self.seed, self.n, self.random_number_function)