One idea would be to downsample using sparse operations.
data = data.tocsc() # sparse operations are more efficient on csc
N, M = data.shape
s, t = 400, 400 # decimation factors for y and x directions
T = sparse.csc_matrix((np.ones((M,)), np.arange(M), np.r_[np.arange(0, M, t), M]), (M, (M-1) // t + 1))
S = sparse.csr_matrix((np.ones((N,)), np.arange(N), np.r_[np.arange(0, N, s), N]), ((N-1) // s + 1, N))
result = S @ data @ T # downsample by binning into s x t rectangles
result = result.todense() # ready for plotting
This code snippet implements a simple binning, but could be refined to incorporate more sophisticated filters. The binning matrices are just binned id matrices, for example S_ij = 1 if j // s = i else 0.
Some more explanation. Since the original matrix is very large there is scope to downsample it, without any visually noticable difference in the output.
The question is how to downsample without creating a dense representation first. One possible answer is to express the binning in terms of matrix multiplication and then use sparse matrix multiplication.
So, if multiplying your original data from the right with a binning matrix T
then the columns of T
correspond to the column bins, in particular the number of columns of T
will determine how many pixels the downsampled data will have in x direction. Each column of T
determines what goes into the corresponding bin and what not. In the example I set a number of elements encoding adjacent columns (of the original matrix) to 1 and the rest to 0. This takes these columns sums across them and puts the sum in the result matrix, in other words it bins these columns together.
Multiplying from the left works in exactly the same way, only it affects rows, not columns.
If you feel that binning is too crude you can replace the simple zero one scheme for example with a smooth kernel, just make sure that the resulting matrix remains sparse. Setting up such a matrix requires a bit more effort, but is not difficult. You are using a sparse matrix for your data, so I assume you are familiar with how to construct a sparse matrix.