I have a big 3d numpy array, each slice (2d array) of I want to write out to an imshow-like figure (i.e. a heatmap of the values). As a concrete example, say the array is of shape 3x3x3000, so I want 3000 images, each of which represents a 3x3 matrix. Looping over it with a single thread is a bit slow. Since the iterations are completely independent, I thought to use the multiprocessing module to speed it up a bit. Code is as below.
def write_tensor_image(t_slice_wrapper):
idx = t_slice_wrapper['idx']
t_slice = t_slice_wrapper['t_slice']
folder_path=t_slice_wrapper['folder_path']
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
ax.imshow(t_slice,interpolation='none')
fig.tight_layout()
fname_ = os.path.join(folder_path,'tmp_%s.png'%str(idx))
fig.savefig(fname_, bbox_inches="tight")
def write_tensor_image_sequence(tensor, folder_path='/home/foo/numpy_cache'):
os.system('mkdir -p %s'%folder_path)
os.system('rm -rf %s/*'%folder_path)
slices = [None]*tensor.shape[2]
for i in range(0,tensor.shape[2]):
slices[i] = {'t_slice':tensor[:,:,i], 'idx':i, 'folder_path':folder_path}
pool = multiprocessing.Pool(processes=4)
pool.map(write_tensor_image, slices)
pool.close()
pool.join()
However this does not work - the single threaded case works fine (just calling write_tensor_image() in the for-loop) but using the pool either causes total lockup of the machine or gives something like the following error:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 849 requests (849 known processed) with 28 events remaining.
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 849 requests (849 known processed) with 28 events remaining.
after 849 requests (849 known processed) with 28 events remaining.
X Error of failed request: BadPixmap (invalid Pixmap parameter)
Major opcode of failed request: 54 (X_FreePixmap)
Resource id in failed request: 0x4e0001e
Serial number of failed request: 851
Current serial number in output stream: 851
I think I'm on the right track (according to e.g. How to fix the python multiprocessing matplotlib savefig() issue? and Matplotlib: simultaneous plotting in multiple threads), but I must be missing something.