-
Notifications
You must be signed in to change notification settings - Fork 11
/
logging_metrics.py
51 lines (43 loc) · 1.65 KB
/
logging_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2022 Twitter, Inc.
# SPDX-License-Identifier: Apache-2.0
class AverageMetrics():
'''Object keeping running average of relevant metrics to log. '''
def __init__(self, *args):
self.metrics = {arg: 0 for arg in args}
self.latest_metrics = {arg: 0 for arg in args}
self.samples = {arg: 1e-8 for arg in args}
self.logged_metrics = [arg for arg in args]
def reset(self, ):
for arg in self.metrics:
self.metrics[arg] = 0
self.samples[arg] = 1e-8
def add(self, *args):
for arg in args:
if arg not in self.metrics:
self.logged_metrics.append(arg)
self.metrics[arg] = 0
self.latest_metrics[arg] = 0
self.samples[arg] = 1e-8
def update(self, **kwargs):
for arg, val in kwargs.items():
if arg not in self.metrics:
self.logged_metrics += arg
self.metrics[arg] = 0
self.latest_metrics[arg] = 0
self.samples[arg] = 1e-8
self.metrics[arg] += val
self.samples[arg] += 1
def set(self, **kwargs):
for arg, val in kwargs.items():
if arg not in self.metrics:
self.logged_metrics += arg
self.metrics[arg] = val
self.samples[arg] = 1
self.metrics[arg] = val
self.samples[arg] = 1
def get(self, ):
for arg, metric_agg in self.metrics.items():
samples = self.samples[arg]
if samples >= 1:
self.latest_metrics[arg] = metric_agg/samples
return self.latest_metrics