{"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":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2 as cv\nfrom imutils import paths\nimport skimage\nfrom skimage.filters import sobel\nfrom skimage import segmentation\nfrom skimage.color import label2rgb\nfrom skimage.color import rgb2hed, hed2rgb\nfrom skimage.exposure import rescale_intensity\nimport tifffile as tifi\nfrom sklearn.preprocessing import StandardScaler\nfrom skimage.measure import regionprops, regionprops_table\nfrom scipy import ndimage as ndi\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-03T10:12:25.543889Z","iopub.execute_input":"2022-08-03T10:12:25.544903Z","iopub.status.idle":"2022-08-03T10:12:25.552112Z","shell.execute_reply.started":"2022-08-03T10:12:25.544861Z","shell.execute_reply":"2022-08-03T10:12:25.551012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\nother = pd.read_csv(\"../input/mayo-clinic-strip-ai/other.csv\")\ntest = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:25.671218Z","iopub.execute_input":"2022-08-03T10:12:25.67164Z","iopub.status.idle":"2022-08-03T10:12:25.687772Z","shell.execute_reply.started":"2022-08-03T10:12:25.671598Z","shell.execute_reply":"2022-08-03T10:12:25.686713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:25.789596Z","iopub.execute_input":"2022-08-03T10:12:25.790511Z","iopub.status.idle":"2022-08-03T10:12:25.80254Z","shell.execute_reply.started":"2022-08-03T10:12:25.790476Z","shell.execute_reply":"2022-08-03T10:12:25.801259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:25.904803Z","iopub.execute_input":"2022-08-03T10:12:25.905191Z","iopub.status.idle":"2022-08-03T10:12:25.91771Z","shell.execute_reply.started":"2022-08-03T10:12:25.905162Z","shell.execute_reply":"2022-08-03T10:12:25.916596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.023917Z","iopub.execute_input":"2022-08-03T10:12:26.024727Z","iopub.status.idle":"2022-08-03T10:12:26.036517Z","shell.execute_reply.started":"2022-08-03T10:12:26.02469Z","shell.execute_reply":"2022-08-03T10:12:26.035109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking null values in the dataset\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.115517Z","iopub.execute_input":"2022-08-03T10:12:26.116319Z","iopub.status.idle":"2022-08-03T10:12:26.125535Z","shell.execute_reply.started":"2022-08-03T10:12:26.116285Z","shell.execute_reply":"2022-08-03T10:12:26.124253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.215326Z","iopub.execute_input":"2022-08-03T10:12:26.215725Z","iopub.status.idle":"2022-08-03T10:12:26.229909Z","shell.execute_reply.started":"2022-08-03T10:12:26.215692Z","shell.execute_reply":"2022-08-03T10:12:26.228632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking null values in the dataset\nother.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.317943Z","iopub.execute_input":"2022-08-03T10:12:26.318851Z","iopub.status.idle":"2022-08-03T10:12:26.327657Z","shell.execute_reply.started":"2022-08-03T10:12:26.318814Z","shell.execute_reply":"2022-08-03T10:12:26.326609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.417894Z","iopub.execute_input":"2022-08-03T10:12:26.418543Z","iopub.status.idle":"2022-08-03T10:12:26.43154Z","shell.execute_reply.started":"2022-08-03T10:12:26.418504Z","shell.execute_reply":"2022-08-03T10:12:26.430219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(other.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.516837Z","iopub.execute_input":"2022-08-03T10:12:26.517251Z","iopub.status.idle":"2022-08-03T10:12:26.673842Z","shell.execute_reply.started":"2022-08-03T10:12:26.517219Z","shell.execute_reply":"2022-08-03T10:12:26.672771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will handle missing values by replacing them with the 'Mode' value\n# We can do this because of these are all Categorical features\nother['other_specified']=other['other_specified'].fillna(other['other_specified'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.676104Z","iopub.execute_input":"2022-08-03T10:12:26.676791Z","iopub.status.idle":"2022-08-03T10:12:26.682947Z","shell.execute_reply.started":"2022-08-03T10:12:26.676757Z","shell.execute_reply":"2022-08-03T10:12:26.681976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(other.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.703765Z","iopub.execute_input":"2022-08-03T10:12:26.704135Z","iopub.status.idle":"2022-08-03T10:12:26.857422Z","shell.execute_reply.started":"2022-08-03T10:12:26.704106Z","shell.execute_reply":"2022-08-03T10:12:26.856251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(other.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.859786Z","iopub.execute_input":"2022-08-03T10:12:26.860736Z","iopub.status.idle":"2022-08-03T10:12:26.866749Z","shell.execute_reply.started":"2022-08-03T10:12:26.860692Z","shell.execute_reply":"2022-08-03T10:12:26.865588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.896006Z","iopub.execute_input":"2022-08-03T10:12:26.896389Z","iopub.status.idle":"2022-08-03T10:12:26.90511Z","shell.execute_reply.started":"2022-08-03T10:12:26.896359Z","shell.execute_reply":"2022-08-03T10:12:26.903925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:26.993576Z","iopub.execute_input":"2022-08-03T10:12:26.993981Z","iopub.status.idle":"2022-08-03T10:12:27.002791Z","shell.execute_reply.started":"2022-08-03T10:12:26.993946Z","shell.execute_reply":"2022-08-03T10:12:27.001434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['CE', 'LAA']\ncounts = [547, 207]\nexplode = (0.1, 0)\nplt.pie(counts, explode=explode, labels=labels, autopct='%1.1f%%',\n        shadow=True, startangle=60) \nplt.legend()\nplt.title('train label')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:27.088692Z","iopub.execute_input":"2022-08-03T10:12:27.089158Z","iopub.status.idle":"2022-08-03T10:12:27.316861Z","shell.execute_reply.started":"2022-08-03T10:12:27.089123Z","shell.execute_reply":"2022-08-03T10:12:27.315311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16, 6))\nsns.countplot(data=train, x=\"image_num\", hue=\"center_id\")\nplt.ylim(0, 350)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:27.319948Z","iopub.execute_input":"2022-08-03T10:12:27.32088Z","iopub.status.idle":"2022-08-03T10:12:27.731485Z","shell.execute_reply.started":"2022-08-03T10:12:27.320822Z","shell.execute_reply":"2022-08-03T10:12:27.730146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 6))\nsns.scatterplot(data=train, x=\"image_num\", y=\"center_id\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:27.733205Z","iopub.execute_input":"2022-08-03T10:12:27.733693Z","iopub.status.idle":"2022-08-03T10:12:27.97341Z","shell.execute_reply.started":"2022-08-03T10:12:27.733647Z","shell.execute_reply":"2022-08-03T10:12:27.972231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 6))\nsns.countplot(data=other, x=\"other_specified\")\nplt.ylabel('Number of them')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:27.975547Z","iopub.execute_input":"2022-08-03T10:12:27.975951Z","iopub.status.idle":"2022-08-03T10:12:28.221718Z","shell.execute_reply.started":"2022-08-03T10:12:27.975923Z","shell.execute_reply":"2022-08-03T10:12:28.220599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 6))\nsns.countplot(data=other, x=\"image_num\")\nplt.ylabel('Number of them')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:28.22326Z","iopub.execute_input":"2022-08-03T10:12:28.224135Z","iopub.status.idle":"2022-08-03T10:12:28.637533Z","shell.execute_reply.started":"2022-08-03T10:12:28.224092Z","shell.execute_reply":"2022-08-03T10:12:28.636612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 6))\nsns.scatterplot(data=other, x=\"image_num\", y=\"other_specified\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:28.639657Z","iopub.execute_input":"2022-08-03T10:12:28.640532Z","iopub.status.idle":"2022-08-03T10:12:28.896515Z","shell.execute_reply.started":"2022-08-03T10:12:28.640485Z","shell.execute_reply":"2022-08-03T10:12:28.895477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:28.898249Z","iopub.execute_input":"2022-08-03T10:12:28.898567Z","iopub.status.idle":"2022-08-03T10:12:28.907142Z","shell.execute_reply.started":"2022-08-03T10:12:28.898525Z","shell.execute_reply":"2022-08-03T10:12:28.906031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['Unknown', 'Other']\ncounts = [331, 65]\nexplode = (0.1, 0)\nplt.pie(counts, explode=explode, labels=labels, autopct='%1.1f%%',\n        shadow=True, startangle=60) \nplt.legend()\nplt.title('Other labels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:28.908809Z","iopub.execute_input":"2022-08-03T10:12:28.909185Z","iopub.status.idle":"2022-08-03T10:12:29.188596Z","shell.execute_reply.started":"2022-08-03T10:12:28.909139Z","shell.execute_reply":"2022-08-03T10:12:29.187206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(other.label)\n#sns.countplot(kill.manner_of_death)\nplt.title(\"Other labels\",color = 'blue',fontsize=15)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.195576Z","iopub.execute_input":"2022-08-03T10:12:29.196178Z","iopub.status.idle":"2022-08-03T10:12:29.372229Z","shell.execute_reply.started":"2022-08-03T10:12:29.196121Z","shell.execute_reply":"2022-08-03T10:12:29.371148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom IPython.display import Image\nimport glob as glob\nimport imageio\nimport scipy.ndimage as ndimage\nfrom scipy.ndimage.interpolation import zoom\nfrom skimage import measure, morphology, segmentation\nfrom skimage.transform import resize\nfrom time import time, sleep\nfrom tqdm import trange, tqdm","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.374148Z","iopub.execute_input":"2022-08-03T10:12:29.37462Z","iopub.status.idle":"2022-08-03T10:12:29.380945Z","shell.execute_reply.started":"2022-08-03T10:12:29.374544Z","shell.execute_reply":"2022-08-03T10:12:29.379784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/mayo-clinic-strip-ai/train'\ntrain_images = sorted(list(paths.list_images(path)))\nprint('There are ' + str(len(train_images)) + \" images in the train folder\")\ntrain_images[:5]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.382663Z","iopub.execute_input":"2022-08-03T10:12:29.383321Z","iopub.status.idle":"2022-08-03T10:12:29.399502Z","shell.execute_reply.started":"2022-08-03T10:12:29.38328Z","shell.execute_reply":"2022-08-03T10:12:29.398303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/mayo-clinic-strip-ai/other'\nother_images = sorted(list(paths.list_images(path)))\nprint('There are ' + str(len(other_images)) + \" images in the other folder\")\nother_images[:5]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.40106Z","iopub.execute_input":"2022-08-03T10:12:29.401395Z","iopub.status.idle":"2022-08-03T10:12:29.413831Z","shell.execute_reply.started":"2022-08-03T10:12:29.401345Z","shell.execute_reply":"2022-08-03T10:12:29.412579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_tiff(path):\n    image = tifi.imread(path)\n    filename = path.split('/')[-1].rstrip('.tif')\n    print(\"image_id: \" + filename)\n    return image, filename","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.415164Z","iopub.execute_input":"2022-08-03T10:12:29.415475Z","iopub.status.idle":"2022-08-03T10:12:29.420636Z","shell.execute_reply.started":"2022-08-03T10:12:29.415446Z","shell.execute_reply":"2022-08-03T10:12:29.419794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image, filename = read_tiff(train_images[15])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:29.421415Z","iopub.execute_input":"2022-08-03T10:12:29.421717Z","iopub.status.idle":"2022-08-03T10:12:43.431499Z","shell.execute_reply.started":"2022-08-03T10:12:29.42169Z","shell.execute_reply":"2022-08-03T10:12:43.430424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.433344Z","iopub.execute_input":"2022-08-03T10:12:43.433697Z","iopub.status.idle":"2022-08-03T10:12:43.441439Z","shell.execute_reply.started":"2022-08-03T10:12:43.433667Z","shell.execute_reply":"2022-08-03T10:12:43.439861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image(image):\n    re_sized_image = cv.resize(image,(int(image.shape[1]/33),int(image.shape[0]/33)),interpolation= cv.INTER_LINEAR)\n    return re_sized_image","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.442513Z","iopub.execute_input":"2022-08-03T10:12:43.44285Z","iopub.status.idle":"2022-08-03T10:12:43.461636Z","shell.execute_reply.started":"2022-08-03T10:12:43.442822Z","shell.execute_reply":"2022-08-03T10:12:43.4606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re_sized_image = resize_image(image)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.463154Z","iopub.execute_input":"2022-08-03T10:12:43.463668Z","iopub.status.idle":"2022-08-03T10:12:43.482529Z","shell.execute_reply.started":"2022-08-03T10:12:43.463636Z","shell.execute_reply":"2022-08-03T10:12:43.481626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re_sized_image.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.484104Z","iopub.execute_input":"2022-08-03T10:12:43.484403Z","iopub.status.idle":"2022-08-03T10:12:43.491175Z","shell.execute_reply.started":"2022-08-03T10:12:43.484376Z","shell.execute_reply":"2022-08-03T10:12:43.490012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_image_grayscale(image):\n    gray_image = cv.cvtColor(image, cv.COLOR_RGB2GRAY)\n    return gray_image","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.493156Z","iopub.execute_input":"2022-08-03T10:12:43.49364Z","iopub.status.idle":"2022-08-03T10:12:43.499443Z","shell.execute_reply.started":"2022-08-03T10:12:43.493599Z","shell.execute_reply":"2022-08-03T10:12:43.498284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_gray_img = convert_image_grayscale(re_sized_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.50136Z","iopub.execute_input":"2022-08-03T10:12:43.501953Z","iopub.status.idle":"2022-08-03T10:12:43.51088Z","shell.execute_reply.started":"2022-08-03T10:12:43.501901Z","shell.execute_reply":"2022-08-03T10:12:43.509819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(resized_gray_img, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.513083Z","iopub.execute_input":"2022-08-03T10:12:43.513691Z","iopub.status.idle":"2022-08-03T10:12:43.832869Z","shell.execute_reply.started":"2022-08-03T10:12:43.513652Z","shell.execute_reply":"2022-08-03T10:12:43.831943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segment_images(resized_gray_img):\n    elevation_map = sobel(resized_gray_img)\n    markers = np.zeros_like(resized_gray_img)\n    markers[resized_gray_img >= resized_gray_img.mean()] = 1\n    markers[resized_gray_img < resized_gray_img.mean()] = 2\n    segmented_img = segmentation.watershed(elevation_map, markers)\n    filled_segments = ndi.binary_fill_holes(segmented_img - 1)\n    labeled_segments, _ = ndi.label(filled_segments)\n    return labeled_segments","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.834752Z","iopub.execute_input":"2022-08-03T10:12:43.835118Z","iopub.status.idle":"2022-08-03T10:12:43.841613Z","shell.execute_reply.started":"2022-08-03T10:12:43.835085Z","shell.execute_reply":"2022-08-03T10:12:43.840498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labeled_segments = segment_images(resized_gray_img)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:43.843548Z","iopub.execute_input":"2022-08-03T10:12:43.844025Z","iopub.status.idle":"2022-08-03T10:12:44.202273Z","shell.execute_reply.started":"2022-08-03T10:12:43.843973Z","shell.execute_reply":"2022-08-03T10:12:44.201317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_labeled_segments(labeled_segments, resized_gray_img):\n    image_label_overlay = label2rgb(labeled_segments, image=resized_gray_img, bg_label=0)\n    fig, ax = plt.subplots(figsize=(10, 8))\n    ax.imshow(image_label_overlay, cmap=plt.cm.gray)\n    ax.set_title('segmentation')\n    ax.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.208598Z","iopub.execute_input":"2022-08-03T10:12:44.209863Z","iopub.status.idle":"2022-08-03T10:12:44.216369Z","shell.execute_reply.started":"2022-08-03T10:12:44.209815Z","shell.execute_reply":"2022-08-03T10:12:44.215226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_labeled_segments(labeled_segments, resized_gray_img)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.218075Z","iopub.execute_input":"2022-08-03T10:12:44.218444Z","iopub.status.idle":"2022-08-03T10:12:44.651373Z","shell.execute_reply.started":"2022-08-03T10:12:44.218414Z","shell.execute_reply":"2022-08-03T10:12:44.650005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_object_coordinates(labeled_segments):\n    properties =['area','bbox','convex_area','bbox_area', 'major_axis_length', 'minor_axis_length', 'eccentricity']\n    df = pd.DataFrame(regionprops_table(labeled_segments, properties=properties))\n    standard_scaler = StandardScaler()\n    scaled_area = standard_scaler.fit_transform(df.area.values.reshape(-1,1))\n    df['scaled_area'] = scaled_area\n    df.sort_values(by=\"scaled_area\", ascending=False, inplace=True)\n    objects = df[df['scaled_area']>=.75]\n    display(objects.head())\n    object_coordinates = [(row['bbox-0'],row['bbox-1'],row['bbox-2'],row['bbox-3'] )for index, row in objects.iterrows()]\n    return object_coordinates","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.653336Z","iopub.execute_input":"2022-08-03T10:12:44.654004Z","iopub.status.idle":"2022-08-03T10:12:44.661421Z","shell.execute_reply.started":"2022-08-03T10:12:44.653968Z","shell.execute_reply":"2022-08-03T10:12:44.660503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_coordinates = get_object_coordinates(labeled_segments)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.66274Z","iopub.execute_input":"2022-08-03T10:12:44.663265Z","iopub.status.idle":"2022-08-03T10:12:44.880866Z","shell.execute_reply.started":"2022-08-03T10:12:44.663229Z","shell.execute_reply":"2022-08-03T10:12:44.880044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.patches import Rectangle\ndef plot_object_coordinates(object_coordinates, re_sized_image):\n    fig, ax = plt.subplots(1,1, figsize=(18, 16), dpi = 80)\n    for blob in object_coordinates:\n        width = blob[3] - blob[1]\n        height = blob[2] - blob[0]\n        patch = Rectangle((blob[1],blob[0]), width, height, edgecolor='r', facecolor='none')\n        ax.add_patch(patch)\n        ax.imshow(re_sized_image);\n        ax.set_axis_off()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.88189Z","iopub.execute_input":"2022-08-03T10:12:44.882915Z","iopub.status.idle":"2022-08-03T10:12:44.890662Z","shell.execute_reply.started":"2022-08-03T10:12:44.882869Z","shell.execute_reply":"2022-08-03T10:12:44.889543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_object_coordinates(object_coordinates, re_sized_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:44.89196Z","iopub.execute_input":"2022-08-03T10:12:44.893083Z","iopub.status.idle":"2022-08-03T10:12:47.280038Z","shell.execute_reply.started":"2022-08-03T10:12:44.893052Z","shell.execute_reply":"2022-08-03T10:12:47.278929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_objects(object_coordinates, image):\n    plt.figure(figsize=(10,18))\n    for i in range(len(object_coordinates)):\n        ax = plt.subplot(int(np.ceil(len(object_coordinates)/3)),3,i+1)\n        coordinates = object_coordinates[i]\n        # print(coordinates)\n        object_image = image[int(coordinates[0]):int(coordinates[2]), int(coordinates[1]):int(coordinates[3])]\n        plt.imshow(object_image)\n        ax.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:47.28173Z","iopub.execute_input":"2022-08-03T10:12:47.282327Z","iopub.status.idle":"2022-08-03T10:12:47.290605Z","shell.execute_reply.started":"2022-08-03T10:12:47.282284Z","shell.execute_reply":"2022-08-03T10:12:47.289848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_objects(object_coordinates, re_sized_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:47.29154Z","iopub.execute_input":"2022-08-03T10:12:47.292587Z","iopub.status.idle":"2022-08-03T10:12:48.197201Z","shell.execute_reply.started":"2022-08-03T10:12:47.292531Z","shell.execute_reply":"2022-08-03T10:12:48.196156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_patch(patch_name, cropped_image, cmap=None):\n    plt.figure(figsize=(10,8), dpi=150)\n    ax = plt.subplot()\n    plt.imshow(cropped_image, cmap=cmap)\n    ax.set_title(patch_name)\n    ax.axis('off')\n    plt.show()\n\ndef crop_patch(coordinates, image):\n    x1, y1, x2, y2 = coordinates\n    cropped_image = image[x1:x2, y1:y2]\n    return cropped_image","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:48.198455Z","iopub.execute_input":"2022-08-03T10:12:48.198815Z","iopub.status.idle":"2022-08-03T10:12:48.205835Z","shell.execute_reply.started":"2022-08-03T10:12:48.198784Z","shell.execute_reply":"2022-08-03T10:12:48.204613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['image_id']==filename]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:48.207606Z","iopub.execute_input":"2022-08-03T10:12:48.208298Z","iopub.status.idle":"2022-08-03T10:12:48.227859Z","shell.execute_reply.started":"2022-08-03T10:12:48.208265Z","shell.execute_reply":"2022-08-03T10:12:48.226766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_coordinates(object_location, image):\n    top, bottom, left, right = object_location\n    left = int(left * image.shape[0])\n    bottom = int(bottom * image.shape[1])\n    right = int(right * image.shape[0])\n    top = int(top * image.shape[1])\n    return top, bottom, left, right\n\ndef normalize_coordinates(object_coordinates, image):\n    top, bottom, left, right = object_coordinates\n    left = (int(left) / image.shape[0])\n    bottom = (int(bottom) / image.shape[1])\n    right = int(left) + (int(right) / image.shape[0])\n    top = int(bottom) + (int(top) / image.shape[1])\n    \n    # object_location = top, bottom, left, right\n    # top, bottom, left, right = rescale_coordinates(object_location, image)\n    \n    return top, bottom, left, right\n\ndef patches_dictionary(object_coordinates, re_sized_image, image, filename):\n    patches = {}\n    for i in range(len(object_coordinates)):\n        coordinates = object_coordinates[i]\n        normal_cords = normalize_coordinates(coordinates, re_sized_image)\n        re_scaled_cords = rescale_coordinates(normal_cords, image)\n        patches[str(filename)+\"_\"+str(i+1)] = [normal_cords, re_scaled_cords]\n    patches = {filename:patches}\n    return patches\npatches = patches_dictionary(object_coordinates, re_sized_image, image, filename)\npatches","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:48.229748Z","iopub.execute_input":"2022-08-03T10:12:48.230192Z","iopub.status.idle":"2022-08-03T10:12:48.248292Z","shell.execute_reply.started":"2022-08-03T10:12:48.230151Z","shell.execute_reply":"2022-08-03T10:12:48.247478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patch_name = str(filename)+\"_\"+str(1)\ncoordinates = patches[filename][patch_name][1]\ncropped_image = crop_patch(coordinates, image)\nplot_patch(patch_name, cropped_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:48.249501Z","iopub.execute_input":"2022-08-03T10:12:48.250014Z","iopub.status.idle":"2022-08-03T10:12:53.318126Z","shell.execute_reply.started":"2022-08-03T10:12:48.249984Z","shell.execute_reply":"2022-08-03T10:12:53.316871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_image(path):\n    image, filename = read_tiff(path)\n    re_sized_image = resize_image(image)\n    resized_gray_img = convert_image_grayscale(re_sized_image)\n    labeled_segments = segment_images(resized_gray_img)\n    plot_labeled_segments(labeled_segments, resized_gray_img)\n    object_coordinates = get_object_coordinates(labeled_segments)\n    patches = patches_dictionary(object_coordinates, re_sized_image, image, filename)\n    print(str(len(patches[filename]))+\" patches\")\n    cropped_images = []\n    for i in range(len(patches[filename])):\n        patch_name = str(filename)+\"_\"+str(i+1)\n        coordinates = patches[filename][patch_name][1]\n        cropped_image = crop_patch(coordinates, image)\n        cropped_images.append([patch_name,cropped_image])\n    return patches, cropped_images, filename\n\npatches, cropped_images, filename = process_image(train_images[15])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:12:53.31969Z","iopub.execute_input":"2022-08-03T10:12:53.320045Z","iopub.status.idle":"2022-08-03T10:13:13.112429Z","shell.execute_reply.started":"2022-08-03T10:12:53.320015Z","shell.execute_reply":"2022-08-03T10:13:13.11132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display individual patches\npatch_number = 5\nimage_meta = train[train.image_id==filename]\npatch_name = str(filename)+\"_\"+str(patch_number)\npatch = cropped_images[patch_number-1][1]\ndisplay(image_meta)\nplot_patch(patch_name, patch)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:14:10.824315Z","iopub.execute_input":"2022-08-03T10:14:10.825154Z","iopub.status.idle":"2022-08-03T10:14:14.248422Z","shell.execute_reply.started":"2022-08-03T10:14:10.82511Z","shell.execute_reply":"2022-08-03T10:14:14.247173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ihc_hed = rgb2hed(patch)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:14:25.960658Z","iopub.execute_input":"2022-08-03T10:14:25.961176Z","iopub.status.idle":"2022-08-03T10:14:27.818792Z","shell.execute_reply.started":"2022-08-03T10:14:25.961129Z","shell.execute_reply":"2022-08-03T10:14:27.81647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null = np.zeros_like(ihc_hed[:, :, 0])\nihc_h = hed2rgb(np.stack((ihc_hed[:, :, 0], null, null), axis=-1))\nihc_e = hed2rgb(np.stack((null, ihc_hed[:, :, 1], null), axis=-1))\nihc_d = hed2rgb(np.stack((null, null, ihc_hed[:, :, 2]), axis=-1))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:14:31.387526Z","iopub.execute_input":"2022-08-03T10:14:31.388102Z","iopub.status.idle":"2022-08-03T10:14:36.707322Z","shell.execute_reply.started":"2022-08-03T10:14:31.388053Z","shell.execute_reply":"2022-08-03T10:14:36.705938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1,4,figsize=(10,8),sharex=True,sharey=True)\nax = axes.ravel()\nax[0].imshow(patch)\nax[0].set_title(\"Original image\")\nax[1].imshow(ihc_h)\nax[1].set_title(\"Hematoxylin\")\nax[2].imshow(ihc_e)\nax[2].set_title(\"Eosin\")\nax[3].imshow(ihc_d)\nax[3].set_title(\"DAB\")\nfor a in ax.ravel():\n    a.axis('off')\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T10:14:38.644448Z","iopub.execute_input":"2022-08-03T10:14:38.644903Z","iopub.status.idle":"2022-08-03T10:14:56.41333Z","shell.execute_reply.started":"2022-08-03T10:14:38.644868Z","shell.execute_reply":"2022-08-03T10:14:56.41209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}