{ "cells": [ { "cell_type": "markdown", "id": "d9953ed2", "metadata": {}, "source": [ "# Brain Tumor Detection Experiment Tracking\n", "This notebook helps track dataset layout, model performance, and evaluation metrics for the brain tumor detection project." ] }, { "cell_type": "code", "execution_count": null, "id": "51214413", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import numpy as np\n", "import pandas as pd\n", "from PIL import Image\n", "from src.utils import load_history_npz, load_metrics_json\n", "\n", "DATASET_ROOT = Path('dataset')\n", "ARTIFACTS_ROOT = Path('artifacts')\n", "MODEL_TYPES = ['cnn', 'transfer', 'vit']\n", "\n", "\n", "def inspect_dataset_structure(dataset_root):\n", " root = Path(dataset_root)\n", " layout = {}\n", " for split in ['train', 'val', 'test']:\n", " split_dir = root / split\n", " if not split_dir.exists():\n", " layout[split] = None\n", " continue\n", " counts = {}\n", " for class_dir in sorted(split_dir.iterdir()):\n", " if class_dir.is_dir():\n", " counts[class_dir.name] = sum(1 for _ in class_dir.glob('*') if _.is_file())\n", " layout[split] = counts\n", " return layout\n", "\n", "\n", "def build_comparison_frame(artifacts_root):\n", " rows = []\n", " for model_type in MODEL_TYPES:\n", " metrics_file = artifacts_root / f'{model_type}_evaluation_metrics.json'\n", " if metrics_file.exists():\n", " metrics = load_metrics_json(metrics_file)\n", " report = metrics.get('classification_report', {})\n", " weighted = report.get('weighted avg', report.get('weighted_avg', {})) if isinstance(report, dict) else {}\n", " rows.append({\n", " 'model': model_type,\n", " 'accuracy': metrics.get('accuracy', report.get('accuracy')),\n", " 'precision': weighted.get('precision'),\n", " 'recall': weighted.get('recall'),\n", " 'f1_score': weighted.get('f1-score', weighted.get('f1_score')),\n", " 'roc_auc': metrics.get('roc_auc'),\n", " })\n", " return pd.DataFrame(rows)" ] }, { "cell_type": "code", "execution_count": null, "id": "9e9a0986", "metadata": {}, "outputs": [], "source": [ "# Dataset layout inspection\n", "layout = inspect_dataset_structure(DATASET_ROOT)\n", "print('Dataset root:', DATASET_ROOT.resolve())\n", "for split, classes in layout.items():\n", " if classes is None:\n", " print(f'{split}: MISSING')\n", " elif not classes:\n", " print(f'{split}: EMPTY')\n", " else:\n", " print(f'{split}:')\n", " for class_name, count in classes.items():\n", " print(f' - {class_name}: {count}')" ] }, { "cell_type": "code", "execution_count": null, "id": "5e00b8a9", "metadata": {}, "outputs": [], "source": [ "# Compare performance across models (if metrics exist)\n", "comparison_df = build_comparison_frame(ARTIFACTS_ROOT)\n", "if comparison_df.empty:\n", " print('No evaluation metrics found in artifacts directory yet.')\n", "else:\n", " display(comparison_df)" ] }, { "cell_type": "code", "execution_count": null, "id": "6b065548", "metadata": {}, "outputs": [], "source": [ "# Load a training history file if available\n", "history_files = sorted((ARTIFACTS_ROOT / 'cnn').glob('history_*.npz'))\n", "if history_files:\n", " history = load_history_npz(history_files[-1])\n", " print('Loaded history file:', history_files[-1])\n", " for metric_name, values in history.items():\n", " print(metric_name, len(values))\n", "else:\n", " print('No history file found for CNN.')" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }