janpipek / physt

Python histogram library - histograms as updateable, fully semantic objects with visualization tools. [P]ython [HYST]ograms.
MIT License
129 stars 15 forks source link

Smooth polar histograms? #64

Closed horsto closed 3 years ago

horsto commented 3 years ago

Thanks for writing this awesome library!

I have a question regarding smoothing of polar 2D histograms. I am constructing a histogram like described on this page https://physt.readthedocs.io/en/latest/special_histograms.html#Polar-histogram and now I want to smooth it with a Gaussian kernel (like scipy.ndimage.gaussian_filter). What is the most elegant / correct method to do that?

janpipek commented 3 years ago

Hi Horst, to be honest, I have no idea how to do this (especially in a correct manner). Hope you will find something. Jan

horsto commented 3 years ago

Thanks, Jan, it is ok for me to represent in cartesian and do further analysis there I think. A related question I had: Have you ever tried to analyse the correlation between 2D polar histograms? Do you have any ideas for that? (for me again it would be to extract .frequencies and correlate those in between datasets, but how do I do that correctly for polar 2D histograms...)

horsto commented 3 years ago

Also, to my understanding the .densities compensates for varying bin sizes in those polar histograms - is that assumption correct?

janpipek commented 3 years ago

Eh, sorry for the late answers:

1) I've never done this either :-( What you suggest is perhaps right but only you know whether the implicitly larger weight for the areas around the center is justifiable or not (and potentially how to account for it).

2) Yes, .densities properly divides by the area of the bin.

Good luck! Jan

horsto commented 3 years ago

Thanks, ok, but then I have a final dumb question: Can I get the .frequencies or .densities 2D arrays in cartesian coordinates (similar to what is described here: https://stackoverflow.com/questions/60615268/rearrange-data-in-two-dimensional-array-according-to-transformation-from-polar-t) - do you have a convenience function to switch between the two (polar vs. cartesian)?

janpipek commented 3 years ago

I am not sure what exactly your goal is. But let's assume that you want to know the density at cartesian points (x_i, y_i) from a polar histogram. In such case...

As an example:

xrange = np.arange(-2, 2, .1)
yrange = np.arange(-2, 2, .1)
bins = [[hist.find_bin((x, y)) for y in yrange] for x in xrange]
values = [[hist.densities[bin[0], bin[1]] if bin else 0 for bin in row] for row in bins]
values = np.asarray(values)
ax = plt.imshow(values.T[::-1])  # imshow treats axes differently
janpipek commented 3 years ago

Hope you succeeded.