Skip to content

Commit ee8aa83

Browse files
committed
ft-analyzer add docstrings and type hints
1 parent fbca6f8 commit ee8aa83

10 files changed

Lines changed: 460 additions & 347 deletions

File tree

tools/ft-analyzer/ftanalyzer/counter/continuous_counter.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ def __init__(
1212
has_negatives: bool = False,
1313
measure_start_time: np.uint64 = None,
1414
measure_end_time: np.uint64 = None,
15-
):
16-
"""Constructor
17-
15+
) -> None:
16+
"""
17+
Initialize a continuous-time counter.
1818
Args:
19-
variable (str): _description_
20-
start_time (np.uint64, optional): _description_. Defaults to np.uint64(0).
19+
variable: Name of the observed variable.
20+
sim: Simulation state object.
21+
factor: Scaling factor for values.
22+
has_negatives: If True, allow negative values for min.
23+
measure_start_time: Start time for measurement window.
24+
measure_end_time: End time for measurement window.
2125
"""
2226
super().__init__(
2327
variable, "counter type: continuous-time counter", has_negatives
@@ -35,13 +39,23 @@ def __init__(
3539
self._sim = sim
3640

3741
def get_mean(self) -> np.float64:
42+
"""
43+
Returns the mean value over the measurement interval.
44+
Returns:
45+
Mean value as np.float64.
46+
"""
3847
interval = self.last_sample_time - self.first_sample_time
3948
if interval > 0:
4049
return np.float64(self.get_sum_power_one()) / np.float64(interval)
4150
else:
4251
return np.float64(0)
4352

4453
def get_variance(self) -> np.float64:
54+
"""
55+
Returns the variance over the measurement interval.
56+
Returns:
57+
Variance as np.float64.
58+
"""
4559
interval = self.last_sample_time - self.first_sample_time
4660
if interval > 0:
4761
mean = self.get_mean()
@@ -54,6 +68,11 @@ def get_variance(self) -> np.float64:
5468
return np.float64(0)
5569

5670
def count(self, x: np.float64) -> None:
71+
"""
72+
Count a new sample, updating statistics with time-weighted increments.
73+
Args:
74+
x: Value to count.
75+
"""
5776
if (
5877
self._sim.get_time() < self._measure_start_time
5978
or self._sim.get_time() > self._measure_end_time
@@ -78,6 +97,9 @@ def count(self, x: np.float64) -> None:
7897
self.last_sample_time = current_time
7998

8099
def reset(self) -> None:
100+
"""
101+
Reset all statistics and measurement window.
102+
"""
81103
super().reset()
82104
self.first_sample_time = self._sim.get_time()
83105
self.last_sample_time = self._sim.get_time()

tools/ft-analyzer/ftanalyzer/counter/counter.py

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88

99
class 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

tools/ft-analyzer/ftanalyzer/counter/discrete_counter.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44

55
class DiscreteCounter(Counter):
66
"""
7-
Implements a discrete time counter, which updates statistics per discrete
8-
observation.
7+
Implements a discrete time counter, updating statistics per observation.
98
"""
109

1110
def __init__(
1211
self, variable: str, counter_type: str = "counter type: discrete-time counter"
13-
):
12+
) -> None:
1413
"""
15-
Constructor for DiscreteCounter.
16-
14+
Initialize a discrete time counter.
1715
Args:
18-
variable (str): Name of the observed variable
19-
counter_type (str, optional): Counter type label. Defaults to discrete-time counter.
16+
variable: Name of the observed variable.
17+
counter_type: Counter type label.
2018
"""
2119
super().__init__(variable, counter_type)
2220

2321
def get_mean(self) -> np.float64:
2422
"""
25-
Computes the mean of the observed variable over samples.
26-
23+
Compute the mean of the observed variable over samples.
2724
Returns:
28-
np.float64: the mean value
25+
Mean value as np.float64.
2926
"""
3027
if self.get_num_samples() > 0:
3128
return np.float64(self.get_sum_power_one()) / np.float64(
@@ -36,10 +33,9 @@ def get_mean(self) -> np.float64:
3633

3734
def get_variance(self) -> np.float64:
3835
"""
39-
Computes the sample variance of the observed variable.
40-
36+
Compute the sample variance of the observed variable.
4137
Returns:
42-
np.float64: the variance
38+
Variance as np.float64.
4339
"""
4440
n = self.get_num_samples()
4541
if n > 1:
@@ -52,10 +48,9 @@ def get_variance(self) -> np.float64:
5248

5349
def count(self, x: np.float64) -> None:
5450
"""
55-
Counts a new observation and updates first/second moments.
56-
51+
Count a new observation and update first/second moments.
5752
Args:
58-
x (np.float64): the observation
53+
x: The observation value.
5954
"""
6055
super().count(x)
6156
self.increase_sum_power_one(x)

0 commit comments

Comments
 (0)