Module 3 Lab: Preprocessing and augmentation#
Apply preprocessing and document risks.
Lab Context#
This lab uses synthetic 8x8 grayscale image arrays with a small bright lesion pattern and non-lesion variation as a safe proxy for the course setting. It is not a substitute for institutional data, but it lets you practice the reasoning, metrics, and documentation pattern before working with real records.
Lab Tasks#
Run the baseline analysis.
Identify the decision the metric supports.
Change one threshold, score weight, or input assumption.
Compare the result before and after your change.
Record one deployment risk that the synthetic data cannot reveal.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(3)
n = 80
images = rng.normal(0.18, 0.05, size=(n, 8, 8))
labels = np.zeros(n, dtype=int)
for idx in range(n // 2, n):
row = rng.integers(2, 6)
col = rng.integers(2, 6)
images[idx, row-1:row+1, col-1:col+1] += rng.uniform(0.28, 0.42)
labels[idx] = 1
lesion_score = images[:, 2:6, 2:6].mean(axis=(1, 2)) - images.mean(axis=(1, 2))
threshold = float(np.quantile(lesion_score, 0.62))
pred = (lesion_score > threshold).astype(int)
accuracy = float((pred == labels).mean())
sensitivity = float(((pred == 1) & (labels == 1)).sum() / max((labels == 1).sum(), 1))
specificity = float(((pred == 0) & (labels == 0)).sum() / max((labels == 0).sum(), 1))
plt.figure(figsize=(6, 3))
plt.subplot(1, 2, 1)
plt.imshow(images[10], cmap="gray")
plt.title("non-lesion proxy")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(images[-1], cmap="gray")
plt.title("lesion proxy")
plt.axis("off")
plt.tight_layout()
metrics = {"accuracy": accuracy, "sensitivity": sensitivity, "specificity": specificity, "threshold": threshold}
metrics
{'accuracy': 0.8875,
'sensitivity': 0.775,
'specificity': 1.0,
'threshold': 0.023840973720853465}
reflection = {
"what_changed": "",
"metric_before": "",
"metric_after": "",
"interpretation": "",
"synthetic_data_limit": "",
"next_real_world_evidence_needed": "",
}
reflection
{'what_changed': '',
'metric_before': '',
'metric_after': '',
'interpretation': '',
'synthetic_data_limit': '',
'next_real_world_evidence_needed': ''}