Skip to content

fly_ruler_std.runtime.record ​

本页由 fr-docs-dev gen-api 从源码 docstring 生成,请勿手工编辑;重新生成执行 just api。

记录子包:同步与异步记录器、数据加载器及列布局工具。

公开成员 ​

名称类型定义位置
Recorder类本页
AsyncRecorder类本页
Loader类本页
RecordSample类本页

定义 ​

Recorder ​

python
class Recorder(model: RuntimeInstance[ArrayT] | BatchModel[ArrayT], output_dir: Path | str = './results')

把仿真样本规范化为扁平数组并写出 parquet 与元数据的记录器。

__init__ ​

重载

调用形式返回类型
__init__(model: BatchModel[ArrayT], output_dir: Path | str = './results')None
__init__(model: RuntimeInstance[ArrayT], output_dir: Path | str = './results')None

构造记录器并准备输出目录。

参数

名称类型默认值说明
modelBatchModel[ArrayT] | RuntimeInstance[ArrayT]—提供单实例变量布局的标量或批量已编译模型。
output_dirPath | str'./results'parquet 与元数据的输出目录,默认 ./results。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:54

model ​

python
model = model

用于规范化取值与生成列名的已编译模型。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:65

output_dir ​

python
output_dir = Path(output_dir)

parquet 与元数据的输出目录,构造时创建。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:67

record ​

python
def record(step: int, time: float, x: RecordValue = None, xd: RecordValue = None, y: RecordValue = None, u: RecordValue = None, p: RecordValue = None, effect: RecordValue = None, batch_idx: int = 0) -> None

记录一个仿真样本。

参数

名称类型默认值说明
stepint—仿真步数索引。
timefloat—仿真时间,单位秒。
xRecordValueNone连续状态,支持数组、序列或路径映射;None 表示不记录。
xdRecordValueNone离散状态,取值形式同 x。
yRecordValueNone输出,取值形式同 x。
uRecordValueNone输入,取值形式同 x。
pRecordValueNone参数,取值形式同 x。
effectRecordValueNone效果或探测量,取值形式同 x。
batch_idxint0批次索引,默认 0。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:88

record_batch ​

python
def record_batch(times: ArrayLike, x: ArrayLike | None = None, xd: ArrayLike | None = None, y: ArrayLike | None = None, u: ArrayLike | None = None, p: ArrayLike | None = None, effect: ArrayLike | None = None) -> None

记录成批仿真数据。

times 形状为 (steps,) 或 (batch, steps);其余数组形状为 (batch, steps, dim),而 p 也可为 (batch, dim) 并在各步重复。

参数

名称类型默认值说明
timesArrayLike—时间序列,形状 (steps,) 或 (batch, steps)。
xArrayLike | NoneNone连续状态,形状 (batch, steps, dim)。
xdArrayLike | NoneNone离散状态,形状 (batch, steps, dim)。
yArrayLike | NoneNone输出,形状 (batch, steps, dim)。
uArrayLike | NoneNone输入,形状 (batch, steps, dim)。
pArrayLike | NoneNone参数,形状 (batch, steps, dim) 或 (batch, dim)。
effectArrayLike | NoneNone效果或探测量,形状 (batch, steps, dim)。

异常

类型说明
ValueError数据批大小与绑定的批量模型视图不一致时抛出。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:124

clear ​

python
def clear() -> None

清空已记录样本与活跃变量类别。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:181

save ​

python
def save(filename: str = 'simulation_data') -> None

把已记录数据写为 parquet 与 JSON 元数据。

参数

名称类型默认值说明
filenamestr'simulation_data'输出文件名(不含扩展名),默认 simulation_data。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:186

get_dataframe ​

python
def get_dataframe(batch_id: int | None = None) -> pl.DataFrame

返回当前记录的列式快照,可显式筛选batch,无需保存文件。

参数

名称类型默认值说明
batch_idint | NoneNone可选轨迹编号,None保留全部轨迹。

返回

类型说明
pl.DataFrame与保存Parquet相同布局的Polars数据表。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:218

samples ​

python
samples: tuple[RecordSample, ...]

已记录样本的不可变快照。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:235

metadata ​

python
metadata: JsonObject

元数据字典的浅拷贝,含时间戳与编译模型信息。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:240

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/recorder.py:37

AsyncRecorder ​

python
class AsyncRecorder(model: RuntimeInstance[ArrayT] | BatchModel[ArrayT], output_dir: Path | str = './results', max_queue_size: int = 0, drop_policy: DropPolicy = 'raise')

用 asyncio 队列包装 Recorder 的非阻塞异步记录器。

取值进入队列前已规范化并复制,工作协程只把扁平 numpy 数组转交 Recorder.record,parquet 写入仍在线程池中执行。

__init__ ​

