Home > Net >  how to make a regular grid base on some irregular points in python
how to make a regular grid base on some irregular points in python

Time:02-08

I have a numpy array of x and y coordinates and want to make it regular. The array is sorted based on its x values (first column):

import numpy as np
Irregular_points = np.array([[1.1,5.], [0.85,7.1], [0.9,9], [1.1,11], [1.,13.1],
                   [1.9,5.2], [2.,6.9], [1.95,9], [2.1,11.1], [2.,13.1],
                   [3.0,5.1], [3.1,7.0], [3.,9], [3.0,11.], [3.1,12.8]])

I want to firtly find out which points have almost the same x values: it will be the first five rows, middle five rows and last five rows. One signal for finding these points is that y value decreases when I go to the next group. Then, I want to replace the x values of each group with the average value. For example in the fisrt five rows x values are 1.1, 0.85, 0.9, 1.1 and 1. and the average is 0.98. I want to do the same for next two parts.

For y values I again want to find similar ones which fall into five groups and then replace them with average of each group. y values of the first group are 5., 5.2 and 5.1 and average is 5.1. Finally, my points should be like the following array:

Regular_points = np.array([[0.98,5.1], [0.98,7.0], [0.98,9.0], [0.98,11.03], [0.98,13.0],
                   [1.98,5.1], [1.98,7.0], [1.98,9.0], [1.98,11.03], [1.98,13.0],
                   [3.04,5.1], [3.04,7.0], [3.04,9.0], [3.04,11.03], [3.04,13.0]])

I tried to round numbers but it did not work for real cases and I need to make these averages. I very much appreciate any help. The figure clearly shows what I want. Red dots are irregular points but by replacing averages, blue dots can be resulted.

enter image description here

CodePudding user response:

Since you're averaging rows and columns, you'll need to use a different shape. Then separate x and y coords, average them by different axis and use np.transpose np.meshgrid for nice display:

irregular_points = np.array([[1.1,5.], [0.85,7.1], [0.9,9], [1.1,11], [1.,13.1],
                             [1.9,5.2], [2.,6.9], [1.95,9], [2.1,11.1], [2.,13.1],
                             [3.0,5.1], [3.1,7.0], [3.,9], [3.0,11.], [3.1,12.8]])

points_reshape = irregular_points.reshape(3, 5, 2)
x, y = np.transpose(points_reshape)
x_mean = x.mean(axis=0)
y_mean = y.mean(axis=1) 
regular_points = np.transpose(np.meshgrid(x_mean, y_mean))
regular_points
>>> 
array([[[ 0.99      ,  5.1       ],
        [ 0.99      ,  7.        ],
        [ 0.99      ,  9.        ],
        [ 0.99      , 11.03333333],
        [ 0.99      , 13.        ]],

       [[ 1.99      ,  5.1       ],
        [ 1.99      ,  7.        ],
        [ 1.99      ,  9.        ],
        [ 1.99      , 11.03333333],
        [ 1.99      , 13.        ]],

       [[ 3.04      ,  5.1       ],
        [ 3.04      ,  7.        ],
        [ 3.04      ,  9.        ],
        [ 3.04      , 11.03333333],
        [ 3.04      , 13.        ]]])
  •  Tags:  
  • Related