I have an object occupying an underlying N-dimensional square grid (represented by a numpy array) so that only 25% of the grid points are occupied. Each 1x1x1x... N-cube (i.e., hypercube) in this grid contains the same amount of this object (located only at some of the vertices of this hypercube). I have an array of the coordinates of all the occupied grid points. The task is to cycle through this array and extract the occupied coordinates for each 1x1x1... hypercube and store them in a new array for further processing.
The situation is best explained by example. Consider the 3-D case where the underlying grid has been chosen so that 1<=i,j,k<=4
giving the 2d numpy array:
A
= [
[1 1 1]
[1 2 1]
[1 3 1]
[1 4 1]
[2 1 1]
[2 2 1]
[2 3 1]
[2 4 1]
[3 1 1]
[3 2 1]
[3 3 1]
[3 4 1]
[4 1 1]
[4 2 1]
[4 3 1]
[4 4 1]
[1 1 2]
[1 2 2]
[1 3 2]
[1 4 2]
[2 1 2]
[2 2 2]
[2 3 2]
[2 4 2]
[3 1 2]
[3 2 2]
[3 3 2]
[3 4 2]
[4 1 2]
[4 2 2]
[4 3 2]
[4 4 2]
[1 1 3]
[1 2 3]
[1 3 3]
[1 4 3]
[2 1 3]
[2 2 3]
[2 3 3]
[2 4 3]
[3 1 3]
[3 2 3]
[3 3 3]
[3 4 3]
[4 1 3]
[4 2 3]
[4 3 3]
[4 4 3]
[1 1 4]
[1 2 4]
[1 3 4]
[1 4 4]
[2 1 4]
[2 2 4]
[2 3 4]
[2 4 4]
[3 1 4]
[3 2 4]
[3 3 4]
[3 4 4]
[4 1 4]
[4 2 4]
[4 3 4]
[4 4 4]
]
An example of the 2d numpy array that I need to process in this case is:
B
= [[1,1,1],[1,2,1],[1,3,1],[1,4,1],[2,2,1],[2,3,1],[2,4,1],[3,2,1],[3,3,1],[3,4,1],[4,1,1],[4,3,1],[1,1,2],[1,4,2],[2,1,2],[2,2,2],[2,4,2],[3,1,2],[3,2,2],[3,4,2],[4,1,2],[4,2,2],[4,3,2],[4,4,2],[1,1,3],[1,4,3],[2,1,3],[2,2,3],[2,3,3],[2,4,3],[3,1,3],[3,2,3],[3,3,3],[4,1,3],[4,2,3],[4,3,3],[4,4,3],[1,2,4],[1,3,4],[1,4,4],[2,1,4],[2,2,4],[2,3,4],[3,1,4],[3,2,4],[3,3,4],[4,3,4],[4,4,4]]
B
is the array of the occupied grid points only. Cycling through B
, I would like to get the occupied coordinates pertaining to each of the cubes of the underlying grid. The cube edges are defined by 1<=i,j,k<=2
, (3<=i<=4
) & (1<=j,k<=2
), etc. For instance, for the cube demarcated by 1<=i,j,k<=2
, I would like to extract the array [[1,1,1],[1,1,2],[1,2,1],[2,1,2],[2,2,1],[2,2,2]] from B
and store it in a new array for further processing. This is then repeated until all the 8 cubes in this example are accounted for. Note that the grids are always chosen to be even so that this type of partitioning is always possible. While I can choose the grids to be even, I have no control over the site occupations by the object.
Numpy array slicing doesn't work because, in general, the grid points are not contiguous in B
. I attempted a code using "for" loops to impose the ranges for the edges of the cubes but it got too complicated quickly and it doesn't seem like the right approach. Then there is the "numpy.where" function; but the complexity of the condition makes it rather intractable. Similar challenges arise with "numpy.extract".