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)
    df = pd.DataFrame(data={"Object1": object_ids1, "Object2": object_ids2})
    df.drop_duplicates(inplace=True, ignore_index=True)
    return df

try_match_masks_from_disk(mask_files1, mask_files2)

Source code in steinbock/tools/matching.py
def try_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):
        try:
            df = match_masks(
                io.read_mask(mask_file1), io.read_mask(mask_file2)
            )
            yield mask_file1, mask_file2, df
            del df
        except:
            _logger.exception(f"Error matching masks {mask_file1, mask_file2}")

mosaics

try_extract_tiles_from_disk(img_files, tile_size)

Source code in steinbock/tools/mosaics.py
def try_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:
        try:
            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_size, tile_size, tile
                    del tile
            del img
        except:
            _logger.exception(f"Error extracting tiles: {img_file}")

try_stitch_tiles_from_disk(tile_info_groups)

Source code in steinbock/tools/mosaics.py
def try_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():
        try:
            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
        except:
            _logger.exception(f"Error stitching tiles: {img_stem_name}")
Back to top