mlens.utils.utils module

ML-ENSEMBLE

author:Sebastian Flennerhag
copyright:2017
licence:MIT
class mlens.utils.utils.CMLog(verbose=False)[source]

Bases: object

CPU and Memory logger.

Class for starting a monitor job of CPU and memory utilization in the background in a Python script. The monitor class records the cpu_percent, rss and vms as collected by the psutil library for the parent process’ pid.

CPU usage and memory utilization are stored as attributes in numpy arrays.

Examples

>>> from time import sleep
>>> from mlens.utils.utils import CMLog
>>> cm = CMLog(verbose=True)
>>> cm.monitor(2, 0.5)
>>> _ = [i for i in range(10000000)]
>>>
>>> # Collecting before completion triggers a message but no error
>>> cm._collect()
>>>
>>> sleep(2)
>>> cm._collect()
>>> print('CPU usage:')
>>> cm.cpu
[CMLog] Monitoring for 2 seconds with checks every 0.5 seconds.
[CMLog] Job not finished. Cannot _collect yet.
[CMLog] Collecting... done. Read 4 lines in 0.000 seconds.
CPU usage:
array([ 50. ,  22.4,   6. ,  11.9])
Raises:ImportError : – Depends on psutil. If not installed, raises ImportError on instantiation.
Parameters:verbose (bool) – whether to notify of job start.
collect()[source]

Collect monitored data.

Once a monitor job finishes, call _collect to read the CPU and memory usage into python objects in the current process. If called before the job finishes, _collect issues a print statement to try again later, but no warning or error is raised.

monitor(stop=None, ival=0.1, kill=True)[source]

Start monitoring CPU and memory usage.

Parameters:
  • stop (float or None (default = None)) – seconds to monitor for. If None, monitors until _collect is called.
  • ival (float (default=0.1)) – interval of monitoring.
  • kill (bool (default = True)) – whether to kill the monitoring job if _collect is called before timeout (stop). If set to False, calling _collect will cause the instance to wait until the job completes.
mlens.utils.utils.kwarg_parser(func, kwargs)[source]

Utility function for parsing keyword arguments

mlens.utils.utils.pickle_load(name)[source]

Utility function for loading pickled object

mlens.utils.utils.pickle_save(obj, name)[source]

Utility function for pickling an object

mlens.utils.utils.print_time(t0, message='', **kwargs)[source]

Utility function for printing time

mlens.utils.utils.safe_print(*objects, **kwargs)[source]

Safe print function for backwards compatibility.