Created
June 10, 2014 23:43
-
-
Save ptweir/b6ac1fbfc2e9686a8982 to your computer and use it in GitHub Desktop.
scripts to stack two numpy arrays of possibly unequal dimensions with options for filling and dealing with truncating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def hstack_uneven(a,b,mxn=max,align='top',fill=0): | |
heighta, widtha = a.shape[0], a.shape[1] | |
heightb, widthb = b.shape[0], b.shape[1] | |
outShape = np.array(a.shape) | |
outShape[0] = mxn(heighta,heightb) | |
outShape[1] = widtha+widthb | |
out = np.zeros(outShape,dtype=a.dtype)+fill | |
if align=='top': | |
if mxn==max: | |
out[:heighta,:widtha,...] = a | |
out[:heightb,widtha:,...] = b | |
if mxn==min: | |
out[:,:widtha,...] = a[:min(heighta,heightb),...] | |
out[:,widtha:,...] = b[:min(heighta,heightb),...] | |
elif align=='bottom': | |
if mxn==max: | |
out[-heighta:,:widtha,...] = a | |
out[-heightb:,widtha:,...] = b | |
if mxn==min: | |
out[:,:widtha,...] = a[-min(heighta,heightb):,...] | |
out[:,widtha:,...] = b[-min(heighta,heightb):,...] | |
else: | |
raise TypeError("Expected 'top' or 'bottom' for align argument, got '%s'" % (align,)) | |
return out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def vstack_uneven(a,b,mxn=max,align='left',fill=0): | |
heighta, widtha = a.shape[0], a.shape[1] | |
heightb, widthb = b.shape[0], b.shape[1] | |
outShape = np.array(a.shape) | |
outShape[0] = heighta+heightb | |
outShape[1] = mxn(widtha,widthb) | |
out = np.zeros(outShape,dtype=a.dtype)+fill | |
if align=='left': | |
if mxn==max: | |
out[:heighta,:widtha,...] = a | |
out[heighta:,:widthb,...] = b | |
if mxn==min: | |
out[:heighta,...] = a[:,:min(widtha,widthb),...] | |
out[heighta:,...] = b[:,:min(widtha,widthb),...] | |
elif align=='right': | |
if mxn==max: | |
out[:heighta,-widtha:,...] = a | |
out[heighta:,-widthb:,...] = b | |
if mxn==min: | |
out[:heighta,...] = a[:,-min(widtha,widthb):,...] | |
out[heighta:,...] = b[:,-min(widtha,widthb):,...] | |
else: | |
raise TypeError("Expected 'left' or 'right' for align argument, got '%s'" % (align,)) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment