{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-02T04:39:43.142698Z","iopub.execute_input":"2022-09-02T04:39:43.143096Z","iopub.status.idle":"2022-09-02T04:39:43.237328Z","shell.execute_reply.started":"2022-09-02T04:39:43.143067Z","shell.execute_reply":"2022-09-02T04:39:43.236607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wandb\n\nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:39:43.238781Z","iopub.execute_input":"2022-09-02T04:39:43.239238Z","iopub.status.idle":"2022-09-02T04:40:40.519680Z","shell.execute_reply.started":"2022-09-02T04:39:43.239211Z","shell.execute_reply":"2022-09-02T04:40:40.518665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport tifffile as tiff \nfrom tqdm.auto import tqdm\n\nplt.style.use(\"Solarize_Light2\")\n\n# Wandb Login\nimport wandb\nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:41:12.144292Z","iopub.execute_input":"2022-09-02T04:41:12.145278Z","iopub.status.idle":"2022-09-02T04:41:12.154710Z","shell.execute_reply.started":"2022-09-02T04:41:12.145238Z","shell.execute_reply":"2022-09-02T04:41:12.153935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class config:\n    BASE_PATH = \"../input/hubmap-organ-segmentation/\"\n    TRAIN_PATH = os.path.join(BASE_PATH, \"train\")\n\n# wandb config\nWANDB_CONFIG = {\n     'competition': 'HuBMAP', \n              '_wandb_kernel': 'neuracort'\n    }\n\n# Initialize W&B\nrun = wandb.init(\n    project='hubmap-organ-segmentation', \n    config= WANDB_CONFIG\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:41:24.318263Z","iopub.execute_input":"2022-09-02T04:41:24.318635Z","iopub.status.idle":"2022-09-02T04:41:34.510927Z","shell.execute_reply.started":"2022-09-02T04:41:24.318592Z","shell.execute_reply":"2022-09-02T04:41:34.510044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\n    os.path.join(config.BASE_PATH, \"train.csv\")\n)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:42:02.222242Z","iopub.execute_input":"2022-09-02T04:42:02.222566Z","iopub.status.idle":"2022-09-02T04:42:02.531027Z","shell.execute_reply.started":"2022-09-02T04:42:02.222538Z","shell.execute_reply":"2022-09-02T04:42:02.530003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\nwandb.log({\"df_train\": df})","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:42:47.096337Z","iopub.execute_input":"2022-09-02T04:42:47.096702Z","iopub.status.idle":"2022-09-02T04:42:48.261429Z","shell.execute_reply.started":"2022-09-02T04:42:47.096674Z","shell.execute_reply":"2022-09-02T04:42:48.260456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id_1 = 10044\nimg_1 = tiff.imread(config.BASE_PATH + \"train_images/\" + str(img_id_1) + \".tiff\")\nprint(img_1.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:43:09.749973Z","iopub.execute_input":"2022-09-02T04:43:09.750755Z","iopub.status.idle":"2022-09-02T04:43:10.195580Z","shell.execute_reply.started":"2022-09-02T04:43:09.750723Z","shell.execute_reply":"2022-09-02T04:43:10.194714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.imshow(img_1)\nplt.axis(\"off\")\nwandb.log({\"Image Sample 1\": plt})","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:44:30.814862Z","iopub.execute_input":"2022-09-02T04:44:30.815207Z","iopub.status.idle":"2022-09-02T04:44:33.580526Z","shell.execute_reply.started":"2022-09-02T04:44:30.815179Z","shell.execute_reply":"2022-09-02T04:44:33.579477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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 \ndef rle2mask(mask_rle, shape=(1600,256)):\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-09-02T04:45:20.553395Z","iopub.execute_input":"2022-09-02T04:45:20.553737Z","iopub.status.idle":"2022-09-02T04:45:20.562247Z","shell.execute_reply.started":"2022-09-02T04:45:20.553710Z","shell.execute_reply":"2022-09-02T04:45:20.561597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_1 = rle2mask(df[df[\"id\"]==img_id_1][\"rle\"].iloc[-1], (img_1.shape[1], img_1.shape[0]))\nmask_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:45:34.327581Z","iopub.execute_input":"2022-09-02T04:45:34.328024Z","iopub.status.idle":"2022-09-02T04:45:34.344846Z","shell.execute_reply.started":"2022-09-02T04:45:34.327986Z","shell.execute_reply":"2022-09-02T04:45:34.343971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)\nplt.axis(\"off\")\nwandb.log({\"Mask Sample 1\": plt})","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:45:49.846357Z","iopub.execute_input":"2022-09-02T04:45:49.846872Z","iopub.status.idle":"2022-09-02T04:45:51.487931Z","shell.execute_reply.started":"2022-09-02T04:45:49.846836Z","shell.execute_reply":"2022-09-02T04:45:51.486590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(img_1)\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)\nplt.axis(\"off\")\nwandb.log({\"Image with Mask Sample 1\": plt})","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:46:05.581749Z","iopub.execute_input":"2022-09-02T04:46:05.582125Z","iopub.status.idle":"2022-09-02T04:46:09.264532Z","shell.execute_reply.started":"2022-09-02T04:46:05.582096Z","shell.execute_reply":"2022-09-02T04:46:09.263661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = df.id\nimage_files = glob.glob(config.BASE_PATH + \"/train_images/*\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T04:46:25.533455Z","iopub.execute_input":"2022-09-02T04:46:25.534148Z","iopub.status.idle":"2022-09-02T04:46:25.540601Z","shell.execute_reply.started":"2022-09-02T04:46:25.534118Z","shell.execute_reply":"2022-09-02T04:46:25.539764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_table(table_name):\n    table = wandb.Table(columns=['Id', 'Image', 'Mask', 'Image with Mask'], allow_mixed_types = True)\n\n    for id, img in tqdm(zip(image_ids, image_files), total = len(image_ids)):\n\n        img = tiff.imread(img)\n        mask = rle2mask(df[df[\"id\"]==id][\"rle\"].iloc[-1], (img.shape[1], img.shape[0]))\n        \n        plt.figure(figsize=(10,10))\n        plt.axis(\"off\")\n        plt.imshow(img)\n        plt.imshow(mask, cmap='coolwarm', alpha=0.5)\n        plt.savefig(\"./image.jpg\")\n        plt.close()\n        \n        table.add_data(\n        id, \n        wandb.Image(img), \n        wandb.Image(mask),\n        wandb.Image(cv2.cvtColor(cv2.imread(\"./image.jpg\"), cv2.COLOR_BGR2RGB))\n        )\n\n    wandb.log({table_name : table})\n     \nsave_table(\"Images and Masks Record\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T05:25:28.976421Z","iopub.execute_input":"2022-09-02T05:25:28.977953Z","iopub.status.idle":"2022-09-02T06:02:31.447201Z","shell.execute_reply.started":"2022-09-02T05:25:28.977907Z","shell.execute_reply":"2022-09-02T06:02:31.446316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\ng = sns.countplot(data=df, x=\"organ\", palette=sns.color_palette(\"Set2\", 8))\ng.set_title(\"Organ Counts\", color = \"black\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T06:02:31.450268Z","iopub.execute_input":"2022-09-02T06:02:31.450570Z","iopub.status.idle":"2022-09-02T06:02:31.658950Z","shell.execute_reply.started":"2022-09-02T06:02:31.450545Z","shell.execute_reply":"2022-09-02T06:02:31.657399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\ng = sns.countplot(data=df, x=\"data_source\", palette=sns.color_palette(\"Set2\", 8))\ng.set_title(\"Data Source\", color = \"black\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\ng = sns.histplot(data=df, x=\"age\", palette=sns.color_palette(\"Set2\", 8))\ng.set_title(\"Age\", color = \"black\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T06:02:31.660105Z","iopub.execute_input":"2022-09-02T06:02:31.660401Z","iopub.status.idle":"2022-09-02T06:02:32.788379Z","shell.execute_reply.started":"2022-09-02T06:02:31.660369Z","shell.execute_reply":"2022-09-02T06:02:32.787054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\ng = sns.histplot(data=df, x=\"sex\", palette=sns.color_palette(\"Set2\", 8))\ng.set_title(\"Sex\", color = \"black\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T06:02:32.790939Z","iopub.execute_input":"2022-09-02T06:02:32.791234Z","iopub.status.idle":"2022-09-02T06:02:32.939465Z","shell.execute_reply.started":"2022-09-02T06:02:32.791208Z","shell.execute_reply":"2022-09-02T06:02:32.938449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"More Plots coming soon!\nwork in progress...........","metadata":{},"execution_count":null,"outputs":[]}]}