{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import glob\nimport os\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nimport tifffile as tiff ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**List of training tiff files and the corresponding size**"},{"metadata":{"trusted":true},"cell_type":"code","source":"tiffFileList = glob.glob(\"../input/hubmap-kidney-segmentation/train/*.tiff\")\n\nfor file in tiffFileList:\n    size  = os.path.getsize(file)/1000000\n    print(file[len(\"../input/hubmap-kidney-segmentation/train/\"):], \"---\", \"{:.1f}\".format(size), \"MB\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Select the smallest file aaa6a05cc.tiff (415 MB) for quick visualization\n* Load and display the original image"},{"metadata":{"trusted":true},"cell_type":"code","source":"img = tiff.imread(\"../input/hubmap-kidney-segmentation/train/aaa6a05cc.tiff\")\n\nprint(\"Original Image Shape: \", img.shape)\nplt.figure(figsize=(24, 24), dpi=150)\nplt.imshow(img[:, :, 0], cmap=plt.cm.bone_r)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**If you look closely you can notice the circular features of potential Glomeruli functional tissue units (FTUs).**\n\n**mask to rle function**\nReference: https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def 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\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load the train.csv\ntrain.csv contains the unique IDs for each image, as well as an RLE-encoded representation of the mask for the objects in the image."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/hubmap-kidney-segmentation/train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create the mask from REL data\n\nmask = rle2mask(train_df.iloc[1, 1], (img.shape[1], img.shape[0]))\nmask.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Display the original image and the mask over it"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(24, 24), dpi=150)\nplt.imshow(img[:, :, 0], cmap='bone_r', alpha=0.9)\nplt.imshow(mask[:,:], cmap='bone_r', alpha=0.5)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Display a segment of the original image with the mask"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(24, 24), dpi=300)\nplt.imshow(img[12500:15000, 2000:5000, 0], cmap='bone_r', alpha=0.9)\nplt.imshow(mask[12500:15000, 2000:5000], cmap='bone_r', alpha=0.5)","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}