n-2:slidewindow类
::就是一个记录滑动窗口属性的类,然后定义了对属性进行保护和查看的函数。
n-1:slidewindowfeature类
其需要的参数包括:::slidewindow(其实就是一个标记类),data::和slidewindow这个窗口相对应,代表对应窗口的数据,最后的labels是对data各个维度的文本描述
n-0.5::class AggregationStrategy
###抽象类。代表了聚合重叠buffer使用的策略
其定义了三个子类,算是一种继承和泛化
n.类aggregate的创建和使用
定义了latency和step
self.num_overlapping_windows属性提供了buffers的长度,目的是可以利用重叠信息。
具体由__call__()可以看出,
start = buffers[-1].extent.end - self.latency
region = Segment(start, start + self.step)
return self._prepend(self.aggregate(buffers, region), region, buffers)
对于一个duration,aggregate类进行聚集策略后,只保留了从当前的duration末端的和实时仅相差一个lantency的部分,并且输出的output_window的长度为step。
def __init__(
self,
step: float,
latency: Optional[float] = None,
strategy: Literal["mean", "hamming", "first"] = "hamming",
cropping_mode: Literal["strict", "loose", "center"] = "loose",
):
self.step = step
self.latency = latency
self.strategy = strategy
assert cropping_mode in [
"strict",
"loose",
"center",
], f"Invalid cropping mode `{cropping_mode}`"
self.cropping_mode = cropping_mode
if self.latency is None:
self.latency = self.step
assert self.step <= self.latency, "Invalid latency requested"
self.num_overlapping_windows = int(round(self.latency / self.step))
self.aggregate = AggregationStrategy.build(self.strategy, self.cropping_mode)
def _prepend(
self,
output_window: SlidingWindowFeature,
output_region: Segment,
buffers: List[SlidingWindowFeature],
):
# FIXME instead of prepending the output of the first chunk,
# add padding of `chunk_duration - latency` seconds at the
# beginning of the stream so scores can be aggregated accordingly.
# Remember to shift predictions by the padding.
last_buffer = buffers[-1].extent
# Prepend prediction until we match the latency in case of first buffer
if len(buffers) == 1 and last_buffer.start == 0:
num_frames = output_window.data.shape[0]
first_region = Segment(0, output_region.end)
first_output = buffers[0].crop(
first_region, mode=self.cropping_mode, fixed=first_region.duration
)
first_output[-num_frames:] = output_window.data
resolution = output_region.end / first_output.shape[0]
output_window = SlidingWindowFeature(
first_output,
SlidingWindow(start=0, duration=resolution, step=resolution),
)
return output_window
def __call__(self, buffers: List[SlidingWindowFeature]) -> SlidingWindowFeature:
# Determine overlapping region to aggregate
start = buffers[-1].extent.end - self.latency
region = Segment(start, start + self.step)
return self._prepend(self.aggregate(buffers, region), region, buffers)