hiyata commited on
Commit
8038fba
·
verified ·
1 Parent(s): 17c9ecb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -195,21 +195,37 @@ def predict(file_obj):
195
  )
196
  explanation.expected_value = 0.5 # Start from neutral prediction
197
 
198
- # Create waterfall plot
199
- plt.figure(figsize=(10, 6))
200
- fig = shap.plots._waterfall.waterfall_legacy(
201
- explanation,
202
- show=False,
203
- max_display=11 # Show all features including "Others"
204
- )
205
- plt.title(f"Feature contributions to human probability (final prob: {human_prob:.3f})")
206
-
207
- # Save plot
208
- buf = io.BytesIO()
209
- plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
210
- buf.seek(0)
211
- plot_image = Image.open(buf)
212
- plt.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  # Calculate final probabilities
215
  with torch.no_grad():
 
195
  )
196
  explanation.expected_value = 0.5 # Start from neutral prediction
197
 
198
+ # Calculate step-by-step probabilities
199
+ step_probs = []
200
+ current_prob = 0.5 # Start at neutral
201
+ step_probs.append({"step": "Start", "probability": current_prob, "kmer": "Initial", "change": 0})
202
+
203
+ # Process each k-mer contribution
204
+ for i, kmer in enumerate(important_kmers, 1):
205
+ change = kmer['importance']
206
+ current_prob += change
207
+ step_probs.append({
208
+ "step": str(i),
209
+ "probability": current_prob,
210
+ "kmer": kmer['kmer'],
211
+ "change": change
212
+ })
213
+
214
+ # Add final "Others" contribution
215
+ current_prob += others_sum
216
+ step_probs.append({
217
+ "step": "Others",
218
+ "probability": current_prob,
219
+ "kmer": "Others",
220
+ "change": others_sum
221
+ })
222
+
223
+ # Convert to JSON for React
224
+ steps_json = json.dumps(step_probs, indent=2)
225
+ print(f"Steps data: {steps_json}") # For debugging
226
+
227
+ # Create visualization component
228
+ plot_image = None # We'll use the React component instead
229
 
230
  # Calculate final probabilities
231
  with torch.no_grad():