77
88
99class Counter (StatisticObject , ABC ):
10- """Basic counter that counts: * sum power two * sum power one * minimum * maximum"""
10+ """
11+ Basic counter that tracks sum, sum of squares, minimum, maximum, and sample count for a variable.
12+ """
1113
1214 _sum_power_one : np .float64
1315 """Sum of values counted by this counter
@@ -32,12 +34,13 @@ def __init__(
3234 variable : str ,
3335 type : str = "counter type: base counter" ,
3436 has_negatives : bool = False ,
35- ):
36- """Constructor
37-
37+ ) -> None :
38+ """
39+ Initialize a counter for a given variable.
3840 Args:
39- variable (str): the observed variable
40- type (_type_, optional): the type of counter. Defaults to "counter type: base counter".
41+ variable: Name of the observed variable.
42+ type: Description of the counter type.
43+ has_negatives: If True, allow negative values for min.
4144 """
4245 self .__counter_type = type
4346 self ._observed_variable = variable
@@ -50,102 +53,102 @@ def __init__(
5053
5154 @abstractmethod
5255 def get_mean (self ) -> np .float64 :
53- """Returns the mean of the observed variable
54-
56+ """
57+ Returns the mean of the observed variable.
5558 Returns:
56- np.float64: the mean
59+ Mean value as np.float64.
5760 """
5861 pass
5962
6063 @abstractmethod
6164 def get_variance (self ) -> np .float64 :
62- """Returns the variance of the observed variable
63-
65+ """
66+ Returns the variance of the observed variable.
6467 Returns:
65- np.float64: the variance
68+ Variance as np.float64.
6669 """
6770 pass
6871
6972 def get_std_deviation (self ) -> np .float64 :
70- """Returns the standard deviation of the observed variable
71-
73+ """
74+ Returns the standard deviation of the observed variable.
7275 Returns:
73- np.float64: the standard deviation
76+ Standard deviation as np.float64.
7477 """
7578 return math .sqrt (max (self .get_variance (), 0 ))
7679
7780 def get_cvar (self ) -> np .float64 :
78- """Returns the co-variance of the observed variable
79-
81+ """
82+ Returns the coefficient of variation of the observed variable.
8083 Returns:
81- np.float64: the co-variance
84+ Coefficient of variation as np.float64.
8285 """
8386 if self .get_mean () == 0 :
8487 return 0 if self .get_std_deviation () == 0 else np .finfo (np .float64 ).max
8588 else :
8689 return self .get_std_deviation () / self .get_mean ()
8790
8891 def get_min (self ) -> np .float64 :
89- """Returns the minimum of the observed variable
90-
92+ """
93+ Returns the minimum value observed.
9194 Returns:
92- np.float64: the minimum
95+ Minimum value as np.float64.
9396 """
9497 return self .__min
9598
9699 def get_max (self ) -> np .float64 :
97- """Returns the maximum of the observed variable
98-
100+ """
101+ Returns the maximum value observed.
99102 Returns:
100- np.float64: the maximum
103+ Maximum value as np.float64.
101104 """
102105 return self .__max
103106
104107 def get_num_samples (self ) -> np .uint64 :
105- """Returns the number of counted samples
106-
108+ """
109+ Returns the number of counted samples.
107110 Returns:
108- np.uint64: the number of samples
111+ Number of samples as np.uint64.
109112 """
110113 return self .__num_samples
111114
112115 def get_sum_power_one (self ) -> np .float64 :
113- """Returns the sum of all counted samples
114-
116+ """
117+ Returns the sum of all counted samples.
115118 Returns:
116- np.float64: the sum of all samples
119+ Sum as np.float64.
117120 """
118121 return self ._sum_power_one
119122
120- def increase_sum_power_one (self , value : np .float64 ):
121- """Adds the given value to the sum of counted samples
122-
123+ def increase_sum_power_one (self , value : np .float64 ) -> None :
124+ """
125+ Add the given value to the sum of counted samples.
123126 Args:
124- value (np.float64): the value to add
127+ value: Value to add.
125128 """
126129 self ._sum_power_one += value
127130
128131 def get_sum_power_two (self ) -> np .float64 :
129- """Returns the sum of all counted samples power two
130-
132+ """
133+ Returns the sum of all counted samples squared.
131134 Returns:
132- np.float64: the sum of all samples power two
135+ Sum of squares as np.float64.
133136 """
134137 return self ._sum_power_two
135138
136- def increase_sum_power_two (self , value : np .float64 ):
137- """Adds the given value to the sum of counted samples
138-
139+ def increase_sum_power_two (self , value : np .float64 ) -> None :
140+ """
141+ Add the given value to the sum of counted samples squared.
139142 Args:
140- value (np.float64): the value to add
143+ value: Value to add.
141144 """
142145 self ._sum_power_two += value
143146
144- def count (self , x : np .float64 ):
145- """Counts a new sample (set min/max and increment sample counter)
146-
147+ def count (self , x : np .float64 ) -> None :
148+ """
149+ Count a new sample (set min/max and increment sample counter).
147150 Args:
148- x (np.float64): the value to count
151+ x: Value to count.
149152 """
150153 self .__min = min (self .__min , x )
151154 if not self ._has_negatives :
@@ -154,10 +157,10 @@ def count(self, x: np.float64):
154157 self .__num_samples += 1
155158
156159 def report (self ) -> str :
157- """Outputs the report of this counter to the command line
158-
160+ """
161+ Output a string report of this counter.
159162 Returns:
160- str: output as string
163+ Report as string.
161164 """
162165 out : str = ""
163166 if self ._observed_variable :
@@ -176,19 +179,28 @@ def report(self) -> str:
176179
177180 return out
178181
179- def csv_report (self , output_dir : PathLike , is_ref : bool = False ):
180- """Write Counter details to csv-file
181-
182+ def csv_report (self , output_dir : PathLike , is_ref : bool = False ) -> None :
183+ """
184+ Write counter details to a CSV file.
182185 Args:
183- output_dir (PathLike): _description_
186+ output_dir: Output directory for CSV file.
187+ is_ref: If True, write to expected-counters folder.
184188 """
185189 content : str = f"{ self ._observed_variable } ;{ self .__num_samples } ;{ self .get_mean ()} ;{ self .get_variance ()} ;{ self .get_std_deviation ()} ;{ self .get_cvar ()} ;{ self .get_min ()} ;{ self .get_max ()} \n "
186190 labels : str = "#counter ; numSamples ; MEAN; VAR; STD; CVAR; MIN; MAX\n "
187191 self ._write_csv (output_dir , content , labels , is_ref )
188192
189193 def _write_csv (
190194 self , output_dir : PathLike , content : str , labels : str , is_ref : bool = False
191- ):
195+ ) -> None :
196+ """
197+ Helper to write CSV content to file.
198+ Args:
199+ output_dir: Output directory.
200+ content: CSV content string.
201+ labels: CSV header string.
202+ is_ref: If True, write to expected-counters folder.
203+ """
192204 try :
193205 dest = os .path .join (
194206 output_dir , "expected-counters" if is_ref else "counters"
@@ -206,7 +218,10 @@ def _write_csv(
206218 except IOError as e :
207219 print (f"IOError while writing CSV: { e } " )
208220
209- def reset (self ):
221+ def reset (self ) -> None :
222+ """
223+ Reset all statistics to initial state.
224+ """
210225 self ._sum_power_one = 0
211226 self ._sum_power_two = 0
212227 self .__min = np .inf
0 commit comments