重载

调用形式返回类型
__init__(model: BatchModel[ArrayT], output_dir: Path | str = './results', max_queue_size: int = 0, drop_policy: DropPolicy = 'raise')None
__init__(model: RuntimeInstance[ArrayT], output_dir: Path | str = './results', max_queue_size: int = 0, drop_policy: DropPolicy = 'raise')None

构造异步记录器。

参数

名称类型默认值说明
modelBatchModel[ArrayT] | RuntimeInstance[ArrayT]—提供单实例变量布局的标量或批量已编译模型。
output_dirPath | str'./results'parquet 与元数据的输出目录,默认 ./results。
max_queue_sizeint0队列容量;0 表示无界。
drop_policyDropPolicy'raise'队列满时的策略,raise、drop_newest 或 drop_oldest,默认 raise。

异常

类型说明
ValueErrormax_queue_size 为负数或 drop_policy 不是允许值。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:70

recorder ​

python
recorder = cast(Recorder[ArrayT], Recorder(model, output_dir))

底层同步记录器。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:95

drop_policy ​

python
drop_policy = drop_policy

队列满时采用的丢弃策略。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:97

pending ​

python
pending: int

当前队列中待处理样本的数量。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:106

start ​

python
def start() -> None

启动后台工作协程,已启动则不重复创建。

异常

类型说明
RuntimeError记录器已关闭。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:110

record ​

python
def record(step: int, time: float, x: RecordValue = None, xd: RecordValue = None, y: RecordValue = None, u: RecordValue = None, p: RecordValue = None, effect: RecordValue = None, batch_idx: int = 0) -> None

把单个样本放入队列并在需要时启动工作协程。

参数

名称类型默认值说明
stepint—仿真步数索引。
timefloat—仿真时间,单位秒。
xRecordValueNone连续状态,支持数组、序列或路径映射;None 表示不记录。
xdRecordValueNone离散状态,取值形式同 x。
yRecordValueNone输出,取值形式同 x。
uRecordValueNone输入,取值形式同 x。
pRecordValueNone参数,取值形式同 x。
effectRecordValueNone效果或探测量,取值形式同 x。
batch_idxint0批次索引,默认 0。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:122

record_nowait ​

python
def record_nowait(*, step: int, time: float, x: RecordValue = None, xd: RecordValue = None, y: RecordValue = None, u: RecordValue = None, p: RecordValue = None, effect: RecordValue = None, batch_idx: int = 0) -> bool

尝试立即把单个样本放入队列,不等待队列腾出空间。

参数

名称类型默认值说明
stepint—仿真步数索引。
timefloat—仿真时间,单位秒。
xRecordValueNone连续状态,支持数组、序列或路径映射;None 表示不记录。
xdRecordValueNone离散状态,取值形式同 x。
yRecordValueNone输出,取值形式同 x。
uRecordValueNone输入,取值形式同 x。
pRecordValueNone参数,取值形式同 x。
effectRecordValueNone效果或探测量,取值形式同 x。
batch_idxint0批次索引,默认 0。

返回

类型说明
boolTrue 表示已入队;策略丢弃或替换旧样本时返回 False。

异常

类型说明
RuntimeError记录器已关闭。
asyncio.QueueFull队列已满且策略为 raise。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:162

flush ​

python
def flush() -> None

等待队列中所有样本被处理完毕。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:223

save ​

python
def save(filename: str = 'simulation_data') -> None

先清空队列,再在线程池中写出数据文件。

参数

名称类型默认值说明
filenamestr'simulation_data'输出文件名(不含扩展名),默认 simulation_data。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:228

close ​

python
def close(*, save: bool = False, filename: str = 'simulation_data') -> None

关闭记录器并回收后台工作协程,重复调用无副作用。

参数

名称类型默认值说明
saveboolFalse关闭前是否写出数据文件,默认 False。
filenamestr'simulation_data'save 为 True 时使用的文件名。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:237

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/async_recorder.py:45

Loader ​

python
class Loader(data_dir: Path | str, filename: str = 'simulation_data', *, model: RuntimeInstance | BatchModel | None = None)

读取 Recorder 写出的 parquet 与 JSON 元数据。

通过保存的 spaces 可独立按完整路径访问变量与元数据,支持索引、时间区间与批次过滤;旧记录可显式提供匹配的 RuntimeInstance。

python
loader = Loader("./results_step", model=compiled_model)
x_data = loader.get_x("Root.ss::x")          # 全部步, 形状 (N, 2)
x0 = loader.get_x("Root.ss::x", index=0)      # 只取第一个元素
subset = loader.get_x(time_range=(0.0, 1.0))  # 按时间过滤

__init__ ​

python
def __init__(data_dir: Path | str, filename: str = 'simulation_data', *, model: RuntimeInstance | BatchModel | None = None) -> None

