Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
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
B
©r_(    ã@s*ddlZddlmZmZGdd„dƒZdS)éN)ÚListÚTuplec@sfeZdZdZdedœdd„Zdeedœdd    „Zd
d „Zed œd d„Z    ed œdd„Z
dd„Z dd„Z dS)Ú HistoryBufferz†
    Track a series of scalar values and provide access to smoothed values over a
    window or the global average of the series.
    é@B)Ú
max_lengthcCs||_g|_d|_d|_dS)zÔ
        Args:
            max_length: maximal number of values that can be stored in the
                buffer. When the capacity of the buffer is exhausted, old
                values will be removed.
        rN)Ú _max_lengthÚ_dataÚ_countÚ _global_avg)Úselfr©r ú./utils/history_buffer.pyÚ__init__szHistoryBuffer.__init__N)ÚvalueÚ    iterationcCsf|dkr|j}t|jƒ|jkr*|j d¡|j ||f¡|jd7_|j||j|j7_dS)zÁ
        Add a new scalar value produced at certain iteration. If the length
        of the buffer exceeds self._max_length, the oldest element will be
        removed from the buffer.
        Nré)r    ÚlenrrÚpopÚappendr
)r rrr r r Úupdates zHistoryBuffer.updatecCs|jddS)zE
        Return the latest scalar value added to the buffer.
        éÿÿÿÿr)r)r r r r Úlatest)szHistoryBuffer.latest)Ú window_sizecCs t dd„|j| d…Dƒ¡S)zU
        Return the median of the latest `window_size` values in the buffer.
        cSsg|] }|d‘qS)rr )Ú.0Úxr r r ú
<listcomp>3sz(HistoryBuffer.median.<locals>.<listcomp>N)ÚnpÚmedianr)r rr r r r/szHistoryBuffer.mediancCs t dd„|j| d…Dƒ¡S)zS
        Return the mean of the latest `window_size` values in the buffer.
        cSsg|] }|d‘qS)rr )rrr r r r9sz%HistoryBuffer.avg.<locals>.<listcomp>N)rÚmeanr)r rr r r Úavg5szHistoryBuffer.avgcCs|jS)z™
        Return the mean of all the elements in the buffer. Note that this
        includes those getting removed due to limited buffer storage.
        )r
)r r r r Ú
global_avg;szHistoryBuffer.global_avgcCs|jS)z`
        Returns:
            list[(number, iteration)]: content of the current buffer.
        )r)r r r r ÚvaluesBszHistoryBuffer.values)r)N) Ú__name__Ú
__module__Ú __qualname__Ú__doc__ÚintrÚfloatrrrrr r!r r r r rs r)ÚnumpyrÚtypingrrrr r r r Ú<module>s