{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Imports\nimport os\n\nimport openslide\nfrom IPython.display import Image, display\n#     Allows viewing of images\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## How to View Train_Images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Setting dataset directories to variables\n\ntrain_dir = \"/kaggle/input/prostate-cancer-grade-assessment/train_images/\"\n#     train_images\n\nmask_dir = \"/kaggle/input/prostate-cancer-grade-assessment/train_label_masks/\"\n#     train_maskes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Viewing train images using OpenSlide and IPython.display\n# Special Thanks to the getting started guide: https://www.kaggle.com/wouterbulten/getting-started-with-the-panda-dataset\n\nimage_name = \"00412139e6b04d1e1cee8421f38f6e90.tiff\"\n#     file_name\n\nslide = openslide.OpenSlide(os.path.join(train_dir, image_name))\n#     Creates OpenSlide object at the directed path variable\n#     os.path.join creates a path string inserting '/' if needed\n\ndisplay(slide.get_thumbnail(size=(400,500)))\n#     Opens the image and displays a thumbnail in the notebook\n\nslide.close()\n#     Close image from memory","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We can also view a subsection of the image if you want\nimage_name = \"005e66f06bce9c2e49142536caf2f6ee.tiff\"\nslide = openslide.OpenSlide(os.path.join(train_dir, image_name))\ndisplay(slide.read_region((17800,19500), 0, (500,500)))\n#     At location X = 17800 and Y = 19500\n#     At image level zero 0\n#     View a 500 by 500 pixel region of the image\nslide.close()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Image Properties","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# We can also view an images properties\nimage_name = \"00412139e6b04d1e1cee8421f38f6e90.tiff\"\nslide = openslide.OpenSlide(os.path.join(train_dir, image_name))\nproperties = str(slide.properties)\n#     Print commands always occur at the end of the cell so the picture properties must be grabbed before closing the slide.\nslide.close()\n\nprint(properties)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Better View of the Properties\nimport ast\n\nast.literal_eval(properties.strip(\"<,>,_,PropertyMap,\").strip())\n#     literal_eval can convert a string dictionary into a dictionary for an easier print format\n#     But we first have to remove extra characters and extra space ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**EXPLAINING IMAGE PROPERTIES**\n- **level-count**: how many resolutions of the image we can grab. Level 0 is the raw image at the highest resolution while higher levels have smaller resolutions due to downsampling\n- **height**: image height at that level\n- **width**: image width at that level\n- **downsample**: downsampling ratio\n- **quickhash-1**: unique slide identity\n- **vendor**: provider of the image, 'generic-tiff' means tiff image with no vendor info provided\n- **ResolutionUnit**: Unit for X and YResolution. Pixels/ResolutionUnit\n- **XResolution**: X-axis Pixels per Resolution Unit\n- **YResolution**: Y-axis Pixels per Resolution Unit","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## How to View Image Masks","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Viewing Image Masks using MatPlotLib\nimage_name = \"00412139e6b04d1e1cee8421f38f6e90_mask.tiff\"\nslide = openslide.OpenSlide(os.path.join(mask_dir, image_name))\nplt.imshow(slide.read_region((0,0), slide.level_count - 1, slide.level_dimensions[-1]).split()[0])\n#     plt.imshow let's you show a matrix of numbers as an image, which is what a mask is\n#     read_region still works on this matrix, but it spits out four different matrixs \n#     Only the first matrix in the list has the relevant information for plotting\nslide.close()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Confirming there are equal amount of train_images, train_maskes, and image_id (in train.csv)","execution_count":null},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"from os import listdir\n#     listdir generates a list of items in the directory","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create a list of the three for list comparisions\n\nlist_trainimages = listdir(train_dir)\nlist_trainmask = listdir(mask_dir)\nlist_imageid = pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv')['image_id']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"\"\"\nTrain Images: {len(list_trainimages)}\nTrain Masks: {len(list_trainmask)}\nImage Ids: {len(list_imageid)}\n\"\"\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's find the images that don't have masks\n\nlist_trainimages = [image.split(\".\")[0] for image in list_trainimages]\nlist_trainmask = [mask.split(\"_\")[0] for mask in list_trainmask]\n#     Removes .tiff and _mask.tiff from each file name","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image in list_trainimages:\n    if image not in list_trainmask:\n        print(image)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Here are the list of images that don't have a mask. The masks were provided as a guidance for training our models. It would be interesting to see if the impact of this will disrupt my project in the future.**","execution_count":null}],"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}