{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":83633,"databundleVersionId":9850585,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Gettting a few libraries","metadata":{}},{"cell_type":"code","source":"import random\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport os, json\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-10-18T15:27:42.372728Z","iopub.execute_input":"2024-10-18T15:27:42.373104Z","iopub.status.idle":"2024-10-18T15:27:42.741230Z","shell.execute_reply.started":"2024-10-18T15:27:42.373068Z","shell.execute_reply":"2024-10-18T15:27:42.740150Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Visualization\n\n## Classes weights in training set","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/eclipse-megamovie/train.csv\")\ndf_train.hist(bins=20)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T15:27:42.743538Z","iopub.execute_input":"2024-10-18T15:27:42.744099Z","iopub.status.idle":"2024-10-18T15:27:43.119478Z","shell.execute_reply.started":"2024-10-18T15:27:42.744050Z","shell.execute_reply":"2024-10-18T15:27:43.118338Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Reading labels mapping","metadata":{}},{"cell_type":"code","source":"import json\nwith open(\"/kaggle/input/eclipse-megamovie/label_num_to_phase_map.json\") as f:\n    labels_map = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T15:27:43.121105Z","iopub.execute_input":"2024-10-18T15:27:43.121458Z","iopub.status.idle":"2024-10-18T15:27:43.128726Z","shell.execute_reply.started":"2024-10-18T15:27:43.121423Z","shell.execute_reply":"2024-10-18T15:27:43.127719Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_map","metadata":{"execution":{"iopub.status.busy":"2024-10-18T15:27:43.130246Z","iopub.execute_input":"2024-10-18T15:27:43.131068Z","iopub.status.idle":"2024-10-18T15:27:43.137944Z","shell.execute_reply.started":"2024-10-18T15:27:43.131031Z","shell.execute_reply":"2024-10-18T15:27:43.136832Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Histograms of width, height on train images","metadata":{}},{"cell_type":"code","source":"# Specify the folder path containing images\nfolder_path = '/kaggle/input/eclipse-megamovie/train'\n\n# Get list of image files in the folder\nimage_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.jpg', '.jpeg'))]\n\n# Create an empty DataFrame\ndata = {col:[] for col in ['width', 'height']}\n\n# Saving images and filename\nimages = []\npaths = []\n\n# Iterate through image files and add their shapes to the DataFrame\nfor image_file in image_files:\n    file_path = os.path.join(folder_path, image_file)\n    paths.append(file_path)\n    with Image.open(file_path) as img:\n        images.append(img)\n        width, height = img.size\n        channels = len(img.getbands())\n        for col, value in {'width': width, 'height': height}.items():\n            data[col].append(value)\n\n# Display the resulting DataFrame\nshape_df = pd.DataFrame(data)\nshape_df.plot.scatter(\"width\", \"height\")\nshape_df.hist()","metadata":{"execution":{"iopub.status.busy":"2024-10-18T15:29:00.327166Z","iopub.execute_input":"2024-10-18T15:29:00.328067Z","iopub.status.idle":"2024-10-18T15:29:01.909800Z","shell.execute_reply.started":"2024-10-18T15:29:00.328011Z","shell.execute_reply":"2024-10-18T15:29:01.908541Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Plotting images from each class","metadata":{}},{"cell_type":"code","source":"# Function to get label name from image path\ndef get_label(path):\n    basename = os.path.basename(path)\n    label = df_train.loc[df_train[\"image_id\"] == basename].iloc[0,1]\n    label_name = list(labels_map.keys())[label]\n    return label_name","metadata":{"execution":{"iopub.status.busy":"2024-10-18T13:30:30.470420Z","iopub.execute_input":"2024-10-18T13:30:30.470777Z","iopub.status.idle":"2024-10-18T13:30:30.476876Z","shell.execute_reply.started":"2024-10-18T13:30:30.470731Z","shell.execute_reply":"2024-10-18T13:30:30.475762Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for label_name, index in labels_map.items():\n    print(label_name, index)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-18T13:35:44.516812Z","iopub.execute_input":"2024-10-18T13:35:44.517953Z","iopub.status.idle":"2024-10-18T13:35:44.523886Z","shell.execute_reply.started":"2024-10-18T13:35:44.517872Z","shell.execute_reply":"2024-10-18T13:35:44.522834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_nums = df_train.label.unique().tolist()\nlabels_nums","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-18T13:51:02.273698Z","iopub.execute_input":"2024-10-18T13:51:02.274168Z","iopub.status.idle":"2024-10-18T13:51:02.282411Z","shell.execute_reply.started":"2024-10-18T13:51:02.274127Z","shell.execute_reply":"2024-10-18T13:51:02.281168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_labels = len(labels_nums)\nimage_per_class = 4\n\nfig, axes = plt.subplots(n_labels, image_per_class, figsize=(12, 4*n_labels))\n\nfor label_num in range(n_labels):\n    for j, (_, row) in enumerate(df_train[df_train[\"label\"]==label_num].sample(image_per_class).iterrows()):\n        path = os.path.join(\"/kaggle/input/eclipse-megamovie/train\", row.image_id)\n        img = Image.open(path)\n        axes[label_num, j].imshow(img)\n        axes[label_num, j].axis('off')\n        axes[label_num, j].set_title(get_label(path))\n\nplt.tight_layout()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-18T14:00:09.861422Z","iopub.execute_input":"2024-10-18T14:00:09.862817Z","iopub.status.idle":"2024-10-18T14:01:28.305555Z","shell.execute_reply.started":"2024-10-18T14:00:09.862756Z","shell.execute_reply":"2024-10-18T14:01:28.303875Z"}},"outputs":[],"execution_count":null}]}