Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement summed area table integration trick #5

Open
sauln opened this issue Aug 12, 2018 · 4 comments
Open

Implement summed area table integration trick #5

sauln opened this issue Aug 12, 2018 · 4 comments

Comments

@sauln
Copy link
Member

sauln commented Aug 12, 2018

See https://en.wikipedia.org/wiki/Summed-area_table

@rannbaron
Copy link
Contributor

Do you just want a class that takes an array, computes the summed area table, and has a function sum(x1,y1,x2,y2) that returns the sum with those opposite corners?

@sauln
Copy link
Member Author

sauln commented Aug 15, 2018

@ctralie might have a better idea of how to go about doing this as he introduced me to the trick. I imagine the implementation would replace this code:

persim/persim/persim.py

Lines 110 to 120 in 0978c7d

# Implement this as a `summed-area table` - it'll be way faster
spread = self.spread if self.spread else dx
for point in landscape:
x_smooth = norm.cdf(xs_upper, point[0], spread) - norm.cdf(
xs_lower, point[0], spread
)
y_smooth = norm.cdf(ys_upper, point[1], spread) - norm.cdf(
ys_lower, point[1], spread
)
img += np.outer(x_smooth, y_smooth) * weighting(point)
img = img.T[::-1]

Where it is object oriented, a function, or inlined is up to the implementer. Hopefully whatever is easiest.

@rannbaron
Copy link
Contributor

rannbaron commented Aug 16, 2018

Well, if it helps, here's a quick class that implements the basic algorithm.

class SummedAreaTable:

    def __init__(self,img):
        self.img = img
        self.table = np.zeros_like(self.img)
        rows,cols = self.img.shape
        
        # First row
        self.table[0] = np.cumsum(self.img[0])            
        # First column
        self.table[1:,0] = np.cumsum(self.img[:,0])[1:]
        
        for row in range(1,rows):
            for col in range(1,cols):
                self.table[row,col] = self.img[row,col] + self.table[row,col-1] + self.table[row-1,col] - self.table[row-1,col-1]
                
    def tablesum(self,x1,y1,x2,y2):
        return self.table[x2,y2] + self.table[x1,y1] - self.table[x1,y2] - self.table[x2,y1] 

@ctralie
Copy link
Member

ctralie commented Aug 16, 2018

Thanks Rann. I'm just going to copy in something inline like what I've done here:
https://github.com/scikit-tda/ripser.py/blob/bottleneck/DGMTools.py#L311

sauln pushed a commit that referenced this issue Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants