{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":100385,"databundleVersionId":12076007,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# EDA of Spectral Data\n\nThis notebook uses the spectral package to visualize the images in the training set.\n\nI am absolutely a novice analyzing this type of data, so this is my first effort.\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-05-18T16:10:25.078812Z","iopub.execute_input":"2025-05-18T16:10:25.079127Z","iopub.status.idle":"2025-05-18T16:10:25.461992Z","shell.execute_reply.started":"2025-05-18T16:10:25.079105Z","shell.execute_reply":"2025-05-18T16:10:25.460666Z"}}},{"cell_type":"code","source":"%%capture \n!pip install -q spectral\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T16:10:32.682436Z","iopub.execute_input":"2025-05-18T16:10:32.682940Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport spectral as spy\nimport matplotlib.pyplot as plt\nimport math","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Function to Visualize Spectral images\ndef visualize_hyperspectral(image_id, dataset=\"Train\", mode=\"rgb\", bands=(60, 30, 10)):\n    # Load the .npy file\n    image_folder = image_id[0]\n    file_path = f\"/kaggle/input/beyond-visible-spectrum-ai-for-agriculture-2025p2/{dataset}/{image_folder}/{image_id}.npy\"\n    image_data = np.load(file_path)\n\n    if image_data.ndim != 3:\n        raise ValueError(\"Expected a 3D hyperspectral image (H, W, C)\")\n\n    H, W, C = image_data.shape\n\n    if mode == \"rgb\":\n        # Show false-color RGB image using selected bands\n        if max(bands) >= C:\n            raise ValueError(f\"Bands {bands} exceed available channels (0–{C-1})\")\n        view = spy.imshow(image_data, bands=bands)\n        plt.title(f\"False RGB Composite (bands={bands})\")\n        plt.show()\n\n    elif mode == \"grid\":\n        # Show all bands in a grid\n        grid_size = int(math.ceil(math.sqrt(C)))\n        fig, axes = plt.subplots(grid_size, grid_size, figsize=(15, 15))\n        axes = axes.flatten()\n        for i in range(C):\n            ax = axes[i]\n            ax.imshow(image_data[:, :, i], cmap='gray')\n            ax.axis('off')\n            ax.set_title(f'Band {i+1}', fontsize=6)\n        for j in range(C, len(axes)):\n            axes[j].axis('off')\n        plt.tight_layout()\n        plt.show()\n\n    elif mode == \"interactive\":\n        # Launch SPy interactive viewer\n        view = spy.imshow(image_data, bands=bands)\n        print(\"Click on a pixel in the viewer to see its spectral signature.\")\n        return view\n\n    else:\n        raise ValueError(\"Invalid mode. Choose from 'rgb', 'grid', or 'interactive'.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T16:12:10.531867Z","iopub.execute_input":"2025-05-18T16:12:10.532221Z","iopub.status.idle":"2025-05-18T16:12:10.544805Z","shell.execute_reply.started":"2025-05-18T16:12:10.532189Z","shell.execute_reply":"2025-05-18T16:12:10.543748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"visualize_hyperspectral(\"0100\", mode=\"rgb\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T16:09:31.617954Z","iopub.execute_input":"2025-05-18T16:09:31.618309Z","iopub.status.idle":"2025-05-18T16:09:31.969933Z","shell.execute_reply.started":"2025-05-18T16:09:31.618283Z","shell.execute_reply":"2025-05-18T16:09:31.968778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#visualize_hyperspectral(\"0100\", mode=\"pca\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"visualize_hyperspectral(\"0100\", mode=\"grid\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T16:09:41.925789Z","iopub.execute_input":"2025-05-18T16:09:41.926403Z","iopub.status.idle":"2025-05-18T16:09:55.740957Z","shell.execute_reply.started":"2025-05-18T16:09:41.926370Z","shell.execute_reply":"2025-05-18T16:09:55.738461Z"}},"outputs":[],"execution_count":null}]}