{"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 libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom skimage import io\n\nfrom openslide import OpenSlide\n\nfrom skimage import io, exposure\nfrom skimage.transform import rescale\nfrom skimage.color import rgb2gray","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-09T05:39:09.725196Z","iopub.execute_input":"2022-07-09T05:39:09.725536Z","iopub.status.idle":"2022-07-09T05:39:11.364437Z","shell.execute_reply.started":"2022-07-09T05:39:09.725463Z","shell.execute_reply":"2022-07-09T05:39:11.363327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Context:\n* The goal of this competition is to classify the blood clot origins in ischemic stroke.\n* Using whole slide digital pathology images, we'll build a model that differentiates between the two major acute ischemic stroke (AIS) etiology subtypes: cardiac and large artery atherosclerosis.\n* Our goal is to classify the images into CE(Cardioembolic) or LAA(Large Artery Atherosclerosis) in the test set for each patient.","metadata":{}},{"cell_type":"markdown","source":"#### In this notebook, we are going to get basic understanding of the csv files and the images provided in the dataset","metadata":{}},{"cell_type":"code","source":"# PATHS\nTRAIN_IMAGES = \"../input/mayo-clinic-strip-ai/train/*.tif\"\nTEST_IMAGES = \"../input/mayo-clinic-strip-ai/test/*.tif\"\nTRAIN_CSV = \"../input/mayo-clinic-strip-ai/train.csv\"\nTEST_CSV = \"../input/mayo-clinic-strip-ai/test.csv\"\nSAMPLE_SUB_CSV = \"../input/mayo-clinic-strip-ai/sample_submission.csv\"\nOTHERS_CSV = \"../input/mayo-clinic-strip-ai/other.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.366558Z","iopub.execute_input":"2022-07-09T05:39:11.36687Z","iopub.status.idle":"2022-07-09T05:39:11.373786Z","shell.execute_reply.started":"2022-07-09T05:39:11.366841Z","shell.execute_reply":"2022-07-09T05:39:11.372064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Analyzing csv files","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(TRAIN_CSV)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.375199Z","iopub.execute_input":"2022-07-09T05:39:11.375542Z","iopub.status.idle":"2022-07-09T05:39:11.426794Z","shell.execute_reply.started":"2022-07-09T05:39:11.375507Z","shell.execute_reply":"2022-07-09T05:39:11.425656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.429818Z","iopub.execute_input":"2022-07-09T05:39:11.430636Z","iopub.status.idle":"2022-07-09T05:39:11.438078Z","shell.execute_reply.started":"2022-07-09T05:39:11.430605Z","shell.execute_reply":"2022-07-09T05:39:11.436686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.439286Z","iopub.execute_input":"2022-07-09T05:39:11.439502Z","iopub.status.idle":"2022-07-09T05:39:11.468405Z","shell.execute_reply.started":"2022-07-09T05:39:11.43948Z","shell.execute_reply":"2022-07-09T05:39:11.467433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum() # no nulls in the train data","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.469785Z","iopub.execute_input":"2022-07-09T05:39:11.470024Z","iopub.status.idle":"2022-07-09T05:39:11.482416Z","shell.execute_reply.started":"2022-07-09T05:39:11.470002Z","shell.execute_reply":"2022-07-09T05:39:11.481142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.title(\"Unique values in train columns\")\nunique_counts = train_df.nunique().to_dict()\nax = sns.barplot(list(unique_counts.keys()), list(unique_counts.values()))\nax.bar_label(ax.containers[0])\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.484603Z","iopub.execute_input":"2022-07-09T05:39:11.484896Z","iopub.status.idle":"2022-07-09T05:39:11.68392Z","shell.execute_reply.started":"2022-07-09T05:39:11.484863Z","shell.execute_reply":"2022-07-09T05:39:11.682926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in [\"center_id\",\"image_num\",\"label\"]:\n    print(train_df[i].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.68536Z","iopub.execute_input":"2022-07-09T05:39:11.685667Z","iopub.status.idle":"2022-07-09T05:39:11.695611Z","shell.execute_reply.started":"2022-07-09T05:39:11.685633Z","shell.execute_reply":"2022-07-09T05:39:11.694659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"label\"].value_counts(\"%\").mul(100) # Labels are present in 73:27 ratio","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.696512Z","iopub.execute_input":"2022-07-09T05:39:11.696723Z","iopub.status.idle":"2022-07-09T05:39:11.710299Z","shell.execute_reply.started":"2022-07-09T05:39:11.6967Z","shell.execute_reply":"2022-07-09T05:39:11.709232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_num_counts = train_df['image_num'].value_counts(\"%\").mul(100)\nimage_num_counts.index = image_num_counts.index+1\nax = image_num_counts.plot.bar()\nax.bar_label(ax.containers[0])\nax.set_xlabel(\"No. of images obtained from one patient\")\nax.set_ylabel(\"% of patients\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.714956Z","iopub.execute_input":"2022-07-09T05:39:11.715272Z","iopub.status.idle":"2022-07-09T05:39:11.889412Z","shell.execute_reply.started":"2022-07-09T05:39:11.71524Z","shell.execute_reply":"2022-07-09T05:39:11.887948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# patients with more than 2 clot images\ngrouped_pts = train_df.groupby(\"patient_id\").max().reset_index()\ngrouped_pts[grouped_pts[\"image_num\"]>1]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:11.890861Z","iopub.execute_input":"2022-07-09T05:39:11.892415Z","iopub.status.idle":"2022-07-09T05:39:12.016139Z","shell.execute_reply.started":"2022-07-09T05:39:11.892388Z","shell.execute_reply":"2022-07-09T05:39:12.015155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Insights so far:\n1. There are two label categories: CE has 72% of the share amongst the two categories.\n2. There are a total of 11 unique centers where sample was obtained.\n3. Data contains 754 images obtained from 632 patients.\n4. We have one clot image for 83% of the patients, less than 5% of the patients have more than 2 clot images in this dataset","metadata":{}},{"cell_type":"code","source":"# TEST DATA\ntest_df = pd.read_csv(TEST_CSV)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.017547Z","iopub.execute_input":"2022-07-09T05:39:12.018184Z","iopub.status.idle":"2022-07-09T05:39:12.0317Z","shell.execute_reply.started":"2022-07-09T05:39:12.018151Z","shell.execute_reply":"2022-07-09T05:39:12.031079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.033022Z","iopub.execute_input":"2022-07-09T05:39:12.033543Z","iopub.status.idle":"2022-07-09T05:39:12.040379Z","shell.execute_reply.started":"2022-07-09T05:39:12.03351Z","shell.execute_reply":"2022-07-09T05:39:12.039011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample submission file\nsubmission = pd.read_csv(SAMPLE_SUB_CSV)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.041597Z","iopub.execute_input":"2022-07-09T05:39:12.042335Z","iopub.status.idle":"2022-07-09T05:39:12.061019Z","shell.execute_reply.started":"2022-07-09T05:39:12.042302Z","shell.execute_reply":"2022-07-09T05:39:12.060155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### We have to predict for 4 patient_ids given 4 clot images respectively. All the clot images are from same test center(#11)","metadata":{}},{"cell_type":"markdown","source":"### Analyzing Image Data","metadata":{}},{"cell_type":"code","source":"def read_tif(image_path):\n    image = io.imread(image_path)\n    image = np.squeeze(image) # some images have unnecessary axes with shape 1 --> remove\n    if image.shape[0] == 3: # some images have color as first axis -> swap axes\n        image = image.swapaxes(0,1)\n        image = image.swapaxes(1,2)\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.062094Z","iopub.execute_input":"2022-07-09T05:39:12.062894Z","iopub.status.idle":"2022-07-09T05:39:12.069005Z","shell.execute_reply.started":"2022-07-09T05:39:12.062863Z","shell.execute_reply":"2022-07-09T05:39:12.067946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_file_path = \"../input/mayo-clinic-strip-ai/train/\"+str(train_df[\"image_id\"].iloc[0])+\".tif\"\n# image = read_tif(file_path)\n# image.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.070359Z","iopub.execute_input":"2022-07-09T05:39:12.071122Z","iopub.status.idle":"2022-07-09T05:39:12.087078Z","shell.execute_reply.started":"2022-07-09T05:39:12.071084Z","shell.execute_reply":"2022-07-09T05:39:12.085946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using OpenSlide to open a image and plot zoomed portion of it.\n\nslide = OpenSlide(sample_file_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=(8, 8))\nplt.imshow(region)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:12.100238Z","iopub.execute_input":"2022-07-09T05:39:12.101755Z","iopub.status.idle":"2022-07-09T05:39:41.91509Z","shell.execute_reply.started":"2022-07-09T05:39:12.101699Z","shell.execute_reply":"2022-07-09T05:39:41.914243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Using sklearn to plot image\n\n# img = io.imread(sample_file_path)\n# image_resized = rescale(img, 0.2, anti_aliasing=True)\n# img_gray = rgb2gray(img)\n\n# fig, ax = plt.subplots(1,2, figsize=(15,20))\n# ax[0].imshow(image_resized)\n# ax[1].imshow(img_gray, cmap='gray')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:41:20.499219Z","iopub.execute_input":"2022-07-09T05:41:20.499763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Baseline submission","metadata":{}},{"cell_type":"code","source":"submission[\"CE\"] = train_df[\"label\"].value_counts(\"%\")[\"CE\"]\nsubmission[\"LAA\"] = train_df[\"label\"].value_counts(\"%\")[\"LAA\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:41.916125Z","iopub.execute_input":"2022-07-09T05:39:41.917033Z","iopub.status.idle":"2022-07-09T05:39:41.923178Z","shell.execute_reply.started":"2022-07-09T05:39:41.917006Z","shell.execute_reply":"2022-07-09T05:39:41.92241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:41.924287Z","iopub.execute_input":"2022-07-09T05:39:41.924675Z","iopub.status.idle":"2022-07-09T05:39:41.951355Z","shell.execute_reply.started":"2022-07-09T05:39:41.924646Z","shell.execute_reply":"2022-07-09T05:39:41.950266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T05:39:41.952576Z","iopub.execute_input":"2022-07-09T05:39:41.95334Z","iopub.status.idle":"2022-07-09T05:39:41.962196Z","shell.execute_reply.started":"2022-07-09T05:39:41.953302Z","shell.execute_reply":"2022-07-09T05:39:41.961004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# \n## DO UPVOTE!","metadata":{}}]}