Incorporate classifier
Browse files
app.py
CHANGED
@@ -1,17 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
import umap
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import os
|
6 |
-
import tempfile
|
7 |
import scanpy as sc
|
8 |
-
import argparse
|
9 |
import subprocess
|
10 |
import sys
|
|
|
|
|
11 |
from io import BytesIO
|
|
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def main(input_file_path, species):
|
16 |
|
17 |
# Get the current working directory
|
@@ -40,8 +68,6 @@ def main(input_file_path, species):
|
|
40 |
##############
|
41 |
# UCE #
|
42 |
##############
|
43 |
-
from evaluate import AnndataProcessor
|
44 |
-
from accelerate import Accelerator
|
45 |
|
46 |
# # python eval_single_anndata.py --adata_path "./data/10k_pbmcs_proc.h5ad" --dir "./" --model_loc "minwoosun/uce-100m"
|
47 |
# script_name = "/home/user/app/UCE/eval_single_anndata.py"
|
@@ -77,6 +103,25 @@ def main(input_file_path, species):
|
|
77 |
print(result.stderr)
|
78 |
print("---> FINSIH UCE")
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
##############
|
81 |
# UMAP #
|
82 |
##############
|
@@ -84,12 +129,12 @@ def main(input_file_path, species):
|
|
84 |
|
85 |
if (UMAP):
|
86 |
|
87 |
-
# Set output file path
|
88 |
-
file_name_with_ext = os.path.basename(input_file_path)
|
89 |
-
file_name = os.path.splitext(file_name_with_ext)[0]
|
90 |
-
output_file = "/home/user/app/UCE/" + f"{file_name}_uce_adata.h5ad"
|
91 |
|
92 |
-
adata = sc.read_h5ad(output_file)
|
93 |
|
94 |
labels = pd.Categorical(adata.obs["cell_type"])
|
95 |
|
@@ -123,7 +168,7 @@ def main(input_file_path, species):
|
|
123 |
img = None
|
124 |
print("no image")
|
125 |
|
126 |
-
return img, output_file
|
127 |
|
128 |
|
129 |
if __name__ == "__main__":
|
@@ -181,12 +226,13 @@ if __name__ == "__main__":
|
|
181 |
with gr.Row():
|
182 |
image_output = gr.Image(type="numpy", label="UMAP of UCE Embeddings")
|
183 |
file_output = gr.File(label="Download embeddings")
|
|
|
184 |
|
185 |
# Add the components and link to the function
|
186 |
run_button.click(
|
187 |
fn=main,
|
188 |
inputs=[file_input, species_input],
|
189 |
-
outputs=[image_output, file_output]
|
190 |
)
|
191 |
|
192 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
import umap
|
5 |
+
import json
|
6 |
import matplotlib.pyplot as plt
|
7 |
import os
|
8 |
+
# import tempfile
|
9 |
import scanpy as sc
|
10 |
+
# import argparse
|
11 |
import subprocess
|
12 |
import sys
|
13 |
+
from evaluate import AnndataProcessor
|
14 |
+
from accelerate import Accelerator
|
15 |
from io import BytesIO
|
16 |
+
from sklearn.linear_model import LogisticRegression
|
17 |
from huggingface_hub import hf_hub_download
|
18 |
|
19 |
|
20 |
+
def load_and_predict_with_classifier(x, model_path, output_path):
|
21 |
+
|
22 |
+
# Load the model parameters from the JSON file
|
23 |
+
with open(model_path, 'r') as f:
|
24 |
+
model_params = json.load(f)
|
25 |
+
|
26 |
+
# Reconstruct the logistic regression model
|
27 |
+
model_loaded = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)
|
28 |
+
model_loaded.coef_ = np.array(model_params["coef"])
|
29 |
+
model_loaded.intercept_ = np.array(model_params["intercept"])
|
30 |
+
model_loaded.classes_ = np.array(model_params["classes"])
|
31 |
+
|
32 |
+
# output predictions
|
33 |
+
y_pred = model_loaded.predict(x)
|
34 |
+
|
35 |
+
# Convert the array to a Pandas DataFrame
|
36 |
+
if save:
|
37 |
+
df = pd.DataFrame(y_pred, columns=["predicted_cell_type"])
|
38 |
+
df.to_csv(output_path, index=False, header=False)
|
39 |
+
|
40 |
+
return y_pred
|
41 |
+
|
42 |
+
|
43 |
def main(input_file_path, species):
|
44 |
|
45 |
# Get the current working directory
|
|
|
68 |
##############
|
69 |
# UCE #
|
70 |
##############
|
|
|
|
|
71 |
|
72 |
# # python eval_single_anndata.py --adata_path "./data/10k_pbmcs_proc.h5ad" --dir "./" --model_loc "minwoosun/uce-100m"
|
73 |
# script_name = "/home/user/app/UCE/eval_single_anndata.py"
|
|
|
103 |
print(result.stderr)
|
104 |
print("---> FINSIH UCE")
|
105 |
|
106 |
+
################################
|
107 |
+
# Cell-type classification #
|
108 |
+
################################
|
109 |
+
|
110 |
+
|
111 |
+
# Set output file path
|
112 |
+
file_name_with_ext = os.path.basename(input_file_path)
|
113 |
+
file_name = os.path.splitext(file_name_with_ext)[0]
|
114 |
+
pred_file = "/home/user/app/UCE/" + f"{file_name}_predictions.csv"
|
115 |
+
model_path = hf_hub_download(repo_id="minwoosun/uce-misc", filename="tabula_sapiens_v1_logistic_regression_model_weights.json")
|
116 |
+
|
117 |
+
file_name_with_ext = os.path.basename(input_file_path)
|
118 |
+
file_name = os.path.splitext(file_name_with_ext)[0]
|
119 |
+
output_file = "/home/user/app/UCE/" + f"{file_name}_uce_adata.h5ad"
|
120 |
+
adata = sc.read_h5ad(output_file)
|
121 |
+
x = adata.obsm['X_uce']
|
122 |
+
|
123 |
+
y_pred = load_and_predict_with_classifier(x, model_path, pred_file, save=True)
|
124 |
+
|
125 |
##############
|
126 |
# UMAP #
|
127 |
##############
|
|
|
129 |
|
130 |
if (UMAP):
|
131 |
|
132 |
+
# # Set output file path
|
133 |
+
# file_name_with_ext = os.path.basename(input_file_path)
|
134 |
+
# file_name = os.path.splitext(file_name_with_ext)[0]
|
135 |
+
# output_file = "/home/user/app/UCE/" + f"{file_name}_uce_adata.h5ad"
|
136 |
|
137 |
+
# adata = sc.read_h5ad(output_file)
|
138 |
|
139 |
labels = pd.Categorical(adata.obs["cell_type"])
|
140 |
|
|
|
168 |
img = None
|
169 |
print("no image")
|
170 |
|
171 |
+
return img, output_file, pred_file
|
172 |
|
173 |
|
174 |
if __name__ == "__main__":
|
|
|
226 |
with gr.Row():
|
227 |
image_output = gr.Image(type="numpy", label="UMAP of UCE Embeddings")
|
228 |
file_output = gr.File(label="Download embeddings")
|
229 |
+
pred_output = gr.File(label="Download predictions")
|
230 |
|
231 |
# Add the components and link to the function
|
232 |
run_button.click(
|
233 |
fn=main,
|
234 |
inputs=[file_input, species_input],
|
235 |
+
outputs=[image_output, file_output, pred_output]
|
236 |
)
|
237 |
|
238 |
demo.launch()
|