{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport glob as glob\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-28T17:23:13.741345Z","iopub.execute_input":"2022-06-28T17:23:13.741856Z","iopub.status.idle":"2022-06-28T17:23:14.595491Z","shell.execute_reply.started":"2022-06-28T17:23:13.741746Z","shell.execute_reply":"2022-06-28T17:23:14.593853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"../input/hubmap-organ-segmentation/\"\nTRAIN_IMGS = glob.glob(DATA_DIR+\"train_images/*.tiff\")\ntrain_df = pd.read_csv(DATA_DIR+\"train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:14.598112Z","iopub.execute_input":"2022-06-28T17:23:14.598631Z","iopub.status.idle":"2022-06-28T17:23:14.958541Z","shell.execute_reply.started":"2022-06-28T17:23:14.598584Z","shell.execute_reply":"2022-06-28T17:23:14.957608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/pestipeti/decoding-rle-masks/notebook\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:14.960088Z","iopub.execute_input":"2022-06-28T17:23:14.960673Z","iopub.status.idle":"2022-06-28T17:23:14.970778Z","shell.execute_reply.started":"2022-06-28T17:23:14.960640Z","shell.execute_reply":"2022-06-28T17:23:14.969882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organs = np.unique(train_df.organ)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:14.973426Z","iopub.execute_input":"2022-06-28T17:23:14.974675Z","iopub.status.idle":"2022-06-28T17:23:14.993959Z","shell.execute_reply.started":"2022-06-28T17:23:14.974603Z","shell.execute_reply":"2022-06-28T17:23:14.992817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,len(organs),figsize=(15,8))\n\nfor i in range(len(organs)):\n    organ_df = train_df[train_df['organ']==organs[i]].reset_index()\n    idx = np.random.randint(organ_df.shape[0])\n    image = plt.imread(f\"{DATA_DIR}train_images/{organ_df.id[idx]}.tiff\")\n    mask = rle2mask(organ_df.rle[idx],shape=(organ_df.img_height[idx],organ_df.img_width[idx]))\n    ax[i].imshow(image)\n    ax[i].imshow(mask,alpha=0.4)\n    ax[i].set_title(organ_df.organ[idx])\n    ax[i].axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:14.995946Z","iopub.execute_input":"2022-06-28T17:23:14.996864Z","iopub.status.idle":"2022-06-28T17:23:24.356999Z","shell.execute_reply.started":"2022-06-28T17:23:14.996815Z","shell.execute_reply":"2022-06-28T17:23:24.355361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"males = []\nfemales = []\nfor i in range(len(organs)):\n    organ_df = train_df[train_df['organ']==organs[i]].reset_index()\n    males.append(organ_df.sex.value_counts().Male)\n    \n    if len(organ_df.sex.value_counts())!=1:\n        females.append(organ_df.sex.value_counts().Female)\n    else:\n        females.append(0)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:24.358964Z","iopub.execute_input":"2022-06-28T17:23:24.360318Z","iopub.status.idle":"2022-06-28T17:23:24.387773Z","shell.execute_reply.started":"2022-06-28T17:23:24.360272Z","shell.execute_reply":"2022-06-28T17:23:24.386417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(np.arange(len(organs))-0.2,males,0.4,label='males')\nplt.bar(np.arange(len(organs))+0.2,females,0.4,label='females')\nplt.xticks(np.arange(len(organs)),organs)\nplt.title(\"Number of Males and Females\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:23:24.389642Z","iopub.execute_input":"2022-06-28T17:23:24.390240Z","iopub.status.idle":"2022-06-28T17:23:24.563177Z","shell.execute_reply.started":"2022-06-28T17:23:24.390155Z","shell.execute_reply":"2022-06-28T17:23:24.562324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(organs)):\n    organ_df = train_df[train_df['organ']==organs[i]].reset_index()\n    plt.hist(organ_df.age,bins=10,range=(0,100),edgecolor=\"black\")\n    plt.xlabel(\"Age\")\n    plt.ylabel(\"Count\")\n    plt.title(f\"Age histogram for {organs[i]}\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-28T17:43:53.233943Z","iopub.execute_input":"2022-06-28T17:43:53.234845Z","iopub.status.idle":"2022-06-28T17:43:54.090058Z","shell.execute_reply.started":"2022-06-28T17:43:53.234777Z","shell.execute_reply":"2022-06-28T17:43:54.088539Z"},"trusted":true},"execution_count":null,"outputs":[]}]}