pythonnumpymatplotliblinear-algebra

Unfolding a cartesian binned dataset into polar coordinates


I have a dataset of binned events, corresponding to a cartesian coordinate set

[[ 0.  0.  0.  0.  0.  0.  2.  5.  2.  3.  3.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  4. 10.  9.  7. 10.  6.  6.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  9. 12. 10. 11. 14. 13. 11. 12.  6.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0. 12. 16. 17. 14. 13. 14. 13. 12. 12.  6.  0.  0.  0.  0.]
 [ 0.  0. 10. 11.  0. 14. 18. 16. 14. 18. 16. 14. 13.  0.  9.  0.  0.  0.]
 [ 0.  7. 10. 13. 13. 16. 16. 15. 14. 16. 13. 16. 13. 13. 13.  7.  0.  0.]
 [ 1.  6. 15. 14. 17. 14. 13. 13. 14. 15.  1. 13. 13. 12. 12.  7.  2.  0.]
 [ 5. 13. 11. 14. 12. 14. 14. 16. 16. 16. 12.  1. 12. 14. 12.  9.  5.  0.]
 [ 2. 11. 11. 16. 13. 17. 15. 14.  0. 14. 14. 13. 13. 16. 10.  9.  6.  1.]
 [ 4. 11. 13. 12. 14. 14. 16. 16. 14. 18. 16.  1. 14. 12. 12. 11.  5.  1.]
 [ 1.  7. 10. 11. 13. 14.  1. 19. 15. 19.  1.  1. 14. 14. 11. 10.  1.  0.]
 [ 0.  5. 10. 15. 14. 15. 16.  1. 14.  1.  1. 16. 12. 13. 10.  5.  0.  0.]
 [ 0.  0.  7. 12. 16. 15. 13. 17. 14. 16. 14. 14. 14. 14.  7.  0.  0.  0.]
 [ 0.  0.  0.  7.  0. 14. 14. 15. 16. 16. 14. 11. 13.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  8. 12. 14. 12. 14. 10. 11. 12.  7.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  8.  8. 11.  9.  9. 10.  5.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  3.  7.  6.  3.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  2.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]]

So the coordinate (x, y) corresponds to the bin [x, y]. Some cells do not provide useful information and are set to None. (The detection cell is broken). The dataset appears to be roughly circular. To test this I want to show this dataset with cos(phi) on the x axis and the radius from the center (8, 8) on the y axis.

The minimal reproducable example shows the dataset in cartesian coordinates:

import numpy as np

binned_data = np.asarray(
[[ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  5.,  2.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  0.,  4., 10.,  9.,  7., 10.,  6.,  6.,  0.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  9., 12., 10., 11., 14., 13., 11., 12.,  6.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0., 12., 16., 17., 14., 13., 14., 13., 12., 12.,  6.,  0.,  0.,  0.,  0.],
 [ 0.,  0., 10., 11.,  0., 14., 18., 16., 14., 18., 16., 14., 13.,  0.,  9.,  0.,  0.,  0.],
 [ 0.,  7., 10., 13., 13., 16., 16., 15., 14., 16., 13., 16., 13., 13., 13.,  7.,  0.,  0.],
 [ 1.,  6., 15., 14., 17., 14., 13., 13., 14., 15.,  1., 13., 13., 12., 12.,  7.,  2.,  0.],
 [ 5., 13., 11., 14., 12., 14., 14., 16., 16., 16., 12.,  1., 12., 14., 12.,  9.,  5.,  0.],
 [ 2., 11., 11., 16., 13., 17., 15., 14.,  0., 14., 14., 13., 13., 16., 10.,  9.,  6.,  1.],
 [ 4., 11., 13., 12., 14., 14., 16., 16., 14., 18., 16.,  1., 14., 12., 12., 11.,  5.,  1.],
 [ 1.,  7., 10., 11., 13., 14.,  1., 19., 15., 19.,  1.,  1., 14., 14., 11., 10.,  1.,  0.],
 [ 0.,  5., 10., 15., 14., 15., 16.,  1., 14.,  1.,  1., 16., 12., 13., 10.,  5.,  0.,  0.],
 [ 0.,  0.,  7., 12., 16., 15., 13., 17., 14., 16., 14., 14., 14., 14.,  7.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  7.,  0., 14., 14., 15., 16., 16., 14., 11., 13.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  8., 12., 14., 12., 14., 10., 11., 12.,  7.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  0.,  8.,  8., 11.,  9.,  9., 10.,  5.,  0.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  0.,  0.,  4.,  3.,  7.,  6.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
 [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]
)/12

binned_data[4][4] = None
binned_data[-5][-5] = None

binned_data[4][-5] = None
binned_data[-5][4] = None

binned_data[8][8] = None

#Broken Cells
binned_data[6, 10] = None
binned_data[10, 6] = None
binned_data[7, 11] = None
binned_data[11, 7] = None
binned_data[11, 9] = None
binned_data[11, 10] = None
binned_data[10, 11] = None
binned_data[9, 11] = None
binned_data[10, 10] = None



from matplotlib import pyplot as plt
from matplotlib.patches import Circle

fig, ax = plt.subplots()
plt.pcolor(binned_data)
plt.colorbar(label = "Count per hour")

circle = Circle((8.5, 8.5), 8, edgecolor='red', facecolor='none', linewidth=1)
ax.add_patch(circle)

circle = Circle((8.5, 8.5), 6, edgecolor='red', facecolor='none', linewidth=1)
ax.add_patch(circle)

plt.show()

My problem is less with the code itself and more about wrapping my head around the problem. Any help would be heavily appreceated.


Solution

  • Here is a solution with 2d interpolation (if I understood you correctly):

    import scipy.interpolate
    
    rect = scipy.interpolate.RectBivariateSpline(
        np.arange(0, binned_data.shape[0]),
        np.arange(0, binned_data.shape[1]),
        binned_data)
    cx = (binned_data.shape[0]-1)//2
    cy = (binned_data.shape[1]-1)//2
    polar = np.asarray([[rect.ev(
        cx + r*math.cos(phi*math.pi/180),
        cy + r*math.sin(phi*math.pi/180))
            for phi in range(-180, 180)]
              for r in np.arange(0, 10, 0.1)])
    
    plt.pcolor(polar)
    plt.show()
    

    Output (without None points, r step = 0.1, phi step = 1 degree):

    enter image description here