{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport tifffile\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"List all directories and files in the \"train\" directory"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"BASE_PATH = \"../input/hubmap-kidney-segmentation/\"\nTRAIN_PATH = os.path.join(BASE_PATH, \"train\")\nprint(os.listdir(BASE_PATH))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The file \"train.csv\" contains all images and has the ID for each one of them the the masks for each one encoded. This next code shows a sample of the file."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\n    os.path.join(BASE_PATH, \"train.csv\")\n)\ndf_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The file \"sample_submission.csv\" shows the format for the submission of the contest"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sub = pd.read_csv(\n    os.path.join(BASE_PATH, \"sample_submission.csv\"))\ndf_sub","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As seen on the previous cells, there are 8 images as training files and 5 images to test predictions."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Number of train images: {df_train.shape[0]}\")\nprint(f\"Number of test images: {df_sub.shape[0]}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The file \"HuBMAP-20-dataset_information.csv\" shows the extra information about each image, includin patient information. There are 13 rows of data (8 train and 5 test) in this file."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_info = pd.read_csv(\n    os.path.join(BASE_PATH, \"HuBMAP-20-dataset_information.csv\")\n)\ndf_info","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can read a .tiff file and show it in this way:"},{"metadata":{"trusted":true},"cell_type":"code","source":"im = tifffile.imread(\n    os.path.join(BASE_PATH, \"train/0486052bb.tiff\")\n)\n#plt.figure(figsize=(16, 16))\n#plt.imshow(im)\n#plt.axis(\"off\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To see the mask in front of the image, we first have to decode the mask with this function"},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\ndef rle2mask(mask_rle, shape):\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mask = rle2mask(\n    df_train[df_train[\"id\"] == \"0486052bb\"][\"encoding\"].values[0], \n    (im.shape[1], im.shape[0])\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Showing only the mask"},{"metadata":{"trusted":true},"cell_type":"code","source":"#plt.figure(figsize=(16, 16))\n#plt.imshow(mask)\n#plt.axis(\"off\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Showing the image with the mask"},{"metadata":{"trusted":true},"cell_type":"code","source":"im.shape, mask.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(im)\nplt.imshow(mask, cmap='coolwarm', alpha=0.5)\nplt.axis(\"off\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}