history
Former-commit-id: c078eab42975bcbc5fd98aae28228937b5694d19
This commit is contained in:
100
utils.py
100
utils.py
@ -2,6 +2,106 @@ import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Tuple, Optional
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
@dataclass
|
||||
class HistoryRecord:
|
||||
"""历史记录的单个条目"""
|
||||
timestamp: float
|
||||
state: str # 'transport', 'middle', 'about', 'colored'
|
||||
rate: float
|
||||
volume: float
|
||||
image: np.ndarray
|
||||
|
||||
|
||||
class History:
|
||||
"""滑动窗口历史记录管理类"""
|
||||
|
||||
def __init__(self, max_window_size: float = 5.0, base_time = 5.0):
|
||||
"""
|
||||
初始化历史记录管理器
|
||||
|
||||
Args:
|
||||
max_window_size: 最大窗口时间长度(秒)
|
||||
"""
|
||||
if base_time > max_window_size:
|
||||
max_window_size = base_time+0.2
|
||||
# raise ValueError("Base time must be less than or equal to max window size.")
|
||||
self.records: List[HistoryRecord] = []
|
||||
self.max_window_size = max_window_size
|
||||
self.fulled = False
|
||||
self.base = None
|
||||
self._base_time = base_time
|
||||
|
||||
def add_record(self, timestamp: float, state: str, rate: float, volume: float, image: np.ndarray):
|
||||
"""添加新的历史记录"""
|
||||
record = HistoryRecord(timestamp, state,rate, volume, image)
|
||||
self.records.append(record)
|
||||
self._cleanup_old_records(timestamp)
|
||||
if self.base is None and (timestamp-self._base_time)>= self.records[0].timestamp:
|
||||
base_records = self.get_recent_records(self._base_time, timestamp)
|
||||
self.base = sum([rec.rate for rec in base_records]) / len(base_records)
|
||||
get_endpoint_logger().info("Base rate calculated: %.2f", self.base)
|
||||
|
||||
def _cleanup_old_records(self, current_time: float):
|
||||
"""清理过期的历史记录"""
|
||||
cutoff_time = current_time - self.max_window_size
|
||||
if not self.records and self.records[0].timestamp < cutoff_time:
|
||||
return False
|
||||
while self.records and self.records[0].timestamp < cutoff_time:
|
||||
self.records.pop(0)
|
||||
return True
|
||||
|
||||
def get_records_in_timespan(self, start_time: float, end_time: Optional[float] = None) -> List[HistoryRecord]:
|
||||
"""获取指定时间段内的记录"""
|
||||
if end_time is None:
|
||||
return [record for record in self.records if record.timestamp >= start_time]
|
||||
else:
|
||||
return [record for record in self.records
|
||||
if start_time <= record.timestamp <= end_time]
|
||||
|
||||
def get_recent_records(self, duration: float, current_time: float) -> List[HistoryRecord]:
|
||||
"""获取最近指定时间长度内的记录"""
|
||||
start_time = current_time - duration
|
||||
return self.get_records_in_timespan(start_time, current_time)
|
||||
|
||||
def get_state_ratio(self, target_state: str, records: Optional[List[HistoryRecord]] = None) -> float:
|
||||
"""计算指定状态在记录中的比例"""
|
||||
if records is None:
|
||||
records = self.records
|
||||
|
||||
if not records:
|
||||
return 0.0
|
||||
|
||||
target_count = sum(1 for record in records if record.state == target_state)
|
||||
return target_count / len(records)
|
||||
|
||||
def get_states_by_type(self, target_state: str) -> List[float]:
|
||||
"""获取所有指定状态的时间戳"""
|
||||
return [record.timestamp for record in self.records if record.state == target_state]
|
||||
|
||||
def find_record_by_timestamp(self, target_timestamp: float) -> Optional[HistoryRecord]:
|
||||
"""根据时间戳查找记录"""
|
||||
for record in self.records:
|
||||
if record.timestamp == target_timestamp:
|
||||
return record
|
||||
return None
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""检查历史记录是否为空"""
|
||||
return len(self.records) == 0
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""返回历史记录的数量"""
|
||||
return len(self.records)
|
||||
|
||||
def clear(self):
|
||||
"""清空所有历史记录"""
|
||||
self.records.clear()
|
||||
|
||||
|
||||
class State:
|
||||
"""滴定状态管理类"""
|
||||
|
Reference in New Issue
Block a user