{"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":"Goal of the Competition\n\nThe goal of this competition is to classify the blood clot origins in ischemic stroke. Using whole slide digital pathology images, you'll build a model that differentiates between the two major acute ischemic stroke (AIS) etiology subtypes: cardiac and large artery atherosclerosis.\n\nYour work will enable healthcare providers to better identify the origins of blood clots in deadly strokes, making it easier for physicians to prescribe the best post-stroke therapeutic management and reducing the likelihood of a second stroke.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom sklearn.preprocessing import LabelEncoder\nfrom PIL import Image \nImage.MAX_IMAGE_PIXELS = 1000000000000000  \npd.set_option('max_columns',50)\nplt.style.use('fivethirtyeight')\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:00.738063Z","iopub.execute_input":"2022-07-13T05:11:00.738632Z","iopub.status.idle":"2022-07-13T05:11:03.813915Z","shell.execute_reply.started":"2022-07-13T05:11:00.738512Z","shell.execute_reply":"2022-07-13T05:11:03.812588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\nother = pd.read_csv(\"../input/mayo-clinic-strip-ai/other.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:03.81594Z","iopub.execute_input":"2022-07-13T05:11:03.816437Z","iopub.status.idle":"2022-07-13T05:11:03.840721Z","shell.execute_reply.started":"2022-07-13T05:11:03.816398Z","shell.execute_reply":"2022-07-13T05:11:03.839752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train.csv Contains annotations for images in the train/ folder.\n\nimage_id - A unique identifier for this instance having the form {patient_id}_{image_num}. Corresponds to the image {image_id}.tif.\n\ncenter_id - Identifies the medical center where the slide was obtained.\n\npatient_id - Identifies the patient from whom the slide was obtained.\n\nimage_num - Enumerates images of clots obtained from the same patient.\n\nlabel - The etiology of the clot, either CE or LAA. This field is the classification target.","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:03.842114Z","iopub.execute_input":"2022-07-13T05:11:03.842664Z","iopub.status.idle":"2022-07-13T05:11:03.867582Z","shell.execute_reply.started":"2022-07-13T05:11:03.842628Z","shell.execute_reply":"2022-07-13T05:11:03.866709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:03.869883Z","iopub.execute_input":"2022-07-13T05:11:03.870422Z","iopub.status.idle":"2022-07-13T05:11:03.883942Z","shell.execute_reply.started":"2022-07-13T05:11:03.870388Z","shell.execute_reply":"2022-07-13T05:11:03.882971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train,x=\"label\")\nplt.title(\"Label Distribution in Train\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:03.885568Z","iopub.execute_input":"2022-07-13T05:11:03.886243Z","iopub.status.idle":"2022-07-13T05:11:04.069127Z","shell.execute_reply.started":"2022-07-13T05:11:03.886207Z","shell.execute_reply":"2022-07-13T05:11:04.067667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see an imbalance in the target distribution, the same would need to be taken care of when modelling.","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=other,x=\"other_specified\")\nplt.title(\"Label Distribution in the supplemental data provided\")\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.071725Z","iopub.execute_input":"2022-07-13T05:11:04.073017Z","iopub.status.idle":"2022-07-13T05:11:04.314493Z","shell.execute_reply.started":"2022-07-13T05:11:04.072942Z","shell.execute_reply":"2022-07-13T05:11:04.312943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the supplemental data we are provided with some other etiology causes of blood clots, their distribution also seems to be highly imabalanced.\n\nWe would have to think how could we use the suplemental data to enrich our training data set as they do not have the same columns. Would having centre id as 0 work?","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=train,x=\"image_num\")\nplt.title(\"Number of images taken per patient\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.316208Z","iopub.execute_input":"2022-07-13T05:11:04.316603Z","iopub.status.idle":"2022-07-13T05:11:04.507972Z","shell.execute_reply.started":"2022-07-13T05:11:04.316567Z","shell.execute_reply":"2022-07-13T05:11:04.506776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most of the patients have a single image, but we do have some patients which have multiple images taken.","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=other,x=\"image_num\",hue=\"label\")\nplt.title(\"Number of images taken per patient in the supplemental_data\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.510089Z","iopub.execute_input":"2022-07-13T05:11:04.510826Z","iopub.status.idle":"2022-07-13T05:11:04.758138Z","shell.execute_reply.started":"2022-07-13T05:11:04.510771Z","shell.execute_reply":"2022-07-13T05:11:04.756799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Even in the suplemental data we have patients with more than one images taken but the cause of their blood clot is unknown","metadata":{}},{"cell_type":"code","source":"train['patient_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.760073Z","iopub.execute_input":"2022-07-13T05:11:04.760481Z","iopub.status.idle":"2022-07-13T05:11:04.769138Z","shell.execute_reply.started":"2022-07-13T05:11:04.760445Z","shell.execute_reply":"2022-07-13T05:11:04.767844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other['patient_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.772758Z","iopub.execute_input":"2022-07-13T05:11:04.773343Z","iopub.status.idle":"2022-07-13T05:11:04.784507Z","shell.execute_reply.started":"2022-07-13T05:11:04.77329Z","shell.execute_reply":"2022-07-13T05:11:04.783023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have 632 patients in our training dataset and 336 patients in our supplemental data set.\nFor an image classification task the number of patients seems to be quite less also most of these patients in the supplemental dataset do not have their label ascertained. ","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=train,x=\"center_id\",hue=\"label\")\nplt.title(\"Centre id for each label\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:04.786653Z","iopub.execute_input":"2022-07-13T05:11:04.787227Z","iopub.status.idle":"2022-07-13T05:11:05.119803Z","shell.execute_reply.started":"2022-07-13T05:11:04.787175Z","shell.execute_reply":"2022-07-13T05:11:05.118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I am interested to know if we have any correlation between fields num_of_images, centre_id and label","metadata":{}},{"cell_type":"code","source":"\ntrain_sub = train[['image_num','center_id','label']]\nle = LabelEncoder()\ntrain_sub.label = le.fit_transform(train_sub.label)\ncorr = train_sub.corr()\nmask = np.triu(np.ones_like(corr, dtype=np.bool))\nsns.heatmap(corr,mask=mask,annot=True)\n#train_sub","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:11:05.121572Z","iopub.execute_input":"2022-07-13T05:11:05.121996Z","iopub.status.idle":"2022-07-13T05:11:05.641089Z","shell.execute_reply.started":"2022-07-13T05:11:05.121962Z","shell.execute_reply":"2022-07-13T05:11:05.639165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now let us view some tiff images.","metadata":{}},{"cell_type":"code","source":"# Code reference: https://www.kaggle.com/code/datark1/eda-images-processing-and-exploration\nfrom glob import glob\ntrain_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/train/*\")\nfig, axes = plt.subplots(1,5, figsize=(16,16))\nfor ax in axes.reshape(-1):\n    img_path = np.random.choice(train_images)\n    img = Image.open(img_path)   \n    img.thumbnail((300,300), Image.Resampling.LANCZOS)\n    ax.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T05:27:04.429755Z","iopub.execute_input":"2022-07-13T05:27:04.431221Z","iopub.status.idle":"2022-07-13T05:27:46.750316Z","shell.execute_reply.started":"2022-07-13T05:27:04.431171Z","shell.execute_reply":"2022-07-13T05:27:46.749026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Message from Author:\n\n* This notebook is a work in progress.\n\n* If you think anything in this notebook is wrong, please let me know in the comments.\n\n* If you like the notebook, please do upvote! \n\n","metadata":{}}]}