Update app.py
Browse files
app.py
CHANGED
|
@@ -51,7 +51,6 @@ def load_all():
|
|
| 51 |
knn = KNeighborsClassifier(n_neighbors=K, metric='cosine', n_jobs=-1)
|
| 52 |
knn.fit(ref_emb.values, ref_clin['uberon_tissue'].values)
|
| 53 |
|
| 54 |
-
# Try loading full VAE model (optional — needed for new sample encoding)
|
| 55 |
model = None
|
| 56 |
pth = MODEL_DIR / "vae_tissue.final_model.pth"
|
| 57 |
if pth.exists():
|
|
@@ -107,21 +106,23 @@ def classify(df):
|
|
| 107 |
ni = n2i.get('nan', None)
|
| 108 |
if ni is not None:
|
| 109 |
logits[:, ni] = -1e9
|
| 110 |
-
probs
|
| 111 |
-
pred_idx
|
| 112 |
emb = mu.numpy()
|
| 113 |
pred_labels = [lmap[int(i)] for i in pred_idx]
|
| 114 |
max_probs = probs.numpy().max(axis=1)
|
| 115 |
-
|
| 116 |
from sklearn.decomposition import TruncatedSVD
|
| 117 |
-
n_comp
|
| 118 |
-
svd
|
| 119 |
emb_reduced = svd.fit_transform(X_scaled)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
emb = np.concatenate([emb_reduced, pad], axis=1)
|
| 123 |
pred_labels = knn_ref.predict(emb).tolist()
|
| 124 |
max_probs = np.ones(len(pred_labels)) * float('nan')
|
|
|
|
|
|
|
|
|
|
| 125 |
# ── Upload ──
|
| 126 |
st.markdown("---")
|
| 127 |
col1, col2 = st.columns([2, 1])
|
|
@@ -153,7 +154,6 @@ if uploaded:
|
|
| 153 |
st.warning("Low gene overlap — results may be unreliable.")
|
| 154 |
|
| 155 |
emb, pred_labels, max_probs = classify(df)
|
| 156 |
-
|
| 157 |
distances, indices = knn_ref.kneighbors(emb)
|
| 158 |
breakdowns = []
|
| 159 |
for i in range(len(emb)):
|
|
@@ -197,4 +197,4 @@ st.caption(
|
|
| 197 |
"Flexynesis Tissue VAE · Akalin Lab, MDC Berlin/BIMSB · "
|
| 198 |
"75,619 samples · 43 UBERON tissues · 90.7% balanced accuracy · "
|
| 199 |
"github.com/BIMSBbioinfo/flexynesis"
|
| 200 |
-
)
|
|
|
|
| 51 |
knn = KNeighborsClassifier(n_neighbors=K, metric='cosine', n_jobs=-1)
|
| 52 |
knn.fit(ref_emb.values, ref_clin['uberon_tissue'].values)
|
| 53 |
|
|
|
|
| 54 |
model = None
|
| 55 |
pth = MODEL_DIR / "vae_tissue.final_model.pth"
|
| 56 |
if pth.exists():
|
|
|
|
| 106 |
ni = n2i.get('nan', None)
|
| 107 |
if ni is not None:
|
| 108 |
logits[:, ni] = -1e9
|
| 109 |
+
probs = torch.softmax(logits, dim=1)
|
| 110 |
+
pred_idx = logits.argmax(dim=1).numpy()
|
| 111 |
emb = mu.numpy()
|
| 112 |
pred_labels = [lmap[int(i)] for i in pred_idx]
|
| 113 |
max_probs = probs.numpy().max(axis=1)
|
| 114 |
+
else:
|
| 115 |
from sklearn.decomposition import TruncatedSVD
|
| 116 |
+
n_comp = min(121, X_scaled.shape[0] - 1, X_scaled.shape[1])
|
| 117 |
+
svd = TruncatedSVD(n_components=n_comp, random_state=42)
|
| 118 |
emb_reduced = svd.fit_transform(X_scaled)
|
| 119 |
+
pad = np.zeros((emb_reduced.shape[0], 121 - emb_reduced.shape[1]))
|
| 120 |
+
emb = np.concatenate([emb_reduced, pad], axis=1)
|
|
|
|
| 121 |
pred_labels = knn_ref.predict(emb).tolist()
|
| 122 |
max_probs = np.ones(len(pred_labels)) * float('nan')
|
| 123 |
+
|
| 124 |
+
return emb, pred_labels, max_probs
|
| 125 |
+
|
| 126 |
# ── Upload ──
|
| 127 |
st.markdown("---")
|
| 128 |
col1, col2 = st.columns([2, 1])
|
|
|
|
| 154 |
st.warning("Low gene overlap — results may be unreliable.")
|
| 155 |
|
| 156 |
emb, pred_labels, max_probs = classify(df)
|
|
|
|
| 157 |
distances, indices = knn_ref.kneighbors(emb)
|
| 158 |
breakdowns = []
|
| 159 |
for i in range(len(emb)):
|
|
|
|
| 197 |
"Flexynesis Tissue VAE · Akalin Lab, MDC Berlin/BIMSB · "
|
| 198 |
"75,619 samples · 43 UBERON tissues · 90.7% balanced accuracy · "
|
| 199 |
"github.com/BIMSBbioinfo/flexynesis"
|
| 200 |
+
)
|