I am processing very large data sets on python 64 bits and need some help to optimize my interpolation code.
I am used to using numpy to avoid loops but here there are 2 loops that I can't find a way to avoid.
The main problem is also that the size of the arrays I need to compute gives a Memory Error when I use numpy so I switched to scipy sparse arrays which works but takes way too much time to compute the 2 left loops...
I tried to build iteratively my matrix using numpy.fromfunction but it won't run because the size of the array is too large.
I have already read a lot of posts about building large arrays but the arrays that were asked about were too simple compared to what I have to build so the solutions don't work here.
I cannot reduce the size of the data set, since it is a point cloud that I have already tiled in 10x10 tiles.
Here is my interpolation code :
z_int = ss.dok_matrix((x_int.shape))
n,p = x_obs.shape
m = y_obs.shape[0]
a1 = ss.coo_matrix( (n, 3), dtype=np.int64 )
a2 = ss.coo_matrix( (3, 3), dtype=np.int64 )
a3 = ss.dok_matrix( (n, m))
a4 = ss.coo_matrix( (3, n), dtype=np.int64)
b = ss.vstack((z_obs, ss.coo_matrix( (3, 1), dtype=np.int64 ))).tocoo()
a1 = ss.hstack((ss.coo_matrix(np.ones((n,p))), ss.coo_matrix(x_obs), ss.coo_matrix(y_obs)))
shape_a3 = a3.shape[0]
for l in np.arange(0, shape_a3):
for c in np.arange(0, shape_a3) :
if l == c:
a3[l, c] = rho
else:
a3[l, c] = phi(x_obs[l] - x_obs[c], y_obs[l] - y_obs[c])
a4 = a1.transpose()
a12 = ss.vstack((a1, a2))
a34 = ss.vstack((a3, a4))
a = ss.hstack((a12, a34)).tocoo()
x = spsolve(a, b)
for i in np.arange(0, z_int.shape[0]):
for j in np.arange(0, z_int.shape[0]):
z_int[i, j] = x[0] + x[1] * x_int[i, j] + x[2] * y_int[i, j] + np.sum(x[3:] * phi(x_int[i, j] - x_obs, y_int[i, j] - y_obs).T)
return z_int.todense()
where dist() is a function that computes distance, and phi is the following :
return dist(dx, dy) ** 2 * np.log(dist(dx, dy))
I need the code to run faster and I am aware that it might be very badly written but I would like to learn how to write something more optimized to improve my coding skills.