{"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 cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"id":"Va4kvw5nOGb2","execution":{"iopub.status.busy":"2022-06-26T06:41:17.890914Z","iopub.execute_input":"2022-06-26T06:41:17.891407Z","iopub.status.idle":"2022-06-26T06:41:19.333084Z","shell.execute_reply.started":"2022-06-26T06:41:17.891310Z","shell.execute_reply":"2022-06-26T06:41:19.332251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ndf.head()","metadata":{"id":"QQIWLKCUOpCt","outputId":"aea77c15-504f-41b6-e435-b733df0fa09a","execution":{"iopub.status.busy":"2022-06-26T06:41:19.334977Z","iopub.execute_input":"2022-06-26T06:41:19.335780Z","iopub.status.idle":"2022-06-26T06:41:19.815528Z","shell.execute_reply.started":"2022-06-26T06:41:19.335741Z","shell.execute_reply":"2022-06-26T06:41:19.814250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"MipXQkEMScOj","outputId":"f83e5920-eab0-4d1b-e3b8-2d42345df7db","execution":{"iopub.status.busy":"2022-06-26T06:41:19.816994Z","iopub.execute_input":"2022-06-26T06:41:19.819208Z","iopub.status.idle":"2022-06-26T06:41:19.852298Z","shell.execute_reply.started":"2022-06-26T06:41:19.819136Z","shell.execute_reply":"2022-06-26T06:41:19.851147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.countplot(df['organ'])\nplt.show()","metadata":{"id":"ur9y4-7SRC5j","outputId":"d5841106-722b-42ca-ef69-60243d247bbb","execution":{"iopub.status.busy":"2022-06-26T06:41:19.854253Z","iopub.execute_input":"2022-06-26T06:41:19.855269Z","iopub.status.idle":"2022-06-26T06:41:20.051419Z","shell.execute_reply.started":"2022-06-26T06:41:19.855232Z","shell.execute_reply":"2022-06-26T06:41:20.050395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.countplot(df['sex'])\nplt.show()","metadata":{"id":"bf9Xqv-RSM6Y","outputId":"678aebc2-0b0f-4eb4-c374-c3606f63036a","execution":{"iopub.status.busy":"2022-06-26T06:41:20.054695Z","iopub.execute_input":"2022-06-26T06:41:20.055512Z","iopub.status.idle":"2022-06-26T06:41:20.205590Z","shell.execute_reply.started":"2022-06-26T06:41:20.055462Z","shell.execute_reply":"2022-06-26T06:41:20.204257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.countplot(df['sex'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-26T06:41:20.207371Z","iopub.execute_input":"2022-06-26T06:41:20.208108Z","iopub.status.idle":"2022-06-26T06:41:20.358255Z","shell.execute_reply.started":"2022-06-26T06:41:20.208055Z","shell.execute_reply":"2022-06-26T06:41:20.356927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['img_height'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-06-26T06:41:20.359943Z","iopub.execute_input":"2022-06-26T06:41:20.360627Z","iopub.status.idle":"2022-06-26T06:41:20.373979Z","shell.execute_reply.started":"2022-06-26T06:41:20.360587Z","shell.execute_reply":"2022-06-26T06:41:20.372408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.countplot(x = df['organ'], hue=df['sex'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-26T06:41:20.375740Z","iopub.execute_input":"2022-06-26T06:41:20.382337Z","iopub.status.idle":"2022-06-26T06:41:20.672144Z","shell.execute_reply.started":"2022-06-26T06:41:20.382266Z","shell.execute_reply":"2022-06-26T06:41:20.671366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.distplot(x = df['age'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-26T06:41:20.674522Z","iopub.execute_input":"2022-06-26T06:41:20.675362Z","iopub.status.idle":"2022-06-26T06:41:20.896182Z","shell.execute_reply.started":"2022-06-26T06:41:20.675316Z","shell.execute_reply":"2022-06-26T06:41:20.894721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def helper(mask, img_shape):\n  \n    canvas = np.zeros(img_shape).T\n    canvas[tuple(zip(*mask))] = 1.0\n\n      # This is the Equivalent for loop of the above command for better understanding.\n      # for pos in range(len(p_loc)):\n      #   canvas[pos[0], pos[1]] = 1\n\n    return canvas\n\ndef get_mask(rle_string, img_shape):\n    rle = [int(i) for i in rle_string.split(' ')]\n    pairs = list(zip(rle[0::2],rle[1::2]))\n\n    p_loc = []\n\n    for start, length in pairs:\n        for p_pos in range(start, start + length):\n            p_loc.append((p_pos % img_shape[1], p_pos // img_shape[0]))\n  \n    return helper(p_loc, img_shape)","metadata":{"id":"s9kRtWe0R35S","execution":{"iopub.status.busy":"2022-06-26T06:41:20.901436Z","iopub.execute_input":"2022-06-26T06:41:20.901983Z","iopub.status.idle":"2022-06-26T06:41:20.920695Z","shell.execute_reply.started":"2022-06-26T06:41:20.901936Z","shell.execute_reply":"2022-06-26T06:41:20.919151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image_path'] = '../input/hubmap-organ-segmentation/train_images/'\ndf['image_path'] = df['image_path'].str.cat(df['id'].astype(str))\ndf['image_path'] = df['image_path'] + '.tiff'\ndf.head()","metadata":{"id":"9KzenZ9vU-Ld","outputId":"bdfa77bb-957c-4d11-fc3e-9fab0a8d388e","execution":{"iopub.status.busy":"2022-06-26T06:41:20.925748Z","iopub.execute_input":"2022-06-26T06:41:20.929460Z","iopub.status.idle":"2022-06-26T06:41:20.968716Z","shell.execute_reply.started":"2022-06-26T06:41:20.929403Z","shell.execute_reply":"2022-06-26T06:41:20.966914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for part in df['organ'].unique():\n\n  x = df[df['organ'] == part]\n  index_list = x.index\n  idx = index_list[np.random.randint(0, x.shape[0])]\n\n  class_of_scan = df.loc[idx, 'organ']\n  image_path = df.loc[idx, 'image_path']\n  id = df.loc[idx, 'id'] \n  \n  image = np.array(Image.open(image_path)) / 255\n  k = (df.loc[idx, 'img_height'], df.loc[idx, 'img_width'])\n\n  rle_string = df.loc[idx, 'rle']\n  mask = get_mask(rle_string, k)\n\n\n  fig, ax = plt.subplots(1,3, figsize=(15,15))\n  ax[0].set_title(f'Image : {id}')\n  ax[0].imshow(image)\n\n  ax[1].set_title(f'Mask : {id}')\n  ax[1].imshow(mask)\n\n  ax[2].set_title(f'{class_of_scan} Segmented : {id}')\n  ax[2].imshow(np.dstack((mask, np.zeros(mask.shape), np.argmax(image, axis=-1))))\n  plt.show()","metadata":{"id":"jTnykaGGeqgO","outputId":"cca95566-8a44-406a-dcd9-9782e9054b37","execution":{"iopub.status.busy":"2022-06-26T06:41:20.970398Z","iopub.execute_input":"2022-06-26T06:41:20.971108Z","iopub.status.idle":"2022-06-26T06:41:58.663749Z","shell.execute_reply.started":"2022-06-26T06:41:20.971059Z","shell.execute_reply":"2022-06-26T06:41:58.662403Z"},"trusted":true},"execution_count":null,"outputs":[]}]}