mlair.helpers
¶
Collection of different supporting functions and classes.
Subpackages¶
Submodules¶
Package Contents¶
Classes¶
Assert that a given string meets some expectations. |
|
Track time to measure execution time. |
|
Wrapper implementation of TimeTracking class. |
|
Basic logger class to unify all logging outputs. |
Functions¶
|
|
|
Check for equality in nested structures. Use precision to indicate number of decimals to check for consistency |
|
Remove item(s) from either list, tuple or dictionary. |
|
Perform given rounding operation on number with the precision of decimals. |
|
Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>. |
|
Transform given object to list if obj is not already a list. Sets are also transformed to a list. |
|
|
|
Extract all key values pairs whose key is contained in the sel_list. |
|
Sort elements of list_obj as ordered in sorted_obj. Length of sorted_obj as allowed to be higher than length of |
|
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
-
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.
-
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
-
__enter__
(self)¶ Context manager.
-
-
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, …
-
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