mlair.helpers.helpers

Collection of different help functions.

Module Contents

Functions

unpack(model, training_config, weights)

make_keras_pickable()

to_list(obj: Any) → List

Transform given object to list if obj is not already a list. Sets are also transformed to a list.

sort_like(list_obj: list, sorted_obj: list)

Sort elements of list_obj as ordered in sorted_obj. Length of sorted_obj as allowed to be higher than length of

dict_to_xarray(d: Dict, coordinate_name: str) → xarray.DataArray

Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>.

float_round(number: float, decimals: int = 0, round_type: Callable = math.ceil) → float

Perform given rounding operation on number with the precision of decimals.

relative_round(x: float, sig: int, ceil=False, floor=False) → float

Round small numbers according to given “significance”.

get_order(x: float)

Get order of number (as power of 10)

remove_items(obj: Union[List, Dict, Tuple], items: Any)

Remove item(s) from either list, tuple or dictionary.

select_from_dict(dict_obj: dict, sel_list: Any, remove_none: bool = False, filter_cond: bool = True) → dict

Extract all key values pairs whose key is contained in the sel_list.

extract_value(encapsulated_value)

is_xarray(arr) → bool

Returns True if arr is xarray.DataArray or xarray.Dataset.

convert2xrda(arr: Union[xarray.DataArray, xarray.Dataset, numpy.ndarray, int, float], use_1d_default: bool = False, **kwargs) → Union[xarray.DataArray, xarray.Dataset]

Converts np.array, int or float object to xarray.DataArray.

filter_dict_by_value(dictionary: dict, filter_val: Any, filter_cond: bool) → dict

Filter dictionary by its values.

str2bool(v)

squeeze_coords(d)

Look for unused coords and remove them. Does only work for xarray DataArrays.

Attributes

__author__

__date__

mlair.helpers.helpers.__author__ = Lukas Leufen, Felix Kleinert
mlair.helpers.helpers.__date__ = 2019-10-21
mlair.helpers.helpers.unpack(model, training_config, weights)
mlair.helpers.helpers.make_keras_pickable()
mlair.helpers.helpers.to_list(obj: Any) → List

Transform given object to list if obj is not already a list. Sets are also transformed to a list.

Parameters

obj – object to transform to list

Returns

list containing obj, or obj itself (if obj was already a list)

mlair.helpers.helpers.sort_like(list_obj: list, sorted_obj: list)

Sort elements of list_obj as ordered in sorted_obj. Length of sorted_obj as allowed to be higher than length of list_obj, but must contain at least all objects of list_obj. Will raise AssertionError, if not all elements of list_obj are also in sorted_obj. Also it is required for list_obj and sorted_obj to have only unique elements.

Parameters
  • list_obj – list to sort

  • sorted_obj – list to use ordering from

Returns

sorted list

mlair.helpers.helpers.dict_to_xarray(d: Dict, coordinate_name: str) → xarray.DataArray

Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>.

Parameters
  • d – dictionary with 2D-xarrays

  • coordinate_name – name of the new created axis (2D -> 3D)

Returns

combined xarray

mlair.helpers.helpers.float_round(number: float, decimals: int = 0, round_type: Callable = math.ceil)float

Perform given rounding operation on number with the precision of decimals.

Parameters
  • number – the number to round

  • decimals – numbers of decimals of the rounding operations (default 0 -> round to next integer value)

  • round_type – the actual rounding operation. Can be any callable function like math.ceil, math.floor or python built-in round operation.

Returns

rounded number with desired precision

mlair.helpers.helpers.relative_round(x: float, sig: int, ceil=False, floor=False)float

Round small numbers according to given “significance”.

Example: relative_round(0.03112, 2) -> 0.031, relative_round(0.03112, 1) -> 0.03

Params x

number to round

Params sig

“significance” to determine number of decimals

Returns

rounded number

mlair.helpers.helpers.get_order(x: float)

Get order of number (as power of 10)

mlair.helpers.helpers.remove_items(obj: Union[List, Dict, Tuple], items: Any)

Remove item(s) from either list, tuple or dictionary.

Parameters
  • obj – object to remove items from (either dictionary or list)

  • items – elements to remove from obj. Can either be a list or single entry / key

Returns

object without items

mlair.helpers.helpers.select_from_dict(dict_obj: dict, sel_list: Any, remove_none: bool = False, filter_cond: bool = True)dict

Extract all key values pairs whose key is contained in the sel_list.

Does not perform a check if all elements of sel_list are keys of dict_obj. Therefore, the number of pairs in the returned dict is always smaller or equal to the number of elements in the sel_list. If filter_cond is given, this method either return the parts of the input dictionary that are included or not in sel_list.

mlair.helpers.helpers.extract_value(encapsulated_value)
mlair.helpers.helpers.is_xarray(arr)bool

Returns True if arr is xarray.DataArray or xarray.Dataset. :param arr: variable in question :type arr: Any :return: :rtype: bool

mlair.helpers.helpers.convert2xrda(arr: Union[xarray.DataArray, xarray.Dataset, numpy.ndarray, int, float], use_1d_default: bool = False, **kwargs) → Union[xarray.DataArray, xarray.Dataset]

Converts np.array, int or float object to xarray.DataArray.

If a xarray.DataArray or xarray.Dataset is passed, returns that unchanged. :param arr: :type arr: xr.DataArray, xr.Dataset, np.ndarray, int, float :param use_1d_default: :type use_1d_default: bool :param kwargs: Any additional kwargs which are accepted by xr.DataArray() :type kwargs: :return: :rtype: xr.DataArray, xr.DataSet

mlair.helpers.helpers.filter_dict_by_value(dictionary: dict, filter_val: Any, filter_cond: bool)dict

Filter dictionary by its values.

Parameters
  • dictionary – dict to filter

  • filter_val – search only for key value pair with a value equal to filter_val

  • filter_cond – indicate to use either all dict entries that fulfil the filter_val criteria (if True) or that do not match the criteria (if False)

Returns

a filtered dict with either matching or non-matching elements depending on the filter_cond

mlair.helpers.helpers.str2bool(v)
mlair.helpers.helpers.squeeze_coords(d)

Look for unused coords and remove them. Does only work for xarray DataArrays.