{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"#matplotlib template from:  https://stackoverflow.com/questions/46615554/how-to-display-multiple-images-in-one-figure-correctly/46616645\n#mask segmentation prediction string: https://www.kaggle.com/its7171/mmdetection-for-segmentation-inference","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Human Protein Atlas: Visualize Dataset\n\n# Contents\n\n1. [Load and view dataset](#1)\n1. [Read and show image data](#2)\n1. [Visualization information in HPA-dataset](#3)"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"1\"></a> <br>\n# <div class=\"alert alert-block alert-success\">Load and view dataset</div>\n\n## Import Libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import collections\nimport json\nimport os\nimport uuid\nfrom tqdm import tqdm\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image, ImageDraw, ImageFilter\nimport tifffile as tiff \nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"efb90745-88ca-4aaf-9c07-645e9cff2b20","_cell_guid":"ba482404-ee9a-4147-8821-bdb63329a9ba","trusted":true},"cell_type":"markdown","source":"## Load dataset"},{"metadata":{},"cell_type":"markdown","source":"### Image files"},{"metadata":{"trusted":true},"cell_type":"code","source":"#get an overview of some input image files\n%ls ../input/hpa-single-cell-image-classification/train | head -12","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#save the file directories to variables \n#save the file names to lists\ntraindir=\"../input/hpa-single-cell-image-classification\"+\"/train/\"\ntestdir=\"../input/hpa-single-cell-image-classification\"+\"/test/\"\ntrain = os.listdir(traindir)\ntest = os.listdir(testdir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check file amounts and if all images have same file extension\nn_png = [filename for filename in train if filename.find('png') != -1]\nprint(\"Train:\")\nprint(f\"   PNG files :  {len(n_png)}\")\nprint(f\"   total files :  {len(train)}\")\nn_png = [filename for filename in test if filename.find('png') != -1]\nprint(\"Test:\")\nprint(f\"   PNG files :  {len(n_png)}\")\nprint(f\"   total files :  {len(test)}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check if every imageID has one image per color or if some are missing\nn_blue = [filename for filename in train if filename.find('blue') != -1]\nn_blue = len(n_blue)\nn_green = [filename for filename in train if filename.find('green') != -1]\nn_green = len(n_green)\nn_red = [filename for filename in train if filename.find('red') != -1]\nn_red = len(n_red)\nn_yellow = [filename for filename in train if filename.find('yellow') != -1]\nn_yellow = len(n_yellow)\nprint(\"Train:\")\nprint(f\"   blue :  {n_blue}\")\nprint(f\"   green :  {n_green}\")\nprint(f\"   red :  {n_red}\")\nprint(f\"   yellow :  {n_yellow}\")\nn_blue = [filename for filename in test if filename.find('blue') != -1]\nn_blue = len(n_blue)\nn_green = [filename for filename in test if filename.find('green') != -1]\nn_green = len(n_green)\nn_red = [filename for filename in test if filename.find('red') != -1]\nn_red = len(n_red)\nn_yellow = [filename for filename in test if filename.find('yellow') != -1]\nn_yellow = len(n_yellow)\nprint(\"Test:\")\nprint(f\"   blue :  {n_blue}\")\nprint(f\"   green :  {n_green}\")\nprint(f\"   red :  {n_red}\")\nprint(f\"   yellow :  {n_yellow}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### CSV file"},{"metadata":{"trusted":true},"cell_type":"code","source":"#read class information and make a list of classes for each image\nimage_dataframe=pd.read_csv(\"../input/hpa-single-cell-image-classification/train.csv\")\nimage_dataframe[\"Label\"]=image_dataframe[\"Label\"].apply(lambda x:list(map(int, x.split(\"|\"))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_dataframe.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names={\n    0: \"Nucleoplasm\",\n    1: \"Nuclear membrane\",\n    2: \"Nucleoli\",\n    3: \"Nucleoli fibrillar center\",\n    4: \"Nuclear speckles\",\n    5: \"Nuclear bodies\",\n    6: \"Endoplasmic reticulum\",\n    7: \"Golgi apparatus\",\n    8: \"Intermediate filaments\",\n    9: \"Actin filaments\",\n    10: \"Microtubules\",\n    11: \"Mitotic spindle\",\n    12: \"Centrosome\",\n    13: \"Plasma membrane\",\n    14: \"Mitochondria\",\n    15: \"Aggresome\",\n    16: \"Cytosol\",\n    17: \"Vesicles and punctate cytosolic patterns\",\n    18: \"Negative\"\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"flat_list = [class_names[item] for sublist in image_dataframe[\"Label\"] for item in sublist]\nflat_list = pd.DataFrame(flat_list)\nflat_list.columns=[\"name\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#make a bar plot of the label frequencies\nsns.countplot(y = 'name',\n              data = flat_list,\n              order = flat_list['name'].value_counts().index)\n# Show the plot\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"2\"></a> <br>\n# <div class=\"alert alert-block alert-warning\">Read and show image data</div>\n\nWe are given microscopic images of human cells, tissues, and organs. These images are all in png format."},{"metadata":{"trusted":true},"cell_type":"code","source":"#visualize the four color maps of one of the images\ntrain.sort()\nfor i in range(4):\n    print(train[i])\n\nw = 10\nh = 10\nfig = plt.figure(figsize=(15, 15))\ncolumns = 2\nrows = 2\n\n# ax enables access to manipulate each of subplots\nax = []\ntitles=[\"Nuclei ['..._blue.png']\",\n        \"Proteins of interest ['..._green.png']\",\n        \"Microtubuli ['..._red.png']\",\n        \"Endoplasmatic Reticulum ['..._yellow.png']\"]\n\nfor i in range(columns*rows):\n    img = plt.imread(traindir+train[i])\n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[i].set_title(titles[i])  # set title\n    plt.imshow(img)\n\nplt.show()  # render the plot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check and visualize the image dimensions of the images\n\nshapes=[]\nfor file in tqdm(train):\n    image = plt.imread(traindir+file)\n    shapes.append([image.shape[0],image.shape[1]])\n    \nshapesdf = pd.DataFrame(shapes, columns =['width', 'height'])\nsns.jointplot(x=shapesdf['width'], y=shapesdf['height'])","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}