[docs]classIncrementalAverage:def__init__(self)->None:self._current_mean:AverageableType=Noneself._current_size:int=0@propertydefcurrent_mean(self)->Optional[AverageableType]:"""Get the current mean."""returnself._current_mean@propertydefcurrent_size(self)->int:"""Get the number of objects that have been averaged."""returnself._current_size
[docs]defadd(self,addition:AverageableType,)->AverageableType:"""Add a new value to the average."""# Store initial if nothing yetifself.current_meanisNone:self._current_mean=additionself._current_size+=1returnself.current_mean# Update if actual additionself._current_mean=update_average(self.current_mean,self.current_size,addition,)self._current_size+=1returnself.current_mean
def__str__(self)->str:"""Print details."""returnf"<IncrementalAverage [current_size: {self.current_size}]>"def__repr__(self)->str:"""Get the object representation."""returnstr(self)
[docs]classIncrementalStats:def__init__(self)->None:self._current_mean:Optional[AverageableType]=Noneself._current_size:int=0self._current_max:Optional[AverageableType]=Noneself._current_min:Optional[AverageableType]=None@propertydefcurrent_mean(self)->Optional[AverageableType]:"""Get the current mean."""returnself._current_mean@propertydefcurrent_size(self)->int:"""Get the number of objects that have been managed."""returnself._current_size@propertydefcurrent_max(self)->Optional[AverageableType]:"""Get the current max."""returnself._current_max@propertydefcurrent_min(self)->Optional[AverageableType]:"""Get the current min."""returnself._current_min
[docs]defadd(self,addition:AverageableType,)->"IncrementalStats":"""Add a new value to mean (and check for new max and min)."""# Store initial mean if not set or updateifself.current_meanisNone:self._current_mean=additionelse:self._current_mean=update_average(self.current_mean,self.current_size,addition,)# Update size regardlessself._current_size+=1# Store initial max if not set or updateifself.current_maxisNone:self._current_max=additionelse:self._current_max=np.maximum(self.current_max,addition)# Store initial min if not set or updateifself.current_minisNone:self._current_min=additionelse:self._current_min=np.minimum(self.current_min,addition)returnself
def__str__(self)->str:"""Print details."""returnf"<IncrementalStats [current_size: {self.current_size}]>"def__repr__(self)->str:"""Get the object representation."""returnstr(self)