计时与媒体工具#
genesis.utils.tools 模块提供用于循环计时和保存媒体的轻量辅助函数,在 gs.utils.tools 下暴露。
计时#
- genesis.utils.tools.create_timer(name=None, new=False, level=0, qd_sync=False, skip_first_call=False)[source]#
- class genesis.utils.tools.Rate(rate)[source]#
Fixed-frequency loop limiter: call
sleeponce per iteration to hold the loop atrateHz.Each wake-up is scheduled against an ideal clock advanced by exactly the periods slept per tick, rather than from the actual (over-slept) wake time, so
time.sleepovershoot does not accumulate and the average rate stays on target. When an iteration runs longer than the periods it sleeps, the limiter does not sleep and resets its schedule from the current time, so a loop that cannot keep up simply runs as fast as it can without building a sleep debt to burn off afterwards.periodmay be changed between two ticks, the next one following the new value.- property rate#
The frequency in Hz the loop is held at.
- advance(n_periods=1)[source]#
Schedule the next tick ‘n_periods’ periods after the previous one, and return the time left until it.
The time left is 0 when the loop is behind, and the schedule then starts anew from the current time. A caller that waits the time left in a way of its own, such as keeping a window responsive (see ‘Viewer.sleep’), uses it in place of ‘sleep’.
- class genesis.utils.tools.StepTimer[source]#
Times the phases of the steps of a stepped process.
A step opens with
start, its phases run underphase, named freely, or betweenstart_phaseand the nextstart_phaseorstop_phasewhere awithblock would indent too much, andstepcloses it. A closed step leaves intimings, until the next one, the wall time of every phase it timed, of the whole step understep, and of the period from the start of the previous step to its own underperiod. The period covers the time spent between the two steps as well, so its inverse is the rate the loop achieves. A step thatstartopens andstepnever closes leaves no trace, as the nextstartdiscards it. The first step has no period. The code timing a phase declares it withadd_phasesbefore the first step, so that a log laid out once lists every phase a step may time.- property phases: tuple[str, ...]#
Every phase a step may time,
stepandperiodfirst, in declaration order (seeadd_phases).
- property timings: dict[str, float]#
Wall time in seconds of every phase the latest recorded step timed, by phase, empty before any.
- start_phase(phase: str)[source]#
Time the code that follows as part of a phase, until the next
start_phaseorstop_phase.It lets a loop time each of its iterations under its own phase without a
withblock. A phase running from a previousstart_phasestops here.
- stop_phase()[source]#
Stop the phase
start_phaseopened, adding its time to what the phase already took in this step.
- class genesis.utils.tools.RunningMean(window: int | None = None, alpha: float | None = None)[source]#
Mean of a scalar fed one sample at a time, over the latest samples or exponentially weighted.
Exactly one of ‘window’ and ‘alpha’ is given. With ‘window’, ‘value’ is the mean of the latest ‘window’ samples, NaN until that many have arrived, and it follows a change fully after that many samples. With ‘alpha’, it is the exponential moving average, the value so far weighing ‘alpha’ and each new sample ‘1 - alpha’, starting from the first sample and NaN before it: it never forgets a sample entirely, and costs nothing per sample whatever the smoothing.
- property value: float#
The mean of the samples so far, NaN until there are enough of them (see the class).
媒体#
- genesis.utils.tools.animate(imgs, filename=None, fps=60)[source]#
Create a video from a list of images.
- Parameters:
imgs (list) – List of input images.
filename (str, optional) – Name of the output video file. If not provided, the name will default to the name of the script the process was launched from, with a timestamp and ‘.mp4’ extension.
另请参阅#
记录与回放:把仿真数据记录到文件和图表中。