def linRegressUsingMasked2dArrays(xVals, arrays, badMask=None, <EOL>zeroOffset=False,<EOL>calcError=False):
assert arrays.ndim == <NUM_LIT:3>, '<STR_LIT>'<EOL>if badMask is not None:<EOL><INDENT>assert arrays.shape == badMask.shape, '<STR_LIT>'<EOL><DEDENT>s = arrays.shape<EOL>y = arrays.reshape(s[<NUM_LIT:0>], s[<NUM_LIT:1>] * s[<NUM_LIT:2>]).astype(float)<EOL>s = s[<NUM_LIT:1>:]<EOL>if zeroOffset:<EOL><INDENT>A = np.array(xVals)[:,np.newaxis]<EOL>ascent = solution = np.linalg.lstsq(A, y)[<NUM_LIT:0>].reshape(s)<EOL>offset = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>A = np.vstack([xVals, np.ones(len(xVals))]).T<EOL>solution = np.linalg.lstsq(A, y)[<NUM_LIT:0>]   <EOL>offset = solution[<NUM_LIT:1>].reshape(s)<EOL>ascent = solution[<NUM_LIT:0>].reshape(s)<EOL><DEDENT>if badMask is not None:<EOL><INDENT>x, y = np.where(np.sum(badMask, axis=<NUM_LIT:0>))<EOL>for xi, yi in zip(x, y):<EOL><INDENT>valid = ~badMask[:, xi, yi]<EOL>try:<EOL><INDENT>if zeroOffset:<EOL><INDENT>A = x[valid,np.newaxis]<EOL>asc = np.linalg.lstsq(A, arrays[valid, xi, yi])[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>asc, offs = linregress(x[valid], arrays[valid, xi, yi])[:<NUM_LIT:2>]<EOL>offset[xi, yi] = offs<EOL><DEDENT>ascent[xi, yi] = asc<EOL><DEDENT>except ValueError:<EOL><INDENT>ascent[xi, yi] = np.nan<EOL>if not zeroOffset:<EOL><INDENT>offset[xi, yi] = np.nan<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if calcError:<EOL><INDENT>error = np.empty(shape=arrays.shape)<EOL>for n, (y, x) in enumerate(zip(arrays, xVals)):<EOL><INDENT>error[n] = (y - (x * ascent + offset))**<NUM_LIT:2><EOL><DEDENT>error = error.mean(axis=<NUM_LIT:0>)**<NUM_LIT:0.5><EOL><DEDENT>else:<EOL><INDENT>error = None<EOL><DEDENT>return ascent, offset, error<EOL>
if you have multiple 2d arrays each with position given by
xVals[array-index]
and you want to do a linear regression on all cells
but you also might mask different areas in each 2darray

zeroOffset - whether plot goest through origin

returns ascent, offset, RMS-error