{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"! pip install https://github.com/CellProfiling/HPA-Cell-Segmentation/archive/master.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport cv2\nimport random\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Utils"},{"metadata":{"trusted":true},"cell_type":"code","source":"def prep_images(ID, img_dir='../input/hpa-single-cell-image-classification/train/'):\n    green_p = img_dir + f'{ID}_green.png'\n    blue_p = img_dir + f'{ID}_blue.png'\n    red_p = img_dir + f'{ID}_red.png'\n    yellow_p = img_dir + f'{ID}_yellow.png'\n\n    protein_img = cv2.imread(green_p)\n    nucleus_img = cv2.imread(blue_p)\n    microtubules_img = cv2.imread(red_p)\n    ER_img = cv2.imread(yellow_p)\n\n    return dict(protein=protein_img,\n                microtubules=microtubules_img,\n                endoplasmic_reticulum=ER_img,\n                nucleus=nucleus_img,\n                )\n\n\ndef merge_image(images):\n    \"\"\"\n    Return a 3-channel image.\n    The channels are: microtubules, endoplasmic reticulum, and nucleus\n    \"\"\"\n\n    merged = np.dstack((images['microtubules'][:, :, 0],\n                        images['endoplasmic_reticulum'][:, :, 0],\n                        images['nucleus'][:, :, 0],\n                        ))\n\n    return merged\n\n\ndef visualize(images, show_merge=True, ID=None, df=None):\n    if show_merge:\n        images['merge'] = 0\n\n    n = len(images)\n    f = plt.figure(figsize=(12, 4))\n    for i, (key, image) in enumerate(images.items()):\n        plt.subplot(1, n, i + 1)\n\n        if key == 'merge':\n            image = merge_image(images)\n\n        title = ' '.join(key.split('_')).title()\n        plt.imshow(image)\n        plt.title(title)\n        plt.xticks([])\n        plt.yticks([])\n\n        if ID is not None:\n            labels = df.set_index('ID').loc[ID, 'Label'].split('|')\n            labels = [label_map[key] for key in labels]\n            f.suptitle(labels)\n\n    f.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/hpa-single-cell-image-classification/train.csv')\n\ntrain","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# What should I expect the data format to be?"},{"metadata":{},"cell_type":"markdown","source":"The training image-level labels are provided for each sample in train.csv. The bulk of the data for images - train.zip. Each sample consists of four files. Each file represents a different filter on the subcellular protein patterns represented by the sample. The format should be [filename]_[filter color].png for the PNG files. Colors are red for microtubule channels, blue for nuclei channels, yellow for Endoplasmic Reticulum (ER) channels, and green for protein of interest."},{"metadata":{},"cell_type":"markdown","source":"# What am I predicting?"},{"metadata":{},"cell_type":"markdown","source":"You are predicting protein organelle localization labels for each cell in the image. Border cells are included when there is enough information to decide on the labels.\n\nThere are in total 19 different labels present in the dataset (18 labels for specific locations, and label 18 for negative and unspecific signal). The dataset is acquired in a highly standardized way using one imaging modality (confocal microscopy). However, the dataset comprises 17 different cell types of highly different morphology, which affect the protein patterns of the different organelles. All image samples are represented by four filters (stored as individual files), the protein of interest (green) plus three cellular landmarks: nucleus (blue), microtubules (red), endoplasmic reticulum (yellow). The green filter should hence be used to predict the label, and the other filters are used as references. The labels are represented as integers that map to the following:"},{"metadata":{"trusted":true},"cell_type":"code","source":"image_paths = glob('../input/hpa-single-cell-image-classification/train/*.png')\n\nlen(image_paths)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = {'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":"for _ in range(5):\n    ID = random.choice(train.ID)\n    images = prep_images(ID)\n    visualize(images, ID=ID, df=train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Cell Segmentation"},{"metadata":{"trusted":true},"cell_type":"code","source":"from hpacellseg import cellsegmentator\nfrom hpacellseg.utils import label_cell, label_nuclei","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_ids = []\nfor _ in range(5):\n    image_ids.append(random.choice(train.ID.unique()))\n    \nimage_dicts = [prep_images(image_id) for image_id in image_ids]\n\nimage_lists = []\nfor key in image_dicts[0].keys():\n    if key == 'protein':\n        continue\n    images = [image_dict[key][:, :, 0] for image_dict in image_dicts]\n    image_lists.append(images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"segmentator = cellsegmentator.CellSegmentator(nuclei_model='.nuclei_model.pth',\n                                              cell_model='.cell_model.pth',\n                                              scale_factor=0.25,\n                                              device='cuda',\n                                              padding=False,\n                                              multi_channel_model=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cell_segmentations = segmentator.pred_cells(image_lists)\nnuc_segmentations = segmentator.pred_nuclei(image_lists[2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(5):\n    img = merge_image(image_dicts[i])\n    nuclei_mask, cell_mask = label_cell(nuc_segmentations[i], cell_segmentations[i])\n    visualize(dict(image=img,\n                  nuclei_mask=nuclei_mask,\n                  cell_mask=cell_mask,\n                  ),\n              show_merge=False\n             )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}