import nbformat as nbf

nb = nbf.v4.new_notebook()

text1 = """\
# Part 1: Exploratory Data Analysis

In this notebook, we explore the preprocessed datasets generated from the autoimmunity profiling study on Exfoliative Glaucoma (XFG).

We will focus primarily on `slice_1_significant_7.csv`, which contains the 7 protein fragments found to be significantly associated with the condition.
"""

code1 = """\
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Load the slices
slice_1 = pd.read_csv('slice_1_significant_7.csv')
slice_2 = pd.read_csv('slice_2_sig7_plus_33_random.csv')
slice_3 = pd.read_csv('slice_3_all_proteins.csv')

print("Slice 1 (Significant 7) shape:", slice_1.shape)
print("Slice 2 (Sig 7 + 33 random) shape:", slice_2.shape)
print("Slice 3 (All proteins) shape:", slice_3.shape)

slice_1.head()
"""

text2 = """\
### Class Distribution

Let's check the distribution of our target variable `group`, where `1` represents Exfoliative Glaucoma (XFG) and `0` represents Healthy Controls.
"""

code2 = """\
plt.figure(figsize=(6, 4))
sns.countplot(x='group', data=slice_1, hue='group', palette='Set2', legend=False)
plt.title('Distribution of Target Variable (Group)')
plt.xlabel('Group (0 = Healthy, 1 = XFG)')
plt.ylabel('Count')
plt.show()

print(slice_1['group'].value_counts())
"""

text3 = """\
### Feature Distributions

We will examine the distributions of the 7 significant protein fragments across the two groups. This helps us visualize how well individual features might separate the classes.
"""

code3 = """\
# Extract the feature columns (excluding identifiers and target)
significant_cols = [c for c in slice_1.columns if c not in ['Internal LIMS ID', 'Original ID', 'group']]

fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(18, 10))
axes = axes.flatten()

for i, col in enumerate(significant_cols):
    sns.histplot(data=slice_1, x=col, hue='group', kde=True, ax=axes[i], palette='Set2')
    axes[i].set_title(col)

# Remove the empty subplot
fig.delaxes(axes[7])

plt.tight_layout()
plt.show()
"""

text4 = """\
### Correlation Analysis

Let's look at the correlation matrix to see if these 7 significant protein fragments are highly correlated with each other. If they are highly correlated, they might carry redundant information for our Decision Tree.
"""

code4 = """\
plt.figure(figsize=(8, 6))
sns.heatmap(slice_1[significant_cols].corr(), annot=True, cmap='coolwarm', fmt=".2f", vmin=-1, vmax=1)
plt.title('Correlation Matrix of Significant Features')
plt.show()
"""

text5 = """\
### Motivating the Data for Classification

This dataset is highly suitable for a binary classification task for the following reasons:
1. **Clear Target Variable:** We have a well-defined, discrete target variable (`group`), which represents the presence or absence of Exfoliative Glaucoma (1 vs 0).
2. **Numeric Features:** The autoantibody reactivity levels (median fluorescent intensities transformed via Box-Cox) act as continuous numeric features.
3. **Biological Relevance:** The selected features (the 'Significant 7') have demonstrated statistical significance in separating the groups based on a Moderated t-test, providing a solid biological foundation that they contain predictive signal.
4. **Iterative Model Building:** The slices provided (7 features, 40 features, and all features) will allow us to experiment with varying degrees of dimensionality and find the optimal representation to avoid underfitting or overfitting our Decision Tree and MLP classifiers.
"""

nb['cells'] = [
    nbf.v4.new_markdown_cell(text1),
    nbf.v4.new_code_cell(code1),
    nbf.v4.new_markdown_cell(text2),
    nbf.v4.new_code_cell(code2),
    nbf.v4.new_markdown_cell(text3),
    nbf.v4.new_code_cell(code3),
    nbf.v4.new_markdown_cell(text4),
    nbf.v4.new_code_cell(code4),
    nbf.v4.new_markdown_cell(text5)
]

with open('EDA.ipynb', 'w') as f:
    nbf.write(nb, f)

print("EDA.ipynb created successfully.")
