evokit.watch package
Submodules
- evokit.watch.memory.PYMPLER_ASIZEOF_RECURSION_LIMIT: int = 5
Recursion limit of
pympler.asizeof.asizeof.
- class evokit.watch.memory.MemoryWatcherMetric[source]
Bases:
EnumMetrics that can be measured by
MemoryWatcher.- psutil_rss = 1
- psutil_vms = 2
- psutil_rss_plus_children = 3
- psutil_vms_plus_children = 4
- pympler_asizeof_algorithm = 5
- guppy3_domisize_algorithm = 6
- tracemalloc_total_current = 7
- tracemalloc_total_peak = 8
- tracemalloc_snapshot = 9
- class evokit.watch.memory.MemoryWatcher[source]
Bases:
Watcher[C,dict[MemoryWatcherMetric,int|Snapshot]]A highly evolved
Watcherwith the ability to explore entire memory footprints. Returns aMemory profiling is tricky. For example, the
tracemallocmodule is only able to track memory allocations made inside the Python iterator and therefore does not work well with PyTorch tensors, NumPy arrays, and Cython modules.To remedy this, this watcher supports several metrics (see
MemoryWatcher.Metric). Only those given toMemoryWatcher.__init__()will be measured and reported.Also note that, because this watcher is attached to an algorithm, it is also part of the algorithm’s memory footprint.
- supported_metrics: set[MemoryWatcherMetric] = {MemoryWatcherMetric.guppy3_domisize_algorithm, MemoryWatcherMetric.psutil_rss, MemoryWatcherMetric.psutil_rss_plus_children, MemoryWatcherMetric.psutil_vms, MemoryWatcherMetric.psutil_vms_plus_children, MemoryWatcherMetric.pympler_asizeof_algorithm, MemoryWatcherMetric.tracemalloc_snapshot, MemoryWatcherMetric.tracemalloc_total_current, MemoryWatcherMetric.tracemalloc_total_peak}
- metric_to_measure: dict[MemoryWatcherMetric, Callable[[Algorithm, MemoryWatcher], int | tuple[int, int] | Snapshot]] = {MemoryWatcherMetric.guppy3_domisize_algorithm: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.psutil_rss: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.psutil_rss_plus_children: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.psutil_vms: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.psutil_vms_plus_children: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.pympler_asizeof_algorithm: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.tracemalloc_snapshot: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.tracemalloc_total_current: <function MemoryWatcher.<lambda>>, MemoryWatcherMetric.tracemalloc_total_peak: <function MemoryWatcher.<lambda>>}
Map from each
MemoryWatcherMetricto a handler.
- __init__(events: Container[str], metrics: Iterable[MemoryWatcherMetric], stride: int = 1, watch_post_step: bool = False)[source]
- Parameters:
- Effect:
Cause
tracemallocto start tracing memory allocations. CallMemoryWatcher.close()to stop.
- metrics: Iterable[MemoryWatcherMetric]
Metrics that are measured and reported by this watcher.
- class evokit.watch.memory.AttributeMemoryWatcher[source]
Bases:
Watcher[C,dict[str,int]]A
MemoryWatcherthat inspects select attributes.The initialiser accepts a list of attribute names. For each name, that attribute of the
Watcher.subjectis measured withasizeof(from Pympler) andIdentitySet.domisize(from Guppy3).- __init__(events: Container[str], attributes: Iterable[str], stride: int = 1, watch_post_step: bool = False)[source]
- attributes
A collection of Guppy3
IsoSets. Initialised bysubscribe().
- class evokit.watch.visual.PrintableRecord[source]
Bases:
NamedTuplePrintableRecord(time, event, value)
- time: float
Alias for field number 0
- event: str
Alias for field number 1
- value: tuple[float, ...]
Alias for field number 2
- evokit.watch.visual.plot(records: Sequence[WatcherRecord[tuple[float, ...]]] | Sequence[WatcherRecord[float]], show_generation: bool = False, use_line: bool = False, show_legend: bool = True, axes: Axes | None = None, *args: Any, **kwargs: Any)[source]
Plot a sequence of
WatcherRecord`s. Plot :attr:`WatcherRecord.valueagainstWatcherRecord.time. Also set the X axis label.- Parameters:
records – Sequence of records. Each
WatcherRecord.valuemust only hold eitherfloator a 1-tuple of type tuple[float].show_generation – If
True, then also plot values collected at"STEP_BEGIN"and"POST_STEP"as bigger (s=50), special (marker="*") markers. Otherwise, plot them as any other values.use_line – If
True, then plot a line plot. Otherwise, plot a scatter graph.args – Passed to
matplotlib.plot().kwargs – Passed to
matplotlib.plot().
- Effects:
Plot to the current Matplotlib
Axes.
Note
The parameter
use_lineis provided for convenience. Since some values might benan, plotting and connecting only available data points could produce misleading plots.
- evokit.watch.visual.plot_dict(records: Sequence[WatcherRecord[dict[Any, float]]], keys: Collection[Any] | None = None, show_generation: bool = False, show_legend: bool = True, use_line: bool = False, axes: Axes | None = None, *args: Any, **kwargs: Any)[source]
- class evokit.watch.watcher.WatcherRecord[source]
Bases:
Generic[T]A record collected by an
Watcherfrom anAlgorithm. Also records the generation count and time of collection.- event: str
Event that triggered the handler.
- value: T
Data collected in
generationafterevent.
- __init__(event: str, generation: int, value: T, time: float) None
- class evokit.watch.watcher.Watcher[source]
Bases:
Generic[C,T],Sequence[WatcherRecord[T]]Observes and collect data from a running
Algorithm.The
Watchershould be registered to anAlgorithm, which then becomes the watcher’ssubject. When an event fires in the subject, if that event is in the watcher’sevents, then thehandlerwill be called with the subject as argument. Results are collected as a sequence ofWatcherRecords.Call
Algorithm.register()to register anWatcherto aAlgorithm. Callreport()to retrieve collected records.For type checking purposes, the
Watcherhas two type parameterCandT.Cis the type of the observedAlgorithm;Tis the type of .value in the reportedWatcherRecord.Tutorial: Collect and Plotting Statistics.
- MANUAL_EVENT: str = 'MANUAL_TRIGGER'
- __init__(events: Container[str], handler: Callable[[C], T], stride: int = 1, *, watch_post_step: bool = False, timer: Callable[[], float] = <built-in function process_time>)[source]
- Parameters:
events – Events that trigger the
handler.handler – Callable that takes the attached algorithm as input.
stride – Collection interval. Only
strideth event triggershandler.watch_post_step – If
True, also watch thePOST_STEPevent. This event fires automatically afterAlgorithm.step().
- subject: C | None
The attached
Algorithm.
- unsubscribe() None[source]
Unsubscribe this watcher from the
subject. Reset this watcher to be registered with another algorithm.
- update(event: str) None[source]
When the
subjectcallsAlgorithm.update(), the subject calls this method on every watcher registered to it.When an event matches a key in
handlers, call the corresponding value with the subject as argument. Store the result inrecords.To trigger collections, call
manual_update()instead of this method.- Raises:
RuntimeError – If no
Algorithmis attached.
- force_update(event: str = 'MANUAL_EVENT') None[source]
Manually trigger
update(), bypassing all checks of whether event is observed.- Parameters:
event – Name of an event. Defaults to
MANUAL_EVENT.- Raises:
RuntimeError – If no
Algorithmis attached.
- report() list[WatcherRecord[T]][source]
Report collected records.
- evokit.watch.watchers.create_fitness_watcher(events: list[str], stride: int = 1, *, watch_post_step: bool = False, timer: ~typing.Callable[[], float] = <built-in function process_time>) Watcher[HomogeneousAlgorithm[Individual[Any]], tuple[float, ...]][source]
Return an
Watcherthat collects the bestIndividual.fitnessof aHomogeneousAlgorithm.population.See
Watcher.__init__()for parameters.
- evokit.watch.watchers.create_size_watcher(events: list[str], stride: int = 1, *, watch_post_step: bool = False, timer: ~typing.Callable[[], float] = <built-in function process_time>) Watcher[HomogeneousAlgorithm[Individual[Any]], int][source]
Return an
Watcherthat collects the size of the population.See
Watcher.__init__()for parameters.
- evokit.watch.watchers.create_cpu_watcher(events: list[str], stride: int = 1, *, watch_post_step: bool = False, timer: ~typing.Callable[[], float] = <built-in function process_time>) Watcher[HomogeneousAlgorithm[Individual[Any]], float][source]
Return an
Watcherthat collects the CPU time of the process.See
Watcher.__init__()for parameters.
- evokit.watch.watchers.create_rss_watcher(events: list[str], stride: int = 1, *, watch_post_step: bool = False, timer: ~typing.Callable[[], float] = <built-in function process_time>) Watcher[HomogeneousAlgorithm[Individual[Any]], float][source]
Return an
Watcherthat collects the memory usage (RSS) of an algorithm.See
Watcher.__init__()for parameters.