{"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\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport seaborn as sns\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-25T11:08:51.831744Z","iopub.execute_input":"2022-06-25T11:08:51.832173Z","iopub.status.idle":"2022-06-25T11:08:52.257516Z","shell.execute_reply.started":"2022-06-25T11:08:51.832140Z","shell.execute_reply":"2022-06-25T11:08:52.256407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"darkgrid\")","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.479800Z","iopub.execute_input":"2022-06-25T10:30:28.480201Z","iopub.status.idle":"2022-06-25T10:30:28.486337Z","shell.execute_reply.started":"2022-06-25T10:30:28.480165Z","shell.execute_reply":"2022-06-25T10:30:28.484482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '../input/hubmap-organ-segmentation/'","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.488169Z","iopub.execute_input":"2022-06-25T10:30:28.488863Z","iopub.status.idle":"2022-06-25T10:30:28.501495Z","shell.execute_reply.started":"2022-06-25T10:30:28.488827Z","shell.execute_reply":"2022-06-25T10:30:28.500533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(PATH + 'train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.504065Z","iopub.execute_input":"2022-06-25T10:30:28.505235Z","iopub.status.idle":"2022-06-25T10:30:28.886311Z","shell.execute_reply.started":"2022-06-25T10:30:28.505195Z","shell.execute_reply":"2022-06-25T10:30:28.885083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Area'] = train_df['img_height'] * train_df['img_width'] * (train_df['pixel_size']**2)\ntrain_df['Volume'] = train_df['Area'] * train_df['tissue_thickness'] \ntrain_df['Shape'] = [(x, y) for x, y in zip(train_df['img_width'], train_df['img_height'])]","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.887804Z","iopub.execute_input":"2022-06-25T10:30:28.888264Z","iopub.status.idle":"2022-06-25T10:30:28.905722Z","shell.execute_reply.started":"2022-06-25T10:30:28.888219Z","shell.execute_reply":"2022-06-25T10:30:28.904367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.907445Z","iopub.execute_input":"2022-06-25T10:30:28.907806Z","iopub.status.idle":"2022-06-25T10:30:28.974128Z","shell.execute_reply.started":"2022-06-25T10:30:28.907777Z","shell.execute_reply":"2022-06-25T10:30:28.973018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.975288Z","iopub.execute_input":"2022-06-25T10:30:28.976047Z","iopub.status.idle":"2022-06-25T10:30:28.997806Z","shell.execute_reply.started":"2022-06-25T10:30:28.976015Z","shell.execute_reply":"2022-06-25T10:30:28.996987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(PATH + 'test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:28.999643Z","iopub.execute_input":"2022-06-25T10:30:29.000462Z","iopub.status.idle":"2022-06-25T10:30:29.023610Z","shell.execute_reply.started":"2022-06-25T10:30:29.000415Z","shell.execute_reply":"2022-06-25T10:30:29.022301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(x='organ',hue='data_source',data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:29.025478Z","iopub.execute_input":"2022-06-25T10:30:29.025951Z","iopub.status.idle":"2022-06-25T10:30:29.300397Z","shell.execute_reply.started":"2022-06-25T10:30:29.025869Z","shell.execute_reply":"2022-06-25T10:30:29.299020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nax = sns.countplot(x='Shape',hue='organ',data=train_df)\nax.tick_params(axis ='x', rotation = 90)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:29.304038Z","iopub.execute_input":"2022-06-25T10:30:29.304892Z","iopub.status.idle":"2022-06-25T10:30:30.218934Z","shell.execute_reply.started":"2022-06-25T10:30:29.304848Z","shell.execute_reply":"2022-06-25T10:30:30.217743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nax = sns.countplot(x='Area',hue='organ',data=train_df)\nax.tick_params(axis ='x', rotation = 90)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:30.220654Z","iopub.execute_input":"2022-06-25T10:30:30.221275Z","iopub.status.idle":"2022-06-25T10:30:30.991919Z","shell.execute_reply.started":"2022-06-25T10:30:30.221211Z","shell.execute_reply":"2022-06-25T10:30:30.990996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(x='pixel_size',hue='organ',data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:30.993224Z","iopub.execute_input":"2022-06-25T10:30:30.994256Z","iopub.status.idle":"2022-06-25T10:30:31.222276Z","shell.execute_reply.started":"2022-06-25T10:30:30.994205Z","shell.execute_reply":"2022-06-25T10:30:31.221005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(x='tissue_thickness',hue='organ',data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:31.223885Z","iopub.execute_input":"2022-06-25T10:30:31.224417Z","iopub.status.idle":"2022-06-25T10:30:31.450091Z","shell.execute_reply.started":"2022-06-25T10:30:31.224383Z","shell.execute_reply":"2022-06-25T10:30:31.448806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(x='sex',hue='organ',data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:31.452165Z","iopub.execute_input":"2022-06-25T10:30:31.452600Z","iopub.status.idle":"2022-06-25T10:30:31.685209Z","shell.execute_reply.started":"2022-06-25T10:30:31.452559Z","shell.execute_reply":"2022-06-25T10:30:31.683777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nax = sns.countplot(x='age',hue='sex',data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T10:30:31.686812Z","iopub.execute_input":"2022-06-25T10:30:31.687181Z","iopub.status.idle":"2022-06-25T10:30:32.246151Z","shell.execute_reply.started":"2022-06-25T10:30:31.687149Z","shell.execute_reply":"2022-06-25T10:30:32.245074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(annotation, shape):\n    s = annotation.split()\n    mask = np.zeros(shape[0] * shape[1])\n    positions = np.asarray([int(x) for x in s[0::2]])\n    lens = np.asarray([int(x) for x in s[1::2]])\n    for i in range(len(positions)):\n        mask[positions[i]-1:positions[i]+lens[i]-1] = 1.00\n    return np.reshape(mask,shape).T\n        \ndef get_mask(image_id):\n    e = train_df[train_df['id'] == int(image_id)].iloc[0]['rle']\n    shape = train_df[train_df['id'] == int(image_id)].iloc[0]['Shape']\n    mask = rle_decode(e, shape)\n    return mask\n\ndef plot_mask(image_id):\n    image = cv2.imread(PATH + 'train_images/' + str(image_id) + '.tiff')\n    plt.figure(figsize=(15,8))\n    plt.subplot(1,3,1)\n    plt.imshow(image,interpolation='none')\n    \n    m =get_mask(image_id)\n    plt.title(str(image_id) + ': ' + str(m.shape))\n    plt.subplot(1,3,2)\n    plt.imshow(m,interpolation='none',cmap='inferno')\n\n    plt.title('mask')\n    plt.subplot(1,3,3)\n    mask1 = np.zeros((*m.shape, 3))\n    mask1[:,:,1] = np.clip(m,0,1)*255\n    masked_img = cv2.addWeighted(image,0.75,np.asarray(mask1,np.uint8),0.25,0.0)\n    plt.imshow(masked_img,interpolation='none')\n    \n    plt.title('Mask Overlay')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:49:54.866961Z","iopub.execute_input":"2022-06-25T11:49:54.867755Z","iopub.status.idle":"2022-06-25T11:49:54.883064Z","shell.execute_reply.started":"2022-06-25T11:49:54.867713Z","shell.execute_reply":"2022-06-25T11:49:54.881648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organs = train_df['organ'].unique()\nprint(organs[0])\nprint('..................................')\ndf = train_df[train_df['organ']==organs[0]]\nshapes = df['Shape'].unique()\ni = 1\nfor s in shapes:\n    img_id = df[df['Shape'] == s].iloc[0]['id']\n    plot_mask(img_id)\n    i +=1\nprint('..................................')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:51:20.700753Z","iopub.execute_input":"2022-06-25T11:51:20.701199Z","iopub.status.idle":"2022-06-25T11:51:34.620599Z","shell.execute_reply.started":"2022-06-25T11:51:20.701166Z","shell.execute_reply":"2022-06-25T11:51:34.619342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(organs[1])\nprint('..................................')\ndf = train_df[train_df['organ']==organs[1]]\nshapes = df['Shape'].unique()\ni = 1\nfor s in shapes:\n    img_id = df[df['Shape'] == s].iloc[0]['id']\n    plot_mask(img_id)\n    i +=1\nprint('..................................')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:52:18.410130Z","iopub.execute_input":"2022-06-25T11:52:18.411018Z","iopub.status.idle":"2022-06-25T11:52:27.345658Z","shell.execute_reply.started":"2022-06-25T11:52:18.410981Z","shell.execute_reply":"2022-06-25T11:52:27.344281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(organs[2])\nprint('..................................')\ndf = train_df[train_df['organ']==organs[2]]\nshapes = df['Shape'].unique()\ni = 1\nfor s in shapes:\n    img_id = df[df['Shape'] == s].iloc[0]['id']\n    plot_mask(img_id)\n    i +=1\nprint('..................................')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:52:34.048608Z","iopub.execute_input":"2022-06-25T11:52:34.049125Z","iopub.status.idle":"2022-06-25T11:52:37.945531Z","shell.execute_reply.started":"2022-06-25T11:52:34.049084Z","shell.execute_reply":"2022-06-25T11:52:37.944249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(organs[3])\nprint('..................................')\ndf = train_df[train_df['organ']==organs[3]]\nshapes = df['Shape'].unique()\ni = 1\nfor s in shapes:\n    img_id = df[df['Shape'] == s].iloc[0]['id']\n    plot_mask(img_id)\n    i +=1\nprint('..................................')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:53:25.051066Z","iopub.execute_input":"2022-06-25T11:53:25.051503Z","iopub.status.idle":"2022-06-25T11:53:48.034338Z","shell.execute_reply.started":"2022-06-25T11:53:25.051464Z","shell.execute_reply":"2022-06-25T11:53:48.033527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(organs[4])\nprint('..................................')\ndf = train_df[train_df['organ']==organs[4]]\nshapes = df['Shape'].unique()\ni = 1\nfor s in shapes:\n    img_id = df[df['Shape'] == s].iloc[0]['id']\n    plot_mask(img_id)\n    i +=1\nprint('..................................')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T11:54:06.447447Z","iopub.execute_input":"2022-06-25T11:54:06.447810Z","iopub.status.idle":"2022-06-25T11:54:08.787398Z","shell.execute_reply.started":"2022-06-25T11:54:06.447781Z","shell.execute_reply":"2022-06-25T11:54:08.786473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}