sunpy icon indicating copy to clipboard operation
sunpy copied to clipboard

Introduced area-based contours

Open Paras20222 opened this issue 1 year ago • 5 comments

PR Description

This PR introduces area-based contour calculations to enhance visualization accuracy. The changes address issue #7420 and provide a more intuitive method for determining contour levels. This improvement enables better data interpretation in X-ray and radio imaging applications.

Paras20222 avatar Feb 11 '24 15:02 Paras20222

Thank you for the PR @Paras20222. I will do a quick review now.

nabobalis avatar Feb 11 '24 17:02 nabobalis

Hi @Paras20222, will you have some time soon to address the outstanding comments?

nabobalis avatar Mar 20 '24 22:03 nabobalis

Hello, i have made few changes to the gist. `import numpy as np import matplotlib.pyplot as plt

def area_contour_image(img: np.ndarray, quantiles: list[float], ax=None, x=None, y=None, cmap='Reds', **contour_kwds):

# Normalizing img data
normalized_img = (img - np.min(img)) / np.ptp(img)

# Flatten img data
flattened_img = normalized_img.ravel()

# Sort pixels: small areas first, large areas later
sorted_values = np.sort(flattened_img)[::-1]

# Computing cumi sum
cumulative_sum = np.cumsum(sorted_values) / np.sum(sorted_values)

# Indices corresponding to specified quantiles
indices = np.searchsorted(cumulative_sum, quantiles)

# Threshold values for desired percentile and for drawing contours
thresholds = np.sort(sorted_values[indices])

if ax is None:
    fig, ax = plt.subplots()
if x is None or y is None:
    return ax.contour(normalized_img, levels=thresholds, cmap=cmap, **contour_kwds)
else:
    return ax.contour(x, y, normalized_img, levels=thresholds, cmap=cmap, **contour_kwds)

def test(): x = np.linspace(-10, 10, num=1000) y = x.copy() xx, yy = np.meshgrid(x, y)

# Similar to Gaussian func
sigx, sigy = 2, 5
z = 5 * np.exp(-(xx**2 / sigx**2 + yy**2 / sigy**2))

# Plot
fig, ax = plt.subplots(figsize=(6, 6))
ax.pcolormesh(xx, yy, z, cmap='inferno')

# Generating contours
contours = area_contour_image(z, [0.05, 0.3, 0.5, 0.8], ax=ax, x=x, y=y, cmap='Reds', linewidths=1.75)

# Plot customization
ax.set(xlabel='x', ylabel='y', title='Area Contours')
ax.grid(True)
plt.colorbar(contours, ax=ax)
plt.show()

if name == 'main': test()`

AquarlisPrime avatar Mar 28 '24 06:03 AquarlisPrime

Hello, i have made few changes to the gist. `import numpy as np import matplotlib.pyplot as plt

Modifying the gist is not the most useful, we can't make suggestions nor does it move this pull request forward.

I would suggest that you take over this pull request, you will need to fetch the original comments from this pull request onto your sunpy fork, then submit a new pull request with any changes you think need to be made as well addressing the comments that i left here?

Does that sound ok? We can also help with any of the steps if you get stuck.

nabobalis avatar Mar 28 '24 15:03 nabobalis

Sure, I would be happy to work upon it. I am new to open source contributions, so I am thankful for your words of guidance.

AquarlisPrime avatar Mar 28 '24 18:03 AquarlisPrime

Hi @Paras20222, sorry for the late response.

I am going to close this PR for the time being. If you want to take this forward, I would suggest reopening the PR!

nabobalis avatar Jul 03 '24 16:07 nabobalis