hiyata commited on
Commit
041d3b3
·
verified ·
1 Parent(s): cdd8a58

Update app.py

Browse files

Added raw frequencies and scaled frequencies and importance for each.

Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -126,6 +126,9 @@ def predict(file_obj):
126
  kmer_vector = scaler.transform(kmer_vector.reshape(1, -1))
127
  X_tensor = torch.FloatTensor(kmer_vector).to(device)
128
 
 
 
 
129
  # Get predictions and feature importance
130
  with torch.no_grad():
131
  output = model(X_tensor)
@@ -135,8 +138,8 @@ def predict(file_obj):
135
  importance = model.get_feature_importance(X_tensor)
136
  kmer_importance = importance[0].cpu().numpy()
137
 
138
- # Weight importance by actual k-mer frequency
139
- kmer_importance *= kmer_vector[0]
140
 
141
  # Get top 10 k-mers
142
  top_k = 10
@@ -145,7 +148,7 @@ def predict(file_obj):
145
  {
146
  'kmer': list(kmer_dict.keys())[list(kmer_dict.values()).index(i)],
147
  'importance': float(kmer_importance[i]),
148
- 'frequency': float(kmer_vector[0][i])
149
  }
150
  for i in top_indices
151
  ]
@@ -163,7 +166,7 @@ Non-human probability: {float(probs[0][0]):0.4f}
163
  Most influential k-mers:"""
164
 
165
  for kmer in important_kmers:
166
- result += f"\n {kmer['kmer']}: importance={kmer['importance']:.4f}, frequency={kmer['frequency']:.4f}"
167
 
168
  results.append(result)
169
  except Exception as e:
 
126
  kmer_vector = scaler.transform(kmer_vector.reshape(1, -1))
127
  X_tensor = torch.FloatTensor(kmer_vector).to(device)
128
 
129
+ # Get raw frequency vector before scaling
130
+ raw_freq_vector = sequence_to_kmer_vector(seq)
131
+
132
  # Get predictions and feature importance
133
  with torch.no_grad():
134
  output = model(X_tensor)
 
138
  importance = model.get_feature_importance(X_tensor)
139
  kmer_importance = importance[0].cpu().numpy()
140
 
141
+ # Normalize importance scores to match original scale
142
+ kmer_importance = kmer_importance / np.max(np.abs(kmer_importance)) * 0.002
143
 
144
  # Get top 10 k-mers
145
  top_k = 10
 
148
  {
149
  'kmer': list(kmer_dict.keys())[list(kmer_dict.values()).index(i)],
150
  'importance': float(kmer_importance[i]),
151
+ 'frequency': float(raw_freq_vector[i])
152
  }
153
  for i in top_indices
154
  ]
 
166
  Most influential k-mers:"""
167
 
168
  for kmer in important_kmers:
169
+ result += f"\n {kmer['kmer']}: importance={kmer['importance']:.4f}, raw_freq={kmer['raw_frequency']:.4f} ({kmer['raw_frequency']*100:.2f}%), scaled_freq={kmer['scaled_frequency']:.4f}"
170
 
171
  results.append(result)
172
  except Exception as e: