Skip to content

Commit 63b4d74

Browse files
committed
Add old tiling back.
1 parent 9c9e1e8 commit 63b4d74

2 files changed

Lines changed: 100 additions & 7 deletions

File tree

stlearn/image_preprocessing/image_tiling.py

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,104 @@
1-
import os
2-
from pathlib import Path
3-
4-
import numpy as np
1+
from typing import Optional, Union
52
from anndata import AnnData
63
from PIL import Image
7-
8-
# Test progress bar
4+
from pathlib import Path
95
from tqdm import tqdm
6+
import numpy as np
7+
import os
8+
9+
10+
def old_tiling(
11+
adata: AnnData,
12+
out_path: Union[Path, str] = "./tiling",
13+
library_id: Union[str, None] = None,
14+
crop_size: int = 40,
15+
target_size: int = 299,
16+
img_fmt: str = "JPEG",
17+
verbose: bool = False,
18+
copy: bool = False,
19+
) -> Optional[AnnData]:
20+
"""\
21+
Tiling H&E images to small tiles based on spot spatial location
22+
23+
Parameters
24+
----------
25+
adata
26+
Annotated data matrix.
27+
out_path
28+
Path to save spot image tiles
29+
library_id
30+
Library id stored in AnnData.
31+
crop_size
32+
Size of tiles
33+
verbose
34+
Verbose output
35+
copy
36+
Return a copy instead of writing to adata.
37+
target_size
38+
Input size for convolutional neuron network
39+
Returns
40+
-------
41+
Depending on `copy`, returns or updates `adata` with the following fields.
42+
**tile_path** : `adata.obs` field
43+
Saved path for each spot image tiles
44+
"""
45+
46+
if library_id is None:
47+
library_id = list(adata.uns["spatial"].keys())[0]
48+
49+
# Check the exist of out_path
50+
if not os.path.isdir(out_path):
51+
os.mkdir(out_path)
52+
53+
image = adata.uns["spatial"][library_id]["images"][
54+
adata.uns["spatial"][library_id]["use_quality"]
55+
]
56+
if image.dtype == np.float32 or image.dtype == np.float64:
57+
image = (image * 255).astype(np.uint8)
58+
img_pillow = Image.fromarray(image)
59+
60+
if img_pillow.mode == "RGBA":
61+
img_pillow = img_pillow.convert("RGB")
62+
63+
tile_names = []
64+
65+
with tqdm(
66+
total=len(adata),
67+
desc="Tiling image",
68+
bar_format="{l_bar}{bar} [ time left: {remaining} ]",
69+
) as pbar:
70+
for imagerow, imagecol in zip(adata.obs["imagerow"], adata.obs["imagecol"]):
71+
imagerow_down = imagerow - crop_size / 2
72+
imagerow_up = imagerow + crop_size / 2
73+
imagecol_left = imagecol - crop_size / 2
74+
imagecol_right = imagecol + crop_size / 2
75+
tile = img_pillow.crop(
76+
(imagecol_left, imagerow_down, imagecol_right, imagerow_up)
77+
)
78+
tile.thumbnail((target_size, target_size), Image.Resampling.LANCZOS)
79+
tile = tile.resize((target_size, target_size))
80+
tile_name = str(imagecol) + "-" + str(imagerow) + "-" + str(crop_size)
81+
82+
if img_fmt == "JPEG":
83+
out_tile = Path(out_path) / (tile_name + ".jpeg")
84+
tile_names.append(str(out_tile))
85+
tile.save(out_tile, "JPEG")
86+
else:
87+
out_tile = Path(out_path) / (tile_name + ".png")
88+
tile_names.append(str(out_tile))
89+
tile.save(out_tile, "PNG")
90+
91+
if verbose:
92+
print(
93+
"generate tile at location ({}, {})".format(
94+
str(imagecol), str(imagerow)
95+
)
96+
)
97+
98+
pbar.update(1)
99+
100+
adata.obs["tile_path"] = tile_names
101+
return adata if copy else None
10102

11103

12104
def tiling(

stlearn/pp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .image_preprocessing.feature_extractor import extract_feature, old_extract_feature
2-
from .image_preprocessing.image_tiling import tiling
2+
from .image_preprocessing.image_tiling import tiling, old_tiling
33
from .preprocessing.filter_genes import filter_genes
44
from .preprocessing.graph import neighbors
55
from .preprocessing.log_scale import log1p, scale
@@ -12,6 +12,7 @@
1212
"scale",
1313
"neighbors",
1414
"tiling",
15+
"old_tiling",
1516
"extract_feature",
1617
"old_extract_feature",
1718
]

0 commit comments

Comments
 (0)