{"nbformat_minor":1,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Plotting 9","metadata":{"_cell_guid":"3270caf9-5db0-4a2c-91ba-9a40355b4dac","collapsed":true,"_uuid":"d1f271b7341837967c25e6fe091e96877afb0943"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport imageio\n\ndef read_images(image_id=None, train_dir='../input/stage1_train'):\n    if image_id == None:\n        rand_index = np.random.randint(1,670)\n        image_id = os.listdir(train_dir)[rand_index]\n    \n    image_path = '{}/{}/images/{}.png'.format(train_dir, image_id, image_id)\n    img = imageio.imread(image_path)\n\n    mask_dir = '{}/{}/masks'.format(train_dir, image_id)\n    mask_paths = os.listdir(mask_dir)\n    mask_list = []\n    for mask_path in mask_paths:\n        mask_list.append(imageio.imread(mask_dir + '/' + mask_path))\n    mask_arr = np.stack(mask_list).max(axis=0)\n    \n    # The key to overlays is making invalid data 'nan'\n    mask = np.where(mask_arr > 0, 1, np.nan)\n    \n    return img, mask\n\n\ndef gen_plots(img, mask):\n    # Plot the original image (it is in color)\n    plt.imshow(img)\n    plt.imshow(mask, cmap='Wistia', alpha=0.75)\n    plt.axis('off')\n    return plt.gca()\n\n\ndef plot_image_id(image_id=None):\n    img, mask = read_images(image_id)\n    ax = gen_plots(img, mask)\n    plt.show()","metadata":{"_cell_guid":"356a026e-d355-49a6-99de-cb9940f201b0","collapsed":true,"_uuid":"1f1122cf66ab2fd06eeddb7ded7965f59336c4a0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_id()","metadata":{"_cell_guid":"982e2319-375a-4a48-b629-abc2d9b24e7f","_uuid":"193ec981e90fd9f6a0b2a339b6a32beeb569042b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, mask = read_images()\nax = gen_plots(img, mask)\n\nplt.show()","metadata":{"_cell_guid":"1c505756-5cb9-4d05-b95d-b361fb94e54d","_uuid":"30e4796ad20b6a396435f8849cbef0b7dfe1c773"},"execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","name":"python3","language":"python"},"language_info":{"mimetype":"text/x-python","name":"python","pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"version":3,"name":"ipython"}}}}