def __new__(mcs, name, bases, dict): def gen_test_success(d, config, transfunc, expectedshape): def test(self): config.grid_dims = d g = Grid2D(config, transfunc) self.assertIsInstance(g, Grid2D) self.assertEqual(g.grid.shape, expectedshape) return test def gen_test_valerr(d, config, transfunc): def test(self): config.grid_dims = d l = lambda c=config: Grid2D(c, transfunc) self.assertRaises(ValueError, l) return test config = CAConfig('test/testdescriptions/2dbasic.py') dimensions = 2 config.states = 0,1,2 config.nhood_arr = [1,1,1] shapes = range(0, 5) def transfunc(grid, neighbourcounts): return grid for i in shapes: for j in shapes: testname = "test_grid_2d_{i}{j}".format(i=i, j=j) testdims = (i, j) if i < 3 or j < 3: dict[testname] = gen_test_valerr(testdims, config, transfunc) else: dict[testname] = gen_test_success(testdims, config, transfunc, (i,j)) return type.__new__(mcs, name, bases, dict)
def __new__(mcs, name, bases, dict): def gen_test_success(gens, config, transfunc): def test(self): config.num_generations = gens g = Grid1D(config, transfunc) self.assertIsInstance(g, Grid1D) return test def gen_test_valerr(gens, config, transfunc): def test(self): config.num_generations = gens self.assertRaises(ValueError, lambda c=config: Grid1D(config, transfunc)) return test config = CAConfig("test/testdescriptions/1dbasic.py") config.states = 0, 1 testgens = range(-1, 6) def transfunc(self, grid, neighbourcounts): return grid for g in testgens: testname = "test_1d_numgens_{i}".format(i=g) config.num_generations = g if g < 1: dict[testname] = gen_test_valerr(g, config, transfunc) else: dict[testname] = gen_test_success(g, config, transfunc) return type.__new__(mcs, name, bases, dict)
def test_minimal_2d_comments_fill(self): ca_config = CAConfig(self.filepath2d) self.assertIsInstance(ca_config, CAConfig) ca_config.fill_in_defaults() #values from defaults # + 1 from the inclusion of generation 0 expected_gens = 100 expected_dims = (200, 200) expected_grid = np.zeros(expected_dims) expected_nhood = np.ones((3,3)) self.assertEqual(ca_config.num_generations, expected_gens) self.assertEqual(ca_config.grid_dims, expected_dims) self.assertTrue(np.array_equal(ca_config.initial_grid, expected_grid)) self.assertTrue(np.array_equal(ca_config.nhood_arr, expected_nhood))
def __new__(mcs, name, bases, dict): def gen_test_shape_eq(config, x, shape): def test(self): if (type(x) is tuple): config.set_grid_dims(dims=x) else: config.set_grid_dims(num_generations=x) self.assertEqual(config.initial_grid.shape, shape) self.assertEqual(config.grid_dims, shape) return test #1d #below limit, valid and below current, valid and above current gens = [0, 50, 150] expected = [(2,3), (51,101), (151, 301)] config = CAConfig(TESTDESCRIPTIONS_PATH + "1dbasic.py") config.fill_in_defaults() for g, e in zip(gens, expected): testname = "test_config_setdims_1d_{g}".format(g=g) dict[testname] = gen_test_shape_eq(config, g, e) dict[testname] = gen_test_shape_eq(config, g, e) #2d config = CAConfig(TESTDESCRIPTIONS_PATH + "2dbasic.py") config.fill_in_defaults() shapes = [(2,3), (3,3), (50, 50), (300,300), (300,420)] for s in shapes: testname = "test_config_setdims_2d_{g}".format(g=g) dict[testname] = gen_test_shape_eq(config, s, s) dict[testname] = gen_test_shape_eq(config, s, s) return type.__new__(mcs, name, bases, dict)
def __new__(mcs, name, bases, dict): def gen_test_array_equal(a,b): def test(self): self.assertTrue(np.array_equal(a, b)) return test config = CAConfig('test/testdescriptions/2dbasic.py') dimensions = 2 config.states = 0,1,2 config.nhood_arr = [1,1,1] def transfunc(self, grid, neighbourcounts): return grid #matching dimensions config.grid_dims = (20,20) testname = "test_gridset_2d_{i}{j}".format(i=config.grid_dims[0], j=config.grid_dims[1]) config.initial_grid = np.random.randint(0, 3, config.grid_dims) g = Grid2D(config, transfunc) dict[testname] = gen_test_array_equal(g.grid, config.initial_grid) #non matching testsize = [(19,20), (20,21), (21,21), (19,19)] for s in testsize: testname = "test_gridset_2d_{i}{j}".format(i=s[0], j=s[1]) config.grid_dims = s ig = np.random.randint(0, 3, s) config.initial_grid = ig g = Grid2D(config, transfunc) if s < config.grid_dims: dict[testname] = gen_test_array_equal(g.grid[:s[0], :s[1]], ig) else: dict[testname] = gen_test_array_equal(g.grid, ig[:config.grid_dims[0], :config.grid_dims[1]]) return type.__new__(mcs, name, bases, dict)
def load_ca(self, filepath): """Load a CA description file Pre runs the setup function in the description to populate the CAConfig object. Only then will the GUI be properly initialised (states must be known before adding config frame) Note: If manually specifying a path: sys.path[0] can be used to get the directory of main.py and then '/ca_descriptions/xxx.py' can be appended to create the filepath. Args: filepath (str): Full path to the CA description py file """ if not filepath == '': # loads the ca into the program with a default config # removes previous graph if self.ca_graph is not None: self.ca_graph.clear() # create default config self.ca_config = CAConfig(filepath) self.ca_config = prerun_ca(self.ca_config) if self.ca_config is None: return self.root.wm_title(self.WINDOW_TITLE + " - " + self.ca_config.title) self.add_configuration_controls() # Add ui controls to the gui self.playback_controls.ui.pack(side=tk.LEFT, padx=10) self.playback_controls.ui.sliderframe.pack() self.screenshotui.pack(side=tk.LEFT, padx=10)
def test_minimal_1d_comments_fill(self): ca_config = CAConfig(self.filepath1d) self.assertIsInstance(ca_config, CAConfig) ca_config.fill_in_defaults() #values from defaults expected_rulenum = 0 expected_gens = 100 # + 1 for generation 0 expected_dims = (expected_gens + 1, expected_gens*2 + 1) expected_grid = np.zeros(expected_dims) expected_nhood = np.array([1,1,1]) self.assertEqual(ca_config.rule_num, expected_rulenum) self.assertEqual(ca_config.num_generations, expected_gens) self.assertEqual(ca_config.grid_dims, expected_dims) self.assertTrue(np.array_equal(ca_config.initial_grid, expected_grid)) self.assertTrue(np.array_equal(ca_config.nhood_arr, expected_nhood))
def test_minimal_2d_comments(self): #infer data from comments in file ca_config = CAConfig(self.filepath2d) self.assertIsInstance(ca_config, CAConfig) #values from parsing the file self.assertEqual(ca_config.title, "Example 2D CA Minimal") self.assertEqual(ca_config.dimensions, 2)
def test_minimal_2d_vars_fill(self): ca_config = CAConfig(self.filepath2d) prerun_ca(ca_config) ca_config = load(ca_config.path) self.assertIsInstance(ca_config, CAConfig) self.assertEqual(ca_config.states, (0,1,2)) #values from file ca_config.fill_in_defaults() #values from defaults expected_gens = 100 expected_dims = (200, 200) expected_grid = np.zeros(expected_dims) expected_nhood = np.ones((3,3)) self.assertEqual(ca_config.num_generations, expected_gens) self.assertEqual(ca_config.grid_dims, expected_dims) self.assertTrue(np.array_equal(ca_config.initial_grid, expected_grid)) self.assertTrue(np.array_equal(ca_config.nhood_arr, expected_nhood))
def test_minimal_2d_vars(self): # pre run to get information set by the user ca_config = CAConfig(self.filepath2d) prerun_ca(ca_config) ca_config = load(ca_config.path) self.assertEqual(ca_config.title, "Example 2D CA Minimal") self.assertEqual(ca_config.dimensions, 2) self.assertEqual(ca_config.states, (0,1,2))
def __new__(mcs, name, bases, dict): def gen_test_array_eq(a, b): def test(self): self.assertTrue(np.array_equal(a, b)) return test config = CAConfig("test/testdescriptions/1dbasic.py") config.num_generations = 20 config.states = 0, 1 config.dimensions = 1 config.nhood_arr = [1, 1, 1] def transfunc(self, grid, neighbourcounts): return grid[0] numgens = 20 sizes = range(numgens * 2, numgens * 2 + 3) for s in sizes: testname = "test_initial_grid_set_{s}".format(s=s) initialgrid = np.array([np.random.randint(0, 2, s)]) config.initial_grid = initialgrid g = Grid1D(config, transfunc) #check that the first lines match if s < g.grid.shape[1]: dict[testname] = gen_test_array_eq(g.grid[0, :s], initialgrid[0]) else: dict[testname] = gen_test_array_eq( g.grid[0], initialgrid[0, :g.grid.shape[1]]) return type.__new__(mcs, name, bases, dict)
def test_basic1d(self): ca_config = CAConfig(self.filepath1d) self.assertIsInstance(ca_config, CAConfig) #values from file self.assertEqual(ca_config.title, "Example 1D CA") self.assertEqual(ca_config.dimensions, 1) #pre run to get states prerun_ca(ca_config) ca_config = load(ca_config.path) self.assertEqual(ca_config.states, (0,1))
def test_basic2d(self): ca_config = CAConfig(self.filepath2d) self.assertIsInstance(ca_config, CAConfig) #values from parsing the file self.assertEqual(ca_config.title, "Example 2D CA") self.assertEqual(ca_config.dimensions, 2) # pre run to get information set by the user prerun_ca(ca_config) ca_config = load(ca_config.path) self.assertEqual(ca_config.title, "Example 2D CA") self.assertEqual(ca_config.dimensions, 2) self.assertEqual(ca_config.states, (0,1,2))
def setUp(self): self.config = CAConfig('test/testdescriptions/2dbasic.py') self.dimensions = 2 self.config.states = 0,1,2 self.config.nhood_arr = [1,1,1]
def test_2d_none(self): ca_config = CAConfig(self.filepath2d) self.assertEqual(ca_config.title, "Unamed 2D Automata") self.assertEqual(ca_config.dimensions, 2)
def setUp(self): self.config = CAConfig("test/testdescriptions/1dbasic.py") self.config.num_generations = 100 self.config.states = 0, 1 self.config.dimensions = 1 self.config.nhood_arr = [1, 1, 1]
def test_minimal_1d_comments(self): ca_config = CAConfig(self.filepath1d) #values from file self.assertIsInstance(ca_config, CAConfig) self.assertEqual(ca_config.title, "Example 1D CA Minimal") self.assertEqual(ca_config.dimensions, 1)