utils¶
Collection of helper functions.
- fastddm.utils.chunkify(seq: ndarray, chunksize: int, overlap: int = 0) List[ndarray]¶
Chunkify a sequence into smaller portions.
Takes a sequence
seqand chunks it into smaller portions of sizechunksize, with a givenoverlapwith the previous chunk.The sequence could be e.g. image indices, or an image sequence itself. However, be aware that in the latter case, depending on the chunksize and overlap settings the needed amount of memory could be very high! (It is recommended to use image sequence indices, see example below.)
The last chunk may not be of the right size. The chunking will happen along the first axis.
- Parameters:
seq (numpy.ndarray) – A numpy array of to be chunked content.
chunksize (int) – The size of the output chunks.
overlap (int, optional) – Give a number > 0 by how much the chunks should overlap, i.e.
overlap=2would result in[[1 2 3], [2 3 4], [3 4 5], ...], by default 0.
- Returns:
The list of chunks.
- Return type:
List[numpy.ndarray]
Examples
>>> chunkify(np.arange(10), chunksize=5, overlap=2) [array([0, 1, 2, 3, 4]), array([3, 4, 5, 6, 7]), array([6, 7, 8, 9])]
- fastddm.utils.images2numpy(fnames: Sequence[str], color_seq: Sequence[int] | None = None) ndarray¶
Read a sequence of image files and return it as a numpy array.
- Parameters:
- Returns:
The image sequence as a numpy array. Coordinate convention is
(z,y,x). If color images are imported, convention is(c,z,y,x).- Return type:
- fastddm.utils.read_images(src: str | List[str], seq: Sequence[int] | None = None, color_seq: Sequence[int] | None = None, input_order: str | None = None) ndarray¶
Read a single image file or a list of image files.
The single image file can itself be a sequence of images, like multipage tiff or nd2. If
seqis given, e.g. withseq=range(10)orseq=(10, 11, 12)it will only be applied to single image files, again, like e.g. multipage tiff or nd2.- Parameters:
src (Union[str, List[str]]) – File path to a single image file or a list of file paths.
seq (Sequence[int] | None, optional) – A subset of a multi-image file, can be set e.g. via a
rangeobject, by default None.color_seq (Sequence[int] | None, optional) – A sequence, e.g.
range(2), to describe a specific color sequence to be selected, by default None.input_order (str | None, optional) – The order of input dimensions. Currently only supports up to 4 dimensions
"CTYX", only used for TIFF files, be default None.
- Returns:
The images in array form.
- Return type:
- Raises:
RuntimeError – If the
srctype is not supported (e.g. generator expressions).
- fastddm.utils.read_metadata(src: str) Dict[str, Any]¶
Read an images metadata and return it as a dictionary.
Currently only supports .nd2 files and raw metadata for tiff files.
- Parameters:
src (str) – Path to file location.
- Returns:
A dictionary of all available metadata (no content checking is performed).
- Return type:
Metadata
- Raises:
NotImplementedError – If a folder is given as argument.
FileNotFoundError – If the given file does not exist.
RuntimeError – For non-supported image file types.
- fastddm.utils.tiff2numpy(src: str, seq: Sequence[int] | None = None, color_seq: Sequence[int] | None = None, input_order: str | None = None) ndarray¶
Read a TIFF file (or a sequence inside a multipage TIFF) and return it as a numpy array.
The tiff file is assumed to be of the shape (T,Y,X). If
color_seqis given, the order (C,T,Y,X) is assumed, otherwise the optioninput_orderneeds to be specified. Ifinput_orderis given, the full tiff file is read into memory, the axes ordered accordingly, and thenseqandcolor_seqapplied (if specified).If a non-tiff file is given, it is opened with
skimage.io.imread()using only default parameters.It will be reshaped into (T,Y,X) (or (C,T,Y,X) if color channels are present) before it is returned.
- Parameters:
src (str) – The path to the TIFF file.
seq (Sequence[int] | None, optional) – A sequence, e.g.
range(5, 10), to describe a specific range within a multipage TIFF, by default None.color_seq (Sequence[int] | None, optional) – A sequence, e.g.
range(2), to describe a specific color sequence to be selected, by default None.input_order (str | None, optional) – The order of input dimensions. Currently only supports up to 4 dimensions,
"CTYX", by default None.
- Returns:
The array containing the image information. Coordinate convention is (T,Y,X) (or (C,T,Y,X)).
- Return type: