|
| 1 | +import warnings |
1 | 2 | from typing import List, Iterator, Optional |
2 | 3 | import itertools |
3 | 4 | import numpy as np |
4 | 5 | from scipy.signal import lfilter |
5 | 6 |
|
6 | | - |
7 | 7 | def signed_power(base: float, degree: float) -> float: |
8 | 8 | """ |
9 | 9 | Calculates the degree of a number while preserving the sign. |
@@ -87,7 +87,7 @@ class LBFBmGenerator: |
87 | 87 | Raises: |
88 | 88 | ValueError: If Hurst exponent is not in a range (0, 2) |
89 | 89 | ValueError: If filter length is not positive. |
90 | | - StopIteration: If maximum sequence length has been reached. |
| 90 | + StopIteration('Sequence exhausted') : If maximum sequence length has been reached. |
91 | 91 |
|
92 | 92 | Example usage: |
93 | 93 | >>> generator = LBFBmGenerator(h, filter_len, base) |
@@ -123,7 +123,7 @@ def __iter__(self): |
123 | 123 |
|
124 | 124 | def _init_bins(self): |
125 | 125 | """Initializes the structure of bins and their boundaries.""" |
126 | | - self.bin_sizes = [1] + [int(self.base**n) for n in range(self.filter_len - 1)] |
| 126 | + self.bin_sizes: List[int] = [1] + [int(self.base**n) for n in range(self.filter_len - 1)] |
127 | 127 | self.bins = [0.0] * self.filter_len |
128 | 128 | self.bin_limits = list(itertools.accumulate(self.bin_sizes)) |
129 | 129 | self.max_steps = sum(self.bin_sizes) |
@@ -175,9 +175,11 @@ def _calculate_step(self) -> float: |
175 | 175 | def __next__(self): |
176 | 176 | """Generates the next signal value.""" |
177 | 177 | if self.length is not None and self.current_time >= self.length: |
178 | | - raise StopIteration |
| 178 | + raise StopIteration('Sequence exhausted') |
179 | 179 |
|
180 | 180 | self.current_time += 1 |
| 181 | + if self.current_time >= self.max_steps: |
| 182 | + warnings.warn(f"Sequence length {self.current_time} exceeded the maximum allowed length {self.max_steps}", RuntimeWarning) |
181 | 183 | new_val = next(self.random_generator) |
182 | 184 | self._update_bins(new_val) |
183 | 185 | return self._calculate_step() |
|
0 commit comments