def test_subv2ind(): """ FUNCTION: subv2ind, in general.py. """ """Create data required to test function""" sz = np.array([2., 2.]) sub = np.array([[0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 1], [0, 0], [0, 1], [1, 0], [1, 1], [1, 0], [1, 1], [1, 0], [1, 0], [0, 1], [1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 0], [1, 1], [1, 0], [1, 0], [0, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [0, 0], [1, 1]]) """Execute function""" index = general.subv2ind(sz, sub) """Createn expected output for comparison""" t_index = np.array([[2.], [2.], [1.], [1.], [2.], [1.], [2.], [2.], [1.], [2.], [2.], [1.], [2.], [1.], [2.], [3.], [0.], [2.], [1.], [3.], [1.], [3.], [1.], [1.], [2.], [1.], [1.], [1.], [2.], [2.], [1.], [1.], [0.], [3.], [1.], [1.], [0.], [1.], [0.], [1.], [0.], [2.], [0.], [2.], [2.], [0.], [1.], [1.], [0.], [3.]]) """Assert the function executed correctly""" assert_array_equal(index, t_index)
def create_lattice_sparse_adj_matrix(rows, cols, layers=1): """ Creates an adjacency matrix for a lattice shaped graph with an arbitrary number of rows and columns. NOTE: The subv2ind routine used in the function works in column-major order, and this function assumes row-major order, therefore row and column indices have been switched round when using the subv2ind function. """ """Create adjacency matrix""" adj_mat = sparse.lil_matrix((rows*cols*layers, rows*cols*layers), dtype=int) """Assign the 2 edges for top-left node""" adj_mat[0, 1] = 1 adj_mat[0, cols] = 1 """Assign the 2 edges for top-right node""" ind = subv2ind(np.array([cols, rows]), np.array([cols-1, 0]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-2, 0]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-1, 1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign the 2 edges for bottom-left node""" ind = subv2ind(np.array([cols, rows]), np.array([0, rows-1]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([0, rows-2]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) temp_ind = subv2ind(np.array([cols, rows]), np.array([1, rows-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign the 2 edges for bottom_right node""" ind = subv2ind(np.array([cols, rows]), np.array([cols-1, rows-1]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-2, rows-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-1, rows-2]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign the 3 edges for each left border nodes""" for i in range(1, rows-1): ind = subv2ind(np.array([cols, rows]), np.array([0, i]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([0, i-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) temp_ind = subv2ind(np.array([cols, rows]), np.array([0, i+1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) adj_mat[ind, ind+1] = 1 """Assign the 3 edges for each right border nodes""" for i in range(1, rows-1): ind = subv2ind(np.array([cols, rows]), np.array([cols-1, i]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-1, \ i-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) temp_ind = subv2ind(np.array([cols, rows]), np.array([cols-1, \ i+1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) adj_mat = set_adj_mat_entry(adj_mat, [ind, ind-1]) """Assign the 3 edges for each top border nodes""" for i in range(1, cols-1): ind = subv2ind(np.array([cols, rows]), np.array([i, 0]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, ind-1]) adj_mat = set_adj_mat_entry(adj_mat, [ind, ind+1]) temp_ind = subv2ind(np.array([cols, rows]), np.array([i, 1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign the 3 edges for each bottom border nodes""" for i in range(1, cols-1): ind = subv2ind(np.array([cols, rows]), np.array([i, rows-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, ind-1]) adj_mat = set_adj_mat_entry(adj_mat, [ind, ind+1]) temp_ind = subv2ind(np.array([cols, rows]), np.array([i, rows-2]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign edges for inner, fully-connected nodes""" for i in range(1, rows-1): for j in range(1, cols-1): ind = subv2ind(np.array([cols, rows]), np.array([j, i]))[0, 0] temp_ind = subv2ind(np.array([cols, rows]), np.array([j, i-1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) adj_mat = set_adj_mat_entry(adj_mat, [ind, ind-1]) adj_mat = set_adj_mat_entry(adj_mat, [ind, ind+1]) temp_ind = subv2ind(np.array([cols, rows]), np.array([j, i+1]))[0, 0] adj_mat = set_adj_mat_entry(adj_mat, [ind, temp_ind]) """Assign the edges to the observed nodes""" for i in range(0, adj_mat.shape[0]/2): adj_mat[i, i + adj_mat.shape[0]/2] = 1 return adj_mat
def test_subv2ind(): """ FUNCTION: subv2ind, in general.py. """ """Create data required to test function""" sz = np.array([2.0, 2.0]) sub = np.array( [ [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 1], [0, 0], [0, 1], [1, 0], [1, 1], [1, 0], [1, 1], [1, 0], [1, 0], [0, 1], [1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 0], [1, 1], [1, 0], [1, 0], [0, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [0, 0], [1, 1], ] ) """Execute function""" index = general.subv2ind(sz, sub) """Createn expected output for comparison""" t_index = np.array( [ [2.0], [2.0], [1.0], [1.0], [2.0], [1.0], [2.0], [2.0], [1.0], [2.0], [2.0], [1.0], [2.0], [1.0], [2.0], [3.0], [0.0], [2.0], [1.0], [3.0], [1.0], [3.0], [1.0], [1.0], [2.0], [1.0], [1.0], [1.0], [2.0], [2.0], [1.0], [1.0], [0.0], [3.0], [1.0], [1.0], [0.0], [1.0], [0.0], [1.0], [0.0], [2.0], [0.0], [2.0], [2.0], [0.0], [1.0], [1.0], [0.0], [3.0], ] ) """Assert the function executed correctly""" assert_array_equal(index, t_index)