# ptufile.py # Copyright (c) 2023-2026, Christoph Gohlke # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. """Read and write PicoQuant PTU and related files. Ptufile is a Python library to 1. read data and metadata from PicoQuant PTU and related files (PHU, PCK, PCO, PFS, PUS, PQRES, PQDAT, PQUNI, SPQR, and BIN), and 2. write TCSPC histograms to T3 image mode PTU files. PTU files contain time correlated single photon counting (TCSPC) measurement data and instrumentation parameters. :Author: `Christoph Gohlke `_ :License: BSD-3-Clause :Version: 2026.8.8 :DOI: `10.5281/zenodo.10120021 `_ Quickstart ---------- Install the ptufile package and all dependencies from the `Python Package Index `_:: python -m pip install -U "ptufile[all]" See `Examples`_ for using the programming interface. Source code and support are available on `GitHub `_. Requirements ------------ This revision was tested with the following requirements and dependencies (other versions may work): - `CPython `_ 3.12.10, 3.13.15, 3.14.7, 3.15.0rc 64-bit - `Numpy `_ 2.5.2 - `Xarray `_ 2026.7.0 (recommended) - `Matplotlib `_ 3.11.1 (optional) - `Tifffile `_ 2026.7.31 (optional) - `Numcodecs `_ 0.16.5 (optional) - `Python-dateutil `_ 2.9.0 (optional) - `Cython `_ 3.2.9 (build) Revisions --------- 2026.8.8 - Add PQOVER file type. 2026.6.6 - Remove memmap parameter from PtuFile.read_records (breaking). - Return copy of records from PtuFile.read_records by default (breaking). - Add options for memory-mapping and locked reading to BinaryFile. - Add option to memory-map PTU files. - Drop support for numpy 2.0 (SPEC0). - Support Python 3.15. 2026.3.21 - Add bounds checking to encode_t3_image function. - Use format-dispatch in hot decode loops to allow compiler inlining. - Build wheels on Windows with LLVM (30% faster decoding than MSVC). - Drop support for Python 3.11. 2026.2.6 - Fix code review issues. 2026.1.14 - Improve code quality. 2025.12.12 - Add PQUNI file type. - Add attrs properties and return with xarray DataSets. - Improve code quality. 2025.11.8 - Fix reading files with negative TTResult_NumberOfRecords. - Remove cache argument from PtuFile.read_records (breaking). - Add cache_records property to PtuFile to control caching behavior. - Derive PqFileError from ValueError. - Factor out BinaryFile base class. - Build ABI3 wheels. 2025.9.9 - Log error when decoding image with invalid line or frame masks. 2025.7.30 - Add option to specify pixel time for decoding images. - Add functions to read and write PicoQuant BIN files. - Drop support for Python 3.10. 2025.5.10 - Mark Cython extension free-threading compatible. - Support Python 3.14. 2025.2.20 - … Refer to the CHANGES file for older revisions. Notes ----- `PicoQuant GmbH `_ is a manufacturer of photonic components and instruments. The PicoQuant unified file formats are documented at the `PicoQuant-Time-Tagged-File-Format-Demos `_. All PicoQuant unified tagged formats use the same basic layout: a file header followed by typed tags and an optional data block. PTU is the main container for TTTR event streams, whereas related formats store histograms, instrument settings, analysis results, or other auxiliary data. The following features are currently not implemented due to the lack of test files or documentation: PT2 and PT3 files, decoding images from T2 and SPQR formats, bidirectional per frame, and deprecated image reconstruction. Compatibility with PTU files written by non-PicoQuant software (for example, Leica LAS X or Abberior Imspector) is limited, as is decoding line, bidirectional, and sinusoidal scanning. Other modules for reading or writing PicoQuant files are `Read_PTU.py `_, `readPTU `_, `readPTU_FLIM `_, `fastFLIM `_, `PyPTU `_, `PTU_Reader `_, `PTU_Writer `_, `FlimReader `_, `tangy `_, `tttrlib `_, `picoquantio `_, `ptuparser `_, `phconvert `_, `trattoria `_ (wrapper of `trattoria-core `_, `tttr-toolbox `_), `PAM `_, `FLOPA `_, `FLIMKit `_, and `napari-flim-phasor-plotter `_. Examples -------- Import functions and classes used in these examples: >>> from ptufile import PqFile, PtuFile, imwrite, imread Read properties and tags from any type of PicoQuant unified tagged file: >>> pq = PqFile('tests/data/Settings.pfs') >>> pq.type >>> pq.guid UUID('86d428e2-cb0b-4964-996c-04456ba6be7b') >>> pq.tags {...'CreatorSW_Name': 'SymPhoTime 64', 'CreatorSW_Version': '2.1'...} >>> pq.close() Read metadata from a PicoQuant PTU FLIM file: >>> ptu = PtuFile('tests/data/FLIM.ptu') >>> ptu.type >>> ptu.record_type >>> ptu.measurement_mode >>> ptu.measurement_submode Decode TTTR records from the PTU file to ``numpy.recarray``: >>> decoded = ptu.decode_records() >>> decoded.dtype dtype([('time', '>> decoded['time'][(decoded['marker'] & ptu.frame_change_mask) > 0] array([1571185680], dtype=uint64) Decode TTTR records to overall delay-time histograms per channel: >>> ptu.decode_histogram(dtype='uint8') array([[ 5, 7, 7, ..., 10, 9, 2]], shape=(2, 3126), dtype=uint8) Get information about the FLIM image histogram in the PTU file: >>> ptu.shape (1, 256, 256, 2, 3126) >>> ptu.dims ('T', 'Y', 'X', 'C', 'H') >>> ptu.coords {'T': ..., 'Y': ..., 'X': ..., 'H': ...} >>> ptu.dtype dtype('uint16') >>> ptu.active_channels (0, 1) Decode parts of the image histogram to ``numpy.ndarray`` using slice notation. Slice step sizes define binning, -1 being used to integrate along axis: >>> ptu[:, ..., 0, ::-1] array([[[103, ..., 38], ... [ 47, ..., 30]]], shape=(1, 256, 256), dtype=uint16) Alternatively, decode the first channel and integrate all histogram bins into a ``xarray.DataArray``, keeping reduced axes: >>> ptu.decode_image(channel=0, dtime=-1, asxarray=True) ... array([[[[[103]], ... [[ 30]]]]], shape=(1, 256, 256, 1, 1), dtype=uint16) Coordinates: * T (T) float64... 0.05625 * Y (Y) float64... -0.0001304 ... 0.0001294 * X (X) float64... -0.0001304 ... 0.0001294 * C (C) uint8... 0 * H (H) float64... 0.0 Attributes... name: FLIM.ptu ... Write the TCSPC histogram and metadata to a PicoHarpT3 image mode PTU file: >>> imwrite( ... '_test.ptu', ... ptu[:], ... ptu.global_resolution, ... ptu.tcspc_resolution, ... # optional metadata ... pixel_time=ptu.pixel_time, ... record_type=PtuRecordType.PicoHarpT3, ... comment='Written by ptufile.py', ... tags={'File_RawData_GUID': [ptu.guid]}, ... ) Read back the TCSPC histogram from the file: >>> tcspc_histogram = imread('_test.ptu') >>> import numpy >>> numpy.array_equal(tcspc_histogram, ptu[:]) True Close the file handle: >>> ptu.close() Preview the image and metadata in a PTU file from the console:: python -m ptufile tests/data/FLIM.ptu """ from __future__ import annotations __version__ = '2026.8.8' __all__ = [ 'FILE_EXTENSIONS', 'T2_RECORD_DTYPE', 'T3_RECORD_DTYPE', 'PhuFile', 'PhuMeasurementMode', 'PhuMeasurementSubMode', 'PqFile', 'PqFileError', 'PqFileType', 'PtuFile', 'PtuHwFeatures', 'PtuMeasurementMode', 'PtuMeasurementSubMode', 'PtuMeasurementWarnings', 'PtuRecordType', 'PtuScanDirection', 'PtuScannerType', 'PtuStopReason', 'PtuWriter', '__version__', 'binread', 'binwrite', 'imread', 'imwrite', ] import contextlib import dataclasses import enum import io import logging import math import mmap import os import struct import sys import threading import uuid from datetime import datetime, timedelta from functools import cached_property from typing import TYPE_CHECKING, final, overload, override if TYPE_CHECKING: from collections.abc import Iterable, Sequence from types import EllipsisType, TracebackType from typing import IO, Any, ClassVar, Literal, Self from numpy.typing import ArrayLike, DTypeLike, NDArray from xarray import DataArray import numpy type Dimension = Literal['T', 'C', 'H'] type OutputType = str | IO[bytes] | NDArray[Any] | None @overload def imread( file: str | os.PathLike[str] | IO[bytes], /, selection: Sequence[int | slice | EllipsisType | None] | None = None, *, dtype: DTypeLike | None = None, channel: int | None = None, frame: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, trimdims: Sequence[Dimension] | str | None = None, keepdims: bool = True, memmap: bool = False, asxarray: Literal[False] = ..., out: OutputType = None, ) -> NDArray[Any]: ... @overload def imread( file: str | os.PathLike[str] | IO[bytes], /, selection: Sequence[int | slice | EllipsisType | None] | None = None, *, dtype: DTypeLike | None = None, channel: int | None = None, frame: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, trimdims: Sequence[Dimension] | str | None = None, keepdims: bool = True, memmap: bool = False, asxarray: Literal[True] = ..., out: OutputType = None, ) -> DataArray: ... @overload def imread( file: str | os.PathLike[str] | IO[bytes], /, selection: Sequence[int | slice | EllipsisType | None] | None = None, *, dtype: DTypeLike | None = None, channel: int | None = None, frame: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, trimdims: Sequence[Dimension] | str | None = None, keepdims: bool = True, memmap: bool = False, asxarray: bool = False, out: OutputType = None, ) -> NDArray[Any] | DataArray: ... def imread( file: str | os.PathLike[str] | IO[bytes], /, selection: Sequence[int | slice | EllipsisType | None] | None = None, *, dtype: DTypeLike | None = None, channel: int | None = None, frame: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, trimdims: Sequence[Dimension] | str | None = None, keepdims: bool = True, memmap: bool = False, asxarray: bool = False, out: OutputType = None, ) -> NDArray[Any] | DataArray: """Return decoded image histogram from T3 mode PTU file. Parameters: file: File name or seekable binary stream. selection, dtype, channel, frame, dtime, pixel_time, bishift,\ keepdims, asxarray, out: Passed to :py:meth:`PtuFile.decode_image`. trimdims, memmap: Passed to :py:class:`PtuFile`. Returns: image: Decoded TTTR T3 records as up to 5-dimensional image array. """ with PtuFile(file, trimdims=trimdims, memmap=memmap) as ptu: return ptu.decode_image( selection, dtype=dtype, channel=channel, frame=frame, dtime=dtime, bishift=bishift, pixel_time=pixel_time, keepdims=keepdims, asxarray=asxarray, out=out, ) def imwrite( file: str | os.PathLike[str] | IO[bytes], data: ArrayLike, /, global_resolution: float, tcspc_resolution: float, pixel_time: float | None = None, *, has_frames: bool | None = None, record_type: PtuRecordType | None = None, pixel_resolution: float | None = None, guid: str | uuid.UUID | None = None, comment: str | None = None, datetime: datetime | None = None, tags: dict[str, Any] | None = None, mode: Literal['w', 'wb', 'x', 'xb'] | None = None, ) -> None: """Write TCSPC histogram to T3 image mode PTU file. Parameters: file: File name or writable binary stream. data: TCSPC histogram image stack. The order of dimensions must be 'TYXCH', 'YXH', 'YXCH', or 'TYXH' (with ``has_frames=True``). The dtype must be unsigned integer. global_resolution: Resolution of time tags in s, typically in ns range. The inverse of the synctime or laser frequency. One photon is encoded per time tag. tcspc_resolution: Resolution of TCSPC in s, typically in ps range. The width of a histogram bin. pixel_time: Time per pixel in s, typically in μs range. Photons that cannot be encoded within pixel_time are omitted. By default, pixel_time is set just large enough to encode all photons. has_frames: 4-dimensional data have frames in first axis ('TYXH'), no channels. By default, true if data contains metadata specifying the first dimension is 'T', else false. record_type, pixel_resolution, guid, comment, datetime, tags, mode: Optional parameters passed to :py:class:`PtuWriter`. """ if hasattr(data, 'dims'): has_frames = 'T' in data.dims and data.dims[0] == 'T' data = numpy.asarray(data) if pixel_time is None: data = data.reshape( PtuWriter.normalize_shape(data.shape, has_frames=has_frames) ) pixel_time = global_resolution * max( 1, float(numpy.max(data.sum(axis=(3, 4), dtype=numpy.uint64))) ) with PtuWriter( file, data.shape, global_resolution, tcspc_resolution, pixel_time, record_type=record_type, pixel_resolution=pixel_resolution, has_frames=has_frames, guid=guid, comment=comment, datetime=datetime, tags=tags, mode=mode, ) as ptu: ptu.write(data) @final class PtuWriter: """Write TCSPC histogram to T3 image mode PTU file. T3 TTTR records allow for a maximum of 63 channels and 32768 bins. The TTTR records written can only be used to reconstruct the encoded TCSPC histogram image stack, not for higher-than-pixel-time-resolution intensity time-trace or correlation analysis. Parameters: file: File name or writable binary stream. File names typically end in '.PTU'. shape: Shape of TCSPC histogram image stack to write. The order of dimensions must be 'TYXCH', 'YXH', 'YXCH', or 'TYXH' (with ``has_frames=True``). global_resolution: Resolution of time tags in s, typically in ns range. The inverse of the synctime or laser frequency. One photon is encoded per time tag. tcspc_resolution: Resolution of TCSPC in s, typically in ps range. The width of a histogram bin. pixel_time: Time per pixel in s, typically in μs range. Photons that cannot be encoded within pixel_time are omitted. record_type: Type of TTTR T3 records to write. By default, write ``PicoHarpT3`` records for up to two channels and 4096 bins, else ``GenericT3``. pixel_resolution: Resolution of single pixel in μm. The default is 1 μm. has_frames: 4-dimensional shape has frames in first axis ('TYXH'), no channels. guid: Windows formatted GUID used as global file identifier. By default, a random GUID. Write to File_GUID tag. comment: File comment. Write to File_Comment tag. datetime: File creation date and time. The default is time at function call. Write to File_CreatingTime tag. tags: Additional tag Id and values to write. Critical tags are automatically set and cannot be modified. No validation is performed. Refer to the "PicoQuant Unified Tag Dictionary" for valid Id and values. mode: Binary file open mode if ``file`` is file name. The default is 'w', which opens files for writing, truncating existing files. 'x' opens files for exclusive creation, failing on existing files. Raises: ValueError Not ``0 < tcspc_resolution <= global_resolution <= pixel_time``. """ _fh: IO[bytes] | None _shape: tuple[int, int, int, int, int] _record_type: PtuRecordType _number_records: int _number_records_offset: int _number_frames: int _number_frames_offset: int _global_resolution: float _tcspc_resolution: float _pixel_time: int _line_start = 1 _line_stop = 2 _frame_change = 3 def __init__( self, file: str | os.PathLike[str] | IO[bytes], /, shape: tuple[int, ...], global_resolution: float, tcspc_resolution: float, pixel_time: float, *, record_type: PtuRecordType | None = None, pixel_resolution: float | None = None, has_frames: bool | None = None, guid: str | uuid.UUID | None = None, comment: str | None = None, datetime: datetime | None = None, tags: dict[str, Any] | None = None, mode: Literal['w', 'wb', 'x', 'xb'] | None = None, ) -> None: """Write PTU header to file.""" # 0 < tcspc_resolution <= global_resolution <= pixel_time if tcspc_resolution <= 0.0: msg = f'{tcspc_resolution=} <= 0.0' raise ValueError(msg) if tcspc_resolution > global_resolution: msg = f'{tcspc_resolution=} > {global_resolution=}' raise ValueError(msg) if pixel_time < global_resolution: msg = f'{pixel_time=} < {global_resolution=}' raise ValueError(msg) self._fh = None self._number_records = 0 self._number_records_offset = 0 self._number_frames = 0 self._number_frames_offset = 0 self._global_resolution = global_resolution self._tcspc_resolution = tcspc_resolution self._pixel_time = round(pixel_time / global_resolution) self._shape = shape = PtuWriter.normalize_shape( shape, has_frames=has_frames ) if record_type is None: if shape[3] <= 2 and shape[4] <= 4096: record_type = PtuRecordType.PicoHarpT3 else: record_type = PtuRecordType.GenericT3 if record_type == PtuRecordType.PicoHarpT3: if shape[3] > 4 or shape[4] > 4096: msg = ( f'{record_type=} does not support ' f'{shape[3]} channels and {shape[4]} bins' ) raise ValueError(msg) self._record_type = PtuRecordType.PicoHarpT3 elif record_type in { PtuRecordType.GenericT3, PtuRecordType.HydraHarp2T3, PtuRecordType.TimeHarp260NT3, PtuRecordType.TimeHarp260PT3, }: if shape[3] > 63 or shape[4] > 32768: msg = ( f'{record_type=} does not support ' f'{shape[3]} channels and {shape[4]} bins' ) raise ValueError(msg) self._record_type = PtuRecordType.GenericT3 else: msg = f'{record_type=} not supported' raise ValueError(msg) if comment is None: comment = '' if guid is None: guid = f'{{{uuid.uuid4()}}}' elif isinstance(guid, uuid.UUID): guid = f'{{{guid}}}' elif len(guid) != 38 or guid[9] != '-': msg = 'invalid GUID' raise ValueError(msg) if pixel_resolution is None: pixel_resolution = 1.0 elif pixel_resolution <= 0.0: msg = f'{pixel_resolution=} <= 0.0' raise ValueError(msg) if datetime is None: datetime = now() critical_tags = { # tags not to be overwritten by user 'Measurement_Mode': PtuMeasurementMode.T3, 'Measurement_SubMode': PtuMeasurementSubMode.IMAGE, 'MeasDesc_GlobalResolution': float(self._global_resolution), 'MeasDesc_Resolution': float(self._tcspc_resolution), 'MeasDesc_BinningFactor': 1, 'TTResult_NumberOfRecords': self._number_records, 'TTResult_SyncRate': round(1.0 / self._global_resolution), 'TTResultFormat_TTTRRecType': self._record_type, 'TTResultFormat_BitsPerRecord': 32, 'ImgHdr_Dimensions': 3, 'ImgHdr_Ident': PtuScannerType.LSM, 'ImgHdr_LineStart': self._line_start, 'ImgHdr_LineStop': self._line_stop, 'ImgHdr_Frame': self._frame_change, 'ImgHdr_MaxFrames': 0, 'ImgHdr_TimePerPixel': pixel_time * 1e3, # ms 'ImgHdr_PixX': int(self._shape[2]), 'ImgHdr_PixY': int(self._shape[1]), 'ImgHdr_BiDirect': False, 'ImgHdr_SinCorrection': 0, } pqtags = { # required tags written first, allowed to be overwritten by user 'File_GUID': guid, 'File_Comment': comment, 'File_CreatingTime': datetime, 'CreatorSW_Name': 'ptufile.py', 'CreatorSW_Version': __version__, } pqtags.update(critical_tags) pqtags.update( # other tags allowed to be overwritten by user { 'ImgHdr_PixResol': float(pixel_resolution), 'HW_InpChannels': self._shape[3] + 1, # used by FlimReader # 'TTResult_StopReason': PtuStopReason(0) } ) if tags is not None: # add user tags but do not overwrite critical tags pqtags.update( {k: v for k, v in tags.items() if k not in critical_tags} ) header_list = [b'PQTTTR\x00\x001.0.00\x00\x00'] # magic and version for tagid, value in pqtags.items(): if isinstance(value, (list, tuple)): for index, item in enumerate(value): header_list.append(encode_tag(tagid, item, index)) else: header_list.append(encode_tag(tagid, value)) header_list.append(encode_tag('Header_End', None)) header = b''.join(header_list) offset = header.find(b'TTResult_NumberOfRecords') if offset < 0: msg = 'TTResult_NumberOfRecords tag not found in header' raise RuntimeError(msg) self._number_records_offset = offset + 40 offset = header.find(b'ImgHdr_MaxFrames') if offset < 0: msg = 'ImgHdr_MaxFrames tag not found in header' raise RuntimeError(msg) self._number_frames_offset = offset + 40 if isinstance(file, (str, os.PathLike)): if mode is None: mode = 'wb' elif mode[-1] != 'b': mode += 'b' # type: ignore[assignment] self._fh = open(file, mode) # noqa: SIM115 self._close = True elif hasattr(file, 'write') and hasattr(file, 'seek'): self._fh = file self._close = False else: msg = f'cannot write to {type(file)=}' raise ValueError(msg) self._fh.write(header) def write(self, data: ArrayLike, /) -> None: """Append T3 encoded TCSPC histogram to file. Parameters: data: TCSPC histogram image stack. The shape must be compatible with the shape passed to PtuWriter(). The dtype must be unsigned integer. """ from ._ptufile import encode_t3_image data = numpy.asarray(data) if data.dtype.kind != 'u': msg = f'{data.dtype=} is not an unsigned integer' raise ValueError(msg) data = data.reshape(self._shape) number_photons = int(data.sum(dtype=numpy.uint64)) if self._record_type == PtuRecordType.PicoHarpT3: maxtime = 65536 else: maxtime = 1024 shape = data.shape number_records = ( number_photons # overflows assuming all empty pixels + (shape[0] * shape[1] * shape[2] * self._pixel_time) // maxtime # line markers + shape[0] * shape[1] * 2 # frame markers + shape[0] ) records = numpy.zeros(number_records, dtype=numpy.uint32) number_records = encode_t3_image( records, data, self._record_type, self._pixel_time, int(2 ** (self._line_start - 1)), int(2 ** (self._line_stop - 1)), int(2 ** (self._frame_change - 1)), ) if number_records < 0: msg = f'{number_records=} < 0' raise ValueError(msg) if self._fh is None: msg = 'file handle is closed' raise RuntimeError(msg) self._fh.write(records[:number_records].tobytes()) self._number_records += number_records self._number_frames += shape[0] def close(self) -> None: """Close file handle after writing final tag values.""" if self._fh is None: return if self._number_records_offset > 0: self._fh.seek(self._number_records_offset) self._fh.write(struct.pack(' Self: return self def __exit__( self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, ) -> None: self.close() @staticmethod def normalize_shape( shape: tuple[int, ...], /, *, has_frames: bool | None = None, ) -> tuple[int, int, int, int, int]: """Return TCSPC histogram shape normalized to 5D 'TYXCH'.""" ndim = len(shape) if ndim == 5: 'TYXCH' return shape # type: ignore[return-value] if ndim == 3: # 'YXH' return (1, shape[0], shape[1], 1, shape[2]) if ndim == 4: if has_frames: # 'TYXH' return (shape[0], shape[1], shape[2], 1, shape[3]) # 'YXCH' return (1, shape[0], shape[1], shape[2], shape[3]) msg = f'invalid number of dimensions {len(shape)=}' raise ValueError(msg) class PqFileType(enum.Enum): """PicoQuant file type identifiers.""" PTU = b'PQTTTR\0\0' """TTTR file, PTU, contains raw data in unified TTTR-format.""" PHU = b'PQHISTO\0' """Histogram file, PHU, contains TCSPC histograms.""" PCK = b'PQCHECK\0' """Internal file, PCK, contains post-acquisition analysis results.""" PCO = b'PQCOMNT\0' """Comment file, PCO, contains manually entered text.""" PFS = b'PQDEFLT\0' """Settings file, PFS or PUS, contains factory or user setting defaults.""" PQRES = b'PQRESLT\0' """Result file, PQRES, contains analysis generated during measurement.""" PQDAT = b'PQDATA\0\0' """Data file, PQDAT, contains undocumented data.""" PQUNI = b'PQUNI\0\0\0' """UniHarp file, PQUNI, contains memory and measured data.""" PQOVER = b'PQOVER\0\0' """Unknown file, PQOVER, contains undocumented data.""" SPQR = b'PQSPQR\0\0' """Unknown file, SPQR, contains undocumented data.""" class PqFileError(ValueError): """Exception to indicate invalid PicoQuant tagged file structure.""" class BinaryFile: """Binary file. Parameters: file: File name or seekable binary stream. mode: File open mode if ``file`` is a file name. If not specified, defaults to ``'r'``. Files are always opened in binary mode. memmap: Map file into memory. Ignored if memory mapping is not available or if the stream does not support it. Notes: Memory mapping can improve random-access read performance on large files or repeated reads of the same file regions by reducing syscall overhead and data copying. For sequential one-pass reads, regular buffered I/O may be faster. Raises: TypeError: File is a text stream, or an unsupported type. ValueError: Invalid file name, extension, or stream. File stream is not seekable. """ _fh: IO[bytes] _mm: mmap.mmap | None _mv: memoryview | None # view of _mm _path: str # absolute path of file _name: str # name of file or handle _close: bool # file needs to be closed _closed: bool # file is closed _memmap: bool # open companion files using memmap _lock: contextlib.AbstractContextManager[Any] _ext: ClassVar[set[str]] = set() # valid extensions, empty for any def __init__( self, file: str | os.PathLike[str] | IO[bytes], /, *, mode: Literal['r', 'r+'] | None = None, memmap: bool = False, ) -> None: self._mm = None self._mv = None self._path = '' self._name = 'Unnamed' self._close = False self._closed = False self._memmap = bool(memmap) self._lock = contextlib.nullcontext() if isinstance(file, (str, os.PathLike)): ext = os.path.splitext(file)[-1].lower() if self._ext and ext not in self._ext: msg = f'invalid file extension: {ext!r} not in {self._ext!r}' raise ValueError(msg) if mode is None: mode = 'r' else: if mode[-1:] == 'b': # accept 'rb'/'r+b' mode = mode[:-1] # type: ignore[assignment] if mode not in {'r', 'r+'}: msg = f'invalid {mode=!r}' raise ValueError(msg) self._path = os.path.abspath(file) self._close = True self._fh = open(self._path, mode + 'b') # noqa: SIM115 elif hasattr(file, 'seek'): # binary stream: open file, BytesIO, fsspec LocalFileOpener if isinstance(file, io.TextIOBase): # type: ignore[unreachable] msg = ( # type: ignore[unreachable] f'{file=!r} is not open in binary mode' ) raise TypeError(msg) self._fh = file try: self._fh.tell() except Exception as exc: msg = f'{file=!r} is not seekable' raise ValueError(msg) from exc if hasattr(file, 'path'): self._path = os.path.abspath(file.path) elif hasattr(file, 'name'): self._path = os.path.abspath(file.name) elif hasattr(file, 'open'): # fsspec OpenFile self._fh = file.open() self._close = True try: self._fh.tell() except Exception as exc: with contextlib.suppress(Exception): self._fh.close() msg = f'{file=!r} is not seekable' raise ValueError(msg) from exc if hasattr(file, 'path'): self._path = os.path.abspath(file.path) else: msg = f'cannot handle {type(file)=}' raise TypeError(msg) if hasattr(file, 'name') and file.name: self._name = os.path.basename(file.name) elif self._path: self._name = os.path.basename(self._path) else: self._name = type(file).__name__ if memmap: _fh: Any = self._fh if isinstance(_fh, mmap.mmap): self._mm = _fh self._mv = memoryview(self._mm) else: try: access = ( mmap.ACCESS_WRITE if self._fh.writable() else mmap.ACCESS_READ ) self._mm = mmap.mmap(self._fh.fileno(), 0, access=access) self._mv = memoryview(self._mm) except OSError: pass @property def filehandle(self) -> IO[bytes]: """File handle.""" return self._fh @property def filepath(self) -> str: """Absolute path to file, or empty string if no path is available.""" return self._path @property def filename(self) -> str: """Basename of file path, or empty string if no path is available.""" return os.path.basename(self._path) @property def dirname(self) -> str: """Directory containing file, or empty string if no path available.""" return os.path.dirname(self._path) @property def name(self) -> str: """Display name of file.""" return self._name @name.setter def name(self, value: str) -> None: self._name = value @property def attrs(self) -> dict[str, Any]: """Selected metadata as dict.""" return {'name': self.name, 'filepath': self.filepath} @property def lock(self) -> contextlib.AbstractContextManager[Any]: """Lock for thread-safe file access.""" return self._lock def set_lock(self, enabled: bool, /) -> None: # noqa: FBT001 """Enable or disable thread-safe file access. Parameters: enabled: If true, use a threading.RLock, else a no-op lock. Has no effect when memory-mapped I/O is active. """ if self._mm is not None: return self._lock = threading.RLock() if enabled else contextlib.nullcontext() def _write_at( self, offset: int, data: bytes | bytearray | memoryview, / ) -> None: """Write bytes to file at given offset. Parameters: offset: Byte offset from start of file. data: Data to write. """ if self._mm is not None and self.writable: # writable mmap: direct slice write, no cursor movement self._mm[offset : offset + len(data)] = data else: with self._lock: self._fh.seek(offset) self._fh.write(data) def _read_at(self, offset: int, size: int, /) -> bytes | memoryview: """Read bytes from file at given offset. For memory-mapped files, returned bytes are exposed as a ``memoryview`` of the mapping. Keeping that view alive may keep the memory map (and associated file resources/lock) alive. Parameters: offset: Byte offset from start of file. size: Number of bytes to read. """ mv = self._mv if mv is not None: return mv[offset : offset + size] fh = self._fh with self._lock: fh.seek(offset) return fh.read(size) def _read_into(self, offset: int, buffer: NDArray[numpy.uint8], /) -> int: """Read bytes from file at given offset into existing buffer. Parameters: offset: Byte offset from start of file. buffer: Flat, writable uint8 numpy array to read into. Returns: Number of bytes read. """ nbytes = len(buffer) mv = self._mv if mv is not None: n = min(nbytes, max(0, len(mv) - offset)) buffer[:n] = numpy.frombuffer(mv[offset : offset + n], numpy.uint8) return n fh = self._fh with self._lock: fh.seek(offset) try: return fh.readinto(buffer) # type: ignore[attr-defined, no-any-return] except (AttributeError, OSError): data = fh.read(nbytes) buffer[:] = numpy.frombuffer(data, numpy.uint8) return len(data) def _read_array( self, offset: int, count: int, dtype: DTypeLike, *, copy: bool = False, writable: bool = False, truncate: bool | None = False, ) -> NDArray[Any]: """Read numpy array from file at given offset. For memory-mapped files, returned data are exposed directly as a NumPy array view of the mapping (zero copy). Keeping that array alive may keep the memory map (and associated file resources/lock) alive. Parameters: offset: Byte offset from start of file. count: Number of elements to read. If ``-1``, read to end of file. dtype: Array element type. copy: If true, always return a detached copy in main memory. For memory-mapped files, bypass the direct-view fast path. writable: By default, return read-only array from memory-mapped file. Prevents accidental modification of underlying writable file. Has no effect for non-memory-mapped files (always writable). truncate: Allow partial reads of array. If None, log error on partial read. """ dtype = numpy.dtype(dtype) itemsize = dtype.itemsize if offset < 0: msg = f'{offset=} < 0' raise ValueError(msg) if count < -1: msg = f'{count=} < -1' raise ValueError(msg) mv = self._mv if mv is not None: if count == -1: count = max(0, (len(mv) - offset) // itemsize) nbytes = count * itemsize size = min(nbytes, max(0, len(mv) - offset)) n = size - size % itemsize array = numpy.frombuffer( mv[offset : offset + n], dtype, ) if copy: array = array.copy() elif not writable and array.flags.writeable: array.flags.writeable = False elif count > -1: fh = self._fh nbytes = count * itemsize array = numpy.empty(count, dtype) with self._lock: fh.seek(offset) n = fh.readinto(array.data) # type: ignore[attr-defined] else: fh = self._fh with self._lock: fh.seek(0, os.SEEK_END) size = fh.tell() count = max(0, (size - offset) // itemsize) nbytes = count * itemsize array = numpy.empty(count, dtype) fh.seek(offset) n = fh.readinto(array.data) # type: ignore[attr-defined] if n != nbytes: array = array[: n // itemsize] msg = f'expected {count} items, got {n // itemsize}' if truncate is None: logging.getLogger(__name__.split('.', 1)[0]).error(msg) elif not truncate: raise ValueError(msg) return array @cached_property def filesize(self) -> int: """Size of file in bytes.""" if self._mm is not None: return len(self._mm) fh = self._fh try: return os.fstat(fh.fileno()).st_size except (AttributeError, io.UnsupportedOperation, OSError): pass with self._lock: pos = fh.tell() fh.seek(0, os.SEEK_END) size = fh.tell() fh.seek(pos) return size @property def writable(self) -> bool: """File is open for writing.""" return self._fh.writable() @property def memmapped(self) -> bool: """File is memory-mapped.""" return self._mm is not None @property def closed(self) -> bool: """File is closed.""" return self._closed def close(self) -> None: """Close file.""" self._closed = True # always report file as closed self._mv = None # do not release(), threads may hold local refs if self._mm is not None and self._mm is not self._fh: # type: ignore[comparison-overlap] with contextlib.suppress(Exception): self._mm.close() if self._close: with contextlib.suppress(Exception): self._fh.close() def __enter__(self) -> Self: return self def __exit__( self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, ) -> None: self.close() def __repr__(self) -> str: return f'<{self.__class__.__name__} {self._name!r}>' class PqFile(BinaryFile): """PicoQuant unified tagged file. PTU, PHU, PCK, PCO, PFS, PUS, PQRES, PQDAT, PQUNI, and SPQR files contain measurement metadata and settings encoded as unified tags. ``PqFile`` and subclass instances are not thread safe. All attributes are read-only. ``PqFile`` and subclass instances must be closed with :py:meth:`PqFile.close`, which is automatically called when using the 'with' context manager. Parameters: file: File name or seekable binary stream. mode: File open mode if ``file`` is file name. The default is ``'r'``. Files are always opened in binary mode. memmap: Map file into memory. Ignored if memory mapping is not available or if the stream does not support it. fastload: If true, only read tags marked for fast loading, else read all tags. Raises: PqFileError: File is not a PicoQuant tagged file or is corrupted. """ type: PqFileType """PicoQuant file type identifier.""" version: str """File version.""" tags: dict[str, Any] """PicoQuant unified tags.""" _data_offset: int # position of raw data in file _number_records_offset: int # position of TTResult_NumberOfRecords value _TYPE: ClassVar[set[PqFileType]] = set(PqFileType) _STR_: tuple[str, ...] = ('type', 'version') # attributes listed first def __init__( self, file: str | os.PathLike[str] | IO[bytes], /, *, mode: Literal['r', 'r+'] | None = None, memmap: bool = False, fastload: bool = False, ) -> None: super().__init__(file, mode=mode, memmap=memmap) self.version = '' self.tags = {} try: self._init(fastload=fastload) except Exception: self.close() raise def _init(self, *, fastload: bool) -> None: header = bytes(self._read_at(0, 16)) offset = 16 try: self.type = PqFileType(header[:8]) except ValueError as exc: msg = ( f'{self.filename!r} is not a {self.__class__.__name__} ' f'{header[:8]!r}' ) raise PqFileError(msg) from exc if self.type not in self._TYPE: msg = ( f'{self.filename!r} type={self.type} is not in {self._TYPE!r}' ) raise PqFileError(msg) self.version = header[8:].strip(b'\0').decode() tags = self.tags def errmsg( msg: str, tagid: str, index: int, typecode: int, value: Any ) -> str: return ( f'{msg} @ {self.name!r} ' f'{tagid=}, {index=}, {typecode=}, {value=!r}' )[:160] tagid: str index: int typecode: int value: Any unpack = struct.unpack try: while True: tagid_, index, typecode, value = struct.unpack_from( '<32siI8s', self._read_at(offset, 48) ) offset += 48 # print(tagid.strip(b'\0'), index, typecode, value) tagid = tagid_.rstrip(b'\0').decode('ascii', errors='ignore') # tags must start on positions divisible by 8 # disabled for PQRES and PQDAT if offset % 8 and self.type not in { PqFileType.PQRES, PqFileType.PQDAT, }: logger().error( errmsg( f'tag offset={offset - 48} not divisible by 8', tagid, index, typecode, value, ) ) if tagid == 'Header_End': break if tagid == 'Fast_Load_End': if fastload: break continue match typecode: # frequent typecodes case PqTagType.Int8: value = unpack(' uuid.UUID: """Global identifier of file.""" return uuid.UUID(self.tags['File_GUID']) @property def comment(self) -> str | None: """File comment, if any.""" return self.tags.get('File_Comment') @property def datetime(self) -> datetime | None: """File creation date, if any.""" if 'File_CreatingTime' not in self.tags: return None value = self.tags['File_CreatingTime'] if isinstance(value, datetime): return value if isinstance(value, str): try: return datetime.fromisoformat(value) except ValueError: try: from dateutil import parser return parser.parse(value) except Exception: return None if not isinstance(value, float): return None try: return datetime(1899, 12, 30) + timedelta(days=value) except Exception: return None @override @property def attrs(self) -> dict[str, Any]: """Selected metadata as dict.""" return { **super().attrs, 'type': self.type.name, 'version': self.version, 'guid': str(self.guid), 'comment': self.comment, 'datetime': ( None if self.datetime is None else self.datetime.isoformat() ), 'tags': self.tags, } @override def __enter__(self) -> Self: return self def __str__(self) -> str: return indent( repr(self), *( f'{name}: {getattr(self, name)!r}'[:160] for name in self._STR_ if getattr(self, name) is not None ), *( f'{name}: {getattr(self, name)!r}'[:160] for name in dir(self) if not ( name in self._STR_ or name in {'attrs', 'dirname', 'tags', 'name', 'filehandle'} or name.startswith('_') or callable(getattr(self, name)) ) ), indent( 'tags:', *( f'{key}: {value!r}'[:160] for key, value in self.tags.items() ), ), ) @final class PhuFile(PqFile): """PicoQuant histogram file. PHU files contain a series of TCSPC histograms in addition to unified tags. ``PhuFile`` instances are derived from :py:class:`PqFile`. Parameters: file: File name or seekable binary stream. mode: File open mode if ``file`` is file name. The default is ``'r'``. Files are always opened in binary mode. memmap: Map file into memory. Ignored if memory mapping is not available or if the stream does not support it. Raises: PqFileError: File is not a PicoQuant PHU file or is corrupted. """ _TYPE: ClassVar[set[PqFileType]] = {PqFileType.PHU} _STR_ = ('type', 'version', 'measurement_mode', 'measurement_submode') def __init__( self, file: str | os.PathLike[str] | IO[bytes], /, *, mode: Literal['r', 'r+'] | None = None, memmap: bool = False, ) -> None: super().__init__(file, mode=mode, memmap=memmap) @override def __enter__(self) -> Self: return self @property def measurement_mode(self) -> PhuMeasurementMode: """Kind of measurement: HISTOGRAM or CONTI.""" return PhuMeasurementMode(self.tags['Measurement_Mode']) @property def measurement_submode(self) -> PhuMeasurementSubMode: """Sub-kind of measurement: OSCILLOSCOPE, INTEGRATING, or TRES.""" return PhuMeasurementSubMode(self.tags['Measurement_SubMode']) @property def tcspc_resolution(self) -> float: """Resolution of TCSPC in s (BaseResolution * iBinningFactor).""" return float(self.tags.get('MeasDesc_Resolution', 0.0)) @property def histogram_resolutions(self) -> tuple[float, ...] | None: """Base resolution for each histogram.""" ncurves = self.number_histograms if 'HistResDscr_HWBaseResolution' in self.tags: return tuple(self.tags['HistResDscr_HWBaseResolution']) if 'HW_BaseResolution' in self.tags: return tuple([float(self.tags['HW_BaseResolution'])] * ncurves) return None @property def number_histograms(self) -> int: """Number of histograms stored in file.""" return int(self.tags['HistoResult_NumberOfCurves']) # @property # def bits_per_bin(self) -> int: # """Number of bits per histogram bin.""" # return int(self.tags.get('HistoResult_BitsPerBin', 32)) @override @property def attrs(self) -> dict[str, Any]: """Selected metadata as dict.""" return { **super().attrs, 'measurement_mode': self.measurement_mode.name, 'measurement_submode': self.measurement_submode.name, 'tcspc_resolution': self.tcspc_resolution, } @overload def histograms( self, index: int | slice | None = None, /, *, asxarray: Literal[False] = ..., ) -> tuple[NDArray[numpy.uint32], ...]: ... @overload def histograms( self, index: int | slice | None = None, /, *, asxarray: Literal[True] = ..., ) -> tuple[DataArray, ...]: ... @overload def histograms( self, index: int | slice | None = None, /, *, asxarray: bool = ..., ) -> tuple[NDArray[numpy.uint32] | DataArray, ...]: ... def histograms( self, index: int | slice | None = None, /, *, asxarray: bool = False ) -> tuple[NDArray[numpy.uint32] | DataArray, ...]: """Return sequences of histograms from file. Parameters: index: Index of histogram(s) to return. By default, all histograms are returned. asxarray: If true, return histograms as ``xarray.DataArray``, else ``numpy.ndarray`` (default). """ if index is None: index = slice(None) elif isinstance(index, int): index = slice(index, index + 1) ncurves = self.number_histograms if len(self.tags['HistResDscr_DataOffset']) != ncurves: msg = 'invalid HistResDscr_DataOffset tag' raise ValueError(msg) if len(self.tags['HistResDscr_HistogramBins']) != ncurves: msg = 'invalid HistResDscr_HistogramBins tag' raise ValueError(msg) histograms: list[NDArray[numpy.uint32] | DataArray] = [] for offset, nbins in zip( self.tags['HistResDscr_DataOffset'][index], self.tags['HistResDscr_HistogramBins'][index], strict=True, ): hist = self._read_array( offset, nbins, dtype=numpy.uint32, copy=False, writable=False, truncate=None, ) histograms.append(hist) if asxarray: from xarray import DataArray assert self.histogram_resolutions is not None histograms = [ DataArray( h, dims=('H',), coords={ # TODO: is this correct? 'H': numpy.linspace( 0, h.size * r, h.size, endpoint=False ) }, attrs=self.attrs, # name=self.name, ) for h, r in zip( histograms, self.histogram_resolutions, strict=True ) ] # TODO: do not return tuple if index is integer? return tuple(histograms) def plot(self, *, verbose: bool = False, show: bool = True) -> None: """Plot histograms using matplotlib. Parameters: verbose: Print information about histogram arrays. show: If true (default), display all figures. Else, defer to user or environment to display figures. """ from matplotlib import pyplot from tifffile import Timer t = Timer() histograms = self.histograms(asxarray=True) if verbose: t.print('decode histograms') print() for hist in histograms: print(hist) for i, hist in enumerate(histograms): y = numpy.trim_zeros(hist.values, trim='b') x = hist.coords['H'].values[: y.size] pyplot.plot(x, y, label=f'ch {i}') pyplot.title(repr(self)) pyplot.xlabel('delay time [s]') pyplot.ylabel('photon count') pyplot.legend() if show: pyplot.show() @final class PtuFile(PqFile): """PicoQuant time-tagged time-resolved (TTTR) file. PTU files contain TTTR records in addition to unified tags. ``PtuFile`` instances are not thread-safe. All attributes are read-only. ``PtuFile`` is derived from :py:class:`PqFile`. Parameters: file: File name or seekable binary stream. mode: File open mode if ``file`` is file name. The default is ``'r'``. Files are always opened in binary mode. memmap: Map file into memory. Ignored if memory mapping is not available or if the stream does not support it. trimdims: Axes to trim. The default is ``'TCH'``: - ``'T'``: remove incomplete first or last frame. - ``'C'``: remove leading and trailing channels without photons. Else use record type's default :py:attr:`number_channels_max`. - ``'H'``: remove trailing delay-time bins without photons. Else use record type's default :py:attr:`number_bins_max`. Raises: PqFileError: File is not a PicoQuant PTU file or is corrupted. """ _trimdims: set[str] _cache: bool # cache records in memory _asxarray: bool # return DataArray from slicing and decode_ functions _dtype: numpy.dtype[Any] _records: NDArray[numpy.uint32] | None # cached records _TYPE: ClassVar[set[PqFileType]] = {PqFileType.PTU} _STR_ = ( 'type', 'version', 'record_type', 'measurement_mode', 'measurement_submode', 'scanner', 'sizes', ) def __init__( self, file: str | os.PathLike[str] | IO[bytes], /, *, mode: Literal['r', 'r+'] | None = None, memmap: bool = False, trimdims: Sequence[Dimension] | str | None = None, ) -> None: self._records = None super().__init__(file, mode=mode, memmap=memmap) try: if trimdims is None: self._trimdims = {'T', 'C', 'H'} else: self._trimdims = {ax.upper() for ax in trimdims} self._dtype = numpy.dtype(numpy.uint16) self._asxarray = False self._cache = not memmap # memmap takes care of caching except Exception: self.close() raise @override def __enter__(self) -> Self: return self def __getitem__(self, key: Any, /) -> NDArray[Any] | DataArray: return self.decode_image(key, keepdims=False) @override def close(self) -> None: """Close file handle and free resources.""" del self._records # close numpy.memmap file handle self._records = None super().close() @property def record_offset(self) -> int: """Position of records in file.""" return self._data_offset @property def record_type(self) -> PtuRecordType: """Type of TTTR records. Defines the TCSPC device and type of measurement that produced the records. """ return PtuRecordType(self.tags['TTResultFormat_TTTRRecType']) @property def measurement_mode(self) -> PtuMeasurementMode: """Kind of TCSPC measurement: T2 or T3.""" return PtuMeasurementMode(self.tags['Measurement_Mode']) @property def measurement_submode(self) -> PtuMeasurementSubMode: """Sub-kind of measurement: Point, line, or image scan.""" return PtuMeasurementSubMode(self.tags['Measurement_SubMode']) @property def measurement_ndim(self) -> int: """Dimensionality of measurement.""" # Measurement_SubMode is not always correct submode = self.tags['Measurement_SubMode'] if ( submode == 3 and self.tags.get('ImgHdr_Dimensions', 3) == 3 # optional # and self.tags.get('ImgHdr_PixY', 1) > 1 # may be missing ): return 3 if ( submode == 2 and self.tags.get('ImgHdr_Dimensions', 2) == 2 # optional # and self.tags.get('ImgHdr_PixX', 1) > 1 # may be missing ): # TODO: need linescan test file return 2 return 1 @property def measurement_warnings(self) -> PtuMeasurementWarnings | None: """Warnings during measurement, or None if not specified.""" if 'TTResult_MDescWarningFlags' in self.tags: return PtuMeasurementWarnings( self.tags['TTResult_MDescWarningFlags'] ) return None @property def hardware_features(self) -> PtuHwFeatures | None: """Hardware features, or None if not specified.""" if 'HW_Features' in self.tags: return PtuHwFeatures(self.tags['HW_Features']) return None @property def stop_reason(self) -> PtuStopReason | None: """Reason for measurement end, or None if not specified.""" if 'TTResult_StopReason' in self.tags: return PtuStopReason(self.tags['TTResult_StopReason']) return None @property def scanner(self) -> PtuScannerType | None: """Scanner hardware, or None if not specified.""" if 'ImgHdr_Ident' in self.tags: return PtuScannerType(self.tags['ImgHdr_Ident']) return None @property def global_resolution(self) -> float: """Resolution of time tags in s.""" return float(self.tags['MeasDesc_GlobalResolution']) @property def tcspc_resolution(self) -> float: """Resolution of TCSPC in s (BaseResolution * iBinningFactor).""" return float(self.tags.get('MeasDesc_Resolution', 0.0)) @cached_property def number_records(self) -> int: """Number of TTTR records.""" # NOTE: this does not catch invalid TTResult_NumberOfRecords > 0 count = value = int(self.tags.get('TTResult_NumberOfRecords', 0)) if count <= 0: count = (self.filesize - self._data_offset) // 4 if count != 0: logger().warning( f'{self!r} invalid TTResult_NumberOfRecords={value}. ' 'Using remaining file content as records' ) return count @property def number_photons(self) -> int: """Number of photons counted.""" return self._info.photons @property def number_markers(self) -> int: """Number of marker events.""" return self._info.markers @property def number_images(self) -> int: """Number of images separated by frame change markers.""" return self._info.frames @property def number_lines(self) -> int: """Number of line marker pairs.""" return self._info.lines @property def number_channels_max(self) -> int: """Maximum number of channels for record type.""" return self._info.channels @property def number_channels(self) -> int: """Number of channels, without leading and trailing empty channels.""" return ( 1 + self._info.channels_active_last - self._info.channels_active_first ) @property def active_channels(self) -> tuple[int, ...]: """Indices of un-trimmed channels containing photons.""" channels_active = self._info.channels_active return tuple( ch for ch in range(self._info.channels) if channels_active & (1 << ch) ) @property def number_bins_max(self) -> int: """Maximum number of delay-time bins for record type.""" return self._info.bins @property def number_bins(self) -> int: """Number of bins up to the largest occupied delay-time bin. Not available for T2 records. """ return self._info.bins_used @property def number_bins_in_period(self) -> int: """Delay time in one period. Not available for T2 records. Same as ``global_resolution / tcspc_resolution`` """ if self.tcspc_resolution < 1e-14: return 1 nbins = math.floor(self.global_resolution / self.tcspc_resolution) return max(nbins, 1) @property def line_start_mask(self) -> int: """Marker mask defining line start, or 0 if not defined.""" value = self.tags.get('ImgHdr_LineStart', None) return int(2 ** (value - 1) if value is not None else 0) @property def line_stop_mask(self) -> int: """Marker mask defining line end, or 0 if not defined.""" value = self.tags.get('ImgHdr_LineStop', None) return int(2 ** (value - 1) if value is not None else 0) @property def frame_change_mask(self) -> int: """Marker mask defining image frame change, or 0 if not defined.""" value = self.tags.get('ImgHdr_Frame', None) return int(2 ** (value - 1) if value is not None else 0) @property def global_pixel_time(self) -> int: """Global time per pixel. Multiply with global resolution to get time in s. """ if self.tags.get('ImgHdr_TimePerPixel', 0.0) > 0.0: pixeltime = ( float(self.tags['ImgHdr_TimePerPixel']) / float(self.tags['MeasDesc_GlobalResolution']) / 1e3 ) elif self._info.lines > 0: pixeltime = self._info.line_time / self.pixels_in_line else: pixeltime = 1e-3 / float(self.tags['MeasDesc_GlobalResolution']) return max(1, round(pixeltime)) @property def global_line_time(self) -> int: """Global time per line, excluding retrace. Might be approximate. Multiply with global resolution to get time in s. """ if 'ImgHdr_TimePerPixel' in self.tags: linetime = self.pixels_in_line * self.global_pixel_time elif self._info.lines > 0: linetime = self._info.line_time else: # point scan: line of one pixel linetime = 1e-3 / self.tags['MeasDesc_GlobalResolution'] return round(linetime) @property def global_frame_time(self) -> int: """Global time per image, line, or point scan cycle. Multiply with global resolution to get time in s. """ if self.tags['Measurement_SubMode'] == 3: # image, including retrace if self._info.frames == 0: return self._info.acquisition_time return self._info.acquisition_time // self._info.frames if self._info.lines > 0: # line scan return self._info.line_time # point scan return self.pixels_in_frame * self.global_pixel_time @property def global_acquisition_time(self) -> int: """Global time of acquisition.""" # MeasDesc_AcquisitionTime not reliable # aqt = self.tags.get('MeasDesc_AcquisitionTime', 0.0) # if aqt > 0: # return round(1e-3 * aqt / self.global_resolution) return self._info.acquisition_time @property def pixels_in_frame(self) -> int: """Number of pixels in one scan cycle.""" return self.lines_in_frame * self.pixels_in_line @property def pixels_in_line(self) -> int: """Number of pixels in line.""" ndim = self.measurement_ndim if ndim == 3: # image pixels = self.tags['ImgHdr_PixX'] elif ndim == 2: # line scan time_per_pixel = self.tags['ImgHdr_TimePerPixel'] line_frequency = self.tags['ImgHdr_LineFrequency'] if time_per_pixel > 0.0 and line_frequency > 0.0: pixels = round(1e-3 / (time_per_pixel * line_frequency)) else: pixels = 1 else: pixels = 1 return max(1, int(pixels)) @property def lines_in_frame(self) -> int: """Number of lines in frame.""" if self.measurement_ndim == 3: # TODO: Warning from the PicoQuant documentation: # Attention, in some images one will find a different number # of lines than defined by PixY (less or more, even different # in every frame), so do not trust this value. return max(1, int(self.tags['ImgHdr_PixY'])) return 1 @property def pixel_time(self) -> float: """Time per pixel in s.""" pixel_time = float(self.tags.get('ImgHdr_TimePerPixel', 0.0)) if pixel_time > 0.0: return pixel_time * 1e-3 # ms to s return self.global_pixel_time * self.global_resolution @property def line_time(self) -> float: """Average time between line markers or ``pixel_time`` in s.""" return self.global_line_time * self.global_resolution @property def frame_time(self) -> float: """Time per image, line, or point scan cycle in s. Image scan times include retrace. """ return self.global_frame_time * self.global_resolution @property def acquisition_time(self) -> float: """Duration of acquisition in s.""" # MeasDesc_AcquisitionTime not reliable # if 'MeasDesc_AcquisitionTime' in self.tags: # return self.tags['MeasDesc_AcquisitionTime'] * 1e-3 return self._info.acquisition_time * self.global_resolution @property def frequency(self) -> float: """Repetition frequency in Hz. The inverse of :py:attr:`PtuFile.global_resolution`. """ period = float(self.tags.get('MeasDesc_GlobalResolution', 0.0)) return 1.0 / period if period > 1e-14 else 0.0 @property def syncrate(self) -> int: """Sync events per s as recorded at beginning of measurement.""" return int(self.tags['TTResult_SyncRate']) @property def is_image(self) -> bool: """File contains image data.""" return ( self.tags['Measurement_SubMode'] == 3 and self.tags.get('ImgHdr_Dimensions', 1) == 3 and 'ImgHdr_PixX' in self.tags # some Leica PTU are missing this ) @property def is_t3(self) -> bool: """File contains T3 records.""" return bool(self.tags['Measurement_Mode'] == 3) # return self.tags['TTResultFormat_TTTRRecType'] in { # 0x00010303, 0x00010304, 0x01010304, # 0x00010305, 0x00010306, 0x00010307, # } @property def is_bidirectional(self) -> bool: """Bidirectional scan mode.""" return bool(self.tags.get('ImgHdr_BiDirect', 0) > 0) @property def is_sinusoidal(self) -> bool: """Sinusoidal scan mode.""" return bool(self.tags.get('ImgHdr_SinCorrection', 0) != 0) @property def use_xarray(self) -> bool: """Slicing and decode methods return ``xarray.DataArray``.""" return self._asxarray @use_xarray.setter def use_xarray(self, value: bool, /) -> None: self._asxarray = bool(value) @property def cache_records(self) -> bool: """Cache records read from file in memory.""" return self._cache @cache_records.setter def cache_records(self, value: bool, /) -> None: self._cache = bool(value) if not self._cache: del self._records # close numpy.memmap file handle self._records = None @property def dtype(self) -> numpy.dtype[Any]: """Data type of image histogram array.""" return self._dtype @dtype.setter def dtype(self, dtype: DTypeLike | None, /) -> None: dtype = numpy.dtype('uint16' if dtype is None else dtype) if dtype.kind != 'u': msg = f'{dtype=!r} not an unsigned integer' raise ValueError(msg) self._dtype = dtype @cached_property def shape(self) -> tuple[int, ...]: """Shape of image histogram array.""" if not self.is_t3: return () if 'C' in self._trimdims: nchannels = max(self.number_channels, 1) else: nchannels = self.number_channels_max if 'H' in self._trimdims: nbins = self.number_bins else: nbins = self.number_bins_max ndim = self.measurement_ndim if ndim == 3: return ( max(self._info.frames, 1), self.lines_in_frame, self.pixels_in_line, nchannels, nbins, ) if ndim == 2: return ( max(self._info.lines, 1), self.pixels_in_line, nchannels, nbins, ) if ndim in {0, 1}: return ( max(1, self._info.photons // self.global_pixel_time), nchannels, nbins, ) return () @cached_property def dims(self) -> tuple[str, ...]: """Axes labels for each dimension in image histogram array.""" if not self.shape: return () ndim = self.measurement_ndim if ndim == 3: return ('T', 'Y', 'X', 'C', 'H') if ndim == 2: return ('T', 'X', 'C', 'H') return ('T', 'C', 'H') @property def sizes(self) -> dict[str, int]: """Map dimension names to lengths.""" return dict(zip(self.dims, self.shape, strict=True)) @property def ndims(self) -> int: """Number of dimensions in image histogram array.""" return len(self.dims) @property def nbytes(self) -> int: """Number of bytes consumed by image histogram.""" size = 1 for i in self.shape: size *= int(i) return size * self._dtype.itemsize @property def size(self) -> int: """Number of elements in image histogram.""" size = 1 for i in self.shape: size *= int(i) return size @property def itemsize(self) -> int: """Length of one array element in image histogram in bytes.""" return self._dtype.itemsize @property def _coords_c(self) -> NDArray[Any]: """Coordinate array labelling all channels.""" if 'C' in self._trimdims: return numpy.arange( self._info.channels_active_first, self._info.channels_active_last + 1, dtype=numpy.uint8, ) return numpy.arange(self._info.channels, dtype=numpy.uint8) @property def _coords_h(self) -> NDArray[Any]: """Coordinate array labelling all delay-time bins.""" if 'H' in self._trimdims: nbins = self.number_bins else: nbins = self.number_bins_max return numpy.linspace( 0, nbins * self.tags['MeasDesc_Resolution'], nbins, endpoint=False ) @cached_property def coords(self) -> dict[str, NDArray[Any]]: """Coordinate arrays labelling each point in image histogram array. Coordinates for the time axis are approximate. Exact coordinates are returned with :py:meth:`PtuFile.decode_image` as ``xarray.DataArray``. """ if not self.shape: return {} ndim = self.measurement_ndim coords = {} shape = self.shape # exact time coordinates must be decoded from records coords['T'] = numpy.linspace( 0, shape[0] * self.frame_time, shape[0], endpoint=False ) res = self.tags.get('ImgHdr_PixResol', None) if res is not None: res *= 1e-6 # um if ndim > 2: offset = self.tags.get('ImgHdr_Y0', 0.0) * 1e-6 # um coords['Y'] = numpy.linspace( offset, offset + shape[-4] * res, shape[-4], endpoint=False ) if ndim > 1: offset = self.tags.get('ImgHdr_X0', 0.0) * 1e-6 coords['X'] = numpy.linspace( offset, offset + shape[-3] * res, shape[-3], endpoint=False ) coords['C'] = self._coords_c coords['H'] = self._coords_h return coords @override @property def attrs(self) -> dict[str, Any]: """Selected metadata as dict.""" return { **super().attrs, 'acquisition_time': self.acquisition_time, 'active_channels': self.active_channels, 'frame_time': self.frame_time, 'frequency': self.frequency, 'global_acquisition_time': self.global_acquisition_time, 'global_frame_time': self.global_frame_time, 'global_line_time': self.global_line_time, 'global_pixel_time': self.global_pixel_time, 'global_resolution': self.global_resolution, 'line_time': self.line_time, 'max_delaytime': self.number_bins_max, # for PhasorPy 'measurement_mode': self.measurement_mode.name, 'measurement_submode': self.measurement_submode.name, 'number_bins': self.number_bins, 'number_bins_in_period': self.number_bins_in_period, 'number_bins_max': self.number_bins_max, 'pixel_time': self.pixel_time, 'record_type': self.record_type.name, 'scanner': None if self.scanner is None else self.scanner.name, 'syncrate': self.syncrate, 'tcspc_resolution': self.tcspc_resolution, } @cached_property def _info(self) -> PtuInfo: """Information about decoded records.""" from ._ptufile import decode_info lines_in_frame = 0 if ( 'T' in self._trimdims and 'ImgHdr_PixY' in self.tags and self.tags['Measurement_SubMode'] == 3 ): lines_in_frame = max(1, self.tags['ImgHdr_PixY']) return PtuInfo( *decode_info( self.read_records(copy=False), self.tags['TTResultFormat_TTTRRecType'], self.line_start_mask, self.line_stop_mask, self.frame_change_mask, lines_in_frame, ) ) def read_records(self, *, copy: bool = True) -> NDArray[numpy.uint32]: """Return encoded TTTR records from file. If copy is false for a memory-mapped file, records are exposed directly as a NumPy array view of the mapping. Keeping that array alive may keep the memory map (and associated file resources/lock) alive. Records are cached according to :py:attr:`PtuFile.cache_records`: disabled by default for memory-mapped files, enabled otherwise. Parameters: copy: If true, always return a detached copy in main memory. This avoids cache corruption and returning arrays that reference memory-mapped resources. """ if self._cache and self._records is not None: if copy: return self._records.copy() return self._records if self.tags['TTResultFormat_BitsPerRecord'] not in {0, 32}: msg = f"invalid {self.tags['TTResultFormat_BitsPerRecord']=}" raise ValueError(msg) records = self._read_array( self._data_offset, self.number_records, numpy.uint32, copy=copy, truncate=None, # log errors on partial reads ) if self._cache: self._records = records if copy: return records.copy() return records def decode_records( self, records: NDArray[numpy.uint32] | None = None, /, *, out: OutputType = None, ) -> NDArray[Any]: """Return decoded TTTR records. Parameters: records: Encoded TTTR records. By default, read records from file. out: Array where decoded records are stored. If ``None``, create a new NumPy recarray in main memory. If ``'memmap'``, create a memory-mapped recarray in a temporary file. If a ``numpy.ndarray``, a writable recarray of compatible shape and dtype. If a ``file name`` or ``open file``, create a memory-mapped array in the specified file. Returns: : ``numpy.recarray`` of size :py:attr:`number_records` and dtype :py:attr:`T3_RECORD_DTYPE` or :py:attr:`T2_RECORD_DTYPE`. A channel >= 0 indicates that a record contains a photon. Otherwise, the record contains an overflow event or marker > 0. """ from ._ptufile import decode_t2_records, decode_t3_records if records is None: records = self.read_records(copy=False) rectype = self.tags['TTResultFormat_TTTRRecType'] if self.is_t3: result = create_output( out, (records.size,), T3_RECORD_DTYPE, fillvalue=0 ) decode_t3_records(result, records, rectype) else: result = create_output( out, (records.size,), T2_RECORD_DTYPE, fillvalue=0 ) decode_t2_records(result, records, rectype) return result @overload def decode_histogram( self, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, sampling_time: int | None = None, dtime: int | None = None, asxarray: Literal[False] = ..., out: OutputType = None, ) -> NDArray[Any]: ... @overload def decode_histogram( self, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, sampling_time: int | None = None, dtime: int | None = None, asxarray: Literal[True] = ..., out: OutputType = None, ) -> DataArray: ... @overload def decode_histogram( self, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, sampling_time: int | None = None, dtime: int | None = None, asxarray: bool = ..., out: OutputType = None, ) -> NDArray[Any] | DataArray: ... def decode_histogram( self, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, sampling_time: int | None = None, dtime: int | None = None, asxarray: bool = False, out: OutputType = None, ) -> NDArray[Any] | DataArray: """Return histogram of all photons by channel. Parameters: records: Encoded TTTR records. By default, read records from file. dtype: Unsigned integer type of histogram array. The default is ``uint32`` for T3, else ``uint16``. Increase the bit depth to avoid overflows. sampling_time: Global time per sample for T2 mode. The default is :py:attr:`PtuFile.global_pixel_time`. dtime: Number of bins in histogram. If 0, return :py:attr:`number_bins_in_period` bins. If > 0, return up to specified bin. asxarray: If true, return ``xarray.DataArray``, else ``numpy.ndarray`` (default). out: Array where decoded histogram is stored. If ``None``, create a new NumPy array in main memory. If ``'memmap'``, create a memory-mapped array in a temporary file. If a ``numpy.ndarray``, a writable, initialized array of :py:attr:`shape` and unsigned integer dtype. If a ``file name`` or ``open file``, create a memory-mapped array in the specified file. Returns: : Decoded TTTR T3 records as 2-dimensional histogram array: - ``'C'`` channel - ``'H'`` histogram bins """ from ._ptufile import decode_t2_histogram, decode_t3_histogram if dtype is None: dtype = numpy.uint32 if self.is_t3 else numpy.uint16 dtype = numpy.dtype(dtype) if dtype.kind != 'u': msg = f'not an unsigned integer {dtype=!r}' raise ValueError(msg) if records is None: records = self.read_records(copy=False) rectype = self.tags['TTResultFormat_TTTRRecType'] if 'C' in self._trimdims: first_channel = self._info.channels_active_first else: first_channel = 0 if self.is_t3: if dtime is None: nbins = self.shape[-1] elif dtime == 0: nbins = self.number_bins_in_period elif dtime > 0: nbins = dtime else: msg = f'{dtime=} < 0' raise ValueError(msg) histogram = create_output( out, (self.shape[-2], nbins), dtype, fillvalue=0 ) decode_t3_histogram(histogram, records, rectype, first_channel) coords = numpy.linspace( 0, histogram.shape[-1] * self.tags['MeasDesc_Resolution'], histogram.shape[-1], endpoint=False, ) else: if sampling_time is None or sampling_time <= 0: sampling_time = self.global_pixel_time histogram = create_output( out, ( self.number_channels, max(1, self.global_acquisition_time // sampling_time), ), dtype, fillvalue=0, ) decode_t2_histogram( histogram, records, rectype, sampling_time, first_channel ) coords = numpy.linspace( 0, self.acquisition_time, histogram.shape[1], endpoint=False ) if not self._asxarray and not asxarray: return histogram from xarray import DataArray return DataArray( histogram, dims=('C', 'H'), coords={'C': self._coords_c, 'H': coords}, # name=self.name attrs=self.attrs, ) @overload def decode_image( self, selection: Sequence[int | slice | EllipsisType | None] | None = None, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, frame: int | None = None, channel: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, keepdims: bool = True, asxarray: Literal[False] = ..., out: OutputType = None, ) -> NDArray[Any]: ... @overload def decode_image( self, selection: Sequence[int | slice | EllipsisType | None] | None = None, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, frame: int | None = None, channel: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, keepdims: bool = True, asxarray: Literal[True] = ..., out: OutputType = None, ) -> DataArray: ... @overload def decode_image( self, selection: Sequence[int | slice | EllipsisType | None] | None = None, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, frame: int | None = None, channel: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, keepdims: bool = True, asxarray: bool = ..., out: OutputType = None, ) -> NDArray[Any] | DataArray: ... def decode_image( self, selection: Sequence[int | slice | EllipsisType | None] | None = None, /, *, records: NDArray[numpy.uint32] | None = None, dtype: DTypeLike | None = None, frame: int | None = None, channel: int | None = None, dtime: int | None = None, pixel_time: float | None = None, bishift: int | None = None, keepdims: bool = True, asxarray: bool = False, out: OutputType = None, ) -> NDArray[Any] | DataArray: """Return T3 mode point, line, or image histogram. The histogram may not include photons counted during incomplete frame scans or during line retraces. Parameters: selection: Indices for all dimensions: - ``None``: return all items along axis (default). - ``Ellipsis``: return all items along multiple axes. - ``int``: return single item along axis. - ``slice``: return chunk of axis. ``slice.step`` is binning factor. If ``slice.step=-1``, integrate all items along axis. records: Encoded TTTR records. By default, read records from file. dtype: Unsigned integer type of image histogram array. The default is ``uint16``. Increase the bit depth to avoid overflows when integrating. frame: If < 0, integrate time axis, else return specified frame. Overrides ``selection`` for axis ``T``. channel: If < 0, integrate channel axis, else return specified channel. Overrides ``selection`` for axis ``C``. dtime: Number of bins in image histogram. If 0, return :py:attr:`number_bins_in_period` bins. If < 0, integrate delay time axis. If > 0, return up to specified bin. Overrides ``selection`` for axis ``H``. pixel_time: float, optional Time per pixel in s. If zero, determine pixel times per line from line scan markers (cannot be used with bidirectional or line scans). The default is :py:attr:`PtuFile.pixel_time`. bishift: Global time shift of odd vs. even lines in bidirectional mode. The default is zero. Positive shifts invalidate left odd columns, while negative shifts invalidate right odd columns. keepdims: If true (default), reduced axes are left as size-one dimension. asxarray: If true, return ``xarray.DataArray``, else ``numpy.ndarray`` (default). out: Array where decoded image histogram is stored. If ``None``, create a new NumPy array in main memory. If ``'memmap'``, create a memory-mapped array in a temporary file. If a ``numpy.ndarray``, a writable, initialized array of :py:attr:`shape` and unsigned integer dtype. If a ``file name`` or ``open file``, create a memory-mapped array in the specified file. Returns: image: Decoded TTTR T3 records as up to 5-dimensional image array: - ``'T'`` time/frame - ``'Y'`` slow scan axis for image scans - ``'X'`` fast scan axis for line and image scans - ``'C'`` channel - ``'H'`` histogram bins Raises: NotImplementedError: T2 images, bidirectional sinusoidal scanning, and deprecated image reconstruction are not supported. IndexError: Selection is out of bounds. """ # TODO: support ReqHdr_ScanningPattern = 1, bidirectional per frame if not self.is_t3: # TODO: T2 images msg = 'not a T3 image' raise NotImplementedError(msg) if self.is_bidirectional and not self.is_image: msg = 'bidirectional scanning only supported for images' raise NotImplementedError(msg) if self.is_image and 'ImgHdr_LineStart' not in self.tags: # TODO: deprecated image reconstruction using # ImgHdr_PixResol, ImgHdr_TStartTo, ImgHdr_TStopTo, # ImgHdr_TStartFro, ImgHdr_TStopFro msg = 'old-style image reconstruction' raise NotImplementedError(msg) shape = list(self.shape) ndim = len(shape) keepaxes: list[slice | int] = [slice(None)] * ndim if selection is None: selection = [None] * ndim else: try: len(selection) except TypeError: selection = [selection] # type: ignore[list-item] if len(selection) > ndim: msg = f'too many indices in {selection=}' raise IndexError(msg) if len(selection) == ndim: selection = list(selection).copy() if Ellipsis in selection: selection[selection.index(Ellipsis)] = None # elif len(selection) < ndim: elif Ellipsis in selection: selection = list(selection).copy() i = selection.index(Ellipsis) selection = ( selection[:i] + ([None] * (1 + ndim - len(selection))) + selection[i + 1 :] ) else: selection = list(selection) + [None] * (ndim - len(selection)) if Ellipsis in selection: msg = f'more than one Ellipsis in {selection=}' raise IndexError(msg) if frame is not None: if frame >= shape[0]: msg = f'{frame=} out of range' raise IndexError(msg) selection[0] = frame if frame >= 0 else slice(None, None, -1) if channel is not None: if channel >= shape[-2]: msg = f'{channel=} out of range' raise IndexError(msg) selection[-2] = channel if channel >= 0 else slice(None, None, -1) if dtime is not None: if dtime == 0: dtime = self.number_bins_in_period if dtime > 0: if dtime > self.number_bins_max: msg = f'{dtime=} out of range {self.number_bins_max}' raise IndexError(msg) selection[-1] = slice(0, dtime, 1) shape[-1] = dtime else: selection[-1] = slice(None, None, -1) start = [0] * ndim step = [1] * ndim for i, (index, size) in enumerate( zip(selection, self.shape, strict=True) ): if index is None: pass elif isinstance(index, int): idx = index if idx < 0: idx %= shape[i] if not 0 <= idx < size: msg = f'axis {i} {idx=} out of range [0, {size}]' raise IndexError(msg) start[i] = idx shape[i] = 1 keepaxes[i] = 0 elif isinstance(index, slice): istart = index.start if istart is not None: if istart < 0: istart %= shape[i] if not 0 <= istart < shape[i]: msg = f'axis {i} {index=} start out of range' raise IndexError(msg) start[i] = istart if index.stop is not None: istop = index.stop if istop < 0: istop %= shape[i] if not start[i] < istop <= shape[i]: msg = f'axis {i} {index=} stop out of range' raise IndexError(msg) shape[i] = istop shape[i] -= start[i] if index.step is not None: if index.step == 0: msg = f'axis {i} slice step cannot be zero' raise IndexError(msg) if index.step < 0: # negative step size -> integrate all step[i] = shape[i] keepaxes[i] = 0 else: step[i] = min(index.step, shape[i]) shape[i] = shape[i] // step[i] + min(1, shape[i] % step[i]) else: msg = f'axis {i} index type {type(index)!r} invalid' raise IndexError(msg) if self._info.channels_active_first > 0 and 'C' in self._trimdims: # set channel offset start[-2] += self._info.channels_active_first if pixel_time is None: global_pixel_time = self.global_pixel_time global_line_time = self.global_line_time elif pixel_time <= 0.0: global_pixel_time = 0 global_line_time = 0 if self.is_sinusoidal: msg = f'cannot use sinusoidal correction with {pixel_time=}' raise ValueError(msg) if ndim == 4: msg = f'cannot decode line scan with {pixel_time=}' raise ValueError(msg) else: global_pixel_time = max( 1, round(pixel_time / self.global_resolution) ) global_line_time = global_pixel_time * self.pixels_in_line if self.is_sinusoidal: pixel_at_time = sinusoidal_correction( self.tags['ImgHdr_SinCorrection'], global_line_time, self.pixels_in_line, dtype=numpy.uint16, # should be enough for pixels_in_line ) else: pixel_at_time = None if dtype is None: dtype = self._dtype else: dtype = numpy.dtype(dtype) if dtype.kind != 'u': msg = f'not an unsigned integer {dtype=!r}' raise ValueError(msg) histogram = create_output(out, shape, dtype, fillvalue=0) times = numpy.zeros(shape[0], numpy.uint64) from ._ptufile import decode_t3_image, decode_t3_line, decode_t3_point if records is None: records = self.read_records(copy=False) if ndim == 5: if ( self.line_start_mask == 0 # noqa: PLR1714 or self.line_stop_mask == 0 or (self.frame_change_mask == 0 and shape[0] > 1) or self.line_stop_mask == self.line_start_mask or self.frame_change_mask == self.line_start_mask or self.frame_change_mask == self.line_stop_mask ): logger().error( 'invalid line_start, line_stop, or frame_change masks (' f'0b{self.line_start_mask:b}, ' f'0b{self.line_stop_mask:b}, ' f'0b{self.frame_change_mask:b})' ) decode_t3_image( histogram, times, records, self.tags['TTResultFormat_TTTRRecType'], self.pixels_in_line, global_pixel_time, global_line_time, pixel_at_time if self.is_sinusoidal else None, self.line_start_mask, self.line_stop_mask, self.frame_change_mask, *start, *step, 0 if bishift is None else bishift, self.is_bidirectional, self.is_sinusoidal, self._info.skip_first_frame, ) elif ndim == 4: # not tested if ( self.line_start_mask == 0 # noqa: PLR1714 or self.line_stop_mask == 0 or self.line_stop_mask == self.line_start_mask ): logger().error( 'invalid line_start or line_stop masks (' f'0b{self.line_start_mask:b}, 0b{self.line_stop_mask:b})' ) decode_t3_line( histogram, times, records, self.tags['TTResultFormat_TTTRRecType'], global_pixel_time, self.line_start_mask, self.line_stop_mask, *start, *step, ) elif ndim == 3: decode_t3_point( histogram, times, records, self.tags['TTResultFormat_TTTRRecType'], global_pixel_time, *start, *step, ) if not keepdims: histogram = histogram[tuple(keepaxes)] if not self._asxarray and not asxarray: return histogram from xarray import DataArray if self._info.channels_active_first > 0 and 'C' in self._trimdims: # unset channel offset start[-2] -= self._info.channels_active_first dims = [] coords = self.coords.copy() for i, ax in enumerate(self.dims): if keepdims or keepaxes[i] != 0: dims.append(ax) if ax in coords: index = slice( start[i], start[i] + shape[i] * step[i], step[i] ) coords[ax] = coords[ax][index] elif ax in coords: del coords[ax] if 'H' in dims and len(coords['H']) < shape[-1]: coords['H'] = numpy.linspace( 0, shape[-1] * coords['H'][1], shape[-1], endpoint=False, ) if 'T' in dims: coords['T'] = times * self.global_resolution return DataArray( histogram, dims=dims, coords=coords, attrs=self.attrs, # name=self.name, ) def plot( self, *, samples: int | None = None, frame: int | None = None, channel: int | None = None, dtime: int | None = -1, verbose: bool = False, show: bool = True, **kwargs: Any, ) -> None: """Plot histograms using matplotlib. Parameters: samples: Number of bins along measurement for T2 mode. The default is 1000. frame: If < 0, integrate time axis, else show specified frame. By default, all frames are shown. Applies to T3 images. channel: If < 0, integrate channel axis, else show specified channel. By default, all channels are shown. Applies to T3 images. dtime: Number of bins in T3 histograms. If < 0 (default), integrate delay time axis of images. If 0, show :py:attr:`number_bins_in_period` bins. If > 0, show histograms up to specified bin. If None, show all bins. verbose: Print information about histogram arrays. show: If true (default), display all figures. Else, defer to user or environment to display figures. **kwargs: Additional arguments passed to ``tifffile.imshow``. """ from matplotlib import pyplot from tifffile import Timer, imshow t = Timer() if self.is_t3: if self.measurement_ndim > 1: t.start() histogram: Any = self.decode_image( frame=frame, channel=channel, dtime=dtime, asxarray=True ) if verbose: print() t.print('decode_image') print() print(histogram.squeeze()) imshow( numpy.transpose( histogram.values, (0, 3, 4, 1, 2) ).squeeze(), title=repr(self), photometric='minisblack', **kwargs, ) pyplot.figure() t.start() # histogram = histogram.sum(axis=(0, 1, 2), dtype=numpy.uint32) dtime = None if dtime is None or dtime < 0 else dtime histogram = self.decode_histogram(asxarray=True, dtime=dtime) else: if samples is None or samples < 1: samples = 1000 histogram = self.decode_histogram( sampling_time=self.global_acquisition_time // samples, asxarray=True, ) if verbose: print() t.print('decode_histogram') print() print(histogram) channels = histogram.coords['C'].values for i, hist in enumerate(histogram): pyplot.plot( hist.coords['H'], hist.values, label=f'ch {channels[i]}' ) if self.frequency > 0.0: pyplot.axvline(x=1 / self.frequency, color='0.5', ls=':', lw=0.75) pyplot.title(repr(self)) pyplot.xlabel('delay time [s]' if self.is_t3 else 'time [s]') pyplot.ylabel('photon count') pyplot.legend() if show: pyplot.show() class PhuMeasurementMode(enum.IntEnum): """Kind of TCSPC measurement (Measurement_Mode tag).""" UNKNOWN = -1 """Unknown mode.""" HISTOGRAM = 0 """Histogram mode.""" CONTI = 8 """Conti mode.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(-1) # Unknown obj._value_ = value return obj class PhuMeasurementSubMode(enum.IntEnum): """Kind of measurement (Measurement_SubMode tag).""" UNKNOWN = -1 """Unknown mode.""" OSCILLOSCOPE = 0 """Oscilloscope mode.""" INTEGRATING = 1 """Integrating mode.""" TRES = 2 """Time-Resolved Emission Spectra mode.""" SEQ = 3 """Sequence mode.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(-1) obj._value_ = value return obj class PtuMeasurementMode(enum.IntEnum): """Kind of TCSPC Measurement (Measurement_Mode tag).""" UNKNOWN = -1 """Unknown mode.""" T2 = 2 """T2 mode.""" T3 = 3 """T3 mode.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(-1) # Unknown obj._value_ = value return obj class PtuMeasurementSubMode(enum.IntEnum): """Kind of measurement (Measurement_SubMode tag).""" UNKNOWN = -1 """Unknown mode.""" POINT = 1 """Point scan mode.""" LINE = 2 """Line scan mode.""" IMAGE = 3 """Image scan mode.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(1 if value == 0 else -1) obj._value_ = value return obj class PtuScannerType(enum.IntEnum): """Scanner hardware (ImgHdr_Ident tag).""" UNKNOWN = -1 """Unknown scanner.""" PI_E710 = 1 """PI E-710 scanner.""" LSM = 3 """PicoQuant LSM scanner.""" PI_LINEWBS = 5 """PI Line WB scanner.""" PI_E725 = 6 """PI E-725 scanner.""" PI_E727 = 7 """PI E-727 scanner.""" MCL = 8 """MCL scanner.""" FLIMBEE = 9 """PicoQuant FLIMBee scanner.""" SCANBOX = 10 """Zeiss ScanBox scanner.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(-1) # Unknown obj._value_ = value return obj class PtuScanDirection(enum.IntEnum): """Scan direction (ImgHdr_ScanDirection tag).""" XY = 0 """X-Y scan.""" XZ = 1 """X-Z scan.""" YZ = 2 """Y-Z scan.""" @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int) or value != 0: return None obj = cls(0) # XY obj._value_ = value return obj class PtuStopReason(enum.IntEnum): """Reason for measurement end (TTResult_StopReason tag).""" TIME_OVER = 0 MANUAL = 1 OVERFLOW = 2 ERROR = 3 UNKNOWN = -1 FIFO_OVERRUN = -2 LEGACY_ERROR = -3 TCSPC_ERROR = -4 FILE_ERROR = -5 OUT_OF_MEMORY = -6 SUSPENDED = -7 SYS_ERROR = -8 QUEUE_OVERRUN = -9 DATA_XFER_FAIL = -10 DATA_CHECK_FAIL = -11 REF_CLK_LOST = -12 SYNC_LOST = -13 @classmethod def _missing_(cls, value: object) -> object: if not isinstance(value, int): return None obj = cls(-1) # Unknown obj._value_ = value return obj class PtuMeasurementWarnings(enum.IntFlag): """Warnings during measurement (TTResult_MDescWarningFlags tag).""" SYNC_RATE_ZERO = 0x1 SYNC_RATE_TOO_LOW = 0x2 SYNC_RATE_TOO_HIGH = 0x4 INPT_RATE_ZERO = 0x10 INPT_RATE_TOO_HIGH = 0x40 EVENTS_DROPPED = 0x80 INPT_RATE_RATIO = 0x100 DIVIDER_GT_ONE = 0x200 TIME_SPAN_TOO_SMALL = 0x400 OFFSET_UNNECESSARY = 0x800 class PtuHwFeatures(enum.IntFlag): """Hardware features (HW_Features tag).""" DLL = 0x1 TTTR = 0x2 MARKERS = 0x4 LOW_RES = 0x8 TRIG_OUT = 0x10 PROG_DEADTIME = 0x20 EXT_FPGA = 0x40 PROG_HYSTERESES = 0x80 COINCIDENCE_FILTERING = 0x100 INPUT_MODES = 0x200 class PqTagType(enum.IntEnum): """Tag type definition.""" Empty8 = 0xFFFF0008 Bool8 = 0x00000008 Int8 = 0x10000008 BitSet64 = 0x11000008 Color8 = 0x12000008 Float8 = 0x20000008 TDateTime = 0x21000008 Float8Array = 0x2001FFFF AnsiString = 0x4001FFFF WideString = 0x4002FFFF BinaryBlob = 0xFFFFFFFF class PtuRecordType(enum.IntEnum): """TTTR record type.""" PicoHarpT3 = 0x00010303 """PicoHarp 300 T3.""" PicoHarpT2 = 0x00010203 """PicoHarp 300 T2.""" HydraHarpT3 = 0x00010304 """HydraHarp V1.x T3.""" HydraHarpT2 = 0x00010204 """HydraHarp V1.x T2.""" HydraHarp2T3 = 0x01010304 """HydraHarp V2.x T3.""" HydraHarp2T2 = 0x01010204 """HydraHarp V2.x T2.""" TimeHarp260NT3 = 0x00010305 """TimeHarp 260N T3.""" TimeHarp260NT2 = 0x00010205 """TimeHarp 260N T2.""" TimeHarp260PT3 = 0x00010306 """TimeHarp 260P T3.""" TimeHarp260PT2 = 0x00010206 """TimeHarp 260P T2.""" GenericT2 = 0x00010207 """MultiHarp and Picoharp 330 T2.""" GenericT3 = 0x00010307 """MultiHarp and Picoharp 330 T3.""" @dataclasses.dataclass class PtuInfo: """Information about decoded TTTR records. Returned by ``_ptufile.decode_info``. """ format: int """Type of records.""" records: int """Number of records.""" photons: int """Number of photons counted.""" markers: int """Number of marker events.""" frames: int """Number of frames detected. May exclude incomplete frames.""" lines: int """Number of lines between line markers.""" channels: int """Maximum number of channels for record type.""" channels_active: int """Bitfield identifying channels with photons.""" channels_active_first: int """First channel with photons.""" channels_active_last: int """Last channel with photons.""" bins: int """Maximum delay time for record type.""" bins_used: int """Number of bins up to the largest occupied delay-time bin.""" skip_first_frame: bool """First frame of multi-frame image is incomplete.""" skip_last_frame: bool """Last frame of multi-frame image is incomplete.""" line_time: int """Average global time between line markers.""" acquisition_time: int """Global time of last sync event.""" def __str__(self) -> str: return indent( f'{self.__class__.__name__}(', *(f'{key}={value},' for key, value in self.__dict__.items()), end='\n)', ) T2_RECORD_DTYPE = numpy.dtype( [ ('time', numpy.uint64), ('channel', numpy.int8), ('marker', numpy.uint8), ] ) """Numpy dtype of decoded T2 records.""" T3_RECORD_DTYPE = numpy.dtype( [ ('time', numpy.uint64), ('dtime', numpy.int16), ('channel', numpy.int8), ('marker', numpy.uint8), ] ) """Numpy dtype of decoded T3 records.""" FILE_EXTENSIONS = { '.ptu': PqFileType.PTU, '.phu': PqFileType.PHU, '.pck': PqFileType.PCK, '.pco': PqFileType.PCO, '.pfs': PqFileType.PFS, '.pus': PqFileType.PFS, '.pqres': PqFileType.PQRES, '.pqdat': PqFileType.PQDAT, '.pquni': PqFileType.PQUNI, '.pqover': PqFileType.PQOVER, '.spqr': PqFileType.SPQR, } """File extensions of PicoQuant tagged files.""" def binwrite( filename: str | os.PathLike[str], data: ArrayLike, /, tcspc_resolution: float, pixel_resolution: float, ) -> None: """Write TCSPC image histogram and metadata to PicoQuant BIN file. Parameters: filename: Name of PicoQuant BIN file. data: TCSPC image histogram array of shape (length, width, bins). Must be compatible with dtype.uint32. tcspc_resolution: TCSPC resolution in s. pixel_resolution: Pixel resolution in μm. """ data = numpy.asarray(data, ' tuple[NDArray[Any], dict[str, Any]]: """Return TCSPC image histogram and metadata from PicoQuant BIN file. Parameters: filename: Name of PicoQuant BIN file. memmap: If true, return a read-only memory-mapped array. Returns: tuple: - histogram: TCSPC image histogram array of shape (length, width, bins). - metadata: Dictionary with metadata. - 'shape': Shape of histogram array. - 'pixel_resolution': Pixel resolution in μm. - 'tcspc_resolution': TCSPC resolution in s. """ data: NDArray[Any] with open(filename, 'rb') as fh: size_x, size_y, pixel_resolution, size_h, tcspc_resolution = ( struct.unpack(' bytes: """Return encoded PicoQuant tag. Parameters: tagid: Tag identifier string. value: Tag value to encode. index: Array index for tag values, -1 for single values. Returns: Encoded tag as bytes. """ match value: case None: typecode = PqTagType.Empty8 buffer = b'\x00\x00\x00\x00\x00\x00\x00\x00' case bool() as v: # must check bool before int typecode = PqTagType.Bool8 buffer = struct.pack(' NDArray[Any]: """Return pixel indices of global times in line for sinusoidal scanning. Parameters: sincorrect: Amount of sine wave used for measurement. Either percentage of amplitude (PicoQuant) or period (Leica) depending on ``is_amplitude``. The value of the ``ImgHdr_SinCorrection`` tag. global_line_time: Global time per line. pixels_in_line: Number of pixels in line. is_amplitude: Correction value is percentage of amplitude of sine wave. If false, correction value is percentage of period of sine wave. Returns: Array of size ``global_line_time``, mapping global time in line to pixel index in line. """ dtype = numpy.dtype(numpy.uint16 if dtype is None else dtype) if sincorrect <= 0.0 or sincorrect > 100.0: msg = f'{sincorrect=} out of range' raise ValueError(msg) if global_line_time < 2: msg = f'{global_line_time=} out of range' raise ValueError(msg) if pixels_in_line < 2 or pixels_in_line >= numpy.iinfo(dtype).max: msg = f'{pixels_in_line=} out of range' raise ValueError(msg) if not is_amplitude: sincorrect = math.sin(sincorrect * math.pi / 200.0) * 100.0 limit = math.asin(-sincorrect / 100.0) a = numpy.linspace(limit, -limit, global_line_time, endpoint=False) a = numpy.sin(a) a *= -0.5 * pixels_in_line / a[0] a -= a[0] return a.astype(dtype) def create_output( out: OutputType, /, shape: Sequence[int], dtype: DTypeLike | None, *, mode: Literal['r+', 'w+', 'r', 'c'] = 'w+', suffix: str | None = None, fillvalue: float | None = None, ) -> NDArray[Any] | numpy.memmap[Any, Any]: """Return NumPy array where data of shape and dtype can be copied. Parameters: out: Specifies kind of array of ``shape`` and ``dtype`` to return: ``None``: Return new array. ``numpy.ndarray``: Return view of existing array. ``'memmap'`` or ``'memmap:tempdir'``: Return memory-map to array stored in temporary binary file. ``str`` or open file: Return memory-map to array stored in specified binary file. shape: Shape of array to return. dtype: Data type of array to return. If ``out`` is an existing array, ``dtype`` must be castable to its data type. mode: File mode to create memory-mapped array. The default is 'w+' to create new, or overwrite existing file for reading and writing. suffix: Suffix of ``NamedTemporaryFile`` if ``out`` is ``'memmap'``. The default is '.memmap'. fillvalue: Value to initialize output array. By default, return uninitialized array. Returns: NumPy array or memory-mapped array of ``shape`` and ``dtype``. Raises: ValueError: Existing array cannot be reshaped to ``shape`` or cast to ``dtype``. """ shape = tuple(shape) dtype = numpy.dtype(dtype) if out is None: if fillvalue is None: return numpy.empty(shape, dtype) if fillvalue: return numpy.full(shape, fillvalue, dtype) return numpy.zeros(shape, dtype) if isinstance(out, numpy.ndarray): if product(shape) != product(out.shape): msg = f'cannot reshape {shape} to {out.shape}' raise ValueError(msg) if not numpy.can_cast(dtype, out.dtype): msg = f'cannot cast {dtype} to {out.dtype}' raise ValueError(msg) if out.shape != shape: out = out.reshape(shape, copy=False) if fillvalue is not None: out.fill(fillvalue) return out if isinstance(out, str) and out[:6] == 'memmap': import tempfile tempdir = out[7:] if len(out) > 7 else None if suffix is None: suffix = '.memmap' with tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix) as fh: out = numpy.memmap(fh, shape=shape, dtype=dtype, mode=mode) if fillvalue is not None: out.fill(fillvalue) return out out = numpy.memmap(out, shape=shape, dtype=dtype, mode=mode) if fillvalue is not None: out.fill(fillvalue) return out def product(iterable: Iterable[int], /) -> int: """Return product of integers. Like math.prod, but does not overflow with numpy arrays. """ prod = 1 for i in iterable: prod *= int(i) return prod def now() -> datetime: """Return current date and time.""" return datetime.now() def align_bytes(size: int, align: int, /) -> bytes: """Return trailing bytes to align bytes of size.""" size %= align return b'' if size == 0 else b'\0' * (align - size) def indent(*args: Any, sep: str = '', end: str = '') -> str: """Return joined string representations of objects with indented lines.""" text = (sep + '\n').join( arg if isinstance(arg, str) else repr(arg) for arg in args ) return ( '\n'.join( (' ' + line if line else line) for line in text.splitlines() if line )[4:] + end ) def logger() -> logging.Logger: """Return logger for ptufile module.""" return logging.getLogger('ptufile') def askopenfilename(**kwargs: Any) -> str: """Return file name(s) from Tkinter's file open dialog.""" from tkinter import Tk, filedialog root = Tk() root.withdraw() root.update() filenames = filedialog.askopenfilename(**kwargs) root.destroy() return filenames def main(argv: list[str] | None = None) -> int: """Command line usage main function. Preview image and metadata in specified files or all files in directory. ``python -m ptufile file_or_directory`` """ from glob import glob from tifffile import Timer if argv is None: argv = sys.argv fltr = False if len(argv) == 1: path = askopenfilename( title='Select a PTU file', filetypes=[ (f'{ext.upper()} files', f'*{ext}') for ext in FILE_EXTENSIONS ] + [('All files', '*')], ) files = [path] if path else [] elif '*' in argv[1]: files = glob(argv[1]) elif os.path.isdir(argv[1]): files = glob(f'{argv[1]}/*.p*') fltr = True else: files = argv[1:] for filename in files: if ( fltr and os.path.splitext(filename)[-1].lower() not in FILE_EXTENSIONS ): continue try: with PqFile(filename) as pq: if pq.type == PqFileType.PTU: t = Timer() with PtuFile(filename, memmap=True) as ptu: t.print(' open file') t.start() ptu.read_records(copy=False) t.print('read records') t.start() ptu_info = ptu._info t.print('scan records') # t.start() # ptu.decode_records() # t.print('decode records') print() print(ptu) print(ptu_info) try: ptu.plot(verbose=True) except NotImplementedError as exc: print('NotImplementedError:', exc) elif pq.type == PqFileType.PHU: with PhuFile(filename, memmap=True) as phu: print(phu) phu.plot(verbose=True) else: print(pq) print() except Exception: import traceback print('Failed to read', filename) traceback.print_exc() print() continue return 0 if __name__ == '__main__': sys.exit(main())