LBL-EESA / fastkde

Other
52 stars 11 forks source link

Question about heatmap creation #40

Open jovan-actdigital opened 3 weeks ago

jovan-actdigital commented 3 weeks ago

Is it possible to get the same result as this code, using fastkde:

def points_to_gaussian_heatmap(centers, height, width, scale):
    gaussians = []
    for x,y in centers:
        s = np.eye(2)*scale
        # s = [[1,0.8],[0.8,1]]
        g = multivariate_normal(mean=(x,y), cov=s)
        # g = np.random.multivariate_normal(mean=(x,y), cov=)
        gaussians.append(g)

    # create a grid of (x,y) coordinates at which to evaluate the kernels
    x = np.arange(0, width)
    y = np.arange(0, height)
    xx, yy = np.meshgrid(x,y)
    xxyy = np.stack([xx.ravel(), yy.ravel()]).T

    # evaluate kernels at grid points
    zz = sum(g.pdf(xxyy) for g in gaussians)

    # zz = stats.gaussian_kde(gaussians.T)

    img = zz.reshape((height,width))
    return img  
taobrienlbl commented 3 weeks ago

Hi @jovan-actdigit, I'm glad to hear of your interest in fastKDE.

At first glance, what you describe looks a lot like the first example from the README:

'''python

""" Demonstrate the first README example. """ import numpy as np import fastkde import matplotlib.pyplot as plt

Generate two random variables dataset (representing 100,000 pairs of datapoints)

N = int(1e5) x = 50np.random.normal(size=N) + 0.1 y = 0.01np.random.normal(size=N) - 300

Do the self-consistent density estimate

PDF = fastkde.pdf(x, y, var_names = ['x', 'y'])

PDF.plot(); '''

Could you please check whether this accomplishes what you're hoping?

jovan-actdigital commented 3 weeks ago

What i want is to create heatmap over a floormap given an array of x,y coordinates. The code that worked best so far is:

cov = np.eye(2)*64
    N = 10
    x_points = np.array([])
    y_points = np.array([])
    for center in centers:
        x, y = np.random.multivariate_normal(center, cov, N).T
        # print(x)
        x_points = np.append(x_points, x)
        y_points = np.append(y_points,y)

    fig, ax = plt.subplots()
    density, grid_points = fastKDE.pdf(x_points, y_points)
    # For visualization (optional)
    bg = mpimg.imread('floorplan.png')
    bg = cv2.resize(bg, (1920 , 1080)) 
    plt.imshow(bg)
    ax.contourf(grid_points[0], grid_points[1], density,levels=100, cmap='jet', alpha=0.7)

Problem is I would like for the generated heatmap to be smoother, like one i get with the code i posted above. Do you know what should I change/fix to get the same result?

taobrienlbl commented 3 weeks ago

Can you post example images of your code vs fastKDE? I’m not in a place where I can easily run code right now


From: jovan-skoric-act @.> Sent: Wednesday, August 28, 2024 6:48:37 AM To: LBL-EESA/fastkde @.> Cc: taobrienlbl @.>; Comment @.> Subject: Re: [LBL-EESA/fastkde] Question about heatmap creation (Issue #40)

What i want is to create heatmap over a floormap given an array of x,y coordinates. The code that worked best so far is:

cov = np.eye(2)*64 N = 10 x_points = np.array([]) y_points = np.array([]) for center in centers: x, y = np.random.multivariate_normal(center, cov, N).T

print(x)

    x_points = np.append(x_points, x)
    y_points = np.append(y_points,y)

fig, ax = plt.subplots()
density, grid_points = fastKDE.pdf(x_points, y_points)
# For visualization (optional)
bg = mpimg.imread('floorplan.png')
bg = cv2.resize(bg, (1920 , 1080))
plt.imshow(bg)
ax.contourf(grid_points[0], grid_points[1], density,levels=100, cmap='jet', alpha=0.7)

Problem is I would like for the generated heatmap to be smoother, like one i get with the code i posted above. Do you know what should I change/fix to get the same result?

— Reply to this email directly, view it on GitHubhttps://github.com/LBL-EESA/fastkde/issues/40#issuecomment-2314974703, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACDDUFVTSEXGGGBOYDLM3Z3ZTWTILAVCNFSM6AAAAABNH3ACDGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJUHE3TINZQGM. You are receiving this because you commented.Message ID: @.***>

jovan-actdigital commented 3 weeks ago

My code heatmap: Screenshot from 2024-08-28 12-52-20 FastKDE code heatmap: Screenshot from 2024-08-28 12-56-14

taobrienlbl commented 3 weeks ago

Thanks! What I'm seeing is about what I'd expect from fastKDE--I just wanted to make sure this wasn't an edge case that caused fastKDE to behave oddly (doesn't look like it). That's as good as it will likely be able to do.

In comparing your two images, they look almost equivalent to me. The main difference seems to be transparency in the top one but not the bottom; you should be able to add alpha=0.7 when plotting a heatmap with fastKDE too.

jovan-actdigital commented 3 weeks ago

You mean lowering alpha value in plt.contour or something specific for fastKDE?

taobrienlbl commented 3 weeks ago

yes, exactly: lowering in plt.contour

taobrienlbl commented 3 weeks ago

Yes: you can set the num_points argument. It has to be a power of 2, plus one: so try 257, 513, or 1025


From: jovan-skoric-act @.> Sent: Thursday, August 29, 2024 6:50:29 AM To: LBL-EESA/fastkde @.> Cc: taobrienlbl @.>; Comment @.> Subject: Re: [LBL-EESA/fastkde] Question about heatmap creation (Issue #40)

Is it possible to make it more smooth when zoomed in? Its to pixelated.

— Reply to this email directly, view it on GitHubhttps://github.com/LBL-EESA/fastkde/issues/40#issuecomment-2317307885, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACDDUFWRL6D64MX6WIORTQLZT34HLAVCNFSM6AAAAABNH3ACDGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJXGMYDOOBYGU. You are receiving this because you commented.Message ID: @.***>