{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        pass\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n\n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"f50e95dd-b28b-4c5c-93dc-67b80c3651ca","_cell_guid":"3a31a157-b9d8-41a2-89bc-6f0143301dc3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:14:46.610285Z","iopub.execute_input":"2023-08-09T09:14:46.610685Z","iopub.status.idle":"2023-08-09T09:14:47.125557Z","shell.execute_reply.started":"2023-08-09T09:14:46.610646Z","shell.execute_reply":"2023-08-09T09:14:47.124314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:14:47.128659Z","iopub.execute_input":"2023-08-09T09:14:47.129364Z","iopub.status.idle":"2023-08-09T09:14:47.137279Z","shell.execute_reply.started":"2023-08-09T09:14:47.129318Z","shell.execute_reply":"2023-08-09T09:14:47.136027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls -all","metadata":{"_uuid":"7db959a7-01d8-43b9-98e0-46c78b7f50f6","_cell_guid":"fd2bf35f-3799-4e25-911c-5b16956f3dc5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:14:47.138785Z","iopub.execute_input":"2023-08-09T09:14:47.139248Z","iopub.status.idle":"2023-08-09T09:14:48.333851Z","shell.execute_reply.started":"2023-08-09T09:14:47.13921Z","shell.execute_reply":"2023-08-09T09:14:48.332546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Install requirements","metadata":{"_uuid":"b754ce61-4cab-4e08-9c27-ec74ecb29d18","_cell_guid":"2eeb0b6f-5809-4e9f-9285-5c39065dc6ee","trusted":true}},{"cell_type":"code","source":"pip install -r '/kaggle/input/requires/reqwork.txt'","metadata":{"_uuid":"fec5b817-6993-470e-8132-7783d698dfbb","_cell_guid":"bd3036bf-85ec-49da-a18e-ba76937240db","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:14:48.335553Z","iopub.execute_input":"2023-08-09T09:14:48.336032Z","iopub.status.idle":"2023-08-09T09:18:06.876707Z","shell.execute_reply.started":"2023-08-09T09:14:48.335986Z","shell.execute_reply":"2023-08-09T09:18:06.873638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install imutils","metadata":{"_uuid":"137fcada-4ee9-411a-87ca-e120205f6fe9","_cell_guid":"168e88c4-0f31-48aa-8999-5ef9e96525a7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:18:06.883494Z","iopub.execute_input":"2023-08-09T09:18:06.884059Z","iopub.status.idle":"2023-08-09T09:18:28.84954Z","shell.execute_reply.started":"2023-08-09T09:18:06.884006Z","shell.execute_reply":"2023-08-09T09:18:28.847974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import lib","metadata":{"_uuid":"45f6b5e9-8d48-4938-8e5f-253c5a8df118","_cell_guid":"f128d893-3901-46f2-ad3f-11d4a6f94fe2","trusted":true}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nimport pandas as pd\nimport cv2 as cv\nfrom imutils import paths\nimport skimage\nfrom skimage.filters import sobel\nfrom skimage.color import label2rgb\nfrom skimage.color import rgb2hed, hed2rgb\nfrom skimage.exposure import rescale_intensity\nfrom skimage.measure import regionprops, regionprops_table\nfrom sklearn.preprocessing import StandardScaler\nimport tifffile as tifi\nimport shutil\nfrom scipy import ndimage as ndi\nskimage.__version__","metadata":{"_uuid":"5a79e876-a988-48b2-aa9a-d5feb64330d8","_cell_guid":"2663834e-92f6-4a0a-b1e2-d472fef5d6ae","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:18:28.85139Z","iopub.execute_input":"2023-08-09T09:18:28.851799Z","iopub.status.idle":"2023-08-09T09:18:30.29742Z","shell.execute_reply.started":"2023-08-09T09:18:28.851764Z","shell.execute_reply":"2023-08-09T09:18:30.296069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import segmentation","metadata":{"_uuid":"27dadcdc-140d-49bc-b0f2-048f8a2b021c","_cell_guid":"d563faa2-a0f7-4f8c-8987-d415b877cd34","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:18:30.299111Z","iopub.execute_input":"2023-08-09T09:18:30.300068Z","iopub.status.idle":"2023-08-09T09:18:30.342993Z","shell.execute_reply.started":"2023-08-09T09:18:30.300022Z","shell.execute_reply":"2023-08-09T09:18:30.341652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load training data","metadata":{"_uuid":"168a1cd1-9450-40c9-bcbe-ddd487e3d1c4","_cell_guid":"03b93849-53d6-4127-b5af-9b9f4e60543b","trusted":true}},{"cell_type":"code","source":"train_meta = pd.read_csv('../input/mayo-clinic-strip-ai/other.csv')\ntrain_meta.head()","metadata":{"_uuid":"e8d13b58-c621-4269-9104-64f4d770a58a","_cell_guid":"b75b6ec1-cb3c-4806-ae3b-1bd96177dd44","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:18:30.344878Z","iopub.execute_input":"2023-08-09T09:18:30.345409Z","iopub.status.idle":"2023-08-09T09:18:30.40353Z","shell.execute_reply.started":"2023-08-09T09:18:30.345361Z","shell.execute_reply":"2023-08-09T09:18:30.402045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.label.value_counts()","metadata":{"_uuid":"b8da240d-8a87-4cf9-8260-45fdcc6fbe99","_cell_guid":"f10813d9-59fb-4a4a-8485-049f6952abbd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:18:30.405108Z","iopub.execute_input":"2023-08-09T09:18:30.405515Z","iopub.status.idle":"2023-08-09T09:18:30.420996Z","shell.execute_reply.started":"2023-08-09T09:18:30.405481Z","shell.execute_reply":"2023-08-09T09:18:30.419637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.label[0]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:18:30.422987Z","iopub.execute_input":"2023-08-09T09:18:30.423452Z","iopub.status.idle":"2023-08-09T09:18:30.439462Z","shell.execute_reply.started":"2023-08-09T09:18:30.42341Z","shell.execute_reply":"2023-08-09T09:18:30.438069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# including just other label images. Dropping unknown","metadata":{}},{"cell_type":"code","source":"train_meta = train_meta[train_meta['label'] == 'Other']\ntrain_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:23:17.258725Z","iopub.execute_input":"2023-08-09T09:23:17.259217Z","iopub.status.idle":"2023-08-09T09:23:17.282264Z","shell.execute_reply.started":"2023-08-09T09:23:17.259185Z","shell.execute_reply":"2023-08-09T09:23:17.281069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:23:29.255542Z","iopub.execute_input":"2023-08-09T09:23:29.25603Z","iopub.status.idle":"2023-08-09T09:23:29.266135Z","shell.execute_reply.started":"2023-08-09T09:23:29.255946Z","shell.execute_reply":"2023-08-09T09:23:29.264919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.image_id","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:33:43.196624Z","iopub.execute_input":"2023-08-09T09:33:43.197121Z","iopub.status.idle":"2023-08-09T09:33:43.207218Z","shell.execute_reply.started":"2023-08-09T09:33:43.197075Z","shell.execute_reply":"2023-08-09T09:33:43.205831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_meta.image_id)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:36:48.446004Z","iopub.execute_input":"2023-08-09T09:36:48.446491Z","iopub.status.idle":"2023-08-09T09:36:48.455155Z","shell.execute_reply.started":"2023-08-09T09:36:48.446452Z","shell.execute_reply":"2023-08-09T09:36:48.453559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_meta.image_id)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:38:36.468751Z","iopub.execute_input":"2023-08-09T09:38:36.469212Z","iopub.status.idle":"2023-08-09T09:38:36.477312Z","shell.execute_reply.started":"2023-08-09T09:38:36.469175Z","shell.execute_reply":"2023-08-09T09:38:36.476046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample train data table","metadata":{"_uuid":"6a562ccd-2632-4c39-9f29-c4be42f38468","_cell_guid":"2015ec28-b959-4d04-9c3f-f9f4b78d5d08","trusted":true}},{"cell_type":"code","source":"folder_path = '../input/mayo-clinic-strip-ai/other'\ntrain_images = sorted(list(paths.list_images(folder_path)))\nprint('There are ' + str(len(train_images)) + \" images in the train folder\")\ntrain_images[:5]","metadata":{"_uuid":"ecd1303d-a2d1-4fd5-8d07-b2e846349743","_cell_guid":"ba9936a2-c8be-4959-9316-12865159d9a5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:25:30.466193Z","iopub.execute_input":"2023-08-09T09:25:30.46667Z","iopub.status.idle":"2023-08-09T09:25:30.480728Z","shell.execute_reply.started":"2023-08-09T09:25:30.466637Z","shell.execute_reply":"2023-08-09T09:25:30.479376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_images)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:25:33.664694Z","iopub.execute_input":"2023-08-09T09:25:33.665365Z","iopub.status.idle":"2023-08-09T09:25:33.670922Z","shell.execute_reply.started":"2023-08-09T09:25:33.665329Z","shell.execute_reply":"2023-08-09T09:25:33.670068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = [i.split(\"/\")[-1].rstrip('.tif') for i in train_images]\nlen(image_ids)\nimage_ids[:10]","metadata":{"_uuid":"ce289504-b871-48a6-a138-2530a9787815","_cell_guid":"f9ae5394-5903-41d3-ae7c-599d928628e5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:38:05.41367Z","iopub.execute_input":"2023-08-09T09:38:05.414177Z","iopub.status.idle":"2023-08-09T09:38:05.424127Z","shell.execute_reply.started":"2023-08-09T09:38:05.414141Z","shell.execute_reply":"2023-08-09T09:38:05.42311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(image_ids)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:30:21.409982Z","iopub.execute_input":"2023-08-09T09:30:21.41054Z","iopub.status.idle":"2023-08-09T09:30:21.418918Z","shell.execute_reply.started":"2023-08-09T09:30:21.410499Z","shell.execute_reply":"2023-08-09T09:30:21.417609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"specific_values_list = train_meta.image_id.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:40:22.871888Z","iopub.execute_input":"2023-08-09T09:40:22.872407Z","iopub.status.idle":"2023-08-09T09:40:22.878853Z","shell.execute_reply.started":"2023-08-09T09:40:22.872371Z","shell.execute_reply":"2023-08-09T09:40:22.877499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = [value for value in image_ids if value in specific_values_list]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:41:22.511095Z","iopub.execute_input":"2023-08-09T09:41:22.511585Z","iopub.status.idle":"2023-08-09T09:41:22.518509Z","shell.execute_reply.started":"2023-08-09T09:41:22.511545Z","shell.execute_reply":"2023-08-09T09:41:22.517357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_ids)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:45:18.56268Z","iopub.execute_input":"2023-08-09T09:45:18.56312Z","iopub.status.idle":"2023-08-09T09:45:18.570475Z","shell.execute_reply.started":"2023-08-09T09:45:18.563087Z","shell.execute_reply":"2023-08-09T09:45:18.569193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:41:43.652545Z","iopub.execute_input":"2023-08-09T09:41:43.653094Z","iopub.status.idle":"2023-08-09T09:41:43.663621Z","shell.execute_reply.started":"2023-08-09T09:41:43.653051Z","shell.execute_reply":"2023-08-09T09:41:43.662168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# including just other label images","metadata":{}},{"cell_type":"code","source":"type(train_images)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:44:59.556044Z","iopub.execute_input":"2023-08-09T09:44:59.556474Z","iopub.status.idle":"2023-08-09T09:44:59.564315Z","shell.execute_reply.started":"2023-08-09T09:44:59.556435Z","shell.execute_reply":"2023-08-09T09:44:59.563124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_imagess = train_images[train_images['label'] == 'Other']\n# train_images.head()\n\n# Filter image file paths for specific image_ids\nspecific_image_paths = [path for path in train_images if any(image_id in path for image_id in image_ids)]\nspecific_image_paths[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:44:05.959245Z","iopub.execute_input":"2023-08-09T09:44:05.959716Z","iopub.status.idle":"2023-08-09T09:44:05.971299Z","shell.execute_reply.started":"2023-08-09T09:44:05.959682Z","shell.execute_reply":"2023-08-09T09:44:05.970141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images=specific_image_paths","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:46:16.374572Z","iopub.execute_input":"2023-08-09T09:46:16.375159Z","iopub.status.idle":"2023-08-09T09:46:16.381251Z","shell.execute_reply.started":"2023-08-09T09:46:16.375115Z","shell.execute_reply":"2023-08-09T09:46:16.379768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_images)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:46:26.625273Z","iopub.execute_input":"2023-08-09T09:46:26.625777Z","iopub.status.idle":"2023-08-09T09:46:26.633769Z","shell.execute_reply.started":"2023-08-09T09:46:26.625738Z","shell.execute_reply":"2023-08-09T09:46:26.632361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:46:40.582761Z","iopub.execute_input":"2023-08-09T09:46:40.583229Z","iopub.status.idle":"2023-08-09T09:46:40.591356Z","shell.execute_reply.started":"2023-08-09T09:46:40.583191Z","shell.execute_reply":"2023-08-09T09:46:40.590057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resize image","metadata":{"_uuid":"cd1d71dd-e7ee-4958-a0ed-30b968a945ca","_cell_guid":"556ccdc1-328e-47fe-844f-3036d6db8fa2","trusted":true}},{"cell_type":"code","source":"def resize_image(image):\n    re_sized_image = cv.resize(image,(int(image.shape[1]/7),int(image.shape[0]/7)),interpolation= cv.INTER_LINEAR)\n    return re_sized_image\n# re_sized_image = resize_image(image)","metadata":{"_uuid":"90880fe3-5224-494b-b54a-e412dd60d7b8","_cell_guid":"0d1cf216-9b5b-48fb-9049-a0e3d3b3613c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:46:49.038173Z","iopub.execute_input":"2023-08-09T09:46:49.038629Z","iopub.status.idle":"2023-08-09T09:46:49.045848Z","shell.execute_reply.started":"2023-08-09T09:46:49.038591Z","shell.execute_reply":"2023-08-09T09:46:49.044613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# re_sized_image.shape","metadata":{"_uuid":"98768ea1-e61c-41f5-aea9-a26d918757f7","_cell_guid":"824c5271-d42e-466d-a1bc-ae8400b3fdf2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T07:13:59.542598Z","iopub.execute_input":"2023-08-09T07:13:59.543895Z","iopub.status.idle":"2023-08-09T07:13:59.635366Z","shell.execute_reply.started":"2023-08-09T07:13:59.543848Z","shell.execute_reply":"2023-08-09T07:13:59.633467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Segmentation with sobel filter","metadata":{"_uuid":"b5990fbf-c0be-43d4-b7c0-acb3d8462aa4","_cell_guid":"d44521ed-884c-42e2-8f74-608522517c44","trusted":true}},{"cell_type":"code","source":"from scipy import ndimage as ndi","metadata":{"_uuid":"e4467675-4ab4-40a3-8550-ef45dd39e9f2","_cell_guid":"5e27478b-17b0-481d-bcb1-5d4709e2e972","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:46:54.792525Z","iopub.execute_input":"2023-08-09T09:46:54.792946Z","iopub.status.idle":"2023-08-09T09:46:54.798139Z","shell.execute_reply.started":"2023-08-09T09:46:54.792914Z","shell.execute_reply":"2023-08-09T09:46:54.796942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Find patches","metadata":{"_uuid":"12ba5b69-a85c-4eef-94f0-9d25de6e4bdf","_cell_guid":"03e10249-939b-4acc-89d6-eba78d2b9307","trusted":true}},{"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\n# patches = patches_dictionary(object_coordinates, re_sized_image, image, filename)\n# patches","metadata":{"_uuid":"4d57e0f0-b09c-4830-8b10-759ddde7dcd6","_cell_guid":"662e17eb-9b8b-497e-a8ff-4ff9fe47dd47","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:46:56.184467Z","iopub.execute_input":"2023-08-09T09:46:56.18521Z","iopub.status.idle":"2023-08-09T09:46:56.197923Z","shell.execute_reply.started":"2023-08-09T09:46:56.185174Z","shell.execute_reply":"2023-08-09T09:46:56.196265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot patches","metadata":{"_uuid":"d80890a4-a640-48fe-8f0b-65eb10dab640","_cell_guid":"aa2401a6-6a2d-4c30-9d83-9bdbb37f5038","trusted":true}},{"cell_type":"code","source":"#plotting individual patches\ndef 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":{"_uuid":"dad5e22f-540d-4117-a798-da21bea152a1","_cell_guid":"f7e6f4c9-38cf-4699-8160-921605a8b606","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:46:58.750897Z","iopub.execute_input":"2023-08-09T09:46:58.751393Z","iopub.status.idle":"2023-08-09T09:46:58.760048Z","shell.execute_reply.started":"2023-08-09T09:46:58.751359Z","shell.execute_reply":"2023-08-09T09:46:58.75851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read image metadata\n# train_meta[train_meta['image_id']==filename]","metadata":{"_uuid":"4c0d0d34-a3a4-4157-bc2d-46812181b06b","_cell_guid":"97fd562e-8f1a-4fee-ae1a-e6fa549fed96","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T09:47:02.197506Z","iopub.execute_input":"2023-08-09T09:47:02.197929Z","iopub.status.idle":"2023-08-09T09:47:02.202826Z","shell.execute_reply.started":"2023-08-09T09:47:02.197897Z","shell.execute_reply":"2023-08-09T09:47:02.201879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.label","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:47:44.846598Z","iopub.execute_input":"2023-08-09T09:47:44.847027Z","iopub.status.idle":"2023-08-09T09:47:44.856751Z","shell.execute_reply.started":"2023-08-09T09:47:44.846994Z","shell.execute_reply":"2023-08-09T09:47:44.855487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.label[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# type(label['label'])\n# (label['label'])\n# (label['label'].iloc[-1])","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:13:59.948886Z","iopub.execute_input":"2023-08-09T07:13:59.949768Z","iopub.status.idle":"2023-08-09T07:13:59.961374Z","shell.execute_reply.started":"2023-08-09T07:13:59.949736Z","shell.execute_reply":"2023-08-09T07:13:59.960377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# patch_name = str(filename)+\"_\"+str(1)\n# coordinates = str(patches[filename][patch_name][0])\n# coordinate = patches[filename][patch_name][1]\n\n\n# print(coordinates)\n# print(coordinate)\n\n# label=train_meta[train_meta['image_id']==filename]\n\n# # print(label,label['patient_id'])\n# # print(coordinates[0])\n# with open(patch_name+\".txt\", 'w') as file:\n#     if label['label'].iloc[-1] == 'CE':\n#         file.write(\"1 \"+coordinates[1:-1])\n#     else:\n#         file.write(\"0 \"+coordinates[1:-1])\n# cropped_image = crop_patch(coordinate, image)\n# # image = cv.imread(cropped_image)\n# img=resize_image(cropped_image)\n# cv.imwrite(patch_name+'.png', img)\n# plot_patch(patch_name, img)\n# # type(cropped_image)","metadata":{"_uuid":"b7dd3b37-de71-4b7c-947c-158a189ebc5a","_cell_guid":"e8dee1a8-bf2e-48b5-b753-abb818320823","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-09T07:13:59.96299Z","iopub.execute_input":"2023-08-09T07:13:59.964161Z","iopub.status.idle":"2023-08-09T07:13:59.978222Z","shell.execute_reply.started":"2023-08-09T07:13:59.964115Z","shell.execute_reply":"2023-08-09T07:13:59.977342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# save all patches for a single whole slide image","metadata":{}},{"cell_type":"code","source":"mkdir wsi","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:47:57.998593Z","iopub.execute_input":"2023-08-09T09:47:57.999051Z","iopub.status.idle":"2023-08-09T09:47:59.120213Z","shell.execute_reply.started":"2023-08-09T09:47:57.999016Z","shell.execute_reply":"2023-08-09T09:47:59.118764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cd wsi","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:14:01.097659Z","iopub.execute_input":"2023-08-09T07:14:01.098036Z","iopub.status.idle":"2023-08-09T07:14:01.103212Z","shell.execute_reply.started":"2023-08-09T07:14:01.098003Z","shell.execute_reply":"2023-08-09T07:14:01.102035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# for i in range(len(object_coordinates)):\n# #     \n#     patch_name = str(filename)+\"_\"+str(i+1)\n#     coordinates = str(patches[filename][patch_name][0])\n#     coordinate = patches[filename][patch_name][1]\n\n\n#     print(coordinates)\n#     print(coordinate)\n\n#     label=train_meta[train_meta['image_id']==filename]\n\n#     # print(label,label['patient_id'])\n#     # print(coordinates[0])\n#     with open(patch_name+\".txt\", 'w') as file:\n#         if label['label'].iloc[-1] == 'CE':\n#             file.write(\"1 \"+coordinates[1:-1])\n#         else:\n#             file.write(\"0 \"+coordinates[1:-1])\n#     cropped_image = crop_patch(coordinate, image)\n#     # image = cv.imread(cropped_image)\n#     img=resize_image(cropped_image)\n#     cv.imwrite(patch_name+'.png', img)\n#     # plot_patch(patch_name, img)\n#     # type(cropped_image)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:14:01.104909Z","iopub.execute_input":"2023-08-09T07:14:01.105354Z","iopub.status.idle":"2023-08-09T07:14:01.116796Z","shell.execute_reply.started":"2023-08-09T07:14:01.105303Z","shell.execute_reply":"2023-08-09T07:14:01.11566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.make_archive('wsi', 'zip', '/kaggle/working/')\n# !zip -r /kaggle/working/wsi.zip /kaggle/working/wsi","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:14:01.118117Z","iopub.execute_input":"2023-08-09T07:14:01.119086Z","iopub.status.idle":"2023-08-09T07:14:01.133918Z","shell.execute_reply.started":"2023-08-09T07:14:01.119054Z","shell.execute_reply":"2023-08-09T07:14:01.132524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:48:02.80436Z","iopub.execute_input":"2023-08-09T09:48:02.805146Z","iopub.status.idle":"2023-08-09T09:48:02.813445Z","shell.execute_reply.started":"2023-08-09T09:48:02.805103Z","shell.execute_reply":"2023-08-09T09:48:02.812106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:48:04.819969Z","iopub.execute_input":"2023-08-09T09:48:04.82038Z","iopub.status.idle":"2023-08-09T09:48:05.93168Z","shell.execute_reply.started":"2023-08-09T09:48:04.820343Z","shell.execute_reply":"2023-08-09T09:48:05.929945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# plan for bbox\n1. resize\n2. grayscale\n3. segment\n4. coordinate\n5. save coordinate\n6. save class in same txt file\n7. save every patch with corresponding bbox label","metadata":{"_uuid":"ea8f0057-a61f-41bd-8251-d309ef426e4e","_cell_guid":"4237f208-0861-46f3-bb98-26531edacb37","trusted":true}},{"cell_type":"code","source":"#define function resize_image, patches_dictionary, crop_patch\n#len(train_images)\n#for i in range(11, 27): This loop will output the numbers from 11 to 26;\n#so be careful\n\nfor j in range(65):\n    path=train_images[j]\n    image = tifi.imread(train_images[j])\n    filename = path.split('/')[-1].rstrip('.tif')\n    print(\"img number \",j,\" image_id: \" + filename)\n\n    re_sized_image = cv.resize(image,(int(image.shape[1]/7),int(image.shape[0]/7)),interpolation= cv.INTER_LINEAR)\n    resized_gray_img = cv.cvtColor(re_sized_image, cv.COLOR_RGB2GRAY)\n\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\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    patches = patches_dictionary(object_coordinates, re_sized_image, image, filename)\n    for i in range(len(object_coordinates)):\n    \n        patch_name = str(filename)+\"_\"+str(i+1)\n        print(patch_name)\n        coordinates = str(patches[filename][patch_name][0])\n        coordinate = patches[filename][patch_name][1]\n\n\n#         print(coordinates)\n#         print(coordinate)\n\n        label=train_meta[train_meta['image_id']==filename]\n\n        # print(label,label['patient_id'])\n        # print(coordinates[0])\n        with open(patch_name+\".txt\", 'w') as file:\n            if label['label'].iloc[-1] == 'CE':\n                file.write(\"1 \"+coordinates[1:-1])\n            else:\n                file.write(\"0 \"+coordinates[1:-1])\n        cropped_image = crop_patch(coordinate, image)\n        # image = cv.imread(cropped_image)\n        img=resize_image(cropped_image)\n        cv.imwrite(patch_name+'.png', img)\n        # plot_patch(patch_name, img)\n        # type(cropped_image)\n\n  \n!zip -r /kaggle/working/wsiother0-65.zip /kaggle/working\n!mv /kaggle/working/wsiother0-65.zip /kaggle/working/wsi","metadata":{"execution":{"iopub.status.busy":"2023-08-09T09:48:49.605213Z","iopub.execute_input":"2023-08-09T09:48:49.605847Z","iopub.status.idle":"2023-08-09T10:51:45.625277Z","shell.execute_reply.started":"2023-08-09T09:48:49.605793Z","shell.execute_reply":"2023-08-09T10:51:45.623273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:02:37.616831Z","iopub.execute_input":"2023-08-09T08:02:37.617399Z","iopub.status.idle":"2023-08-09T08:02:37.626714Z","shell.execute_reply.started":"2023-08-09T08:02:37.617358Z","shell.execute_reply":"2023-08-09T08:02:37.625621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.make_archive('wsi', 'zip', '/kaggle/working/')\n# !zip -r /kaggle/working/wsi725-755.zip /kaggle/working\n# !mv /kaggle/working/wsi725-755.zip /kaggle/working/wsi","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cd wsi","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:44:49.68374Z","iopub.status.idle":"2023-08-09T07:44:49.68476Z","shell.execute_reply.started":"2023-08-09T07:44:49.684487Z","shell.execute_reply":"2023-08-09T07:44:49.684521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ls","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:44:49.686119Z","iopub.status.idle":"2023-08-09T07:44:49.687039Z","shell.execute_reply.started":"2023-08-09T07:44:49.686702Z","shell.execute_reply":"2023-08-09T07:44:49.686736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}