Spaces:
Running
Running
File size: 3,710 Bytes
7bc5fde | 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 | from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from data_preprocessing import DataPreprocessor
def analyze_dataset():
root_dir = Path(__file__).resolve().parents[1]
dataset_path = root_dir / "PPMI_Curated_Data_Cut_Public_20250714.csv"
notebooks_dir = root_dir / "notebooks"
notebooks_dir.mkdir(exist_ok=True)
if not dataset_path.exists():
raise FileNotFoundError(f"Dataset not found: {dataset_path}")
df = pd.read_csv(dataset_path)
print("\nDataset Shape:", df.shape)
if "COHORT" not in df.columns:
raise ValueError("Dataset is missing required column: COHORT")
print("\nCOHORT Distribution:")
cohort_dist = df["COHORT"].value_counts(dropna=False)
print(cohort_dist)
preprocessor = DataPreprocessor()
selected_features = list(preprocessor.core.selected_features)
analysis_features = [
col
for col in selected_features
if col in df.columns and col not in {"COHORT", "PATNO"}
]
print(f"\nUsing {len(analysis_features)} available analysis features:")
print(analysis_features)
if not analysis_features:
raise ValueError("No expected analysis features were found in the dataset.")
print("\nKey Features Statistics:")
print(df[analysis_features].describe(include="all").transpose())
print("\nMissing Values Analysis:")
missing = df[analysis_features].isnull().sum()
missing_pct = (missing / len(df)) * 100
missing_summary = pd.DataFrame(
{
"Missing Values": missing,
"Percentage": missing_pct,
}
).sort_values("Percentage", ascending=False)
print(missing_summary[missing_summary["Missing Values"] > 0])
plt.figure(figsize=(10, 6))
sns.countplot(data=df, x="COHORT")
plt.title("Distribution of Cohorts")
plt.tight_layout()
plt.savefig(notebooks_dir / "cohort_distribution.png")
plt.close()
numerical_features = (
df[analysis_features].select_dtypes(include=["number"]).columns.tolist()
)
if numerical_features:
plt.figure(figsize=(12, 8))
correlation_matrix = df[numerical_features].corr(numeric_only=True)
sns.heatmap(correlation_matrix, annot=False, cmap="coolwarm", center=0)
plt.title("Feature Correlation Matrix")
plt.tight_layout()
plt.savefig(notebooks_dir / "correlation_matrix.png")
plt.close()
n_features = len(numerical_features)
n_cols = 3
n_rows = (n_features + n_cols - 1) // n_cols
plt.figure(figsize=(15, 5 * n_rows))
for i, feature in enumerate(numerical_features, 1):
plt.subplot(n_rows, n_cols, i)
sns.histplot(data=df, x=feature, kde=True)
plt.title(feature)
plt.tight_layout()
plt.savefig(notebooks_dir / "feature_distributions.png")
plt.close()
else:
print("\nNo numerical features available for correlation/distribution plots.")
print("\nFeature Types:")
for feature in analysis_features:
dtype = df[feature].dtype
n_unique = df[feature].nunique(dropna=True)
print(f"- {feature}: {dtype}, {n_unique} unique values")
missing_expected = [
col
for col in selected_features
if col not in df.columns and col not in {"COHORT", "PATNO"}
]
if missing_expected:
print("\nExpected features not found in dataset:")
for feature in missing_expected:
print(f"- {feature}")
if __name__ == "__main__":
analyze_dataset()
|