Method summary
- __init__(self, sctype_list = None, sctype_tols = None, recast_options = 'only_if_none')
- cast_to_fp(self, arr, kind, max_size = inf, continue_down = False)
- cast_to_integer(self, arr, prefer = 'i')
- default_sctype_tols(self)
- get_capable_sctype(self, sct)
- recast(self, arr)
- recast_best_sctype(self, arr)
- sctypes_by_size(self, kind)
- smallest_int_sctype(self, mx, mn, prefer = 'i')
Methods
- __init__(self, sctype_list = None, sctype_tols = None, recast_options = 'only_if_none')
Set types for which we are attempting to downcast Input sctype_list - list of acceptable scalar types If None defaults to all system types sctype_tols - dictionary key datatype, values rtol, tol to specify tolerances for checking near equality in downcasting. Note that tolerance values for integers are used for upcasting integers to floats recast_options - dictionary of options for recasting or string specifying one of default options dictionaries. recast_option strings can be: only_if_none - only attempts recast if the type is not in acceptable types smallest - return array of smallest possible type within tolerance fairly_small - compromise set of options between speed of downcast and size of output preserve_precision - recasts arrays only to types that preserve precision Elements in recast_options dictionary: fp_to_int - "always" or "if_none" or "never" When to attempt cast of floating point to int fp_to_fp - "always" or "if_none" or "never" When to attempt cast of floating point to floating point int_to_int - "always" or "if_none" or "never" When to attempt cast of int to int int_to_fp - "always" or "if_none" or "never" When to attempt cast of int to floating point downcast_only - if True, only return datatype of same size or less downcast_within_fp - if True, tries downcasting within fp types, even if there is an fp type that already matches guarantee_fp_to_fp_precision - if True, will only do fp to fp array casting to type of same or higher precision. Note that if fp_to_int recasting is allowed this will allow precision loss of fp values prefer_input_at_threshold - number of bytes. If input array size is less than or equal to this number, and in valid types list, return the array without attempting recasting prefer_int_type - if 'i', when recasting to integer type, prefer int when equal sized uint is also available. Prefer uint otherwise. - cast_to_fp(self, arr, kind, max_size = inf, continue_down = False)
Return fp arr maybe recast to specified kind, different sctype Inputs arr - array to possibly recast kind - kind of array to recast within (one of "c", "f", "u", "i") max_size - maximum size of sctype to return (in bytes) continue_down - if False, return array of largest sctype within tolerance and >= max_size if True, continue downcasting within kind to find smallest possible within tolerance If arr cannot be recast within given tolerances, and size, return None - cast_to_integer(self, arr, prefer = 'i')
Casts arr to smallest integer containing range
Returns None if range of arr cannot be contained in acceptable integer types
- prefer - if == 'i' prefer int for range also compatible
- uint, else prefer uint in same situation
- default_sctype_tols(self)
Default allclose tolerance values for all dtypes
- get_capable_sctype(self, sct)
Return smallest scalar type containing sct type without precision loss
Input sct - scalar type
ID = input type. AT = acceptable type. Return ID if ID is in ATs. Otherwise return smallest AT that is larger than or same size as ID.
If the desired sctype is an integer, returns the smallest integer (int or uint) that can contain the range of the input integer type
If there is no type that can contain sct without loss of precision, return None
- recast(self, arr)
Recast array to type in type list
If cannot recast to an array within tolerance, raise error
- recast_best_sctype(self, arr)
Recast array, return closest sctype to original
Returns tuple of recast array and best sctype to contain original data before recasting
- sctypes_by_size(self, kind)
Returns storage size ordered list of entries of scalar type sctype Input kind - one of "c", "f", "i" or "u" (for complex, float, integer, unsigned integer) - smallest_int_sctype(self, mx, mn, prefer = 'i')
Return integer type with smallest storage containing mx and mn Inputs mx - maximum value mn - minumum value prefer - if == 'i' prefer int for range also compatible uint, else prefer uint in same situation Returns None if no integer can contain this range