从输出目录加载 parquet 与元数据并建立路径索引。

参数

名称类型默认值说明
data_dirPath | str—存放 parquet 与元数据的目录。
filenamestr'simulation_data'数据文件名(不含扩展名),默认 simulation_data。
modelRuntimeInstance | BatchModel | NoneNone可选匹配模型;旧记录缺少 spaces 时,路径解析需要此参数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:51

get_dataframe ​

python
def get_dataframe(batch_id: int | None = None) -> pl.DataFrame

返回底层数据表,可按批次过滤。

参数

名称类型默认值说明
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
pl.DataFrame与 Recorder 相同列布局的 Polars DataFrame。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:279

get_x ​

python
def get_x(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取连续状态数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:293

get_xd ​

python
def get_xd(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取离散状态数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:314

get_y ​

python
def get_y(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取输出数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:335

get_u ​

python
def get_u(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取输入数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:356

get_p ​

python
def get_p(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取参数数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:377

get_effect ​

python
def get_effect(path: str | None = None, index: int | None = None, time_range: tuple[float, float] | None = None, *, batch_id: int | None = None) -> np.ndarray

取效果或探测数据。

参数

名称类型默认值说明
pathstr | NoneNone完整信号路径;None 表示该空间的全部列。
indexint | NoneNone非标量信号最后一维的索引;标量信号忽略。
time_rangetuple[float, float] | NoneNone(起, 止) 时间区间,闭区间,单位秒。
batch_idint | NoneNone批次索引;None 表示不按批次过滤。

返回

类型说明
np.ndarray形状 (记录数, 维度) 的数组;对标量信号使用 index 时为一维。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:398

info ​

python
def info(kind: RecordKind, path: str) -> VariableInfo

读取记录自身保存的变量元数据,不要求绑定原模型。

参数

名称类型默认值说明
kindRecordKind—记录空间 x/xd/y/u/p/effect;诊断量使用单数 effect。
pathstr—声明的完整公开路径,不使用短名推测身份。

返回

类型说明
VariableInfo含逻辑 dtype、单位、说明、原始形状和扁平槽位的只读描述。

异常

类型说明
KeyError记录未保存该变量元数据,包括不带 spaces 的旧记录。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:423

metadata ​

python
metadata: JsonObject

从 JSON 加载的完整元数据字典。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:443

columns ​

python
columns: list[str]

元数据中记录的列名列表。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:448

total_records ​

python
total_records: int

记录总数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:456

timestamp ​

python
timestamp: str | None

最初记录时的时间戳。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:462

save_timestamp ​

python
save_timestamp: str | None

数据保存时的时间戳。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:468

batches ​

python
batches: list[int]

数据中出现过的批次索引,升序排列。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:474

time_range ​

python
time_range: tuple[float, float]

数据中的最小与最大仿真时间,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:479

compiled_model_info ​

python
compiled_model_info: JsonObject

已保存的编译模型元数据;不存在时为空字典。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:487

dataframe ​

python
dataframe: pl.DataFrame

底层原始 Polars DataFrame。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:493

model ​

python
model: RuntimeInstance | BatchModel | None

用于路径解析的已编译模型。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:498

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/loader.py:34

RecordSample ​

python
class RecordSample(batch_idx: int, step: int, time: float, values: Mapping[RecordKind, RecordArray])

一次记录调用的样本,含批次、步数与各变量空间的扁平值。

batch_idx ​

python
batch_idx: int

批次索引,用于区分并行轨迹。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py:33

step ​

python
step: int

仿真步数索引。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py:36

time ​

python
time: float

仿真时间,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py:39

values ​

python
values: Mapping[RecordKind, RecordArray]

变量空间类别到扁平 float64 数组的映射,仅含本次提供的类别。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py:42

__init__ ​

python
def __init__(batch_idx: int, step: int, time: float, values: Mapping[RecordKind, RecordArray]) -> None

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/record/layout.py:29

签名类型说明 ​

以下别名用于解释参数与返回值,不是本包的公开导出对象。

内部别名类型定义
ArrayLiketype ArrayLike = np.ndarray | CasadiArray | JaxArray | Sequence[float] | Sequence[Sequence[float]] | float | int
DropPolicytype DropPolicy = Literal['raise', 'drop_newest', 'drop_oldest']
JsonObjecttype JsonObject = dict[str, JsonValue]
JsonValuetype JsonValue = str | int | float | bool | None | list['JsonValue'] | dict[str, 'JsonValue']
RecordArraytype RecordArray = NumpyArray
RecordKindtype RecordKind = Literal['x', 'xd', 'y', 'u', 'p', 'effect']
RecordValuetype RecordValue = ArrayLike | Mapping[str, ArrayLike] | None