{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":37333,"databundleVersionId":3949526,"sourceType":"competition"},{"sourceId":7274530,"sourceType":"datasetVersion","datasetId":4217325},{"sourceId":7274693,"sourceType":"datasetVersion","datasetId":4217421}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!conda install -y pyvips","metadata":{"_uuid":"4aedf91a-1213-440c-9c0f-03beb1949a79","_cell_guid":"f920d64a-c658-4e20-afc8-29725724ea5a","execution":{"iopub.status.busy":"2023-12-26T14:36:20.171487Z","iopub.execute_input":"2023-12-26T14:36:20.171854Z","iopub.status.idle":"2023-12-26T14:37:35.495559Z","shell.execute_reply.started":"2023-12-26T14:36:20.171825Z","shell.execute_reply":"2023-12-26T14:37:35.494372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt -y update && apt -y upgrade","metadata":{"_uuid":"5dbbca4e-8a76-4f50-a89f-9b3a080da72b","_cell_guid":"68b32583-77c9-4dca-88b2-2a63034eda14","execution":{"iopub.status.busy":"2023-12-26T14:37:35.498612Z","iopub.execute_input":"2023-12-26T14:37:35.49908Z","iopub.status.idle":"2023-12-26T14:37:50.400084Z","shell.execute_reply.started":"2023-12-26T14:37:35.499035Z","shell.execute_reply":"2023-12-26T14:37:50.399007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt -y install -d -o=dir::cache=/kaggle/working libvips libvips-dev libvips-tools","metadata":{"_uuid":"5c59a829-12b2-473b-9363-11a533a23cdb","_cell_guid":"ad69f1a1-32bd-4fe3-8584-4235075aa1bc","execution":{"iopub.status.busy":"2023-12-26T14:37:50.401491Z","iopub.execute_input":"2023-12-26T14:37:50.401852Z","iopub.status.idle":"2023-12-26T14:38:00.885817Z","shell.execute_reply.started":"2023-12-26T14:37:50.401818Z","shell.execute_reply":"2023-12-26T14:38:00.883983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install --upgrade pip\n!pip3 download -d /kaggle/working pyvips\n!dpkg -i --force-depends ./archives/*.deb >/dev/null 2>&1\n!pip3 install --quiet ./pycparser-2.21-py2.py3-none-any.whl\n!pip3 install --quiet ./pyvips-2.2.1.tar.gz\nimport pyvips","metadata":{"_uuid":"8f008194-acb2-44e1-ae15-55ea095ed8f6","_cell_guid":"af9232cc-9853-4e69-a653-8a8473a94319","execution":{"iopub.status.busy":"2023-12-26T14:38:00.889088Z","iopub.execute_input":"2023-12-26T14:38:00.88956Z","iopub.status.idle":"2023-12-26T14:40:00.260601Z","shell.execute_reply.started":"2023-12-26T14:38:00.889517Z","shell.execute_reply":"2023-12-26T14:40:00.259121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport sys\nfrom PIL import Image\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport zipfile\nfrom IPython.display import FileLink\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport tensorflow.keras.layers as l\nimport albumentations as A\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport pyvips","metadata":{"_uuid":"b0f1850d-6943-46e9-b9a4-62bb8798ffd2","_cell_guid":"32d83000-de5f-48ac-8ce9-b77203d05498","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-26T14:40:00.262202Z","iopub.execute_input":"2023-12-26T14:40:00.262561Z","iopub.status.idle":"2023-12-26T14:40:15.500847Z","shell.execute_reply.started":"2023-12-26T14:40:00.262528Z","shell.execute_reply":"2023-12-26T14:40:15.499677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_and_resize_image(image_path):\n    # check if image is less than limit\n    if os.path.getsize(image_path) < 178956970:\n        scale_factor = 1.0\n    \n    else:\n        # Calculate the scale factor needed to achieve the target size\n        scale_factor = np.sqrt(178956970 / os.path.getsize(image_path))\n    \n    img = pyvips.Image.new_from_file(image_path, access='sequential')\n    resized_img = img.resize(1.0 / scale_factor)\n    \n    return resized_img","metadata":{"_uuid":"32663662-4baf-4949-bf17-f123ef295662","_cell_guid":"342b74a6-1265-4be8-a15b-86319f9ee70f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-26T14:40:15.502365Z","iopub.execute_input":"2023-12-26T14:40:15.503244Z","iopub.status.idle":"2023-12-26T14:40:15.511091Z","shell.execute_reply.started":"2023-12-26T14:40:15.503202Z","shell.execute_reply":"2023-12-26T14:40:15.509983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def divide_tiff_into_tiles(input_path, tile_size):\n    img = read_and_resize_image(input_path)\n\n    # Get the size of the input image\n    img_width = img.width\n    img_height = img.height\n\n    # Calculate the number of tiles in the x and y directions\n    num_tiles_x = img_width // tile_size[0]\n    num_tiles_y = img_height // tile_size[1]\n\n    # Initialize an empty list to store the tiles\n    tiles = []\n\n    # Iterate over each tile and append it to the list\n    for y in range(num_tiles_y):\n        for x in range(num_tiles_x):\n            # Define the region for the current tile\n            left = x * tile_size[0]\n            upper = y * tile_size[1]\n            width = tile_size[0]\n            height = tile_size[1]\n\n            # Crop the image to the current tile\n            tile = img.crop(left, upper, width, height)\n\n            # Remove tiles with average intensity less than threshold\n            if tile.avg() < 185:\n                # Append the tile to the list\n                tiles.append(tile)\n\n    return tiles","metadata":{"_uuid":"7199eb12-f086-4d7e-b19e-bf32c9e1c2d9","_cell_guid":"d2e7b6fa-de5b-4038-8ee8-b261db354cda","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-26T14:40:15.51305Z","iopub.execute_input":"2023-12-26T14:40:15.513775Z","iopub.status.idle":"2023-12-26T14:40:15.561507Z","shell.execute_reply.started":"2023-12-26T14:40:15.513743Z","shell.execute_reply":"2023-12-26T14:40:15.560245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/mayo-clinic-strip-ai/train.csv')\ntrain_csv[\"label\"] = train_csv[\"label\"].map({'CE': 0, 'LAA': 1})","metadata":{"execution":{"iopub.status.busy":"2023-12-26T14:40:15.565864Z","iopub.execute_input":"2023-12-26T14:40:15.566595Z","iopub.status.idle":"2023-12-26T14:40:15.607644Z","shell.execute_reply.started":"2023-12-26T14:40:15.566559Z","shell.execute_reply":"2023-12-26T14:40:15.606634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv","metadata":{"execution":{"iopub.status.busy":"2023-12-26T14:40:15.609001Z","iopub.execute_input":"2023-12-26T14:40:15.609326Z","iopub.status.idle":"2023-12-26T14:40:15.635669Z","shell.execute_reply.started":"2023-12-26T14:40:15.609298Z","shell.execute_reply":"2023-12-26T14:40:15.634838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lookup_dic = {}\nfor i in range(len(train_csv)):\n  lookup_dic[train_csv[\"image_id\"].iloc[i]] = train_csv[\"label\"].iloc[i]","metadata":{"execution":{"iopub.status.busy":"2023-12-26T14:40:15.639488Z","iopub.execute_input":"2023-12-26T14:40:15.640063Z","iopub.status.idle":"2023-12-26T14:40:15.674668Z","shell.execute_reply.started":"2023-12-26T14:40:15.640031Z","shell.execute_reply":"2023-12-26T14:40:15.673729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the HSV range for yellow\nlower_yellow = np.array([20, 100, 100])\nupper_yellow = np.array([30, 255, 255])\n\n# Define the HSV range for brown\nlower_brown = np.array([10, 50, 50])\nupper_brown = np.array([20, 255, 255])","metadata":{"execution":{"iopub.status.busy":"2023-12-26T14:40:15.675891Z","iopub.execute_input":"2023-12-26T14:40:15.676411Z","iopub.status.idle":"2023-12-26T14:40:15.682058Z","shell.execute_reply.started":"2023-12-26T14:40:15.676358Z","shell.execute_reply":"2023-12-26T14:40:15.68103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"/kaggle/input/mayo-clinic-strip-ai/train\"\nnames,RBC_ratios, labels = [], [], []\nt = 0\nsorted_files = sorted(all_files[120:240])\n\nfor filename in sorted_files:\n    fname, _ = os.path.splitext(filename)\n    img_path = os.path.join(folder_path, f\"{fname}.tif\")\n    tiles = divide_tiff_into_tiles(img_path, (512, 512))\n    total_yellow_brown_area = 0\n    total_other_colors_area = 0\n    total_tile_area = 0\n\n    if len(tiles) == 0:\n        continue\n\n    for i in range(len(tiles)):\n        stained_image = np.array(tiles[i])  \n        # Convert the image to the HSV color space\n        hsv_image = cv2.cvtColor(stained_image, cv2.COLOR_RGB2HSV)\n\n        # Create masks for yellow and brown\n        yellow_mask = cv2.inRange(hsv_image, lower_yellow, upper_yellow)\n        brown_mask = cv2.inRange(hsv_image, lower_brown, upper_brown)\n\n        # Combine the masks to get the yellow and brown regions\n        yellow_brown_mask = cv2.bitwise_or(yellow_mask, brown_mask)\n        other_colors_mask = cv2.bitwise_not(yellow_brown_mask)\n\n        # Count yellow-brown area for the current tile\n        yellow_brown_area = np.sum(yellow_brown_mask > 0)\n        # Count other colors area for the current tile\n        other_colors_area = np.sum(other_colors_mask > 0)\n        tile_area = yellow_brown_area + other_colors_area\n\n        # Sum up areas for all tiles\n        total_yellow_brown_area += yellow_brown_area\n        total_other_colors_area += other_colors_area\n        total_tile_area += tile_area\n\n    RBC_ratio = total_yellow_brown_area / total_tile_area\n    labels.append(lookup_dic[fname])\n    names.append(fname)\n    RBC_ratios.append(RBC_ratio)\n    t += 1\n    print(t)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T22:24:06.2536Z","iopub.execute_input":"2023-12-26T22:24:06.254089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(RBC_ratios),len(labels),len(names)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T22:10:27.090893Z","iopub.execute_input":"2023-12-26T22:10:27.091329Z","iopub.status.idle":"2023-12-26T22:10:27.1085Z","shell.execute_reply.started":"2023-12-26T22:10:27.091287Z","shell.execute_reply":"2023-12-26T22:10:27.107317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {'Name': names, 'RBC_Ratio': RBC_ratios, 'Label': labels}\ndf = pd.DataFrame(data)\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T22:10:27.110906Z","iopub.execute_input":"2023-12-26T22:10:27.111238Z","iopub.status.idle":"2023-12-26T22:10:27.133412Z","shell.execute_reply.started":"2023-12-26T22:10:27.11121Z","shell.execute_reply":"2023-12-26T22:10:27.131983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom IPython.display import FileLink\n\n# Assuming df is your Pandas DataFrame\n\n# Save the DataFrame to a CSV file\ncsv_filename = '/kaggle/working/my_dataframe2_116_120.csv'\ndf.to_csv(csv_filename, index=False)\n\n# Display a download link\nFileLink(csv_filename)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T22:10:27.135365Z","iopub.execute_input":"2023-12-26T22:10:27.136442Z","iopub.status.idle":"2023-12-26T22:10:27.147146Z","shell.execute_reply.started":"2023-12-26T22:10:27.1364Z","shell.execute_reply":"2023-12-26T22:10:27.146061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The End","metadata":{}},{"cell_type":"code","source":"len(tiles)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:25:04.47764Z","iopub.execute_input":"2023-12-26T21:25:04.478164Z","iopub.status.idle":"2023-12-26T21:25:04.485777Z","shell.execute_reply.started":"2023-12-26T21:25:04.47813Z","shell.execute_reply":"2023-12-26T21:25:04.484681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tiles = divide_tiff_into_tiles(\"/kaggle/input/mayo-clinic-strip-ai/train/026c97_0.tif\", (512, 512))\nstained_image = np.array(tiles[60])  \n\n# Convert the image to the HSV color space\nhsv_image = cv2.cvtColor(stained_image, cv2.COLOR_RGB2HSV)\n\n# Define the HSV range for yellow\nlower_yellow = np.array([20, 100, 100])\nupper_yellow = np.array([30, 255, 255])\n\n# Define the HSV range for brown\nlower_brown = np.array([10, 50, 50])\nupper_brown = np.array([20, 255, 255])\n\n# Create masks for yellow and brown\nyellow_mask = cv2.inRange(hsv_image, lower_yellow, upper_yellow)\nbrown_mask = cv2.inRange(hsv_image, lower_brown, upper_brown)\n\n# Combine the masks to get the yellow and brown regions\nyellow_brown_mask = cv2.bitwise_or(yellow_mask, brown_mask)\nother_colors_mask = cv2.bitwise_not(yellow_brown_mask)\n\n# Apply the masks to the original image\nyellow_brown_part = cv2.bitwise_and(stained_image, stained_image, mask=yellow_brown_mask)\nother_colors_part = cv2.bitwise_and(stained_image, stained_image, mask=other_colors_mask)\n\n# Display the original image and the separated parts\nplt.figure(figsize=(10, 5))\n\nplt.subplot(1, 3, 1)\nplt.imshow(stained_image)\nplt.title('Original Image')\nplt.axis('off')\n\nplt.subplot(1, 3, 2)\nplt.imshow(yellow_brown_part)\nplt.title('Yellow and Brown Parts')\nplt.axis('off')\n\nplt.subplot(1, 3, 3)\nplt.imshow(other_colors_part)\nplt.title('Other Colors')\nplt.axis('off')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:25:04.48759Z","iopub.execute_input":"2023-12-26T21:25:04.487946Z","iopub.status.idle":"2023-12-26T21:25:09.214201Z","shell.execute_reply.started":"2023-12-26T21:25:04.487917Z","shell.execute_reply":"2023-12-26T21:25:09.211864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyradiomics","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:25:09.217304Z","iopub.execute_input":"2023-12-26T21:25:09.217937Z","iopub.status.idle":"2023-12-26T21:26:10.858708Z","shell.execute_reply.started":"2023-12-26T21:25:09.217889Z","shell.execute_reply":"2023-12-26T21:26:10.857103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(other_colors_mask)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:26:10.860747Z","iopub.execute_input":"2023-12-26T21:26:10.861158Z","iopub.status.idle":"2023-12-26T21:26:10.873981Z","shell.execute_reply.started":"2023-12-26T21:26:10.861119Z","shell.execute_reply":"2023-12-26T21:26:10.872888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport SimpleITK as sitk\nfrom radiomics import featureextractor\nimport csv\nfrom radiomics import imageoperations\nimport numpy as np\nimport SimpleITK as sitk\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:26:10.876052Z","iopub.execute_input":"2023-12-26T21:26:10.876433Z","iopub.status.idle":"2023-12-26T21:26:11.581246Z","shell.execute_reply.started":"2023-12-26T21:26:10.876372Z","shell.execute_reply":"2023-12-26T21:26:11.580135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yellow_brown_mask[yellow_brown_mask == 255] = 1\nstained_image = np.array(tiles[60])  \ngray_image_data = cv2.cvtColor(stained_image, cv2.COLOR_BGR2GRAY)\n\nstained_image_sitk = sitk.GetImageFromArray(gray_image_data)\nyellow_brown_mask_sitk = sitk.GetImageFromArray(yellow_brown_mask)\n#other_colors_mask_sitk = sitk.GetImageFromArray(other_colors_mask)\nresampled_spacing = (1.0, 1.0)  \n# Resample the image and mask\nimage, mask = imageoperations.resampleImage(stained_image_sitk, yellow_brown_mask_sitk, resampledPixelSpacing=resampled_spacing)\n\n    # Create a PyRadiomics feature extractor\nextractor = featureextractor.RadiomicsFeatureExtractor()\n\n    # Extract features\nresult = extractor.execute(image, mask)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:26:11.582681Z","iopub.execute_input":"2023-12-26T21:26:11.582987Z","iopub.status.idle":"2023-12-26T21:26:12.965219Z","shell.execute_reply.started":"2023-12-26T21:26:11.58296Z","shell.execute_reply":"2023-12-26T21:26:12.96392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2023-12-26T21:26:12.969899Z","iopub.execute_input":"2023-12-26T21:26:12.970252Z","iopub.status.idle":"2023-12-26T21:26:13.000683Z","shell.execute_reply.started":"2023-12-26T21:26:12.970221Z","shell.execute_reply":"2023-12-26T21:26:12.999105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"0531006b-ffcf-4524-89db-5cc2a22a1051","_cell_guid":"64062482-0a85-4718-98b8-f5390c0d19fc","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"86402e45-ce8a-4f9c-b4f6-2d5055eccdf3","_cell_guid":"2b861f68-494f-42e8-b35a-e74ce4352d92","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_yellow_brown_area = 0\ntotal_other_colors_area = 0\ntotal_tile_area = 0\n\n# Define the HSV range for yellow\nlower_yellow = np.array([20, 100, 100])\nupper_yellow = np.array([30, 255, 255])\n\n# Define the HSV range for brown\nlower_brown = np.array([10, 50, 50])\nupper_brown = np.array([20, 255, 255])\n\nfor i in range(len(tiles)):\n            stained_image = np.array(tiles[i])  \n            # Convert the image to the HSV color space\n            hsv_image = cv2.cvtColor(stained_image, cv2.COLOR_RGB2HSV)\n\n            \n            # Create masks for yellow and brown\n            yellow_mask = cv2.inRange(hsv_image, lower_yellow, upper_yellow)\n            brown_mask = cv2.inRange(hsv_image, lower_brown, upper_brown)\n\n            # Combine the masks to get the yellow and brown regions\n            yellow_brown_mask = cv2.bitwise_or(yellow_mask, brown_mask)\n            other_colors_mask = cv2.bitwise_not(yellow_brown_mask)\n\n            # Count yellow-brown area for the current tile\n            yellow_brown_area = np.sum(yellow_brown_mask > 0)\n            # Count other colors area for the current tile\n            other_colors_area = np.sum(other_colors_mask > 0)\n            tile_area = yellow_brown_area + other_colors_area\n    \n            # Sum up areas for all tiles\n            total_yellow_brown_area += yellow_brown_area\n            total_other_colors_area += other_colors_area\n            total_tile_area += tile_area\n            \n            \n\nRBC_ratio = total_yellow_brown_area / total_tile_area\nprint(\"RBC Area Ratio:\", RBC_ratio)           \n#Other ce","metadata":{"_uuid":"1f5204c4-58de-47ad-8adb-a077ad30ccbd","_cell_guid":"5f35d183-9c6b-4b9b-932b-17620daf041d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-26T21:26:13.003184Z","iopub.execute_input":"2023-12-26T21:26:13.003762Z","iopub.status.idle":"2023-12-26T21:26:14.297828Z","shell.execute_reply.started":"2023-12-26T21:26:13.003713Z","shell.execute_reply":"2023-12-26T21:26:14.29659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}