playnano.io.export_data module

Save AFM image stacks as OME-TIFF, NPZ or HDF5 files.

export_bundles() writes any of the three at once, as playnano process --export does. All formats store heights as float32.

playnano.io.export_data.check_path_is_path(path: str | Path) → Path[source]

Return path as a pathlib.Path.

Parameters:

path (str or pathlib.Path) – The path to convert.

Returns:

path as a Path.

Return type:

pathlib.Path

Raises:

TypeError – If path is neither a str nor a Path.

playnano.io.export_data.export_bundles(afm_stack: AFMImageStack, output_folder: Path, base_name: str, formats: list[str], raw: bool = False) → None[source]

Save a stack in one or more of the formats OME-TIFF, NPZ and HDF5.

Files are named <name>.ome.tif, <name>.npz and <name>.h5. _filtered is added to the name when processed data is saved, that is when raw is False and the stack has processed layers other than "raw".

Parameters:
  • afm_stack (AFMImageStack) – The stack to save.

  • output_folder (str or pathlib.Path or None) – Folder for the files; created if missing. If None, ./output.

  • base_name (str or None) – File name without extension. Any folder part and known extension are removed. If empty or None, the name of the file the stack was loaded from is used.

  • formats (list of str) – Any of "tif", "npz" and "h5".

  • raw (bool, optional) – If True, save the unprocessed snapshot stack.processed["raw"]. See save_npz_bundle(). Default is False.

Raises:

ValueError – If a format is not recognised, or an output file would overwrite the file the stack was loaded from. Nothing is written in either case.

playnano.io.export_data.save_h5_bundle(path: Path, stack: AFMImageStack, raw: bool = False) → None[source]

Save a stack, with its layers, masks and metadata, as an HDF5 bundle.

The file contains these datasets, compressed with gzip where they are arrays:

  • /data: float32, shape (n_frames, height, width).

  • /processed/<name>: float32, one per entry in stack.processed, including /processed/raw.

  • /masks/<name>: bool, one per entry in stack.masks.

  • /timestamps: float64, frame times in seconds.

  • /frame_metadata_json, /provenance_json: JSON text.

  • /state_backups_json: JSON text, only if stack.state_backups is not empty.

and the file attributes pixel_size_nm and channel.

Parameters:
  • path (str or pathlib.Path) – Output file. Its extension is replaced by .h5, and missing folders are created.

  • stack (AFMImageStack) – The stack to save.

  • raw (bool, optional) – If True, save the unprocessed snapshot stack.processed["raw"] instead of stack.data, without masks or processed layers. If the stack has no "raw" snapshot, stack.data is saved. Default is False. The timestamps and frame metadata are then those from before any frames were removed.

See also

playnano.io.data_loaders.load_h5_bundle

Load the bundle again.

playnano.io.export_data.save_npz_bundle(path: Path, stack: AFMImageStack, raw: bool = False) → None[source]

Save a stack, with its layers, masks and metadata, as a NumPy .npz bundle.

The archive is compressed and contains these arrays:

  • data: float32, shape (n_frames, height, width).

  • processed__<name>: float32, one per entry in stack.processed, including processed__raw.

  • masks__<name>: bool, one per entry in stack.masks.

  • timestamps: float64, frame times in seconds.

  • frame_metadata_json, provenance_json, state_backups_json: JSON text.

  • pixel_size_nm: float32 scalar.

  • channel: the channel name.

Text is stored as NumPy unicode arrays, so no array is pickled and the file can be opened with np.load(path, allow_pickle=False). Bundles written before playNano 0.5.4 stored text as object arrays, which need pickle.

Parameters:
  • path (pathlib.Path) – Output file. Its extension is replaced by .npz (out/stack becomes out/stack.npz), and missing folders are created.

  • stack (AFMImageStack) – The stack to save.

  • raw (bool, optional) – If True, save the unprocessed snapshot stack.processed["raw"] instead of stack.data, without masks or processed layers. If the stack has no "raw" snapshot, stack.data is saved. Default is False. The timestamps are then those from before any frames were removed.

See also

playnano.io.data_loaders.load_npz_bundle

Load the bundle again.

Examples

>>> from pathlib import Path
>>> from playnano.io.export_data import save_npz_bundle
>>> save_npz_bundle(Path("output/stack"), stack)
playnano.io.export_data.save_ome_tiff_stack(path: Path, afm_stack: AFMImageStack, raw: bool = False) → None[source]

Save a stack as an OME-TIFF file.

The data is written as float32 with axes (T, C, Z, Y, X) and one channel and Z plane. The OME metadata records the pixel size (in µm), each frame’s time (DeltaT, in s), the interval between the first two frames (TimeIncrement) and the channel name. Two private TIFF tags hold JSON:

  • 65000: the provenance (stack.provenance).

  • 65001: the names of the processed layers. The layers themselves and the masks are not saved; use NPZ or HDF5 for those.

Parameters:
  • path (pathlib.Path) – Output file, for example Path("out/stack.ome.tif"). Used as given; its folder must exist.

  • afm_stack (AFMImageStack) – The stack to save.

  • raw (bool, optional) – If True, save the unprocessed snapshot stack.processed["raw"] instead of stack.data, without masks or processed layers. If the stack has no "raw" snapshot, stack.data is saved. Default is False.

Raises:

ValueError – If the frames do not all have the same pixel size.

See also

playnano.io.data_loaders.load_ome_tiff_stack

Load the file again.

Examples

>>> from pathlib import Path
>>> from playnano.io.export_data import save_ome_tiff_stack
>>> save_ome_tiff_stack(Path("output/stack.ome.tif"), stack)