{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#### this notebook is part of the documentation on my HPA approach  \n    -> main notebook: https://www.kaggle.com/philipjamessullivan/0-hpa-approach-summary\n    \n# 2: combine image channels\n ## RESULTS:\n**dataset name:** 20 datasets with names \"hpa-composite-images-{}-of-20\" (linked to this notebook)  \n        --> dataset is in 20 parts because of kaggle errors with saving everything as one dataset  \n**file type:** png files  \n**contents:** images from HPA dataset combined from RGBY to RGB  ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"#constants\nIMG_FOLDER_PATH=\"../input/hpa-single-cell-image-classification/train/\"\nCSV_FILE_PATH=\"../input/hpa-single-cell-image-classification/train.csv\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load HPA dataset\nimport pandas as pd\nCSV_FILE_PATH=\"../input/hpa-single-cell-image-classification/train.csv\"\nid_labels_array=pd.read_csv(CSV_FILE_PATH)\nid_array=(id_labels_array[\"ID\"]).tolist()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to combine rgby to rgb\n#source:https://www.kaggle.com/kwentar/visualization-examples-of-each-class-in-rgb#Load-data:\nimport numpy as np\ndef rgby_to_rgb(r,g,b,y):\n    image_width,image_height=r.size\n    rgb_image = np.zeros(shape=(image_height, image_width, 3), dtype=np.float)\n    yellow = np.array(y)\n    # yellow is red + green\n    rgb_image[:, :, 0] += yellow/2   \n    rgb_image[:, :, 1] += yellow/2\n    # loop for R,G and B channels\n    for index, channel in enumerate([r,g,b]):\n        current_image = channel\n        rgb_image[:, :, index] += current_image\n    # Normalize image\n    rgb_image = rgb_image / rgb_image.max() * 255\n    return rgb_image.astype(np.uint8)\n#function to get rgb image from only img_id\nfrom PIL import Image\ndef imgid_to_rgb(img_id):\n    r=Image.open(IMG_FOLDER_PATH+img_id+\"_red.png\")\n    g=Image.open(IMG_FOLDER_PATH+img_id+\"_green.png\")\n    b=Image.open(IMG_FOLDER_PATH+img_id+\"_blue.png\")\n    y=Image.open(IMG_FOLDER_PATH+img_id+\"_yellow.png\")\n    rgb=rgby_to_rgb(r,g,b,y)\n    return rgb","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to convert all images to rgb images\n#exceeds 9h kaggle runtime so this must be run locally or separated into 20 parts and run in parallel in separate notebooks\n#these files have been saved in the datasets named hpa-composite-images-x-of-20\nfrom PIL import Image\nimport numpy\nfrom tqdm import tqdm\n\nCOMPOSITE_IMG_PATH=\"./composites/\"\nimport os\nif not os.path.exists(COMPOSITE_IMG_PATH):\n    os.makedirs(COMPOSITE_IMG_PATH)\n\nfor img_id in tqdm(id_array[:10]):  ##ONLY RUN 10 FOR DEMONSTRATION!\n    img_rgb = Image.fromarray(imgid_to_rgb(img_id))\n    img_rgb.save(COMPOSITE_IMG_PATH+img_id+\".png\")\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save to zip\nimport shutil\nzip_name = 'composites'\ndirectory_name = 'composites'\n\nshutil.make_archive(zip_name, 'zip', directory_name)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#remove original folder\n!rm -r composites","metadata":{},"execution_count":null,"outputs":[]}]}