def test_safe_dtype(): yield assert_raises, TypeError, safe_dtype, type('foo') yield assert_raises, TypeError, safe_dtype, type('foo'), np.float64 yield assert_raises, TypeError, safe_dtype, [('x', 'f8')] valid_dtypes = [] valid_dtypes.extend(np.sctypes['complex']) valid_dtypes.extend(np.sctypes['float']) valid_dtypes.extend(np.sctypes['int']) valid_dtypes.extend(np.sctypes['uint']) for dt in valid_dtypes: sdt = safe_dtype(dt) yield assert_equal, sdt, dt # test a few upcastings dt = safe_dtype(np.float32, np.int16, np.bool) yield assert_equal, dt, np.float32 dt = safe_dtype(np.float32, np.int64, np.uint32) yield assert_equal, dt, np.float64
def test_safe_dtype(): yield assert_raises, TypeError, safe_dtype, type('foo') yield assert_raises, TypeError, safe_dtype, type('foo'), np.float64 yield assert_raises, TypeError, safe_dtype, [('x', 'f8')] valid_dtypes = [] valid_dtypes.extend(np.sctypes['complex']) valid_dtypes.extend(np.sctypes['float']) valid_dtypes.extend(np.sctypes['int']) valid_dtypes.extend(np.sctypes['uint']) for dt in valid_dtypes: sdt = safe_dtype(dt) yield assert_equal, sdt, dt # test a few upcastings dt = safe_dtype(np.float32, np.int16, np.bool) yield assert_equal, dt, np.float32 dt = safe_dtype(np.float32, np.int64, np.uint32) yield assert_equal, dt, np.float64 # test byteswapped types - isbuiltin will be false orig_dt = np.dtype('f') dt = safe_dtype(orig_dt, np.int64, np.uint32) yield assert_equal, dt, np.float64 swapped_dt = orig_dt.newbyteorder() dt = safe_dtype(swapped_dt, np.int64, np.uint32) yield assert_equal, dt, np.float64
def __init__(self, affine, input_coords, output_coords): """ Return an CoordinateMap specified by an affine transformation in homogeneous coordinates. Parameters ---------- affine : array-like affine homogenous coordinate matrix input_coords : :class:`CoordinateSystem` input coordinates output_coords : :class:`CoordinateSystem` output coordinates Notes ----- The dtype of the resulting matrix is determined by finding a safe typecast for the input_coords, output_coords and affine. """ affine = np.asarray(affine) dtype = safe_dtype(affine.dtype, input_coords.coord_dtype, output_coords.coord_dtype) inaxes = input_coords.coord_names outaxes = output_coords.coord_names self._input_coords = CoordinateSystem(inaxes, input_coords.name, dtype) self._output_coords = CoordinateSystem(outaxes, output_coords.name, dtype) affine = np.asarray(affine, dtype=dtype) if affine.shape != (self.ndim[1]+1, self.ndim[0]+1): raise ValueError('coordinate lengths do not match ' 'affine matrix shape') self._affine = affine A, b = affines.to_matrix_vector(affine) def _mapping(x): value = np.dot(x, A.T) value += b return value self._mapping = _mapping