Finding images¶
A file contains a list of images. This page shows how to inspect that list, understand the file's structure and select the images you need.
The outline¶
print(f) shows a summary of the file's structure. Use f.outline() to inspect the full structure:
<micro_reader.File 'MouseBrain_41Slices_1Tile_3Channel_2Illuminations_2Angles.czi' backend='czi' format='czi' images=4>
series 0: x view 2, illumination 2, tczyx (1, 3, 41, 1000, 1500) uint16
Each line describes a containment node: a plate, well, field or series. It can include the following information:
- Tiles:
tiles 0-3lists the tiles belonging to that node. - Orthogonal indices:
x view 2, illumination 2lists the orthogonal indices and the number of values for each. Here, two views and two illumination sides give 2 × 2 = 4 images. - Image layout: The axes, shape and data type of the images. If image shapes differ, the outline reports
shapes vary. - Pyramids and ragged parts: The number of resolution levels (for example,
4 levels) and the parts of a ragged image appear at the end.
Here are examples from other file types.
<micro_reader.File 'DAPI-mCherry_3T_4Z_5Lambda.oir' backend='oir' format='oir' images=1>
series 0: tczyx (3, 5, 4, 512, 512) uint16
The five bands are channels of one image (see Lambda scans).
indices and index_range¶
f.indices lists the index of every image, in the same order as f.images:
f.indices
# [
# {'series': 0, 'view': 0, 'illumination': 0},
# {'series': 0, 'view': 0, 'illumination': 1},
# {'series': 0, 'view': 1, 'illumination': 0},
# {'series': 0, 'view': 1, 'illumination': 1}
# ]
f.index_range reports the number of distinct values for each index:
index_range is not an image count or array shape
index_range reports the widest range of each index. For example, if series 0 has 3 tiles and series 1 has 5, the tile range is 5. This does not mean there are 5 tiles in every series, or 8 tiles in total.
Do not use index_range to calculate the number of images. Use len(f.images) instead, or select images within each series.
select()¶
Use f.select(**index) to retrieve images matching all the index values you specify:
f.select(view=1) # both illumination sides of view 1
f.select(view=1, illumination=0)[0] # one image
f.select(series=3) # all images in series 3, including its tiles and views
select() always returns a list. It returns an empty list when no images match.
An unknown index name raises KeyError and reports the valid index names:
f.select(position=1)
# KeyError: "unknown indices ['position']; the indices are ('plate', 'acquisition', 'well', ...)"
Selecting several values¶
An index value can also be a list, tuple, range or set. An image matches if its index is any of the specified values:
For plates, you can also select by well name, row, column or slices. See Plates.
An absent index does not match
Calling select(tile=0) on a file without a tile index returns []. No image in that file has a tile index.
To write code that works with files both with and without tiles, check f.index_range first:
The file's tree¶
f.collection represents the file's contents as a hierarchy. Plates contain wells, wells contain fields, and outside plate formats, series contain their images.
The tree is built from the image indices and does not require reading image pixels or metadata:
tree = f.collection
tree.name # file name without its extension
[c.name for c in tree.children] # ['series0']
tree.images # every image below this node
See the Collection reference for the full API.
Image names¶
Each image has a readable name. When the format does not provide its own name, micro-reader generates one using the image's identifying indices:
Use names for display and logging. For unambiguous identification, use the image's index.
Common patterns¶
Group images by series¶
You can group images by their series index and process each series separately:
from collections import defaultdict
by_series = defaultdict(list)
for image in f.images:
by_series[image.index["series"]].append(image)
for series, images in sorted(by_series.items()):
print(series, len(images), "images")
Pair illumination sides of each tile¶
For acquisitions with two illumination sides, you can select the images for each tile and pair them by their illumination index:
for tile in range(f.index_range.get("tile", 1)):
sides = f.select(tile=tile) if "tile" in f.index_range else f.images
left, right = sorted(
sides,
key=lambda im: im.index["illumination"]
)
fused = (
left.read(c=0, z=20).astype("float32")
+ right.read(c=0, z=20)
) / 2
This example assumes each tile has exactly two illumination sides.
Find an image's position in f.images¶
To get the index of an image in the file's image list:
Plates¶
In plate datasets, images are organised by plate, acquisition, well and field indices rather than series. You can also navigate the plate layout directly through f.plates.
See Plates for details.