I have an array that I'd like to split into sub-arrays based on the obvious and non-overlapping rectangles:
>>> A = array([[ 0., nan, 2., nan, 4., nan, 6, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, 20, nan, 22, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ 32., nan, 34., nan, 36., nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan]])
These locations are easily findable with np.argwhere
, and it seems natural to do it the np.split
. My desired output is
>>> np.split_2d(A)
(array([[ 0., nan ]
[ nan, nan ]
[ nan, nan ]
[ nan, nan ]])
array([[ 2., nan ]
[ nan, nan ]
[ nan, nan ]
[ nan, nan ]])
array([[ 32., nan ]
[ nan, nan ]
[ nan, nan ]
[ nan, nan ]])
array([[ 34., nan ]
[ nan, nan ]
[ nan, nan ]
[ nan, nan ]])
array([[ 4., nan ]
[ nan, nan ]])
array([[ 6., nan ]
[ nan, nan ]])
array([[ 20, nan ]
[ nan, nan ]])
array([[ 22, nan ]
[ nan, nan ]])
array([[ 36., nan, nan, nan ]
[ nan, nan, nan, nan ]
[ nan, nan, nan, nan ]
[ nan, nan, nan, nan ]]))
...
np.split
and the corresponding components vsplit
, hsplit
and dsplit
and only work along a specified axis and an array of indices.
A question on binning answers a similar question, but in my case the bins are not regularly spaced and not the same size.
In my case, I'm trying to approximate an image from only a few samples. Hence, I want the image to be split up in the most obvious and intuitive way. I want the image to be divided up into quadrants essentially. For example, the lower right corner of this image would belong to the 36 term and not the 22 term.
Is there an easy way to do this, or do I have to parse it all myself?
def recurse(A):
if A.shape[0]>A.shape[1]: #split longest axis first
if not np.isnan( A[0,A.shape[1]//2]):
return [rect for part in np.split(A, 2, axis=1) for rect in recurse(part)]
if not np.isnan( A[A.shape[0]//2,0]):
return [rect for part in np.split(A, 2, axis=0) for rect in recurse(part)]
else:
if not np.isnan( A[A.shape[0]//2,0]):
return [rect for part in np.split(A, 2, axis=0) for rect in recurse(part)]
if not np.isnan( A[0,A.shape[1]//2]):
return [rect for part in np.split(A, 2, axis=1) for rect in recurse(part)]
return [A]
This produces the desired split for this dataset; but it hinges on several assumptions about the layout of the data, which you havnt specified, and which may not hold. But one could modify the general idea to accommodate a variety of circumstances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With