mlair.helpers

Collection of different supporting functions and classes.

Subpackages

Submodules

Package Contents

Classes

PyTestRegex

Assert that a given string meets some expectations.

TimeTracking

Track time to measure execution time.

TimeTrackingWrapper

Wrapper implementation of TimeTracking class.

Logger

Basic logger class to unify all logging outputs.

Functions

PyTestAllEqual(check_list: List)

check_nested_equality(obj1, obj2, precision=None, skip_args=None)

Check for equality in nested structures. Use precision to indicate number of decimals to check for consistency

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

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

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

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

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>.

to_list(obj: Any) → List

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

extract_value(encapsulated_value)

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.

make_keras_pickable()

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

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

Filter dictionary by its values.

class mlair.helpers.PyTestRegex(pattern: Union[str, Pattern], flags: int = 0)

Assert that a given string meets some expectations.

Use like

>>> PyTestRegex(r"TestString\d+") == "TestString"
False
>>> PyTestRegex(r"TestString\d+") == "TestString2"
True
Parameters
  • pattern – pattern or string to use for regular expresssion

  • flags – python re flags

__eq__(self, actual: str)bool

Return whether regex matches given string actual or not.

__repr__(self)str

Show regex pattern.

mlair.helpers.PyTestAllEqual(check_list: List)
mlair.helpers.check_nested_equality(obj1, obj2, precision=None, skip_args=None)

Check for equality in nested structures. Use precision to indicate number of decimals to check for consistency

class mlair.helpers.TimeTracking(start=True, name='undefined job', logging_level=logging.INFO, log_on_enter=False)

Bases: object

Track time to measure execution time.

Time tracking automatically starts on initialisation and ends by calling stop method. Duration can always be shown by printing the time tracking object or calling get_current_duration. It is possible to start and stop time tracking by hand like

time = TimeTracking(start=True)  # start=True is default and not required to set
do_something()
time.stop(get_duration=True)

A more comfortable way is to use TimeTracking in a with statement like:

with TimeTracking():
    do_something()

The only disadvantage of the latter implementation is, that the duration is logged but not returned.

_start(self)None

Start time tracking.

_end(self)None

Stop time tracking.

_duration(self)float

Get duration in seconds.

__repr__(self)str

Display current passed time.

run(self)None

Start time tracking.

stop(self, get_duration=False) → Optional[float]

Stop time tracking.

Will raise an error if time tracking was already stopped. :param get_duration: return passed time if enabled.

Returns

duration if enabled or None

duration(self)float

Return duration in seconds.

__enter__(self)

Context manager.

__exit__(self, exc_type, exc_val, exc_tb)None

Stop time tracking on exit and log info about passed time.

class mlair.helpers.TimeTrackingWrapper(func)

Wrapper implementation of TimeTracking class.

Use this implementation easily as decorator for functions, classes and class methods. Implement a custom function and decorate it for automatic time measure.

@TimeTrackingWrapper
def sleeper():
    print("start")
    time.sleep(1)
    print("end")

>>> sleeper()
start
end
INFO: foo finished after 00:00:01 (hh:mm:ss)
__call__(self, *args, **kwargs)

Start time tracking.

__get__(self, instance, cls)

Create bound method object and supply self argument to the decorated method.

class mlair.helpers.Logger(log_path=None, level_file=logging.DEBUG, level_stream=logging.INFO)

Basic logger class to unify all logging outputs.

Logs are saved in local file and returned to std output. In default settings, logging level of file logger is DEBUG, logging level of stream logger is INFO. Class must be imported and initialised in starting script, all subscripts should log with logging.info(), debug, …

static setup_logging_path(path: str = None)

Check if given path exists and creates if not.

If path is None, use path from main. The logging file is named like logging_<runtime>.log where runtime=`%Y-%m-%d_%H-%M-%S` of current run.

Parameters

path – path to logfile

Returns

path of logfile

logger_console(self, level: int)

Define a stream handler which writes messages of given level or higher to std out.

Parameters

level – logging level as integer, e.g. logging.DEBUG or 10

Returns

defines stream handler

mlair.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.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.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.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.extract_value(encapsulated_value)
mlair.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.make_keras_pickable()
mlair.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.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