{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":112899,"databundleVersionId":13449579,"sourceType":"competition"}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\n\nINPUT_CSV = \"/kaggle/input/grand-xray-slam-division-a/train1.csv\"\nOUTPUT_CSV = \"/kaggle/working/Filtered\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.059483Z","iopub.execute_input":"2025-12-24T10:37:06.059849Z","iopub.status.idle":"2025-12-24T10:37:06.065304Z","shell.execute_reply.started":"2025-12-24T10:37:06.059819Z","shell.execute_reply":"2025-12-24T10:37:06.064265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(INPUT_CSV)\ndf.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.067655Z","iopub.execute_input":"2025-12-24T10:37:06.068154Z","iopub.status.idle":"2025-12-24T10:37:06.324708Z","shell.execute_reply.started":"2025-12-24T10:37:06.068082Z","shell.execute_reply":"2025-12-24T10:37:06.323589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#All diseases in the dataset\nALL_DISEASE_COLUMNS = [\n    \"Atelectasis\",\n    \"Cardiomegaly\",\n    \"Consolidation\",\n    \"Edema\",\n    \"Lung Opacity\",\n    \"Pleural Effusion\",\n    \"Pneumonia\",\n    \"Pneumothorax\",\n    \"Support Devices\",\n    \"No Finding\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.325886Z","iopub.execute_input":"2025-12-24T10:37:06.326131Z","iopub.status.idle":"2025-12-24T10:37:06.331563Z","shell.execute_reply.started":"2025-12-24T10:37:06.326108Z","shell.execute_reply":"2025-12-24T10:37:06.330521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TARGET_CLASSES = [\n    \"Pneumothorax\", #Hardest to detect\n    \"Cardiomegaly\", #Easy to detect\n    \"Lung Opacity\", #Most Common in Real World\n    \"Pleural Effusion\", #Very Common in Real World\n    \"Support Devices\" #Easy to Detect\n]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.333039Z","iopub.execute_input":"2025-12-24T10:37:06.333392Z","iopub.status.idle":"2025-12-24T10:37:06.352194Z","shell.execute_reply.started":"2025-12-24T10:37:06.333352Z","shell.execute_reply":"2025-12-24T10:37:06.350975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"META_DATA = [\n    \"Image_name\",\n    \"Sex\",\n    \"ViewCategory\",\n    \"ViewPosition\",\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.354843Z","iopub.execute_input":"2025-12-24T10:37:06.355164Z","iopub.status.idle":"2025-12-24T10:37:06.37304Z","shell.execute_reply.started":"2025-12-24T10:37:06.355133Z","shell.execute_reply":"2025-12-24T10:37:06.371756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#FILTER: Frontal Only\ndf = df[\n    (df[\"ViewCategory\"] == \"Frontal\") &\n    (df[\"ViewPosition\"].isin([\"AP\", \"PA\"]))\n].copy()\n\n#FILTER: Target Diseases\ndf[\"target_positive_count\"] = df[TARGET_CLASSES].sum(axis=1)\ndf = df[df[\"target_positive_count\"] == 1].copy()\n\nNON_TARGET_CLASSES = list(set(ALL_DISEASE_COLUMNS) - set(TARGET_CLASSES))\ndf[\"non_target_positive_count\"] = df[NON_TARGET_CLASSES].sum(axis=1)\ndf = df[df[\"non_target_positive_count\"] == 0].copy()\n\n#Assigning Class Lables\ndf[\"label\"] = df[TARGET_CLASSES].idxmax(axis=1)\n\n#Final Cleanup\ndf = df[META_DATA + TARGET_CLASSES]\ndf.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.374794Z","iopub.execute_input":"2025-12-24T10:37:06.375738Z","iopub.status.idle":"2025-12-24T10:37:06.468854Z","shell.execute_reply.started":"2025-12-24T10:37:06.375704Z","shell.execute_reply":"2025-12-24T10:37:06.467688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Images Per Class\ndf[\"label\"] = df[TARGET_CLASSES].idxmax(axis=1)\ndf[\"label\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.470025Z","iopub.execute_input":"2025-12-24T10:37:06.470283Z","iopub.status.idle":"2025-12-24T10:37:06.484893Z","shell.execute_reply.started":"2025-12-24T10:37:06.470258Z","shell.execute_reply":"2025-12-24T10:37:06.483734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Total Images = \",len(df))\ndf.to_csv(OUTPUT_CSV, index=False)\nprint(\"The file is saved!!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T10:37:06.486155Z","iopub.execute_input":"2025-12-24T10:37:06.486636Z","iopub.status.idle":"2025-12-24T10:37:06.555362Z","shell.execute_reply.started":"2025-12-24T10:37:06.486572Z","shell.execute_reply":"2025-12-24T10:37:06.5544Z"}},"outputs":[],"execution_count":null}]}