Skip to content

Commit fda0ca5

Browse files
committed
Add old one back again.
1 parent e82fd28 commit fda0ca5

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

stlearn/image_preprocessing/feature_extractor.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,89 @@
55
from anndata import AnnData
66
from sklearn.decomposition import PCA
77
from tqdm import tqdm
8+
import pandas as pd
89

910
from .model_zoo import Model
1011

1112
_CNN_BASE = Literal["resnet50", "vgg16", "inception_v3", "xception"]
1213

1314

15+
def old_extract_feature(
16+
adata: AnnData,
17+
cnn_base: _CNN_BASE = "resnet50",
18+
n_components: int = 50,
19+
verbose: bool = False,
20+
copy: bool = False,
21+
seeds: int = 1,
22+
) -> AnnData | None:
23+
"""\
24+
Extract latent morphological features from H&E images using pre-trained
25+
convolutional neural network base
26+
27+
Parameters
28+
----------
29+
adata:
30+
Annotated data matrix.
31+
cnn_base:
32+
Established convolutional neural network bases
33+
choose one from ['resnet50', 'vgg16', 'inception_v3', 'xception']
34+
n_components:
35+
Number of principal components to compute for latent morphological features
36+
verbose:
37+
Verbose output
38+
copy:
39+
Return a copy instead of writing to adata.
40+
seeds:
41+
Fix random state
42+
Returns
43+
-------
44+
Depending on `copy`, returns or updates `adata` with the following fields.
45+
**X_morphology** : `adata.obsm` field
46+
Dimension reduced latent morphological features.
47+
"""
48+
feature_dfs = []
49+
model = Model(cnn_base)
50+
51+
if "tile_path" not in adata.obs:
52+
raise ValueError("Please run the function stlearn.pp.tiling")
53+
54+
def encode(tiles, model):
55+
features = model.predict(tiles)
56+
features = features.ravel()
57+
return features
58+
59+
with tqdm(
60+
total=len(adata),
61+
desc="Extract feature",
62+
bar_format="{l_bar}{bar} [ time left: {remaining} ]",
63+
) as pbar:
64+
for spot, tile_path in adata.obs["tile_path"].items():
65+
tile = Image.open(tile_path)
66+
tile = np.asarray(tile, dtype="int32")
67+
tile = tile.astype(np.float32)
68+
tile = np.stack([tile])
69+
if verbose:
70+
print("extract feature for spot: {}".format(str(spot)))
71+
features = encode(tile, model)
72+
feature_dfs.append(pd.DataFrame(features, columns=[spot]))
73+
pbar.update(1)
74+
75+
feature_df = pd.concat(feature_dfs, axis=1)
76+
77+
adata.obsm["X_tile_feature"] = feature_df.transpose().to_numpy()
78+
79+
from sklearn.decomposition import PCA
80+
81+
pca = PCA(n_components=n_components, random_state=seeds)
82+
pca.fit(feature_df.transpose().to_numpy())
83+
84+
adata.obsm["X_morphology"] = pca.transform(feature_df.transpose().to_numpy())
85+
86+
print("The morphology feature is added to adata.obsm['X_morphology']!")
87+
88+
return adata if copy else None
89+
90+
1491
def extract_feature(
1592
adata: AnnData,
1693
cnn_base: _CNN_BASE = "resnet50",

0 commit comments

Comments
 (0)