-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeMMCMammo.py
More file actions
485 lines (440 loc) · 18.5 KB
/
theMMCMammo.py
File metadata and controls
485 lines (440 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import streamlit as st
import joblib
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import (roc_curve, auc, confusion_matrix,
accuracy_score, precision_score,
recall_score, f1_score, roc_auc_score)
# --- Configuration (Must be first Streamlit command) ---
st.set_page_config(
page_title="MamoInsight - Breast Cancer Diagnostics",
page_icon="🩺",
layout="wide",
initial_sidebar_state="expanded"
)
# --- Constants ---
BANNER_IMAGE = "Mamoinsight.png"
FEATURE_RANGES = {
'mean radius': (6.981, 28.110),
'mean texture': (9.710, 39.280),
'mean perimeter': (43.79, 188.5),
'mean area': (143.5, 2501.0),
'mean smoothness': (0.05263, 0.1634),
'mean compactness': (0.01938, 0.3454),
'mean concavity': (0.0, 0.4268),
'mean concave points': (0.0, 0.2012),
'mean symmetry': (0.106, 0.304),
'mean fractal dimension': (0.04996, 0.09744),
'radius error': (0.1115, 2.873),
'texture error': (0.3602, 4.885),
'perimeter error': (0.757, 21.98),
'area error': (6.802, 542.2),
'smoothness error': (0.00171, 0.03113),
'compactness error': (0.00225, 0.1354),
'concavity error': (0.0, 0.396),
'concave points error': (0.0, 0.05279),
'symmetry error': (0.00788, 0.07895),
'fractal dimension error': (0.00089, 0.02984),
'worst radius': (7.93, 36.04),
'worst texture': (12.02, 49.54),
'worst perimeter': (50.41, 251.2),
'worst area': (185.2, 4254.0),
'worst smoothness': (0.07117, 0.2226),
'worst compactness': (0.02729, 1.058),
'worst concavity': (0.0, 1.252),
'worst concave points': (0.0, 0.291),
'worst symmetry': (0.1565, 0.6638),
'worst fractal dimension': (0.05504, 0.2075)
}
# --- Load Data and Models ---
@st.cache_resource
def load_dataset():
return load_breast_cancer()
@st.cache_resource
def load_models():
return {
'svm': joblib.load('svm_model.pkl'),
'ann': joblib.load('ann_model.pkl'),
'scaler': joblib.load('scaler.pkl')
}
# Initialize app data
data = load_dataset()
models = load_models()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=42
)
X_test_scaled = models['scaler'].transform(X_test)
# Get predictions from both models
svm_pred = models['svm'].predict(X_test_scaled)
svm_probs = models['svm'].predict_proba(X_test_scaled)[:, 1]
ann_pred = models['ann'].predict(X_test_scaled)
ann_probs = models['ann'].predict_proba(X_test_scaled)[:, 1]
# --- Utility Functions ---
def create_radar_chart(input_values):
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=input_values,
theta=data.feature_names,
fill='toself',
line_color='#2980b9'
))
fig.update_layout(
polar=dict(radialaxis=dict(visible=True)),
margin=dict(l=150, r=150),
height=500
)
return fig
# --- Page Sections ---
def home_page():
st.image(BANNER_IMAGE, use_container_width=True)
st.title("Advanced Breast Cancer Diagnostics")
with st.expander("About This App", expanded=True):
col1, col2 = st.columns(2)
with col1:
st.markdown("""
**Clinical Decision Support System**
Leveraging SVM machine learning to analyze FNA results:
- 98.2% validation accuracy
- Real-time malignancy probability estimation
- Comprehensive cellular characteristic analysis
""")
st.markdown("""
**Why SVM Was Chosen:**
- Superior handling of high-dimensional data
- Effective with limited training samples
- Robust to measurement noise
- Clear margin maximization
""")
with col2:
st.image("svm_diagram.png", caption="SVM Kernel Space Transformation", use_container_width=True)
def data_story_page():
st.header("How Can Algorithms help?")
# 1. Dataset and Feature Distributions
with st.expander("1. Dataset Overview & Feature Distributions", expanded=True):
st.markdown("""
The Wisconsin Diagnostic Breast Cancer dataset contains 30 features extracted from digitized images of fine-needle aspirates (FNA) of breast masses.
These features capture nuclear characteristics that help distinguish between benign and malignant tumors.
""")
col1, col2 = st.columns(2)
with col1:
st.subheader("Radius Mean Distribution")
fig = px.histogram(
x=data.data[:, 0],
color=data.target_names[data.target],
nbins=50,
color_discrete_sequence=['#1f77b4', '#ff7f0e']
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("""
**Insight:** The distribution of mean radius shows clear separation between benign and malignant cases.
Malignant tumors tend to have larger nuclei, suggesting the need for models that can capture non-linear boundaries.
""")
with col2:
st.subheader("Texture Mean Distribution")
fig = px.histogram(
x=data.data[:, 1],
color=data.target_names[data.target],
nbins=50,
color_discrete_sequence=['#1f77b4', '#ff7f0e']
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("""
**Insight:** Texture mean also shows separation between classes, with malignant cases generally rougher.
""")
# 2. Feature Relationships
with st.expander("2. Feature Relationships & Correlations", expanded=True):
st.markdown("""
The dataset contains mean, standard error, and 'worst' (max) values for each feature.
Understanding relationships among these helps guide feature selection and model choice.
""")
st.subheader("Feature Correlation Heatmap")
corr_matrix = pd.DataFrame(data.data, columns=data.feature_names).corr()
fig = px.imshow(
corr_matrix,
color_continuous_scale='Viridis',
labels=dict(x="Features", y="Features"),
aspect="auto"
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("""
**Insight:** Many features are highly correlated (e.g., radius, perimeter, area).
This redundancy suggests models that can handle correlated inputs, like SVM with RBF kernel, may excel.
""")
st.subheader("Feature Value Ranges")
features_df = pd.DataFrame({
'Feature': data.feature_names,
'Min': [FEATURE_RANGES[f][0] for f in data.feature_names],
'Max': [FEATURE_RANGES[f][1] for f in data.feature_names]
})
fig = go.Figure()
fig.add_trace(go.Bar(
y=features_df['Feature'],
x=features_df['Min'],
orientation='h',
name='Min',
marker_color='#1f77b4'
))
fig.add_trace(go.Bar(
y=features_df['Feature'],
x=features_df['Max'],
orientation='h',
name='Max',
marker_color='#ff7f0e'
))
fig.update_layout(
barmode='group',
xaxis_title='Value',
yaxis_title='Feature',
plot_bgcolor='white',
legend_title_text="Range"
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("""
**Insight:** The wide range of feature values highlights the importance of feature scaling.
All models were trained on standardized data.
""")
# 3. Model Selection: SVM vs ANN
with st.expander("3. Model Selection: SVM vs ANN", expanded=True):
st.markdown("""
### Why We Chose SVM Over ANN
**Artificial Neural Network (ANN) Architecture:**
- Input: 30
- Hidden 1: 64 neurons (ReLU)
- Hidden 2: 32 neurons (ReLU)
- Output: 1 (neuron, Sigmoid)
**Hyperparameters:**
- LR = 0.001, epochs = 100, full-batch (repeat seeds 0–29)
**Training:** Adam + BCELoss + early stopping across seeds
**Support Vector Machine (SVM):**
- Kernel: RBF
- C: 0.1
- Gamma: 0.01
| Criterion | ANN | SVM |
|-------------------|-------------------------------------|---------------------------------------|
| AUC | 0.9931 | 0.9957 |
| False Positives | Low-typically 1 / 42 | Low-typically 1 / 42 |
| False Negatives | Low-4 / 72 | Very low-≈ 1 / 72 |
| Resource Needs | GPU helpful; careful tuning needed | CPU-only; only two hyperparameters |
| Best for… | Maximizing sensitivity/AUC | Maximizing accuracy & simplicity |
**Summary:**
While both models performed excellently, SVM achieved a slightly higher AUC and, crucially, fewer false negatives-vital in cancer diagnostics. SVM is also easier to deploy and tune, making it the best choice for this dataset and clinical context.
""")
# 4. Model Performance Comparison
with st.expander("4. Model Performance Comparison", expanded=True):
st.markdown("""
We evaluated both SVM and ANN models on the same 20% held-out test set.
The side-by-side comparisons below highlight the relative strengths of each approach.
""")
# ROC Curves Comparison
st.subheader("ROC Curves Comparison")
col1, col2 = st.columns(2)
# SVM ROC
with col1:
fpr_svm, tpr_svm, _ = roc_curve(y_test, svm_probs)
roc_auc_svm = auc(fpr_svm, tpr_svm)
fig_svm = go.Figure()
fig_svm.add_trace(go.Scatter(
x=fpr_svm, y=tpr_svm,
mode='lines',
name=f'ROC (AUC = {roc_auc_svm:.3f})',
line=dict(color='#1f77b4', width=3)
))
fig_svm.add_trace(go.Scatter(
x=[0, 1], y=[0, 1],
mode='lines',
name='Random Chance',
line=dict(color='gray', dash='dash')
))
fig_svm.update_layout(title='SVM Model')
st.plotly_chart(fig_svm, use_container_width=True)
# ANN ROC
with col2:
fpr_ann, tpr_ann, _ = roc_curve(y_test, ann_probs)
roc_auc_ann = auc(fpr_ann, tpr_ann)
fig_ann = go.Figure()
fig_ann.add_trace(go.Scatter(
x=fpr_ann, y=tpr_ann,
mode='lines',
name=f'ROC (AUC = {roc_auc_ann:.3f})',
line=dict(color='#ff7f0e', width=3)
))
fig_ann.add_trace(go.Scatter(
x=[0, 1], y=[0, 1],
mode='lines',
name='Random Chance',
line=dict(color='gray', dash='dash')
))
fig_ann.update_layout(title='ANN Model')
st.plotly_chart(fig_ann, use_container_width=True)
# Confusion Matrices
st.subheader("Confusion Matrices")
col1, col2 = st.columns(2)
# SVM Confusion Matrix
with col1:
cm_svm = confusion_matrix(y_test, svm_pred)
fig_cm_svm = px.imshow(
cm_svm,
labels=dict(x="Predicted", y="Actual", color="Count"),
x=data.target_names,
y=data.target_names,
text_auto=True,
color_continuous_scale='Blues',
title='SVM Confusion Matrix'
)
st.plotly_chart(fig_cm_svm, use_container_width=True)
# ANN Confusion Matrix
with col2:
cm_ann = confusion_matrix(y_test, ann_pred)
fig_cm_ann = px.imshow(
cm_ann,
labels=dict(x="Predicted", y="Actual", color="Count"),
x=data.target_names,
y=data.target_names,
text_auto=True,
color_continuous_scale='Oranges',
title='ANN Confusion Matrix'
)
st.plotly_chart(fig_cm_ann, use_container_width=True)
# Performance Metrics Comparison
st.subheader("Performance Metrics Comparison")
# Calculate metrics for both models
metrics = ['Accuracy', 'Precision', 'Recall', 'F1 Score', 'AUC']
svm_values = [
accuracy_score(y_test, svm_pred),
precision_score(y_test, svm_pred),
recall_score(y_test, svm_pred),
f1_score(y_test, svm_pred),
roc_auc_svm
]
ann_values = [
accuracy_score(y_test, ann_pred),
precision_score(y_test, ann_pred),
recall_score(y_test, ann_pred),
f1_score(y_test, ann_pred),
roc_auc_ann
]
# Create grouped bar chart
metrics_df = pd.DataFrame({
'Metric': metrics,
'SVM': svm_values,
'ANN': ann_values
})
fig = px.bar(
metrics_df,
x='Metric',
y=['SVM', 'ANN'],
barmode='group',
color_discrete_map={'SVM': '#1f77b4', 'ANN': '#ff7f0e'},
text_auto='.3f'
)
fig.update_layout(
title='Model Performance Comparison',
yaxis_title='Score',
yaxis=dict(range=[0.9, 1.0]) # Adjust for better visualization
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("""
**Insight:** While both models achieve excellent performance, the SVM model shows slightly better
results in terms of false negatives (critical for cancer detection), and offers simpler
deployment with fewer parameters to tune.
""")
def prediction_page():
st.header("Clinical Prediction Interface")
# Group features
mean_features = [f for f in data.feature_names if "mean" in f]
se_features = [f for f in data.feature_names if "error" in f or "se" in f]
worst_features = [f for f in data.feature_names if "worst" in f]
input_values = []
with st.form("diagnostic_form"):
st.markdown("### Mean Features")
mean_cols = st.columns(3)
for i, feature in enumerate(mean_features):
min_val, max_val = FEATURE_RANGES[feature]
val = mean_cols[i % 3].slider(
feature.capitalize(),
min_value=float(min_val),
max_value=float(max_val),
value=float((min_val + max_val) / 2),
step=0.001,
format="%.3f",
help=f"Normal range: {min_val:.3f} - {max_val:.3f}",
key=f"mean_{feature}"
)
input_values.append(val)
st.markdown("### Standard Error Features")
se_cols = st.columns(3)
for i, feature in enumerate(se_features):
min_val, max_val = FEATURE_RANGES[feature]
val = se_cols[i % 3].slider(
feature.capitalize(),
min_value=float(min_val),
max_value=float(max_val),
value=float((min_val + max_val) / 2),
step=0.001,
format="%.3f",
help=f"Normal range: {min_val:.3f} - {max_val:.3f}",
key=f"se_{feature}"
)
input_values.append(val)
st.markdown("### Worst Features")
worst_cols = st.columns(3)
for i, feature in enumerate(worst_features):
min_val, max_val = FEATURE_RANGES[feature]
val = worst_cols[i % 3].slider(
feature.capitalize(),
min_value=float(min_val),
max_value=float(max_val),
value=float((min_val + max_val) / 2),
step=0.001,
format="%.3f",
help=f"Normal range: {min_val:.3f} - {max_val:.3f}",
key=f"worst_{feature}"
)
input_values.append(val)
model_choice = st.radio("Select Model", ["SVM", "ANN"], horizontal=True)
if st.form_submit_button("Analyze Sample"):
try:
scaled_input = models['scaler'].transform([input_values])
if model_choice == "SVM":
prediction = models['svm'].predict(scaled_input)
probability = models['svm'].predict_proba(scaled_input)[0][1]
else: # ANN
prediction = models['ann'].predict(scaled_input)
probability = models['ann'].predict_proba(scaled_input)[0][1]
st.subheader("Diagnostic Report")
col1, col2 = st.columns(2)
with col1:
if prediction[0] == 1:
st.error(f"**Malignant Detected** ({probability:.2%})")
else:
st.success(f"**Benign Growth** ({1-probability:.2%})")
with col2:
st.metric("Confidence Score", f"{max(probability, 1-probability):.2%}")
st.info(f"Using {model_choice} model")
st.plotly_chart(create_radar_chart(input_values), use_container_width=True)
except Exception as e:
st.error(f"Validation Error: {str(e)}")
# --- Main App Flow ---
def main():
pages = {
"Home": home_page,
"Data Analysis": data_story_page,
"Clinical Predictor": prediction_page
}
with st.sidebar:
st.title("Navigation")
page = st.radio("Select Page:", list(pages.keys()))
st.markdown("---")
st.markdown("**Clinical Guidelines**")
st.caption("""
- Values outside normal ranges highlighted in red
- Malignancy probability >70% requires biopsy
- Always correlate with imaging findings
""")
pages[page]()
if __name__ == "__main__":
main()