{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nimport glob\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.471068Z","iopub.execute_input":"2026-08-16T18:12:13.471414Z","iopub.status.idle":"2026-08-16T18:12:13.476956Z","shell.execute_reply.started":"2026-08-16T18:12:13.471378Z","shell.execute_reply":"2026-08-16T18:12:13.476118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.616282Z","iopub.execute_input":"2026-08-16T18:12:13.616615Z","iopub.status.idle":"2026-08-16T18:12:13.621420Z","shell.execute_reply.started":"2026-08-16T18:12:13.616583Z","shell.execute_reply":"2026-08-16T18:12:13.620597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input/competitions\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.781463Z","iopub.execute_input":"2026-08-16T18:12:13.781996Z","iopub.status.idle":"2026-08-16T18:12:13.786290Z","shell.execute_reply.started":"2026-08-16T18:12:13.781935Z","shell.execute_reply":"2026-08-16T18:12:13.785461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"comp_path = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024\"\nprint(os.listdir(comp_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.856843Z","iopub.execute_input":"2026-08-16T18:12:13.857190Z","iopub.status.idle":"2026-08-16T18:12:13.862590Z","shell.execute_reply.started":"2026-08-16T18:12:13.857159Z","shell.execute_reply":"2026-08-16T18:12:13.861733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"arch_path = os.path.join(comp_path, \"archive\")\nprint(os.listdir(arch_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.864131Z","iopub.execute_input":"2026-08-16T18:12:13.864411Z","iopub.status.idle":"2026-08-16T18:12:13.878602Z","shell.execute_reply.started":"2026-08-16T18:12:13.864385Z","shell.execute_reply":"2026-08-16T18:12:13.877854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_path = os.path.join(arch_path, \"train\")\nprint(os.listdir(tr_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.881399Z","iopub.execute_input":"2026-08-16T18:12:13.881731Z","iopub.status.idle":"2026-08-16T18:12:13.891170Z","shell.execute_reply.started":"2026-08-16T18:12:13.881693Z","shell.execute_reply":"2026-08-16T18:12:13.890363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_path = os.path.join(tr_path, \"Health\")\nprint(os.listdir(health_path)[:20])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.892635Z","iopub.execute_input":"2026-08-16T18:12:13.892901Z","iopub.status.idle":"2026-08-16T18:12:13.909107Z","shell.execute_reply.started":"2026-08-16T18:12:13.892876Z","shell.execute_reply":"2026-08-16T18:12:13.908423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_files = glob.glob(os.path.join(health_path, \"*.tif\"))\n\nprint(\"Number of Health images:\", len(health_files))\nprint(health_files[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:13.909957Z","iopub.execute_input":"2026-08-16T18:12:13.910221Z","iopub.status.idle":"2026-08-16T18:12:13.916768Z","shell.execute_reply.started":"2026-08-16T18:12:13.910185Z","shell.execute_reply":"2026-08-16T18:12:13.916106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_path = health_files[0]\n\nimage = tiff.imread(image_path)\n\nprint(\"Image loaded successfully!\")\nprint(\"Image shape:\", image.shape)\nprint(\"Data type:\", image.dtype)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:14.046427Z","iopub.execute_input":"2026-08-16T18:12:14.046743Z","iopub.status.idle":"2026-08-16T18:12:14.072117Z","shell.execute_reply.started":"2026-08-16T18:12:14.046712Z","shell.execute_reply":"2026-08-16T18:12:14.071319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import Counter\n\nimage_shapes = Counter()\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n    image_shapes[img.shape] += 1\n\nprint(image_shapes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:14.075339Z","iopub.execute_input":"2026-08-16T18:12:14.075593Z","iopub.status.idle":"2026-08-16T18:12:18.300593Z","shell.execute_reply.started":"2026-08-16T18:12:14.075567Z","shell.execute_reply":"2026-08-16T18:12:18.299795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_125_path = None\nimage_126_path = None\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 125) and image_125_path is None:\n        image_125_path = file_path\n\n    elif img.shape == (64, 64, 126) and image_126_path is None:\n        image_126_path = file_path\n\n    if image_125_path is not None and image_126_path is not None:\n        break\n\nprint(\"125-band image:\", os.path.basename(image_125_path))\nprint(\"126-band image:\", os.path.basename(image_126_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.302036Z","iopub.execute_input":"2026-08-16T18:12:18.302430Z","iopub.status.idle":"2026-08-16T18:12:18.312884Z","shell.execute_reply.started":"2026-08-16T18:12:18.302396Z","shell.execute_reply":"2026-08-16T18:12:18.312049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_125 = tiff.imread(image_125_path)\nimage_126 = tiff.imread(image_126_path)\n\nprint(\"125-band image shape:\", image_125.shape)\nprint(\"126-band image shape:\", image_126.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.313972Z","iopub.execute_input":"2026-08-16T18:12:18.314698Z","iopub.status.idle":"2026-08-16T18:12:18.328924Z","shell.execute_reply.started":"2026-08-16T18:12:18.314660Z","shell.execute_reply":"2026-08-16T18:12:18.328205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band_summary = pd.DataFrame({\n    \"Image\": [\"125-band\", \"126-band\"],\n    \"First Band Mean\": [\n        image_125[:, :, 0].mean(),\n        image_126[:, :, 0].mean()\n    ],\n    \"Last Band Mean\": [\n        image_125[:, :, -1].mean(),\n        image_126[:, :, -1].mean()\n    ]\n})\nband_summary.index = [1, 2]\nprint(band_summary)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.330771Z","iopub.execute_input":"2026-08-16T18:12:18.330998Z","iopub.status.idle":"2026-08-16T18:12:18.350633Z","shell.execute_reply.started":"2026-08-16T18:12:18.330976Z","shell.execute_reply":"2026-08-16T18:12:18.349911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0 = image_125[:, :, 0]\n\nprint(\"Mean:\", band0.mean())\nprint(\"Min:\", band0.min())\nprint(\"Max:\", band0.max())\nprint(\"Standard Deviation:\", band0.std())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.351586Z","iopub.execute_input":"2026-08-16T18:12:18.351856Z","iopub.status.idle":"2026-08-16T18:12:18.357833Z","shell.execute_reply.started":"2026-08-16T18:12:18.351819Z","shell.execute_reply":"2026-08-16T18:12:18.357072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = []\n\ntotal_pixels = image_126.shape[0] * image_126.shape[1]\n\nfor band in range(image_126.shape[2]):\n    band_data = image_126[:, :, band]\n\n    zero_count = np.sum(band_data == 0)\n    max_count = np.sum(band_data == 65535)\n\n    results.append([\n        band + 1,          # Display bands as 1 to 126\n        zero_count,\n        max_count\n    ])\n\nsummary_df = pd.DataFrame(\n    results,\n    columns=[\"Band\", \"Zero Pixels\", \"65535 Pixels\"]\n)\n\ndisplay(summary_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.358891Z","iopub.execute_input":"2026-08-16T18:12:18.359155Z","iopub.status.idle":"2026-08-16T18:12:18.390593Z","shell.execute_reply.started":"2026-08-16T18:12:18.359123Z","shell.execute_reply":"2026-08-16T18:12:18.389936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0_125 = image_125[:, :, 0].flatten()\nband0_126 = image_126[:, :, 0].flatten()\n\nplt.figure(figsize=(15,5))\n\nplt.plot(band0_125, label=\"125-band Image\")\nplt.plot(band0_126, label=\"126-band Image\", alpha=0.7)\n\nplt.title(\"Comparison of Band 0 Pixel Values\")\nplt.xlabel(\"Pixel Index (0 to 4095)\")\nplt.ylabel(\"Pixel Value\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.391490Z","iopub.execute_input":"2026-08-16T18:12:18.391744Z","iopub.status.idle":"2026-08-16T18:12:18.644178Z","shell.execute_reply.started":"2026-08-16T18:12:18.391719Z","shell.execute_reply":"2026-08-16T18:12:18.643349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0_125 = image_125[:, :, -1].flatten()\nband0_126 = image_126[:, :, -1].flatten()\n\nplt.figure(figsize=(15,5))\n\nplt.plot(band0_125, label=\"125-band Image\")\nplt.plot(band0_126, label=\"126-band Image\", alpha=0.7)\n\nplt.title(\"Comparison of Last Band Pixel Values\")\nplt.xlabel(\"Pixel Index (0 to 4095)\")\nplt.ylabel(\"Pixel Value\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.645206Z","iopub.execute_input":"2026-08-16T18:12:18.645546Z","iopub.status.idle":"2026-08-16T18:12:18.828500Z","shell.execute_reply.started":"2026-08-16T18:12:18.645518Z","shell.execute_reply":"2026-08-16T18:12:18.827662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pixel_125 = image_125[32, 32, :]\npixel_126 = image_126[32, 32, :]\n\nplt.figure(figsize=(10, 5))\n\nplt.plot(pixel_125, label=\"125-band image\")\nplt.plot(pixel_126, label=\"126-band image\")\n\nplt.xlabel(\"Spectral Band\")\nplt.ylabel(\"Pixel Value\")\nplt.title(\"Spectral Profile Comparison\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.829491Z","iopub.execute_input":"2026-08-16T18:12:18.829832Z","iopub.status.idle":"2026-08-16T18:12:18.986498Z","shell.execute_reply.started":"2026-08-16T18:12:18.829795Z","shell.execute_reply":"2026-08-16T18:12:18.985688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"last_band_means = []\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n        last_band_mean = img[:, :, -1].mean()\n        last_band_means.append(last_band_mean)\n\nprint(\"Number of 126-band images:\", len(last_band_means))\nprint(\"Last-band means:\")\nprint(last_band_means)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:18.989523Z","iopub.execute_input":"2026-08-16T18:12:18.989799Z","iopub.status.idle":"2026-08-16T18:12:19.396727Z","shell.execute_reply.started":"2026-08-16T18:12:18.989771Z","shell.execute_reply":"2026-08-16T18:12:19.395944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n    \n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n        \n        last_band = img[:, :, -1]\n\n        print(\n            os.path.basename(file_path),\n            \"min =\", last_band.min(),\n            \"max =\", last_band.max(),\n            \"unique values =\", len(np.unique(last_band))\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:19.397649Z","iopub.execute_input":"2026-08-16T18:12:19.397913Z","iopub.status.idle":"2026-08-16T18:12:19.686147Z","shell.execute_reply.started":"2026-08-16T18:12:19.397883Z","shell.execute_reply":"2026-08-16T18:12:19.685480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# results = []\n\n# for band in range(test_image.shape[2]):\n    \n#     band_data = test_image[:, :, band]\n    \n#     results.append({\n#         \"Band\": band + 1,\n#         \"Min\": band_data.min(),\n#         \"Max\": band_data.max(),\n#         \"Mean\": band_data.mean(),\n#         \"Unique Values\": len(np.unique(band_data))\n#     })\n\n# band_stats = pd.DataFrame(results)\n\n# display(band_stats)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:19.687058Z","iopub.execute_input":"2026-08-16T18:12:19.687380Z","iopub.status.idle":"2026-08-16T18:12:19.691821Z","shell.execute_reply.started":"2026-08-16T18:12:19.687348Z","shell.execute_reply":"2026-08-16T18:12:19.691039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n\n        print(\"\\n\", os.path.basename(file_path))\n\n        for band in range(126):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                print(\n                    \"Suspicious Band:\",\n                    band + 1,\n                    \"| Unique values:\",\n                    unique_count\n                )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:19.692902Z","iopub.execute_input":"2026-08-16T18:12:19.693317Z","iopub.status.idle":"2026-08-16T18:12:21.038582Z","shell.execute_reply.started":"2026-08-16T18:12:19.693276Z","shell.execute_reply":"2026-08-16T18:12:21.037726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bad_img = tiff.imread(\"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health/hyper (23).tif\")\n\nplt.imshow(bad_img[:, :, 50], cmap=\"gray\")\nplt.title(\"Band 51 of hyper (23)\")\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:21.039583Z","iopub.execute_input":"2026-08-16T18:12:21.039970Z","iopub.status.idle":"2026-08-16T18:12:21.230740Z","shell.execute_reply.started":"2026-08-16T18:12:21.039940Z","shell.execute_reply":"2026-08-16T18:12:21.229971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 125):\n\n        suspicious_bands = 0\n\n        for band in range(125):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:21.231847Z","iopub.execute_input":"2026-08-16T18:12:21.232172Z","iopub.status.idle":"2026-08-16T18:12:26.200198Z","shell.execute_reply.started":"2026-08-16T18:12:21.232137Z","shell.execute_reply":"2026-08-16T18:12:26.199431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bad_img = tiff.imread(\"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health/hyper (167).tif\")\n\nplt.imshow(bad_img[:, :, 50], cmap=\"gray\")\nplt.title(\"Band 51 of hyper (167)\")\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:26.201197Z","iopub.execute_input":"2026-08-16T18:12:26.201584Z","iopub.status.idle":"2026-08-16T18:12:26.388076Z","shell.execute_reply.started":"2026-08-16T18:12:26.201556Z","shell.execute_reply":"2026-08-16T18:12:26.387335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def analyze_folder(folder_path):\n\n    empty_images = []\n    bad126_images = []\n\n    files = [os.path.join(folder_path, f)\n             for f in os.listdir(folder_path)\n             if f.endswith(\".tif\")]\n\n    print(\"Total images:\", len(files))\n\n    for file in files:\n\n        img = tiff.imread(file)\n\n        # 125-band images\n        if img.shape == (64, 64, 125):\n\n            suspicious = 0\n\n            for band in range(125):\n\n                if len(np.unique(img[:, :, band])) <= 2:\n                    suspicious += 1\n\n            if suspicious == 125:\n                empty_images.append(os.path.basename(file))\n\n        # 126-band images\n        elif img.shape == (64, 64, 126):\n\n            suspicious = 0\n\n            for band in range(126):\n\n                if len(np.unique(img[:, :, band])) <= 2:\n                    suspicious += 1\n\n            if suspicious == 126:\n                empty_images.append(os.path.basename(file))\n            else:\n                bad126_images.append(os.path.basename(file))\n\n    print(\"\\nEmpty images:\", len(empty_images))\n    print(empty_images)\n\n    print(\"\\n126-band images:\", len(bad126_images))\n    print(bad126_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:26.389200Z","iopub.execute_input":"2026-08-16T18:12:26.389799Z","iopub.status.idle":"2026-08-16T18:12:26.397517Z","shell.execute_reply.started":"2026-08-16T18:12:26.389759Z","shell.execute_reply":"2026-08-16T18:12:26.396652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"analyze_folder(health_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:26.398652Z","iopub.execute_input":"2026-08-16T18:12:26.399097Z","iopub.status.idle":"2026-08-16T18:12:32.342551Z","shell.execute_reply.started":"2026-08-16T18:12:26.399060Z","shell.execute_reply":"2026-08-16T18:12:32.341715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_path = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Rust\"\nanalyze_folder(rust_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:32.343647Z","iopub.execute_input":"2026-08-16T18:12:32.344246Z","iopub.status.idle":"2026-08-16T18:12:42.452935Z","shell.execute_reply.started":"2026-08-16T18:12:32.344205Z","shell.execute_reply":"2026-08-16T18:12:42.452054Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_img = tiff.imread(os.path.join(health_path, \"hyper (23).tif\"))\nrust_img = tiff.imread(os.path.join(rust_path, \"hyper (23).tif\"))\n\nprint(\"Health shape:\", health_img.shape)\nprint(\"Rust shape:\", rust_img.shape)\n\nif health_img.shape == rust_img.shape:\n    if np.array_equal(health_img, rust_img):\n        print(\"The two images are exactly the same.\")\n    else:\n        print(\"The two images are different.\")\nelse:\n    print(\"The images have different shapes, so they cannot be identical.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:42.454082Z","iopub.execute_input":"2026-08-16T18:12:42.454445Z","iopub.status.idle":"2026-08-16T18:12:42.465345Z","shell.execute_reply.started":"2026-08-16T18:12:42.454416Z","shell.execute_reply":"2026-08-16T18:12:42.464473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"difference = health_img[:, :, band].astype(float) - rust_img[:, :, band].astype(float)\n\nplt.figure(figsize=(5,5))\nplt.imshow(difference, cmap='bwr')\nplt.colorbar()\nplt.title(\"Difference (Health - Rust) : Band 1\")\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:42.466505Z","iopub.execute_input":"2026-08-16T18:12:42.466925Z","iopub.status.idle":"2026-08-16T18:12:42.589971Z","shell.execute_reply.started":"2026-08-16T18:12:42.466886Z","shell.execute_reply":"2026-08-16T18:12:42.589232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_files = glob.glob(os.path.join(rust_path, \"*.tif\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:42.590842Z","iopub.execute_input":"2026-08-16T18:12:42.591098Z","iopub.status.idle":"2026-08-16T18:12:42.595882Z","shell.execute_reply.started":"2026-08-16T18:12:42.591072Z","shell.execute_reply":"2026-08-16T18:12:42.595147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in rust_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 125):\n\n        suspicious_bands = 0\n\n        for band in range(125):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:42.596848Z","iopub.execute_input":"2026-08-16T18:12:42.597199Z","iopub.status.idle":"2026-08-16T18:12:47.834011Z","shell.execute_reply.started":"2026-08-16T18:12:42.597169Z","shell.execute_reply":"2026-08-16T18:12:47.833237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in rust_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 126):\n\n        suspicious_bands = 0\n\n        for band in range(126):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:47.835069Z","iopub.execute_input":"2026-08-16T18:12:47.835374Z","iopub.status.idle":"2026-08-16T18:12:49.248001Z","shell.execute_reply.started":"2026-08-16T18:12:47.835348Z","shell.execute_reply":"2026-08-16T18:12:49.247163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    print(dirname)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:49.249009Z","iopub.execute_input":"2026-08-16T18:12:49.249348Z","iopub.status.idle":"2026-08-16T18:12:51.800386Z","shell.execute_reply.started":"2026-08-16T18:12:49.249309Z","shell.execute_reply":"2026-08-16T18:12:51.799521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Original Rust folder\ninput_folder = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Rust\"\n\n# New cleaned folder\noutput_folder = \"/kaggle/working/Rust_Clean\"\n\nos.makedirs(output_folder, exist_ok=True)\n\ncount = 0\n\nfor file in os.listdir(input_folder):\n\n    if file.endswith(\".tif\"):\n\n        path = os.path.join(input_folder, file)\n\n        img = tiff.imread(path)\n\n        # If image has 126 bands\n        if img.shape[2] == 126:\n\n            # Remove the last band\n            img = img[:, :, :125]\n            count += 1\n\n        # Save image\n        tiff.imwrite(os.path.join(output_folder, file), img)\n\nprint(f\"Done! Corrected {count} images.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:51.801393Z","iopub.execute_input":"2026-08-16T18:12:51.801661Z","iopub.status.idle":"2026-08-16T18:12:52.344488Z","shell.execute_reply.started":"2026-08-16T18:12:51.801633Z","shell.execute_reply":"2026-08-16T18:12:52.343643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Rust_Clean\"\n\nfor file in os.listdir(folder):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(folder, file))\n\n        if img.shape != (64, 64, 125):\n            print(file, img.shape)\n\nprint(\"Verification Complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:52.345476Z","iopub.execute_input":"2026-08-16T18:12:52.346131Z","iopub.status.idle":"2026-08-16T18:12:52.485792Z","shell.execute_reply.started":"2026-08-16T18:12:52.346103Z","shell.execute_reply":"2026-08-16T18:12:52.485001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Path to the original Healthy folder\ninput_folder = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health\"\n\n# Folder where cleaned images will be saved\noutput_folder = \"/kaggle/working/Healthy_Clean\"\n\nos.makedirs(output_folder, exist_ok=True)\n\ncount = 0\nempty_images = ['hyper (12).tif', 'hyper (153).tif', 'hyper (67).tif', 'hyper (76).tif', 'hyper (23).tif', 'hyper (38).tif', 'hyper (26).tif', 'hyper (34).tif', 'hyper (167).tif']\n\nfor file in os.listdir(input_folder):\n\n    if file in empty_images:\n        continue  # Skip empty image\n\n    img = tiff.imread(os.path.join(input_folder, file))\n\n    if img.shape[2] == 126:\n        img = img[:, :, :125]\n        count+=1\n\n    tiff.imwrite(os.path.join(output_folder, file), img)\nprint(f\"Done! Corrected {count} images.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:52.489890Z","iopub.execute_input":"2026-08-16T18:12:52.490153Z","iopub.status.idle":"2026-08-16T18:12:53.014830Z","shell.execute_reply.started":"2026-08-16T18:12:52.490125Z","shell.execute_reply":"2026-08-16T18:12:53.014036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Healthy_Clean\"\n\nfor file in os.listdir(folder):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(folder, file))\n\n        if img.shape != (64, 64, 125):\n            print(file, img.shape)\n\nprint(\"Verification Complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:53.015814Z","iopub.execute_input":"2026-08-16T18:12:53.016094Z","iopub.status.idle":"2026-08-16T18:12:53.149344Z","shell.execute_reply.started":"2026-08-16T18:12:53.016067Z","shell.execute_reply":"2026-08-16T18:12:53.148665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_folder = \"/kaggle/working/Rust_Clean\"\nhealthy_folder = \"/kaggle/working/Healthy_Clean\"\n\nrust_count = len([f for f in os.listdir(rust_folder) if f.endswith(\".tif\")])\nhealthy_count = len([f for f in os.listdir(healthy_folder) if f.endswith(\".tif\")])\n\nprint(\"Rust images   :\", rust_count)\nprint(\"Healthy images:\", healthy_count)\nprint(\"Total images  :\", rust_count + healthy_count)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:53.150267Z","iopub.execute_input":"2026-08-16T18:12:53.150534Z","iopub.status.idle":"2026-08-16T18:12:53.156967Z","shell.execute_reply.started":"2026-08-16T18:12:53.150508Z","shell.execute_reply":"2026-08-16T18:12:53.156211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = tiff.TiffFile(\"/kaggle/working/Rust_Clean/hyper (1).tif\")\n\nprint(img.pages[0].tags)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:53.158278Z","iopub.execute_input":"2026-08-16T18:12:53.158627Z","iopub.status.idle":"2026-08-16T18:12:53.169695Z","shell.execute_reply.started":"2026-08-16T18:12:53.158600Z","shell.execute_reply":"2026-08-16T18:12:53.168954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input folders\nhealthy_input = \"/kaggle/working/Healthy_Clean\"\nrust_input = \"/kaggle/working/Rust_Clean\"\n\n# Output folders\nhealthy_output = \"/kaggle/working/Healthy_Normalized\"\nrust_output = \"/kaggle/working/Rust_Normalized\"\n\nos.makedirs(healthy_output, exist_ok=True)\nos.makedirs(rust_output, exist_ok=True)\n\n# Find global maximum pixel value\nglobal_max = 0\n\nfor folder in [healthy_input, rust_input]:\n    for file in os.listdir(folder):\n        if file.endswith(\".tif\"):\n            img = tiff.imread(os.path.join(folder, file))\n            global_max = max(global_max, img.max())\n\nprint(\"Global Maximum =\", global_max)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:53.170720Z","iopub.execute_input":"2026-08-16T18:12:53.171161Z","iopub.status.idle":"2026-08-16T18:12:53.486347Z","shell.execute_reply.started":"2026-08-16T18:12:53.171129Z","shell.execute_reply":"2026-08-16T18:12:53.485543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"global_max = 10176.0\n\n# Normalize Healthy images\nfor file in os.listdir(healthy_input):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_input, file))\n        img = img.astype(np.float32) / global_max\n        tiff.imwrite(os.path.join(healthy_output, file), img)\n\n# Normalize Rust images\nfor file in os.listdir(rust_input):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(rust_input, file))\n        img = img.astype(np.float32) / global_max\n        tiff.imwrite(os.path.join(rust_output, file), img)\n\nprint(\"Normalization completed successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:53.487344Z","iopub.execute_input":"2026-08-16T18:12:53.487649Z","iopub.status.idle":"2026-08-16T18:12:54.599595Z","shell.execute_reply.started":"2026-08-16T18:12:53.487616Z","shell.execute_reply":"2026-08-16T18:12:54.598721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"healthy_folder = \"/kaggle/working/Healthy_Normalized\"\n\nresults = []\n\nfor file in sorted(os.listdir(healthy_folder))[:5]:\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_folder, file))\n\n        results.append({\n            \"File Name\": file,\n            \"Shape\": str(img.shape),\n            \"Min\": img.min(),\n            \"Max\": img.max(),\n            \"Mean\": img.mean()\n        })\n\nhealthy_df = pd.DataFrame(results)\n\nhealthy_df.index = range(1, len(healthy_df) + 1)\n\ndisplay(healthy_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:54.600619Z","iopub.execute_input":"2026-08-16T18:12:54.600888Z","iopub.status.idle":"2026-08-16T18:12:54.623477Z","shell.execute_reply.started":"2026-08-16T18:12:54.600854Z","shell.execute_reply":"2026-08-16T18:12:54.622807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"healthy_folder = \"/kaggle/working/Healthy_Normalized\"\nrust_folder = \"/kaggle/working/Rust_Normalized\"\n\nX = []   # Images\ny = []   # Labels\n\n# Healthy = 0\nfor file in sorted(os.listdir(healthy_folder)):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_folder, file))\n        X.append(img)\n        y.append(0)\n\n# Rust = 1\nfor file in sorted(os.listdir(rust_folder)):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(rust_folder, file))\n        X.append(img)\n        y.append(1)\n\n# Convert to NumPy arrays\nX = np.array(X)\ny = np.array(y)\n\nprint(\"Images Shape :\", X.shape)\nprint(\"Labels Shape :\", y.shape)\n\nprint(\"\\nHealthy Images :\", np.sum(y == 0))\nprint(\"Rust Images    :\", np.sum(y == 1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:54.624428Z","iopub.execute_input":"2026-08-16T18:12:54.624681Z","iopub.status.idle":"2026-08-16T18:12:55.706165Z","shell.execute_reply.started":"2026-08-16T18:12:54.624655Z","shell.execute_reply":"2026-08-16T18:12:55.705383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save(\"/kaggle/working/X.npy\", X)\nnp.save(\"/kaggle/working/y.npy\", y)\n\nprint(\"Dataset saved successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:55.707247Z","iopub.execute_input":"2026-08-16T18:12:55.707687Z","iopub.status.idle":"2026-08-16T18:12:56.182679Z","shell.execute_reply.started":"2026-08-16T18:12:55.707648Z","shell.execute_reply":"2026-08-16T18:12:56.181947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# First split: Train (80%) and Temp (20%)\nX_train, X_temp, y_train, y_temp = train_test_split(\n    X,\n    y,\n    test_size=0.20,\n    random_state=42,\n    stratify=y\n)\n\n# Second split: Temp -> Validation (10%) + Test (10%)\nX_val, X_test, y_val, y_test = train_test_split(\n    X_temp,\n    y_temp,\n    test_size=0.50,\n    random_state=42,\n    stratify=y_temp\n)\n\nprint(\"Training Set :\", X_train.shape)\nprint(\"Validation Set:\", X_val.shape)\nprint(\"Test Set :\", X_test.shape)\n\nprint(\"\\nTraining Labels\")\nprint(\"Healthy:\", (y_train == 0).sum())\nprint(\"Rust   :\", (y_train == 1).sum())\n\nprint(\"\\nValidation Labels\")\nprint(\"Healthy:\", (y_val == 0).sum())\nprint(\"Rust   :\", (y_val == 1).sum())\n\nprint(\"\\nTest Labels\")\nprint(\"Healthy:\", (y_test == 0).sum())\nprint(\"Rust   :\", (y_test == 1).sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:56.183741Z","iopub.execute_input":"2026-08-16T18:12:56.184060Z","iopub.status.idle":"2026-08-16T18:12:56.607851Z","shell.execute_reply.started":"2026-08-16T18:12:56.184023Z","shell.execute_reply":"2026-08-16T18:12:56.606939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def augment_image(img):\n\n    augmented = []\n\n    # Original image\n    augmented.append(img)\n\n    # Horizontal Flip\n    augmented.append(np.flip(img, axis=1))\n\n    # Vertical Flip\n    augmented.append(np.flip(img, axis=0))\n\n    # Rotate 90 degrees\n    augmented.append(np.rot90(img, k=1, axes=(0, 1)))\n\n    return augmented","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:56.609175Z","iopub.execute_input":"2026-08-16T18:12:56.610094Z","iopub.status.idle":"2026-08-16T18:12:56.614522Z","shell.execute_reply.started":"2026-08-16T18:12:56.610052Z","shell.execute_reply":"2026-08-16T18:12:56.613811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_aug = []\ny_train_aug = []\n\nfor img, label in zip(X_train, y_train):\n\n    augmented_images = augment_image(img)\n\n    for aug_img in augmented_images:\n        X_train_aug.append(aug_img)\n        y_train_aug.append(label)\n\nX_train_aug = np.array(X_train_aug)\ny_train_aug = np.array(y_train_aug)\n\nprint(\"Original Training Images :\", len(X_train))\nprint(\"Augmented Training Images:\", len(X_train_aug))\nprint(\"Training Labels :\", len(y_train_aug))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:56.615467Z","iopub.execute_input":"2026-08-16T18:12:56.615742Z","iopub.status.idle":"2026-08-16T18:12:57.595357Z","shell.execute_reply.started":"2026-08-16T18:12:56.615707Z","shell.execute_reply":"2026-08-16T18:12:57.594580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save(\"X_train_aug.npy\", X_train_aug)\nnp.save(\"y_train_aug.npy\", y_train_aug)\n\nnp.save(\"X_val.npy\", X_val)\nnp.save(\"y_val.npy\", y_val)\n\nnp.save(\"X_test.npy\", X_test)\nnp.save(\"y_test.npy\", y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:12:57.596315Z","iopub.execute_input":"2026-08-16T18:12:57.596631Z","iopub.status.idle":"2026-08-16T18:13:00.285876Z","shell.execute_reply.started":"2026-08-16T18:12:57.596603Z","shell.execute_reply":"2026-08-16T18:13:00.285034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:13:00.286878Z","iopub.execute_input":"2026-08-16T18:13:00.287106Z","iopub.status.idle":"2026-08-16T18:13:00.292746Z","shell.execute_reply.started":"2026-08-16T18:13:00.287081Z","shell.execute_reply":"2026-08-16T18:13:00.291899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\n# Create a new folder\nfolder_name = \"/kaggle/working/HSI_Final_Dataset\"\nos.makedirs(folder_name, exist_ok=True)\n\n# Files you want to share\nfiles = [\n    \"X_train_aug.npy\",\n    \"y_train_aug.npy\",\n    \"X_val.npy\",\n    \"y_val.npy\",\n    \"X_test.npy\",\n    \"y_test.npy\"\n]\n\n# Copy only selected files\nfor file in files:\n    shutil.copy(\n        f\"/kaggle/working/{file}\",\n        f\"{folder_name}/{file}\"\n    )\n\nprint(\"Selected files copied successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:13:00.293714Z","iopub.execute_input":"2026-08-16T18:13:00.294004Z","iopub.status.idle":"2026-08-16T18:13:06.901605Z","shell.execute_reply.started":"2026-08-16T18:13:00.293966Z","shell.execute_reply":"2026-08-16T18:13:06.900716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\nshutil.make_archive(\n    \"/kaggle/working/HSI_Final_Dataset\",\n    \"zip\",\n    \"/kaggle/working/HSI_Final_Dataset\"\n)\n\nprint(\"ZIP created!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:13:06.903585Z","iopub.execute_input":"2026-08-16T18:13:06.904027Z","iopub.status.idle":"2026-08-16T18:13:22.014946Z","shell.execute_reply.started":"2026-08-16T18:13:06.903982Z","shell.execute_reply":"2026-08-16T18:13:22.013417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Healthy_Clean\"   # Change to Rust_Clean if needed\n\nfile = sorted(os.listdir(folder))[0]\n\nimg = tiff.imread(os.path.join(folder, file))\n\nprint(\"Image:\", file)\nprint(\"Shape:\", img.shape)\n\nbands = [10, 40, 70, 100, 124]\n\nplt.figure(figsize=(15,3))\n\nfor i, b in enumerate(bands):\n    plt.subplot(1,5,i+1)\n    plt.imshow(img[:,:,b], cmap=\"gray\")\n    plt.title(f\"Band {b}\")\n    plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:13:49.460409Z","iopub.execute_input":"2026-08-16T18:13:49.461292Z","iopub.status.idle":"2026-08-16T18:13:49.709521Z","shell.execute_reply.started":"2026-08-16T18:13:49.461217Z","shell.execute_reply":"2026-08-16T18:13:49.708814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pixel = img[32,32,:]\n\nplt.figure(figsize=(10,4))\nplt.plot(pixel)\nplt.title(\"Spectral Signature\")\nplt.xlabel(\"Band Number\")\nplt.ylabel(\"Reflectance\")\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:13:54.530122Z","iopub.execute_input":"2026-08-16T18:13:54.530663Z","iopub.status.idle":"2026-08-16T18:13:54.686598Z","shell.execute_reply.started":"2026-08-16T18:13:54.530630Z","shell.execute_reply":"2026-08-16T18:13:54.685535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Mean :\", np.mean(img))\nprint(\"Std  :\", np.std(img))\nprint(\"Min  :\", np.min(img))\nprint(\"Max  :\", np.max(img))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:14:00.835212Z","iopub.execute_input":"2026-08-16T18:14:00.835596Z","iopub.status.idle":"2026-08-16T18:14:00.847240Z","shell.execute_reply.started":"2026-08-16T18:14:00.835564Z","shell.execute_reply":"2026-08-16T18:14:00.846337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Path to your image\nfile_path = \"/kaggle/working/Healthy_Clean/hyper (1).tif\"\n\n# Read hyperspectral image\nimg = tiff.imread(file_path)\n\nprint(\"Image shape:\", img.shape)\n\n# Select two neighboring spectral bands\nband_60 = img[:, :, 60]\nband_61 = img[:, :, 61]\n\n# Calculate mean of each band\nprint(\"Band 60 Mean:\", band_60.mean())\nprint(\"Band 61 Mean:\", band_61.mean())\n\n# Calculate absolute difference between the two bands\ndifference = np.abs(band_60.astype(float) - band_61.astype(float))\n\nprint(\"Mean Absolute Difference:\", difference.mean())\nprint(\"Maximum Difference:\", difference.max())\n\n# Plot the two bands\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.imshow(band_60, cmap=\"gray\")\nplt.title(\"Band 60\")\nplt.axis(\"off\")\n\nplt.subplot(1, 2, 2)\nplt.imshow(band_61, cmap=\"gray\")\nplt.title(\"Band 61\")\nplt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:14:06.625223Z","iopub.execute_input":"2026-08-16T18:14:06.626106Z","iopub.status.idle":"2026-08-16T18:14:06.761423Z","shell.execute_reply.started":"2026-08-16T18:14:06.626069Z","shell.execute_reply":"2026-08-16T18:14:06.760601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Clean Rust folder\nrust_folder = \"/kaggle/working/Rust_Clean\"\n\n# Select image\nfile_path = os.path.join(rust_folder, \"hyper (1).tif\")\n\n# Read image\nimg = tiff.imread(file_path)\n\nprint(\"Image shape:\", img.shape)\n\n# Store differences between neighboring bands\nmean_differences = []\n\n# Compare Band 0 with Band 1,\n# Band 1 with Band 2, ...,\n# Band 123 with Band 124\nfor i in range(img.shape[2] - 1):\n\n    band_current = img[:, :, i].astype(float)\n    band_next = img[:, :, i + 1].astype(float)\n\n    # Pixel-by-pixel absolute difference\n    difference = np.abs(band_current - band_next)\n\n    # Average difference\n    mean_difference = difference.mean()\n\n    mean_differences.append(mean_difference)\n\n# Print results\nfor i, diff in enumerate(mean_differences):\n\n    print(\n        f\"Band {i} ↔ Band {i+1} : \"\n        f\"Mean Absolute Difference = {diff:.2f}\"\n    )\n\n# Find the largest differences\nlargest_indices = np.argsort(mean_differences)[-10:][::-1]\n\nprint(\"\\nTop 10 largest neighboring-band differences:\")\n\nfor i in largest_indices:\n\n    print(\n        f\"Band {i} ↔ Band {i+1} : \"\n        f\"{mean_differences[i]:.2f}\"\n    )\n\n# Plot neighboring-band differences\nplt.figure(figsize=(12, 5))\n\nplt.plot(\n    range(len(mean_differences)),\n    mean_differences\n)\n\nplt.xlabel(\"Band Pair\")\nplt.ylabel(\"Mean Absolute Difference\")\nplt.title(\"Neighboring Spectral Band Differences\")\n\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:17:58.671598Z","iopub.execute_input":"2026-08-16T18:17:58.672228Z","iopub.status.idle":"2026-08-16T18:17:58.995097Z","shell.execute_reply.started":"2026-08-16T18:17:58.672192Z","shell.execute_reply":"2026-08-16T18:17:58.994323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Clean Rust folder\nrust_folder = \"/kaggle/working/Rust_Clean\"\n\n# Select image\nfile_path = os.path.join(rust_folder, \"hyper (1).tif\")\n\n# Read image\nimg = tiff.imread(file_path)\n\nprint(\"Image shape:\", img.shape)\n\nprint(\"\\nChecking all bands...\\n\")\n\nsuspicious_bands = []\n\nfor band in range(img.shape[2]):\n\n    # Extract one spectral band\n    band_data = img[:, :, band]\n\n    # Calculate statistics\n    band_min = band_data.min()\n    band_max = band_data.max()\n    band_mean = band_data.mean()\n    band_std = band_data.std()\n\n    # Number of unique pixel values\n    unique_values = np.unique(band_data).size\n\n    print(\n        f\"Band {band:3d} | \"\n        f\"Min: {band_min:6.0f} | \"\n        f\"Max: {band_max:6.0f} | \"\n        f\"Mean: {band_mean:8.2f} | \"\n        f\"Std: {band_std:8.2f} | \"\n        f\"Unique: {unique_values}\"\n    )\n\n    # Check for a constant/dead band\n    if band_std == 0:\n        suspicious_bands.append(band)\n\nprint(\"\\n-----------------------------------\")\nprint(\"Suspicious / constant bands:\")\nprint(suspicious_bands)\nprint(\"Number of suspicious bands:\", len(suspicious_bands))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:21:34.586580Z","iopub.execute_input":"2026-08-16T18:21:34.586937Z","iopub.status.idle":"2026-08-16T18:21:34.644155Z","shell.execute_reply.started":"2026-08-16T18:21:34.586902Z","shell.execute_reply":"2026-08-16T18:21:34.643244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input folders\nrust_folder = \"/kaggle/working/Rust_Clean\"\nhealthy_folder = \"/kaggle/working/Healthy_Clean\"\n\nfolders = {\n    \"Rust\": rust_folder,\n    \"Healthy\": healthy_folder\n}\n\n# Store how many images have a suspicious band\nsuspicious_count = {}\n\ntotal_images = 0\n\nfor class_name, input_folder in folders.items():\n\n    print(f\"\\nChecking {class_name} images...\")\n\n    for file in sorted(os.listdir(input_folder)):\n\n        if not file.endswith(\".tif\"):\n            continue\n\n        filepath = os.path.join(input_folder, file)\n\n        img = tiff.imread(filepath)\n\n        total_images += 1\n\n        # img shape = Height, Width, Bands\n        num_bands = img.shape[2]\n\n        for band in range(num_bands):\n\n            band_data = img[:, :, band]\n\n            # Check for completely constant band\n            if np.min(band_data) == np.max(band_data):\n\n                suspicious_count[band] = suspicious_count.get(band, 0) + 1\n\n            # Check for completely zero band\n            elif np.all(band_data == 0):\n\n                suspicious_count[band] = suspicious_count.get(band, 0) + 1\n\n            # Check for completely saturated band\n            elif np.all(band_data == 65535):\n\n                suspicious_count[band] = suspicious_count.get(band, 0) + 1\n\n\nprint(\"\\n\" + \"-\" * 50)\nprint(\"CHECK COMPLETE\")\nprint(\"-\" * 50)\n\nprint(\"Total images checked:\", total_images)\n\nif len(suspicious_count) == 0:\n\n    print(\"\\nNo suspicious bands found!\")\n\nelse:\n\n    print(\"\\nSuspicious bands:\")\n\n    for band, count in sorted(suspicious_count.items()):\n\n        print(\n            f\"Band {band}: \"\n            f\"suspicious in {count} image(s)\"\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T18:23:07.993466Z","iopub.execute_input":"2026-08-16T18:23:07.994316Z","iopub.status.idle":"2026-08-16T18:23:10.203717Z","shell.execute_reply.started":"2026-08-16T18:23:07.994267Z","shell.execute_reply":"2026-08-16T18:23:10.202943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Rust_Clean\"\n\nall_band_scores = []\n\nfiles = sorted([\n    f for f in os.listdir(folder)\n    if f.endswith(\".tif\")\n])\n\nfor file in files:\n\n    img = tiff.imread(os.path.join(folder, file)).astype(np.float32)\n\n    # Keep only first 125 bands if necessary\n    img = img[:, :, :125]\n\n    # First and second spectral differences\n    diff1 = np.diff(img, axis=2)\n    diff2 = np.diff(diff1, axis=2)\n\n    # Mean absolute second difference for each spectral position\n    band_score = np.mean(np.abs(diff2), axis=(0, 1))\n\n    all_band_scores.append(band_score)\n\n# Convert to array\nall_band_scores = np.array(all_band_scores)\n\n# Average across all images\nmean_band_score = np.mean(all_band_scores, axis=0)\n\n# Standard deviation across images\nstd_band_score = np.std(all_band_scores, axis=0)\n\nprint(\"Number of images:\", len(files))\nprint(\"Number of spectral positions:\", len(mean_band_score))\n\nprint(\"\\nTop 15 spectral positions\")\nprint(\"--------------------------\")\n\ntop = np.argsort(mean_band_score)[::-1][:15]\n\nfor i in top:\n\n    print(\n        f\"Position {i + 1}: \"\n        f\"Mean = {mean_band_score[i]:.4f}, \"\n        f\"Std = {std_band_score[i]:.4f}\"\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:11:39.157907Z","iopub.execute_input":"2026-08-16T20:11:39.162913Z","iopub.status.idle":"2026-08-16T20:11:39.991424Z","shell.execute_reply.started":"2026-08-16T20:11:39.162688Z","shell.execute_reply":"2026-08-16T20:11:39.990152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Calculate overall statistics\noverall_mean = np.mean(mean_band_score)\noverall_std = np.std(mean_band_score)\n\nthreshold = overall_mean + 3 * overall_std\n\nprint(\"Overall mean:\", overall_mean)\nprint(\"Overall std :\", overall_std)\nprint(\"Noise candidate threshold:\", threshold)\n\n# Find candidate positions\nnoise_candidates = np.where(\n    mean_band_score > threshold\n)[0]\n\nprint(\"\\nPotential spectral noise positions:\")\nprint(\"------------------------------------\")\n\nfor i in noise_candidates:\n    print(\n        f\"Position {i + 1}: \"\n        f\"{mean_band_score[i]:.4f}\"\n    )\n\nprint(\"\\nTotal candidates:\",\n      len(noise_candidates))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:14:15.189602Z","iopub.execute_input":"2026-08-16T20:14:15.190368Z","iopub.status.idle":"2026-08-16T20:14:15.200335Z","shell.execute_reply.started":"2026-08-16T20:14:15.190320Z","shell.execute_reply":"2026-08-16T20:14:15.199507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compare the first two positions with the rest\n\nprint(\"Potential noise candidates:\")\nprint(\"----------------------------\")\n\nfor pos in [0, 1]:\n    print(\n        f\"Position {pos + 1}: \"\n        f\"Mean = {mean_band_score[pos]:.4f}, \"\n        f\"Std = {std_band_score[pos]:.4f}\"\n    )\n\nprint(\"\\nPercentage above overall mean:\")\n\nfor pos in [0, 1]:\n    percentage = (\n        mean_band_score[pos] / overall_mean\n    ) * 100\n\n    print(\n        f\"Position {pos + 1}: \"\n        f\"{percentage:.1f}% of overall mean\"\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:15:28.416753Z","iopub.execute_input":"2026-08-16T20:15:28.421486Z","iopub.status.idle":"2026-08-16T20:15:28.451300Z","shell.execute_reply.started":"2026-08-16T20:15:28.421272Z","shell.execute_reply":"2026-08-16T20:15:28.449404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check the first 5 raw spectral bands across all Rust images\n\nfirst_band_stats = []\n\nfor file in files:\n    img = tiff.imread(\n        os.path.join(folder, file)\n    ).astype(np.float32)\n\n    img = img[:, :, :125]\n\n    for band in range(5):\n        first_band_stats.append([\n            band + 1,\n            np.mean(img[:, :, band]),\n            np.std(img[:, :, band]),\n            np.min(img[:, :, band]),\n            np.max(img[:, :, band])\n        ])\n\nfirst_band_stats = np.array(first_band_stats)\n\nprint(\"First 5 spectral bands\")\nprint(\"----------------------\")\n\nfor band in range(5):\n    data = first_band_stats[\n        first_band_stats[:, 0] == band + 1\n    ]\n\n    print(\n        f\"Band {band + 1}: \"\n        f\"Mean={np.mean(data[:,1]):.2f}, \"\n        f\"Std={np.mean(data[:,2]):.2f}, \"\n        f\"Min={np.min(data[:,3]):.2f}, \"\n        f\"Max={np.max(data[:,4]):.2f}\"\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:17:09.269934Z","iopub.execute_input":"2026-08-16T20:17:09.270418Z","iopub.status.idle":"2026-08-16T20:17:09.658817Z","shell.execute_reply.started":"2026-08-16T20:17:09.270384Z","shell.execute_reply":"2026-08-16T20:17:09.657851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Healthy_Clean\"\n\nall_band_scores = []\n\nfiles = sorted([\n    f for f in os.listdir(folder)\n    if f.endswith(\".tif\")\n])\n\nfor file in files:\n\n    img = tiff.imread(\n        os.path.join(folder, file)\n    ).astype(np.float32)\n\n    img = img[:, :, :125]\n\n    diff1 = np.diff(img, axis=2)\n    diff2 = np.diff(diff1, axis=2)\n\n    band_score = np.mean(\n        np.abs(diff2),\n        axis=(0, 1)\n    )\n\n    all_band_scores.append(band_score)\n\nall_band_scores = np.array(all_band_scores)\n\nmean_band_score = np.mean(\n    all_band_scores,\n    axis=0\n)\n\nstd_band_score = np.std(\n    all_band_scores,\n    axis=0\n)\n\noverall_mean = np.mean(mean_band_score)\noverall_std = np.std(mean_band_score)\n\nthreshold = overall_mean + 3 * overall_std\n\nnoise_candidates = np.where(\n    mean_band_score > threshold\n)[0]\n\nprint(\"Healthy images:\", len(files))\nprint(\"Overall mean:\", overall_mean)\nprint(\"Overall std:\", overall_std)\nprint(\"Noise candidate threshold:\", threshold)\n\nprint(\"\\nPotential spectral noise positions:\")\nfor i in noise_candidates:\n    print(\n        f\"Position {i+1}: \"\n        f\"{mean_band_score[i]:.4f}\"\n    )\n\nprint(\"Total candidates:\", len(noise_candidates))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:22:42.635500Z","iopub.execute_input":"2026-08-16T20:22:42.635902Z","iopub.status.idle":"2026-08-16T20:22:43.407352Z","shell.execute_reply.started":"2026-08-16T20:22:42.635868Z","shell.execute_reply":"2026-08-16T20:22:43.406333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check the first 5 raw spectral bands across all Rust images\n\nfirst_band_stats = []\n\nfor file in files:\n    img = tiff.imread(\n        os.path.join(folder, file)\n    ).astype(np.float32)\n\n    img = img[:, :, :125]\n\n    for band in range(5):\n        first_band_stats.append([\n            band + 1,\n            np.mean(img[:, :, band]),\n            np.std(img[:, :, band]),\n            np.min(img[:, :, band]),\n            np.max(img[:, :, band])\n        ])\n\nfirst_band_stats = np.array(first_band_stats)\n\nprint(\"First 5 spectral bands\")\nprint(\"----------------------\")\n\nfor band in range(5):\n    data = first_band_stats[\n        first_band_stats[:, 0] == band + 1\n    ]\n\n    print(\n        f\"Band {band + 1}: \"\n        f\"Mean={np.mean(data[:,1]):.2f}, \"\n        f\"Std={np.mean(data[:,2]):.2f}, \"\n        f\"Min={np.min(data[:,3]):.2f}, \"\n        f\"Max={np.max(data[:,4]):.2f}\"\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-16T20:33:06.473202Z","iopub.execute_input":"2026-08-16T20:33:06.477919Z","iopub.status.idle":"2026-08-16T20:33:06.871579Z","shell.execute_reply.started":"2026-08-16T20:33:06.477683Z","shell.execute_reply":"2026-08-16T20:33:06.870014Z"}},"outputs":[],"execution_count":null}]}