def to_affine(aff, dims=None):
if aff is None: return None<EOL>if isinstance(aff, _tuple_type):<EOL><INDENT>if (len(aff) != <NUM_LIT:2>                       or<EOL>not pimms.is_matrix(aff[<NUM_LIT:0>], '<STR_LIT>') or<EOL>not pimms.is_vector(aff[<NUM_LIT:1>], '<STR_LIT>')):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>mtx = np.asarray(aff[<NUM_LIT:0>])<EOL>off = np.asarray(aff[<NUM_LIT:1>])<EOL>if dims is not None:<EOL><INDENT>if mtx.shape[<NUM_LIT:0>] != dims or mtx.shape[<NUM_LIT:1>] != dims:<EOL><INDENT>raise ValueError('<STR_LIT>' % (dims,dims,dims))<EOL><DEDENT>if off.shape[<NUM_LIT:0>] != dims:<EOL><INDENT>raise ValueError('<STR_LIT>' % (dims,dims))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dims = off.shape[<NUM_LIT:0>]<EOL>if mtx.shape[<NUM_LIT:0>] != dims or mtx.shape[<NUM_LIT:1>] != dims:<EOL><INDENT>raise ValueError('<STR_LIT>' % (dims,dims,dims))<EOL><DEDENT><DEDENT>aff = np.zeros((dims+<NUM_LIT:1>,dims+<NUM_LIT:1>), dtype=np.float)<EOL>aff[dims,dims] = <NUM_LIT:1><EOL>aff[<NUM_LIT:0>:dims,<NUM_LIT:0>:dims] = mtx<EOL>aff[<NUM_LIT:0>:dims,dims] = off<EOL>return pimms.imm_array(aff)<EOL><DEDENT>if not pimms.is_matrix(aff, '<STR_LIT>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>aff = np.asarray(aff)<EOL>if dims is None:<EOL><INDENT>dims = aff.shape[<NUM_LIT:1>] - <NUM_LIT:1><EOL><DEDENT>if aff.shape[<NUM_LIT:0>] == dims:<EOL><INDENT>lastrow = np.zeros((<NUM_LIT:1>,dims+<NUM_LIT:1>))<EOL>lastrow[<NUM_LIT:0>,-<NUM_LIT:1>] = <NUM_LIT:1><EOL>aff = np.concatenate((aff, lastrow))<EOL><DEDENT>if aff.shape[<NUM_LIT:1>] != dims+<NUM_LIT:1> or aff.shape[<NUM_LIT:0>] != dims+<NUM_LIT:1>:<EOL><INDENT>arg = (dims, dims,dims+<NUM_LIT:1>, dims+<NUM_LIT:1>,dims+<NUM_LIT:1>)<EOL>raise ValueError('<STR_LIT>' % arg)<EOL><DEDENT>return aff<EOL>
to_affine(None) yields None.
to_affine(data) yields an affine transformation matrix equivalent to that given in data. Such a
  matrix may be specified either as (matrix, offset_vector), as an (n+1)x(n+1) matrix, or, as an
  n x (n+1) matrix.
to_affine(data, dims) additionally requires that the dimensionality of the data be dims; meaning
  that the returned matrix will be of size (dims+1) x (dims+1).