{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Check the train dataset","metadata":{}},{"cell_type":"code","source":"import os, glob\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nDATASET_FOLDER = \"/kaggle/input/mayo-clinic-strip-ai/\"\ndf_train = pd.read_csv(os.path.join(DATASET_FOLDER, \"train.csv\"))\ndisplay(df_train.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-24T22:20:46.14628Z","iopub.execute_input":"2022-07-24T22:20:46.146844Z","iopub.status.idle":"2022-07-24T22:20:46.178238Z","shell.execute_reply.started":"2022-07-24T22:20:46.146803Z","shell.execute_reply":"2022-07-24T22:20:46.177177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## what is the class frequency","metadata":{}},{"cell_type":"code","source":"label_freq = df_train[[\"label\"]].value_counts() / len(df_train)\nprint(label_freq)\n_= df_train[[\"label\"]].value_counts().plot.pie(autopct='%1.1f%%', ylabel=\"label\")","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:20:46.180289Z","iopub.execute_input":"2022-07-24T22:20:46.180844Z","iopub.status.idle":"2022-07-24T22:20:46.295332Z","shell.execute_reply.started":"2022-07-24T22:20:46.18081Z","shell.execute_reply":"2022-07-24T22:20:46.293974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head ../input/mayo-clinic-strip-ai/sample_submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:20:46.297689Z","iopub.execute_input":"2022-07-24T22:20:46.298727Z","iopub.status.idle":"2022-07-24T22:20:47.1245Z","shell.execute_reply.started":"2022-07-24T22:20:46.298673Z","shell.execute_reply":"2022-07-24T22:20:47.122938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bowse the test images","metadata":{}},{"cell_type":"code","source":"import gc\nfrom PIL import Image\nfrom tqdm.auto import tqdm\n\nImage.MAX_IMAGE_PIXELS = 25_000_000_000\nls_imgs = glob.glob(os.path.join(DATASET_FOLDER, \"test\", \"*.tif\"))\n\nsizes = []\nfor img_path in tqdm(ls_imgs):\n    img = Image.open(img_path)\n    sizes.append({\"img_height\": img.height, \"img_width\": img.width})\n    del img; gc.collect()\n\ndf_sizes = pd.DataFrame(sizes)\nfor col in df_sizes.columns:\n    df_train[col] = df_sizes[col]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:20:47.129511Z","iopub.execute_input":"2022-07-24T22:20:47.13036Z","iopub.status.idle":"2022-07-24T22:20:47.791319Z","shell.execute_reply.started":"2022-07-24T22:20:47.130308Z","shell.execute_reply":"2022-07-24T22:20:47.790137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\nfor col in (\"img_width\", \"img_height\"):\n    df_sizes[col] = [round(i / 1000) * 1000 for i in df_sizes[col]]\n\ndf_sizes = df_sizes.groupby([\"img_width\", \"img_height\"], as_index=False).size()\n# display(df_sizes.head())\nfig = px.scatter(df_sizes, x=\"img_width\", y=\"img_height\", size=\"size\", height=400, width=400)\nfig.update_yaxes(scaleanchor=\"x\", scaleratio=1)\nfig.show() ","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:20:47.79303Z","iopub.execute_input":"2022-07-24T22:20:47.794062Z","iopub.status.idle":"2022-07-24T22:20:47.867078Z","shell.execute_reply.started":"2022-07-24T22:20:47.794014Z","shell.execute_reply":"2022-07-24T22:20:47.865938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading test images with pyvips...","metadata":{}},{"cell_type":"code","source":"!conda install ../input/conda-pkg-pyvips-download-4-offline/conda-pkgs/*.tar.bz2 --offline","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-24T22:20:47.868572Z","iopub.execute_input":"2022-07-24T22:20:47.869503Z","iopub.status.idle":"2022-07-24T22:20:55.806597Z","shell.execute_reply.started":"2022-07-24T22:20:47.869464Z","shell.execute_reply":"2022-07-24T22:20:55.805174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir -p /kaggle/temp/images\n\nos.environ['VIPS_CONCURRENCY'] = '2'\nos.environ['VIPS_DISC_THRESHOLD'] = '7gb'","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:20:55.808448Z","iopub.execute_input":"2022-07-24T22:20:55.809117Z","iopub.status.idle":"2022-07-24T22:20:56.620179Z","shell.execute_reply.started":"2022-07-24T22:20:55.809067Z","shell.execute_reply":"2022-07-24T22:20:56.618635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport pyvips\nfrom tqdm.auto import tqdm\n\nfor tif_path in tqdm(ls_imgs):\n    name, _ = os.path.splitext(os.path.basename(tif_path))\n    png_path = os.path.join(\"/kaggle/temp/images\", f\"{name}.png\")\n    pyvips.Image.thumbnail(tif_path, 2000).write_to_file(png_path)\n    gc.collect()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-24T22:20:56.622369Z","iopub.execute_input":"2022-07-24T22:20:56.622784Z","iopub.status.idle":"2022-07-24T22:22:55.474606Z","shell.execute_reply.started":"2022-07-24T22:20:56.62275Z","shell.execute_reply":"2022-07-24T22:22:55.473261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"preds = []\nfor p_img in ls_imgs:\n    name, _ = os.path.splitext(os.path.basename(p_img))\n    rnd = (np.random.random() - 0.5) * 0.2\n    preds.append({\n        \"CE\": round(0.55 + rnd, 9),\n        \"LAA\": round(0.45 - rnd, 9),\n        \"patient_id\": name.split(\"_\")[0],\n    })\nsub = pd.DataFrame(preds)\ndisplay(sub.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:22:55.476321Z","iopub.execute_input":"2022-07-24T22:22:55.476792Z","iopub.status.idle":"2022-07-24T22:22:55.495312Z","shell.execute_reply.started":"2022-07-24T22:22:55.476748Z","shell.execute_reply":"2022-07-24T22:22:55.493914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = sub.groupby(\"patient_id\").mean()\nsub[[\"CE\", \"LAA\"]].round(6).to_csv(\"submission.csv\")\n\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:22:55.499526Z","iopub.execute_input":"2022-07-24T22:22:55.500354Z","iopub.status.idle":"2022-07-24T22:22:56.326594Z","shell.execute_reply.started":"2022-07-24T22:22:55.500294Z","shell.execute_reply":"2022-07-24T22:22:56.325369Z"},"trusted":true},"execution_count":null,"outputs":[]}]}