Coverage for src/abm_initialization_collection/image/select_fov_images.py: 100%
19 statements
« prev ^ index » next coverage.py v7.1.0, created at 2024-07-26 20:12 +0000
« prev ^ index » next coverage.py v7.1.0, created at 2024-07-26 20:12 +0000
1import numpy as np
2import pandas as pd
5def select_fov_images(
6 metadata: pd.DataFrame, cells_per_fov: int, bins: list[int], counts: list[int]
7) -> list[dict]:
8 """
9 Selects FOV images from dataset within each volume bin.
11 Selected FOVs must not contains any outliers or cells in a state other than
12 M0. Selected FOVs will have exactly the number of specified cells. Average
13 volume of all cells in the FOV are used to determine which volume bin the
14 FOV falls in. FOVs are evaluated in the order they appear in the metadata.
16 Parameters
17 ----------
18 metadata
19 Quilt package metadata for FOV images.
20 cells_per_fov
21 Number of cells per FOV.
22 bins
23 Cell volume bin boundaries.
24 counts
25 Number of FOVs to select from each cell volume bin.
27 Returns
28 -------
29 :
30 List of selected FOV image paths and cell ids
31 """
33 total_count = sum(counts)
34 bin_counts = [0] * len(counts)
35 fovs = []
37 for fov_seg_path, seg_group in metadata.groupby("fov_seg_path"):
38 no_outliers = (seg_group.outlier != "Yes").all()
39 only_m0 = (seg_group.cell_stage == "M0").all()
41 if no_outliers and only_m0 and len(seg_group) == cells_per_fov:
42 mean = seg_group["MEM_shape_volume"].mean()
43 bin_index = np.digitize(mean, bins) - 1
45 if bin_counts[bin_index] < counts[bin_index]:
46 bin_counts[bin_index] = bin_counts[bin_index] + 1
47 key = fov_seg_path.split("/")[-1].split("_")[0]
48 fovs.append(
49 {
50 "key": key,
51 "item": fov_seg_path,
52 "cell_ids": list(seg_group["this_cell_index"]),
53 }
54 )
56 if sum(bin_counts) >= total_count:
57 break
59 return fovs