Skip to content

steinbock.tools

matching

match_masks(mask1, mask2)

Source code in steinbock/tools/matching.py
def match_masks(mask1: np.ndarray, mask2: np.ndarray) -> pd.DataFrame:
    nz1 = mask1 != 0
    nz2 = mask2 != 0
    object_ids1 = []
    object_ids2 = []
    for object_id1 in np.unique(mask1[nz1]):
        for object_id2 in np.unique(mask2[nz2 & (mask1 == object_id1)]):
            object_ids1.append(object_id1)
            object_ids2.append(object_id2)
    for object_id2 in np.unique(mask2[nz2]):
        for object_id1 in np.unique(mask1[nz1 & (mask2 == object_id2)]):
            object_ids1.append(object_id1)
            object_ids2.append(object_id2)
    table = pd.DataFrame(data={"Object1": object_ids1, "Object2": object_ids2})
    table.drop_duplicates(inplace=True, ignore_index=True)
    return table

match_masks_from_disk(mask_files1, mask_files2)

Source code in steinbock/tools/matching.py
def match_masks_from_disk(
    mask_files1: Sequence[Union[str, PathLike]],
    mask_files2: Sequence[Union[str, PathLike]],
) -> Generator[Tuple[Path, Path, pd.DataFrame], None, None]:
    for mask_file1, mask_file2 in zip(mask_files1, mask_files2):
        table = match_masks(io.read_mask(mask_file1), io.read_mask(mask_file2))
        yield mask_file1, mask_file2, table
        del table

mosaics

extract_tiles_from_disk(img_files, tile_size)

Source code in steinbock/tools/mosaics.py
def extract_tiles_from_disk(
    img_files: Sequence[Union[str, PathLike]], tile_size: int
) -> Generator[Tuple[Path, int, int, int, int, np.ndarray], None, None]:
    for img_file in img_files:
        img = io.read_image(img_file, ignore_dtype=True)
        for x in range(0, img.shape[2], tile_size):
            for y in range(0, img.shape[1], tile_size):
                tile = img[:, y : (y + tile_size), x : (x + tile_size)]
                yield Path(img_file), x, y, tile.shape[2], tile.shape[1], tile
                del tile
        del img

stitch_tiles_from_disk(tile_info_groups)

Source code in steinbock/tools/mosaics.py
def stitch_tiles_from_disk(
    tile_info_groups: Dict[
        str, Sequence[Tuple[Union[str, PathLike], int, int, int, int]]
    ]
) -> Generator[Tuple[str, np.ndarray], None, None]:
    for img_stem_name, tile_info in tile_info_groups.items():
        first_tile = io.read_image(tile_info[0][0], ignore_dtype=True)
        img_width = max(x + w for _, x, y, w, h in tile_info)
        img_height = max(y + h for _, x, y, w, h in tile_info)
        img = np.zeros(
            (first_tile.shape[0], img_height, img_width),
            dtype=first_tile.dtype,
        )
        for tile_file, x, y, w, h in tile_info:
            img[:, y : (y + h), x : (x + w)] = io.read_image(
                tile_file, ignore_dtype=True
            )
        yield img_stem_name, img
        del img
Back to top