{"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":"code","source":"import os\nimport sys\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\n\nfrom glob import glob\nfrom pprint import pprint\nfrom openslide import OpenSlide\nfrom collections import defaultdict\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-22T12:15:58.395687Z","iopub.execute_input":"2022-08-22T12:15:58.396299Z","iopub.status.idle":"2022-08-22T12:16:00.134202Z","shell.execute_reply.started":"2022-08-22T12:15:58.396192Z","shell.execute_reply":"2022-08-22T12:16:00.132743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\ntest_df = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\nother_df = pd.read_csv('../input/mayo-clinic-strip-ai/other.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.13703Z","iopub.execute_input":"2022-08-22T12:16:00.137536Z","iopub.status.idle":"2022-08-22T12:16:00.177949Z","shell.execute_reply.started":"2022-08-22T12:16:00.13749Z","shell.execute_reply":"2022-08-22T12:16:00.176859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.179835Z","iopub.execute_input":"2022-08-22T12:16:00.180777Z","iopub.status.idle":"2022-08-22T12:16:00.212502Z","shell.execute_reply.started":"2022-08-22T12:16:00.18073Z","shell.execute_reply":"2022-08-22T12:16:00.211132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.215692Z","iopub.execute_input":"2022-08-22T12:16:00.216404Z","iopub.status.idle":"2022-08-22T12:16:00.233468Z","shell.execute_reply.started":"2022-08-22T12:16:00.216358Z","shell.execute_reply":"2022-08-22T12:16:00.232163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.235519Z","iopub.execute_input":"2022-08-22T12:16:00.237045Z","iopub.status.idle":"2022-08-22T12:16:00.254628Z","shell.execute_reply.started":"2022-08-22T12:16:00.236982Z","shell.execute_reply":"2022-08-22T12:16:00.253171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patients_train = train_df['patient_id'].nunique()\npatients_test = test_df['patient_id'].nunique()\npatients_other = other_df['patient_id'].nunique()\n\nprint(f\"Number of unique patients in train set: {patients_train}\")\nprint(f\"Number of unique patients in test set: {patients_test}\")\nprint(f\"Number of unique patients in the 'other' set: {patients_other}\")","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.25886Z","iopub.execute_input":"2022-08-22T12:16:00.259257Z","iopub.status.idle":"2022-08-22T12:16:00.279241Z","shell.execute_reply.started":"2022-08-22T12:16:00.259222Z","shell.execute_reply":"2022-08-22T12:16:00.278043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train_df.groupby('label')['label'].count()\nprint(\"Information about\",labels)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.280833Z","iopub.execute_input":"2022-08-22T12:16:00.281671Z","iopub.status.idle":"2022-08-22T12:16:00.294542Z","shell.execute_reply.started":"2022-08-22T12:16:00.281607Z","shell.execute_reply":"2022-08-22T12:16:00.293323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"centers = train_df.groupby(\"center_id\")['center_id'].count()\nprint(\"Information about\",centers)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.296294Z","iopub.execute_input":"2022-08-22T12:16:00.297069Z","iopub.status.idle":"2022-08-22T12:16:00.304924Z","shell.execute_reply.started":"2022-08-22T12:16:00.297032Z","shell.execute_reply":"2022-08-22T12:16:00.303888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Other types of blood clots not a part of this competition:\")\nprint(list(other_df['other_specified'].unique()))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.306299Z","iopub.execute_input":"2022-08-22T12:16:00.306823Z","iopub.status.idle":"2022-08-22T12:16:00.316493Z","shell.execute_reply.started":"2022-08-22T12:16:00.30679Z","shell.execute_reply":"2022-08-22T12:16:00.315252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train_df.groupby('label')['label'].count()\ncenters = train_df.groupby(\"center_id\")['center_id'].count()\n\nfig, ax = plt.subplots(1,2, figsize=(16,5))\nsns.barplot(x=labels.index, y=labels.values, ax=ax[0])\nax[0].set_title(\"Distribution of a target variable\"), ax[0].set_ylabel(\"count\")\nsns.barplot(x=centers.index, y=centers.values, ax=ax[1])\nax[1].set_title(\"Images per clinic center\"), ax[1].set_ylabel(\"count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.318445Z","iopub.execute_input":"2022-08-22T12:16:00.319927Z","iopub.status.idle":"2022-08-22T12:16:00.73161Z","shell.execute_reply.started":"2022-08-22T12:16:00.319875Z","shell.execute_reply":"2022-08-22T12:16:00.730222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/train/*\")\ntest_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/test/*\")\nother_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/other/*\")\nprint(f\"Number of images in a training set: {len(train_images)}\")\nprint(f\"Number of images in a training set: {len(test_images)}\")\nprint(f\"Number of other: {len(other_images)}\")","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:00.735862Z","iopub.execute_input":"2022-08-22T12:16:00.73627Z","iopub.status.idle":"2022-08-22T12:16:01.080079Z","shell.execute_reply.started":"2022-08-22T12:16:00.736235Z","shell.execute_reply":"2022-08-22T12:16:01.078729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_prop = defaultdict(list)    # defaultdict:provides the default value for a nonexistent key\nfor i, path in enumerate(train_images):\n    img_path = train_images[i]\n    slide = OpenSlide(img_path)\n    img_prop['image_id'].append(img_path[-12:-4])\n    img_prop['width'].append(slide.dimensions[0])\n    img_prop['height'].append(slide.dimensions[1])\n    img_prop['size'].append(round(os.path.getsize(img_path) / 1e6, 2))\n    img_prop['path'].append(img_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:01.081499Z","iopub.execute_input":"2022-08-22T12:16:01.082133Z","iopub.status.idle":"2022-08-22T12:16:23.129892Z","shell.execute_reply.started":"2022-08-22T12:16:01.082098Z","shell.execute_reply":"2022-08-22T12:16:23.128515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data = pd.DataFrame(img_prop)\nimage_data['img_aspect_ratio'] = image_data['width']/image_data['height']\nimage_data.sort_values(by='image_id', inplace=True)\nimage_data.reset_index(inplace=True, drop=True)\n\nimage_data = image_data.merge(train_df, on='image_id')\nimage_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:23.131409Z","iopub.execute_input":"2022-08-22T12:16:23.131901Z","iopub.status.idle":"2022-08-22T12:16:23.173453Z","shell.execute_reply.started":"2022-08-22T12:16:23.131857Z","shell.execute_reply":"2022-08-22T12:16:23.171823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(16,5))\nsns.histplot(x='size', data = image_data, bins=100, ax=ax[0])\nax[0].set_title(\"Distribution of size\"), ax[0].set_ylabel(\"%\")\nsns.histplot(x='img_aspect_ratio', data = image_data, bins=100, ax=ax[1])\nax[1].set_title(\"Image aspect ratio\"), ax[1].set_ylabel(\"%\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:23.177128Z","iopub.execute_input":"2022-08-22T12:16:23.178343Z","iopub.status.idle":"2022-08-22T12:16:23.866603Z","shell.execute_reply.started":"2022-08-22T12:16:23.17829Z","shell.execute_reply":"2022-08-22T12:16:23.86529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slide = OpenSlide(img_path) # opening a full slide\n\nregion = (0, 0) # location of the top left pixel\nlevel = 0 # level of the picture (we have only 0)\nsize = (10000, 10000) # region size in pixels\n\nregion = slide.read_region(region, level, size)\n\nplt.figure(figsize=(10, 10))\nplt.imshow(region)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:16:23.868213Z","iopub.execute_input":"2022-08-22T12:16:23.86913Z","iopub.status.idle":"2022-08-22T12:16:49.39637Z","shell.execute_reply.started":"2022-08-22T12:16:23.869091Z","shell.execute_reply":"2022-08-22T12:16:49.394899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}