Spaces:
Running
Running
Sadjad Alikhani
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -132,10 +132,10 @@ LOS_PATH = "images_LoS"
|
|
132 |
# Define the percentage values
|
133 |
percentage_values_los = np.linspace(0.001, 1, 20) * 100 # 20 percentage values
|
134 |
|
135 |
-
# Function to compute confusion matrix and plot it
|
136 |
from sklearn.metrics import f1_score
|
137 |
import seaborn as sns
|
138 |
|
|
|
139 |
def plot_confusion_matrix_from_csv(csv_file_path, title, save_path):
|
140 |
# Load CSV file
|
141 |
data = pd.read_csv(csv_file_path)
|
@@ -147,30 +147,29 @@ def plot_confusion_matrix_from_csv(csv_file_path, title, save_path):
|
|
147 |
# Compute confusion matrix
|
148 |
cm = confusion_matrix(y_true, y_pred)
|
149 |
|
150 |
-
#
|
151 |
-
f1 = f1_score(y_true, y_pred, average='
|
152 |
-
|
153 |
-
#
|
|
|
154 |
plt.figure(figsize=(5, 5))
|
155 |
-
plt.imshow(cm, interpolation='nearest', cmap='coolwarm') # Dark mode color scheme
|
156 |
-
plt.title(f"{title}\nF1-Score: {f1:.2f}", color='white', pad=20) # Display F1-Score in title, add padding for better visibility
|
157 |
-
plt.colorbar()
|
158 |
-
plt.xticks([0, 1], labels=['Class 0', 'Class 1'], color='white') # White text for dark mode
|
159 |
-
plt.yticks([0, 1], labels=['Class 0', 'Class 1'], color='white') # White text for dark mode
|
160 |
|
161 |
-
#
|
162 |
-
|
163 |
-
for i in range(cm.shape[0]):
|
164 |
-
for j in range(cm.shape[1]):
|
165 |
-
plt.text(j, i, format(cm[i, j], 'd'), ha="center", va="center",
|
166 |
-
color="white" if cm[i, j] > thresh else "black")
|
167 |
|
168 |
-
|
169 |
-
plt.
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
# Save the plot as an image
|
173 |
-
plt.savefig(save_path,
|
174 |
plt.close()
|
175 |
|
176 |
# Return the saved image
|
|
|
132 |
# Define the percentage values
|
133 |
percentage_values_los = np.linspace(0.001, 1, 20) * 100 # 20 percentage values
|
134 |
|
|
|
135 |
from sklearn.metrics import f1_score
|
136 |
import seaborn as sns
|
137 |
|
138 |
+
# Function to compute confusion matrix, F1-score and plot it with dark mode style
|
139 |
def plot_confusion_matrix_from_csv(csv_file_path, title, save_path):
|
140 |
# Load CSV file
|
141 |
data = pd.read_csv(csv_file_path)
|
|
|
147 |
# Compute confusion matrix
|
148 |
cm = confusion_matrix(y_true, y_pred)
|
149 |
|
150 |
+
# Compute F1-score
|
151 |
+
f1 = f1_score(y_true, y_pred, average='macro') # Macro-average F1-score
|
152 |
+
|
153 |
+
# Set dark mode styling
|
154 |
+
plt.style.use('dark_background')
|
155 |
plt.figure(figsize=(5, 5))
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
+
# Plot the confusion matrix with a dark-mode compatible colormap
|
158 |
+
sns.heatmap(cm, annot=True, fmt="d", cmap="magma", cbar=False, annot_kws={"size": 12}, linewidths=0.5, linecolor='white')
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
# Add F1-score to the title
|
161 |
+
plt.title(f"{title} (F1 Score: {f1:.3f})", color="white", fontsize=14)
|
162 |
+
|
163 |
+
# Customize tick labels for dark mode
|
164 |
+
plt.xticks([0.5, 1.5], labels=['Class 0', 'Class 1'], color="white", fontsize=10)
|
165 |
+
plt.yticks([0.5, 1.5], labels=['Class 0', 'Class 1'], color="white", fontsize=10)
|
166 |
+
|
167 |
+
plt.ylabel('True label', color="white", fontsize=12)
|
168 |
+
plt.xlabel('Predicted label', color="white", fontsize=12)
|
169 |
+
plt.tight_layout()
|
170 |
|
171 |
# Save the plot as an image
|
172 |
+
plt.savefig(save_path, transparent=True) # Use transparent to blend with the dark mode website
|
173 |
plt.close()
|
174 |
|
175 |
# Return the saved image
|