{"cells":[{"metadata":{"_uuid":"f362fca5205f338964cc81073a5e6afb45c0f180"},"cell_type":"markdown","source":"## Introduction\nThe hypothesis: if we visualize a few examples for each class, we will get the visual patterns for each class.\n\nAlso, we will get the method for merge channels into one image"},{"metadata":{"_uuid":"8dfd5ee42d7ec011abb41f67db7bae557b242401"},"cell_type":"markdown","source":"## Imports and constants:"},{"metadata":{"trusted":true,"_uuid":"a589c87dc8abc787f213e410dd82da3ab818ce9b"},"cell_type":"code","source":"%matplotlib inline \nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport os\nfrom PIL import Image\n\n\ndata_train_dir = r'../input/train'\nanswer_file_path = r'../input/train.csv'\n\nchannels = ['_yellow', '_red', '_green', '_blue']\n\nindex_class_dict = {\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: \"Peroxisomes\",\n    9: \"Endosomes\",\n    10: \"Lysosomes\",\n    11: \"Intermediate filaments\",\n    12: \"Actin filaments\",\n    13: \"Focal adhesion sites\",\n    14: \"Microtubules\",\n    15: \"Microtubule ends\",\n    16: \"Cytokinetic bridge\",\n    17: \"Mitotic spindle\",\n    18: \"Microtubule organizing center\",\n    19: \"Centrosome\",\n    20: \"Lipid droplets\",\n    21: \"Plasma membrane\",\n    22: \"Cell junctions\",\n    23: \"Mitochondria\",\n    24: \"Aggresome\",\n    25: \"Cytosol\",\n    26: \"Cytoplasmic bodies\",\n    27: \"Rods & rings\"\n}\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7f09a169e9e70a11405fd05ba048c7151de6f263"},"cell_type":"markdown","source":"## Load data:"},{"metadata":{"trusted":true,"_uuid":"016ea7a2796b0a8e40a6c072bfd3a5d65488b965"},"cell_type":"code","source":"train_df = pd.read_csv(answer_file_path)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b387759c46fe66e0271d50c8fda0d77c1b866229"},"cell_type":"markdown","source":"## Create cols for each class:"},{"metadata":{"trusted":true,"_uuid":"97faa7d8a3b27646d23290d4eb4d11cf860b4b87"},"cell_type":"code","source":"train_df[f'target_vec'] = train_df['Target'].map(lambda x: list(map(int, x.strip().split())))\nfor i in range(28):\n    train_df[f'{index_class_dict[i]}'] = train_df['Target'].map(\n             lambda x: 1 if str(i) in x.strip().split() else 0)\ntrain_df.head()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b6e6ec9b6037026b2ff4a554abd0c6ca365b206"},"cell_type":"markdown","source":"## Get part of data with specific class:"},{"metadata":{"trusted":true,"_uuid":"89a623ee477812178d3372e939d65326de332022"},"cell_type":"code","source":"class_index = 1  # Nuclear membrane\ncurrent_part = train_df[train_df[index_class_dict[class_index]] == 1]\nprint(f'shape before {train_df.shape}, shape after {current_part.shape}')\ncurrent_part.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0abf6262d99149ff3236f9fa3cebff99a52d013b"},"cell_type":"markdown","source":"## Functions for visualization:"},{"metadata":{"trusted":true,"_uuid":"544bd427b8035fdd8bf42b7b9a03eec6a42481ba"},"cell_type":"code","source":"def make_rgb_image_from_four_channels(channels: list, image_width=512, image_height=512) -> np.ndarray:\n    \"\"\"\n    It makes literally RGB image from source four channels, \n    where yellow image will be yellow color, red will be red and so on  \n    \"\"\"\n    rgb_image = np.zeros(shape=(image_height, image_width, 3), dtype=np.float)\n    yellow = np.array(Image.open(channels[0]))\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(channels[1:]):\n        current_image = Image.open(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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d763ca7ed46a26062156a78b1d9aabc7176ef32e"},"cell_type":"code","source":"def visualize_part(start_class_index=0, nrows=4, ncols=3):\n    \"\"\"\n    Visualize the part of classes, started from class with index start_class_index,\n    make nrows classes, ncols examples for each one\n    \"\"\"\n    fig, ax = plt.subplots(nrows = nrows, ncols=ncols, figsize=(15, 25))\n    for class_index in range(nrows):\n        current_index = class_index + start_class_index\n        for sample in range(ncols):\n            current_part = train_df[train_df[index_class_dict[current_index]] == 1] \n            # 0 index is id\n            random_index = np.random.choice(current_part.values.shape[0], 1, replace=False)\n            # random line from data with selected class\n            current_line = current_part.values[random_index][0]\n            image_names = [os.path.join(data_train_dir, current_line[0]) \n                           + x + '.png' for x in channels]\n            rgb_image = make_rgb_image_from_four_channels(image_names)\n            # text annotations, main title and subclasses (may be empty in case one label)\n            main_class = index_class_dict[current_index]+'\\n'\n            # 2 index is vector with classes, split version of Target col\n            other_classes = [index_class_dict[x] for x in current_line[2] \n                             if x != (current_index)]\n            subtitle = ', '.join(other_classes)\n            # show image\n            ax[class_index, sample].set_title(main_class, fontsize=18)\n            ax[class_index, sample].text(250, -10, subtitle, \n                                         fontsize=14, horizontalalignment='center')\n            ax[class_index, sample].imshow(rgb_image)\n            ax[class_index, sample].set_xticklabels([])\n            ax[class_index, sample].set_yticklabels([])\n            ax[class_index, sample].tick_params(left=False, bottom=False)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"430efa2e7d62034762ca43e65c4654b2b70f6cd1"},"cell_type":"markdown","source":"## Let's visualize 3 examples for each class:"},{"metadata":{"trusted":true,"_uuid":"45560c00d0042832c6e99439cebdeb3a44e4fd08"},"cell_type":"code","source":"visualize_part(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea1395c988862c62c7bf38d4f2f7a50e8385871c"},"cell_type":"code","source":"visualize_part(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae69a228cbb73c92a6175a3e2cf40c63622e26c4"},"cell_type":"code","source":"visualize_part(8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c61c6fecc7c65068a80e27181e4cceda5383526"},"cell_type":"code","source":"visualize_part(12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b1cddf9c4100131c918a285aaab39f2ef5bb2b2"},"cell_type":"code","source":"visualize_part(16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e2d8c011baedc43d73ae0a7915393d106a55d9a"},"cell_type":"code","source":"visualize_part(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7e3ea26883163eabfd52875e1e0e48d70dd2f1d"},"cell_type":"code","source":"visualize_part(24)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4b82d795c4f3c46b703e86f124d6a1039480a625"},"cell_type":"markdown","source":"## Conclusion\nIn fact, I don't see any special features for each class and don't understand what differences really are. Provably, addition of yellow channel is a bad idea (yellow color is red + green) or we have to analyse only green one, but I did't get good results in my experiments yet. I guess I will allow to network choose the correct features."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}