python - Concatenate large numpy arrays in RAM -
i have 3d image data , want build stack of rgb images out of single channel stacks, i.e. try concatenate 3 arrays of shape (358, 1379, 1042) 1 array of shape (358, 1379, 1042, 3). inspired skimage.color.gray2rgb tried
np.concatenate(( stack1[..., np.newaxis], stack2[..., np.newaxis], stack3[..., np.newaxis]), axis=-1) however, though each of these stacks 1gib fills empty ~12gib ram ... tried pre-allocate array of final shape , fill stacks, like
rgb_stack = np.zeros(stack1.shape + (3,)) rgb_stack[:,:,:,0] = stack1 which exhausted ram once execute second line. tried explicitly copy data stack1 rgb_stack by
rgb_stack = np.zeros(stack1.shape + (3,)) rgb_stack[:,:,:,0] = stack1.copy() with same result. doing wrong?
to wrap can learnt comments question; np.zeros creates array of float64 12gib big. not fill ram linux on commits , sets corresponding ram aside once array gets filled, in case once gets filled image data.
thus creating zeros dtype solves problem, e.g.
rgb_stack = np.zeros(stack1.shape + (3,), dtype=np.uint16) rgb_stack[:,:,:,0] = stack1.copy() works fine uint16 stacks.
Comments
Post a Comment