{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport sys\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom zipfile import ZipFile\nfrom PIL import Image\nfrom matplotlib.pyplot import imshow\nimport cv2\n\ntrain_df = pd.read_csv('../input/train.csv')\ntest_df = pd.read_csv('../input/test.csv')\nsample_df = pd.read_csv(\"../input/sample_submission.csv\")\n# print(train_df.shape)\n# print(test_df.shape)\n\n\nplate_names = [\"Plate1\", \"Plate2\", \"Plate3\", \"Plate4\"]\ntest_experiment_names = [name for name in os.listdir(\"../input/test\")]\ntrain_experiment_names = [name for name in os.listdir(\"../input/train\")]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### See frequency for each 'sirna' in train dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = train_df[\"sirna\"].plot.hist(bins=max(train_df[\"sirna\"]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))\nprint(os.listdir(\"../input/train\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = \"../input/test/HEPG2-11/Plate1/G07_s2_w1.png\"\nimg = Image.open(file)\nimshow(np.asarray(img))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# This will show 5 images for each plate from test dataset\nplate_n = 0\nfor exp in test_experiment_names:\n    for plate in plate_names:\n        path = \"../input/test/\" + exp + \"/\" + plate + \"/\"\n        print(exp + \"/\" + plate)\n        plt.figure(figsize=(18, 16))\n        for image_path in os.listdir(path):\n            file = path + image_path\n            img = Image.open(file)\n            plt.subplot(1,5,plate_n+1), plt.imshow(img)\n            \n            plate_n = plate_n + 1\n            if plate_n == 5:\n                break\n        plt.show()\n        plate_n = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# This will show 5 images for each plate from train dataset\nplate_n = 0\nfor exp in train_experiment_names:\n    for plate in plate_names:\n        path = \"../input/train/\" + exp + \"/\" + plate + \"/\"\n        print(exp + \"/\" + plate)\n        plt.figure(figsize=(18, 16))\n        for image_path in os.listdir(path):\n            file = path + image_path\n            img = Image.open(file)\n            plt.subplot(1,5,plate_n+1), plt.imshow(img)\n            \n            plate_n = plate_n + 1\n            if plate_n == 5:\n                break\n        plt.show()\n        plate_n = 0","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Check how each sirna behaves"},{"metadata":{"trusted":true},"cell_type":"code","source":"plate_n = 0\n# Check all sirnas groupm\nfor label in range(max(train_df[\"sirna\"])):\n    label1 = train_df[train_df[\"sirna\"] == label]\n#     print(\"Sirna == \", label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"label2 = train_df[train_df[\"sirna\"] == 1]\n\nfor exp in train_experiment_names:\n    if exp not in list(label2[\"experiment\"]):\n        continue\n    \n    # Find index of exp found\n    idx = label2.index[label2['experiment'] == exp].tolist()[0]\n    \n    # Find which plate and well\n    plate = \"Plate\" + str(label2[\"plate\"][idx])\n    well = str(label2[\"well\"][idx])\n    \n    path = \"../input/train/\" + exp + \"/\" + plate + \"/\"\n    print(exp + \"/\" + plate, well)\n    plt.figure(figsize=(18, 16))\n    \n    for image_path in os.listdir(path):\n        if image_path.split(well)[0] == \"\":\n            file = path + image_path\n            img = Image.open(file)\n            plt.subplot(1,5,plate_n+1), plt.imshow(np.asarray(img))\n            \n            plate_n = plate_n + 1\n            if plate_n == 5:\n                break\n                \n    plt.show()\n    plate_n = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(file,0)\n\nhist,bins = np.histogram(img.flatten(),256,[0,256])\n\ncdf = hist.cumsum()\ncdf_normalized = cdf * hist.max()/ cdf.max()\n\nplt.plot(cdf_normalized, color = 'b')\nplt.hist(img.flatten(),256,[0,256], color = 'r')\nplt.xlim([0,256])\nplt.legend(('cdf','histogram'), loc = 'upper left')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(file,0)\nequ = cv2.equalizeHist(img)\nres = np.hstack((img, equ))\nplt.imshow(res)\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist,bins = np.histogram(equ.flatten(),256,[0,256])\n\ncdf = hist.cumsum()\ncdf_normalized = cdf * hist.max()/ cdf.max()\n\nplt.plot(cdf_normalized, color = 'b')\nplt.hist(img.flatten(),256,[0,256], color = 'r')\nplt.xlim([0,256])\nplt.legend(('cdf','histogram'), loc = 'upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(file)\n# img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n\ncl1 = clahe.apply(img_gray)\nres = np.hstack((img_gray, cl1))\nplt.imshow(cl1)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(file)\ngray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nret,thresh_trunc = cv2.threshold(gray_image,50,255,cv2.THRESH_TRUNC)\nret,thresh_tozero_inv = cv2.threshold(gray_image,50,255,cv2.THRESH_TOZERO_INV)\n\n#DISPLAYING THE DIFFERENT THRESHOLDING STYLES\nnames = ['Original Image','THRESH_TRUNC','THRESH_TOZERO_INV']\nimages = gray_image,thresh_trunc,thresh_tozero_inv\n\nplt.figure(figsize=(18, 16))\nfor i in range(3):\n    plt.subplot(1,3,i+1),plt.imshow(images[i],'gray')\n    plt.title(names[i])\n    plt.xticks([]),plt.yticks([])\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ret,thresh_global = cv2.threshold(gray_image,127,255,cv2.THRESH_BINARY)\n\nthresh_mean = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)\nthresh_gaussian = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)\n\nnames = ['Original Image','Global Thresholding','Adaptive Mean Threshold','Adaptive Gaussian Thresholding']\nimages = [gray_image,thresh_global,thresh_mean,thresh_gaussian]\n\nplt.figure(figsize=(18, 16))\nfor i in range(4):\n    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')\n    plt.title(names[i])\n    plt.xticks([]),plt.yticks([])\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#using the averaging kernel for image smoothening \naveraging_kernel = np.ones((3,3),np.float32)/3\nfiltered_image = cv2.filter2D(img, -1, averaging_kernel)\nplt.imshow(filtered_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}