playnano.utils.time_utils module

Helpers for frame times and for drawing the time and scale bar on frames.

playnano.utils.time_utils.draw_scale_and_timestamp(image: ndarray, timestamp: float, pixel_size_nm: float, resize_scale: float, annotation_scale: float, bar_length_nm: int = 100, font_scale: float = 1, draw_ts: bool = True, draw_scale: bool = True, color: tuple = (255, 255, 255)) → ndarray[source]

Draw the time and a scale bar on an RGB frame.

The time is drawn at the top left as "1.23 s", the scale bar and its label ("100 nm") at the bottom left, in the bundled Steps-Mono font (or Pillow’s default font if it cannot be loaded).

Parameters:
  • image (numpy.ndarray) – RGB frame, shape (height, width, 3), dtype uint8. Not changed.

  • timestamp (float) – Time to show, in seconds.

  • pixel_size_nm (float) – Pixel size of the frame before resizing, in nm.

  • resize_scale (float) – How much the frame was enlarged (new height / old height). Sets the scale bar’s length in pixels.

  • annotation_scale (float) – Frame height divided by the 512-pixel reference height. Sets margins, bar thickness and text positions so they look the same at any size.

  • bar_length_nm (int, optional) – Length of the scale bar, in nm. Default is 100.

  • font_scale (float, optional) – Text height is 15 * font_scale pixels. Default is 1.

  • draw_ts (bool, optional) – Draw the time. Default is True.

  • draw_scale (bool, optional) – Draw the scale bar, if bar_length_nm and pixel_size_nm are both positive. Default is True.

  • color (tuple of int, optional) – RGB colour of the text and bar. Default is white, (255, 255, 255).

Returns:

A new annotated frame, shape (height, width, 3), dtype uint8.

Return type:

numpy.ndarray

Examples

>>> import numpy as np
>>> from playnano.utils.time_utils import draw_scale_and_timestamp
>>> frame = np.zeros((100, 100, 3), dtype=np.uint8)
>>> out = draw_scale_and_timestamp(frame, 1.5, 1.0, 1.0, 1.0, bar_length_nm=50)
>>> out.shape, out.dtype
((100, 100, 3), dtype('uint8'))
playnano.utils.time_utils.normalize_timestamps(metadata_list: list[dict[str, Any]]) → list[dict[str, Any]][source]

Convert each frame’s "timestamp" to seconds as a float, or None.

  • Python int and float: converted to float (bool counts as int).

  • datetime.datetime: seconds since 1970 (Unix time).

  • ISO 8601 strings: parsed, then seconds since 1970. Times without a time zone are read as local time.

  • Anything else, including NumPy numbers such as numpy.int64, a missing key and strings that cannot be parsed: None.

Parameters:

metadata_list (list of dict) – Per-frame metadata. Not changed.

Returns:

Shallow copies of the dicts, each with a "timestamp" key.

Return type:

list of dict

Examples

>>> from playnano.utils.time_utils import normalize_timestamps
>>> normalize_timestamps([{"timestamp": 1}, {}, {"timestamp": "bad"}])
[{'timestamp': 1.0}, {'timestamp': None}, {'timestamp': None}]
playnano.utils.time_utils.utc_now_iso() → str[source]

Return the current UTC time as an ISO 8601 string.

Returns:

For example "2024-01-15T10:30:00.123456Z".

Return type:

str