NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.volumeutils.calculate_scale

Next topic

nibabel.volumeutils.finite_range

Reggie -- the one

nibabel.volumeutils.can_cast

nibabel.volumeutils.can_cast(in_type, out_type, has_intercept=False, has_slope=False)

Return True if we can safely cast in_type to out_type

Parameters :

in_type : numpy type

type of data we will case from

out_dtype : numpy type

type that we want to cast to

has_intercept : bool, optional

Whether we can subtract a constant from the data (before scaling) before casting to out_dtype. Default is False

has_slope : bool, optional

Whether we can use a scaling factor to adjust slope of relationship of data to data in cast array. Default is False

Returns :

tf : bool

True if we can safely cast, False otherwise

Examples

>>> can_cast(np.float64, np.float32)
True
>>> can_cast(np.complex128, np.float32)
False
>>> can_cast(np.int64, np.float32)
True
>>> can_cast(np.float32, np.int16)
False
>>> can_cast(np.float32, np.int16, False, True)
True
>>> can_cast(np.int16, np.uint8)
False
>>> can_cast(np.int16, np.uint8, False, True)
False
>>> can_cast(np.int16, np.uint8, True, True)
True