Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting multiple submatrices in Python

I am trying to extract multiple submatrices if my sparse matrix has multiple regions of non-zero values.

For example, Say I have the following matrix:

x = np.array([0,0,0,0,0,0],
             [0,1,1,0,0,0],
             [0,1,1,0,0,1],
             [0,0,0,0,1,1],
             [0,0,0,0,1,0])

Then I need to be able to extract the regions with non-zero values, ie

x_1 = [[1,1]
       [1,1]]

and

x_2 = [[0,1],
       [1,1],
       [1,0]]

I have been using np.where() to find the indices of non-zero values and returning the region for only one submatrix, but how can I extend this to all possible subregions in my sparse matrix?

Thanks!

like image 510
alvarezcl Avatar asked Nov 03 '14 00:11

alvarezcl


2 Answers

Procedure:

  1. Delete leading and trailing rows and columns with all zeros. (Not middle ones)
  2. Find all empty rows and columns and split matrix on these indices. This creates a list of matrices
  3. For each newly created matrix repeat the procedure recursively until no further splitting is possible

Code:

def delrc(arr):
    while True:     # delete leading rows with all zeros
    if np.all(arr[0]==0):
        arr=np.delete(arr,0,axis=0)
    else: break
    while True:     # delete trailing rows with all zeros
    if np.all(arr[-1]==0):
        arr=np.delete(arr,-1,axis=0)
    else: break
    while True:     # delete leading cols with all zeros
    if np.all(arr[:,0]==0):
        arr=np.delete(arr,0,axis=1)
    else: break
    while True:     # delete trailing cols with all zeros
    if np.all(arr[:,-1]==0):
        arr=np.delete(arr,-1,axis=1)
    else: break
    return arr

def rcsplit(arr):
    if np.all(arr==0): return []    # if all zeros return
    global  res
    arr = delrc(arr)        # delete leading/trailing rows/cols with all zeros
    print arr
    indr = np.where(np.all(arr==0,axis=1))[0]
    indc = np.where(np.all(arr==0,axis=0))[0]
    if not indr and not indc:   # If no further split possible return
    res.append(arr)
    return
    arr=np.delete(arr,indr,axis=0)  #delete empty rows in between non empty rows
    arr=np.delete(arr,indc,axis=1)  #delete empty cols in between non empty cols
    arr=np.split(arr,indc,axis=1)   # split on empty (all zeros) cols 
    print arr
    arr2=[]
    for i in arr:
    z=delrc(i)  
    arr2.extend(np.split(z,indr,axis=0))   # split on empty (all zeros) rows
    for i in arr2:
    rcsplit(np.array(i))        # recursive split again no further splitting is possible

if __name__=="__main__":

    import numpy as np 
    res = []   
    arr = np.array([[0,0,0,0,0,0],
        [0,1,1,0,0,0],
        [0,1,1,0,0,1],
        [0,0,0,0,1,1],
        [0,0,0,0,1,0]])
    rcsplit(arr)
    for i in res: print i
like image 96
Irshad Bhat Avatar answered Oct 16 '22 05:10

Irshad Bhat


You can use built-ins for this.

from scipy.ndimage.measurements import find_objects, label
from scipy.ndimage import generate_binary_structure as gbs

import numpy as np

# create array
x = np.array([[0,0,0,0,0,0],
             [0,1,1,0,0,0],
             [0,1,1,0,0,1],
             [0,0,0,0,1,1],
             [0,0,0,0,1,0]])

# define labeling structure to inlcude adjacent (including diagonal) cells
struc = gbs(2,2)

# generate array of labeled sections labels
x2, numlabels = label(x,struc)

# pull out first group of "ones"
xy1 = find_objects(x2==1)[0]

# pull out second group of "ones"
xy2 = find_objects(x2==2)[0]

Now test:

>>> x[xy1]
    array([[1, 1],
           [1, 1]])

>>> x[xy2]
    array([[0, 1],
           [1, 1],
           [1, 0]])

Voila! If you want to pull out all subsections you can iterate over , which tells you how many different groups you have within the array.

like image 39
tnknepp Avatar answered Oct 16 '22 04:10

tnknepp