2.1.1.1.3. sf_tools.base.types module

TYPE HANDLING ROUTINES

This module contains methods for handing object types.

Author:Samuel Farrens <samuel.farrens@gmail.com>
Version:1.0
Date:04/04/2017
sf_tools.base.types.check_float(val)[source]

Check if input value is a float or a np.ndarray of floats, if not convert.

Parameters:val (any) – Input value
Returns:
Return type:float or np.ndarray of floats
sf_tools.base.types.check_int(val)[source]

Check if input value is an int or a np.ndarray of ints, if not convert.

Parameters:val (any) – Input value
Returns:
Return type:int or np.ndarray of ints
sf_tools.base.types.val2int(val)[source]

Convert to int

This method checks if input value is an int and if not converts it.

Parameters:val (int, float, str, list, tuple or np.ndarray) – Input value
Returns:
Return type:int interger value or np.ndarray array of ints
Raises:ValueError – For invalid input type

Examples

>>> from sf_tools.base.types import val2int
>>> x = np.arange(5).astype(float)
>>> x
array([ 0.,  1.,  2.,  3.,  4.])
>>> val2int(x)
array([0, 1, 2, 3, 4])
sf_tools.base.types.val2float(val)[source]

Convert to float

This method checks if input value is a float and if not converts it.

Parameters:val (int, float, str, list, tuple or np.ndarray) – Input value
Returns:
Return type:float floating point value or np.ndarray array of floats

Examples

>>> from sf_tools.base.types import val2float
>>> x = np.arange(5)
>>> x
array([0, 1, 2, 3, 4])
>>> val2float(x)
array([ 0.,  1.,  2.,  3.,  4.])
sf_tools.base.types.val2str(val)[source]

Convert to string

This method checks if input value is a string and if not converts it.

Parameters:val (int, float, str, list, tuple or np.ndarray) – Input value
Returns:
Return type:str string or np.ndarray array of strings

Examples

>>> from sf_tools.base.types import val2str
>>> x = np.arange(5)
>>> x
array([0, 1, 2, 3, 4])
>>> val2str(x)
array(['0', '1', '2', '3', '4'],
      dtype='|S21')
sf_tools.base.types.nan2val(array, val=0.0)[source]

Convert NAN to val

This converts all NANs in an array to a specified value.

Parameters:
  • array (np.ndarray, list or tuple) – Input array
  • val (int or float, optional) – Value to replace NANs. Default (val=0.0)
Returns:

Return type:

np.ndarray array without NANs

Notes

Output data type defined by val type.

Examples

>>> from sf_tools.base.types import nan2val
>>> x = [1., 2., np.nan, 4.]
>>> x
[1.0, 2.0, nan, 4.0]
>>> nan2val(x, 3.)
array([ 1.,  2.,  3.,  4.])