def test_change_zoom_level_smaller_manual_check(self): add_stochastic_noise(1, self.dim, self.arr) cy.clear_bounds(self.dim, self.arr) self.arr[0, 0] = 2 # make the zoomer resize dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, self.change_roll, self.dim, self.buf) nu_arr = buf[self.position[0]] self.assertEqual(nu_arr[1, 1], 2) self.assertEqual(change[0, 1], 1) nu_arr[1, 1] = 0 nu_arr_mid = np.copy(nu_arr) nu_arr[0, 0] = 2 #zoom again dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, change, dim, buf) nu_arr = buf[self.position[0]] self.assertEqual(nu_arr[1, 1], 2) #second assert self.assertEqual(change[0, 1], 1) nu_arr[1, 1] = 0 dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, change, dim, buf) nu_arr = buf[self.position[0]] self.assertEqual(change[0, 1], -1) testing.assert_array_equal(nu_arr, nu_arr_mid)
def image_processing(self, kwargs): cy.scroll_bars(kwargs['dim_t'], kwargs['arr_t'], kwargs['bars']) cf.scroll_noise(kwargs['dim_t'], kwargs['arr_t'], kwargs['fuzz']) cf.export_array(self.image, kwargs['colorlist'], kwargs['dim_t'], kwargs['arr_t'], 0) logging.debug('Sending image') self.send_image()
def test_change_zoom_level_smaller_content(self): cy.clear_array(self.dim, self.arr) randomize_center(10, self.dim, self.arr) dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, self.change_roll, self.dim, self.buf) nu_arr = buf[self.position[0]] self.assertEqual(np.sum(self.arr), np.sum(nu_arr))
def test_change_zoom_level_larger_content(self): cy.fill_array(self.dim, self.arr) dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, self.change_roll, self.dim, self.buf) nu_arr = buf[self.position[0]] cy.fill_bounds(dim, nu_arr) testing.assert_array_equal(np.mean(nu_arr), np.mean(self.arr))
def test_create_box(self): arr = tst_arr() cy.create_box(1, 3, 1, 3, tst_dim(), arr) arr2 = tst_arr() arr2[1:4,1:4] = 1 testing.assert_array_equal(arr2, arr)
def test_change_zoom_level_smaller(self): cy.clear_array(self.dim, self.arr) self.assertEqual(len(self.buf_stat), 1) dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, self.change_roll, self.dim, self.buf) self.assertEqual(change[0, 1], -1) self.assertEqual(dim[0], self.dim[0] - 2) self.assertEqual(len(self.buf_stat), 2)
def test_scroll_bars_width(self): bars = np.array([[0, 5, 1, 1, 0, 1]], np.double) arr = tst_arrL() cy.scroll_bars(tst_dimL(), arr, bars) arr2 = tst_arrL() arr2[:, :5] = 1 testing.assert_array_equal(arr, arr2)
def test_set_points(self): points = np.array([[1,1],[2,2]], np.intc) arr = tst_arr() cy.set_points(points, tst_dim(), arr) arr2 = tst_arr() arr2[1,1] = 1 arr2[2,2] = 1 testing.assert_array_equal(arr2, arr)
def test_clear_bounds(self): arr = tst_arr() cy.clear_bounds(tst_dim(), arr) arr2 = tst_arr() arr2[0, :] = 0 arr2[-1, :] = 0 arr2[:, 0] = 0 arr2[:, -1] = 0 testing.assert_array_equal(arr2, arr)
def test_change_zoom_level_same(self): add_stochastic_noise(1, self.dim, self.arr) cy.clear_bounds(self.dim, self.arr) self.assertEqual(len(self.buf_stat), 1) dim, buf, change, self.buf_stat = change_zoom_level( self.position, self.buf_len, self.buf_stat, self.change_roll, self.dim, self.buf) self.assertEqual(change[0, 1], 0) self.assertEqual(dim[0], self.dim[0]) testing.assert_array_equal(self.arr, buf[self.position[0]]) self.assertEqual(len(self.buf_stat), 1)
def test_moore_neighbors_sum_random_versus_roll(self): arr = tst_arr() add_global_noise(0.5, tst_dim(), arr) l = cy.roll_columns(1, tst_dim(), arr) r = cy.roll_columns(-1, tst_dim(), arr) u = cy.roll_rows(1, tst_dim(), arr) d = cy.roll_rows(-1, tst_dim(), arr) NB = np.asarray(l) + np.asarray(r) + np.asarray(u) + np.asarray(d) NB2 = moore_neighbors_array(tst_dim(), arr) testing.assert_array_equal(NB, NB2)
def test_advance_array_wraps(self): self.buf[0] = 0 cy.clear_array(tst_dimL(), self.buf[self.buf_len - 1]) add_global_noise(0.5, tst_dimL(), self.buf[self.buf_len - 1]) testing.assert_almost_equal(np.mean(self.buf[self.buf_len - 1]), np.mean(np.random.randint(0, 2, self.dim)), decimal=2) self.position[0] = self.buf_len - 1 advance_array(self.position, self.buf_len, self.buf) testing.assert_array_equal(self.buf[0], self.buf[self.buf_len - 1])
def test_fill_bounds(self): arr = tst_arr() cy.fill_bounds(tst_dim(), arr) arr2 = tst_arr() arr2[0, :] = 1 arr2[-1, :] = 1 arr2[:, 0] = 1 arr2[:, -1] = 1 testing.assert_array_equal(arr2, arr)
def test_set_bounds(self): arr = tst_arr() bounds = array.array('i', [1,1,1,1]) cy.set_bounds(bounds, tst_dim(), arr) arr2 = tst_arr() arr2[0] = 1 arr2[:, 0] = 1 arr2[tst_dim()[0] - 1] = 1 arr2[:, tst_dim()[1] - 1] = 1 testing.assert_array_equal(arr, arr2)
def test_basic_update_bounds(self): arr = np.copy(self.arr) basic_update( 0, self.beta, 0, prepair_rule(np.array([[-1, 0, 0, 0]], np.intc), self.head_pos), self.dim, self.arr, self.bounds, ) cy.fill_bounds(self.dim, arr) testing.assert_array_equal(arr, self.arr)
def recenter(com, dim, arr): """ Recenters the simulation :param com: center_of_mass :param dim: dimensions of array :param array: the array :return: ver (vertical movement), hor (horizontal movement) """ ver = 0 hor = 0 com = np.asarray(com) center = np.asarray([dim[0] / 2, dim[1] / 2]) if not com.any(): com = center offcenter = center - com norm = cph.norm(offcenter[0], offcenter[1]) if norm > 2: if offcenter[0] > 0: cy.roll_rows_pointer(1, dim, arr) ver += 1 else: cy.roll_rows_pointer(-1, dim, arr) ver -= 1 if offcenter[1] > 0: cy.roll_columns_pointer(1, dim, arr) hor += 1 else: cy.roll_columns_pointer(-1, dim, arr) hor -= 1 return ver, hor
def test_change_buffer_manual_fill(self): # Fill old buffer with 1s self.buf[0] = 1 position = array.array('i', [0, 0]) dim_nu, buf_nu = resize_array_buffer(self.dim, self.buf_len) # Initialise new buffer to 0 buf_nu[0] = 0 # Adds the old buffer, which should fill the whole middle with 1s change_buffer(position, self.buf_len, self.dim, self.buf, dim_nu, buf_nu) # Check the new buffer has empty rim, but full after. self.assertFalse(cy.check_rim(0, dim_nu, buf_nu[0])) self.assertTrue(cy.check_rim(1, dim_nu, buf_nu[0]))
def test_basic_update_scroll(self): arr = np.copy(self.arr) basic_update( 0, self.beta, 0, prepair_rule(np.array([[-1, 0, 0, 0]], np.intc), self.head_pos), self.dim, self.arr, self.bounds, bars=np.array([[1, 1, 1, 0, 0, 1]], np.double), ) cy.fill_bounds(self.dim, arr) arr[1, :] = 1 testing.assert_array_equal(arr, self.arr)
def test_analysis_loop_empty(self): cy.clear_array(tst_dimL(), self.arr) com = center_of_mass(tst_dimL(), self.arr) tot, living, com, Rg, M = analysis_loop(com, tst_dimL(), self.arr) self.assertEqual(len(np.argwhere(self.arr)), tot) self.assertEqual(len(np.argwhere(self.arr)), len(living)) com_P = center_of_mass_P(np.argwhere(self.arr), len(np.argwhere(self.arr))) testing.assert_allclose(com, com_P) com_P = center_of_mass_P(np.argwhere(self.arr), len(np.argwhere(self.arr))) Rg_P = radius_of_gyration_P(com_P, np.argwhere(self.arr), len(np.argwhere(self.arr))) testing.assert_allclose(Rg, Rg_P, 1e-8, 1e-2) self.assertAlmostEqual(polarization(tst_dimL(), self.arr), M)
def test_scroll_bars_multi(self): bars = np.array([ [10, 1, 1, 0, 0, 1], [0, 1, 1, 1, 0, 1], [1, 1, 1, 1, 0, 1], ], np.double) arr = tst_arrL() cy.scroll_bars(tst_dimL(), arr, bars) arr2 = tst_arrL() arr2[10, :] = 1 arr2[:, 0] = 1 arr2[:, 1] = 1 testing.assert_array_equal(arr, arr2)
def test_basic_update_buffer_bounds(self): arr = np.copy(self.arr) basic_update_buffer( 0, self.beta, 0, np.array([[-1, 0, 0, 0]], np.intc), self.head_pos, 10, self.dim, self.arr, self.buf, self.bounds, ) cy.fill_bounds(self.dim, arr) testing.assert_array_equal(arr, self.arr)
def test_change_array_manual_fill_cut_and_gap(self): # Fill old buffer with 1s self.arr[:] = 1 dim_nu, arr_nu = resize_array(self.dim) # Initialise new buffer to 0 arr_nu[:] = 0 # This time when changing, offset and cut the old array offset = array.array('i', [2, 2]) cut = array.array('i', [1, 1]) change_array(self.dim, self.arr, dim_nu, arr_nu, offset, cut) # Check the new buffer has a gap all the way around, one step in from the # outside. self.assertFalse(cy.check_rim(0, dim_nu, arr_nu)) self.assertFalse(cy.check_rim(1, dim_nu, arr_nu)) self.assertTrue(cy.check_rim(2, dim_nu, arr_nu))
def test_change_buffer_manual_fill_cut_and_gap(self): # Fill old buffer with 1s self.buf[0] = 1 position = array.array('i', [0, 0]) dim_nu, buf_nu = resize_array_buffer(self.dim, self.buf_len) # Initialise new buffer to 0 buf_nu[0] = 0 # This time, when changing offset and cut the old array offset = array.array('i', [2, 2]) cut = array.array('i', [1, 1]) change_buffer(position, self.buf_len, self.dim, self.buf, dim_nu, buf_nu, offset, cut) # Check the new buffer has a gap all the way around, one step in from the # outside. self.assertFalse(cy.check_rim(0, dim_nu, buf_nu[0])) self.assertFalse(cy.check_rim(1, dim_nu, buf_nu[0])) self.assertTrue(cy.check_rim(2, dim_nu, buf_nu[0]))
def test_moore_neighbors_same_random_versus_roll(self): arr = tst_arr() add_global_noise(0.5, tst_dim(), arr) l = cy.roll_columns(1, tst_dim(), arr) r = cy.roll_columns(-1, tst_dim(), arr) u = cy.roll_rows(1, tst_dim(), arr) d = cy.roll_rows(-1, tst_dim(), arr) NB = np.asarray(l) + np.asarray(r) + np.asarray(u) + np.asarray(d) NB2 = np.zeros_like(arr) pos = array.array('i', [0, 0]) for i in range(tst_dim()[0]): for j in range(tst_dim()[1]): pos[0] = i pos[1] = j NB2[i, j] = moore_neighbors_same_CP(pos, tst_dim(), arr) if not arr[i, j]: NB[i, j] = 4 - NB[i, j] testing.assert_array_equal(NB, NB2)
def test_analysis_loop_energy_empty(self): cy.clear_array(tst_dimL(), self.arr) com = center_of_mass(tst_dimL(), self.arr) tot, living, com, Rg, e, e2, M = analysis_loop_energy( com, tst_dimL(), self.arr) self.assertEqual(len(np.argwhere(self.arr)), tot) self.assertEqual(len(np.argwhere(self.arr)), len(living)) com_P = center_of_mass_P(np.argwhere(self.arr), len(np.argwhere(self.arr))) testing.assert_allclose(com, com_P) Rg_P = radius_of_gyration_P(com_P, np.argwhere(self.arr), len(np.argwhere(self.arr))) testing.assert_allclose(Rg, Rg_P, 1e-8, 1e-2) E, E2 = neighbor_interaction(tst_dimL(), self.arr) EE, EE2 = neighbor_interaction_moore(tst_dimL(), self.arr) self.assertAlmostEqual(e, -8, 1) self.assertLess(e, e2) self.assertAlmostEqual(E, e, 5) self.assertAlmostEqual(EE, e, 5) self.assertAlmostEqual(E2, e2, 5) self.assertAlmostEqual(EE2, e2, 5) self.assertAlmostEqual(1, M, 2) self.assertAlmostEqual(polarization(tst_dimL(), self.arr), M)
def test_neumann_neighbors_sum_CP_random_versus_roll(self): arr = tst_arr() add_global_noise(0.5, tst_dim(), arr) l = cy.roll_columns(1, tst_dim(), arr) r = cy.roll_columns(-1, tst_dim(), arr) u = cy.roll_rows(1, tst_dim(), arr) d = cy.roll_rows(-1, tst_dim(), arr) ul = cy.roll_rows(1, tst_dim(), l) dl = cy.roll_rows(-1, tst_dim(), l) ur = cy.roll_rows(1, tst_dim(), r) dr = cy.roll_rows(-1, tst_dim(), r) NB = np.asarray(l) + np.asarray(r) + np.asarray(u) + np.asarray(d) +\ np.asarray(ul) + np.asarray(ur) + np.asarray(dl) + np.asarray(dr) NB2 = neumann_neighbors_array(tst_dim(), arr) testing.assert_array_equal(NB, NB2)
def test_check_rim(self): self.assertFalse(cy.check_rim(0, tst_dim(), tst_arr())) self.assertTrue(cy.check_rim(1, tst_dim(), tst_arr()))
def test_sum_rim(self): self.assertEqual(cy.sum_rim(0, tst_dim(), tst_arr()), 0) self.assertEqual(cy.sum_rim(1, tst_dim(), tst_arr()), 4)
def test_roll_rows_pointer_back(self): arrout = tst_arr() cy.roll_rows_pointer(-1, tst_dim(), arrout) cy.roll_rows_pointer(-1, tst_dim(), arrout) testing.assert_array_equal(arrout, np.roll(tst_arr(), -2, axis=0))
def test_roll_columns_pointer_forward(self): arrout = tst_arr() cy.roll_columns_pointer(1, tst_dim(), arrout) cy.roll_columns_pointer(1, tst_dim(), arrout) testing.assert_array_equal(arrout, np.roll(tst_arr(), 2, axis=1))