{"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":"!conda install ../input/how-to-use-pyvips-offline/*.tar.bz2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-17T07:49:05.059579Z","iopub.execute_input":"2022-08-17T07:49:05.060204Z","iopub.status.idle":"2022-08-17T07:49:55.397868Z","shell.execute_reply.started":"2022-08-17T07:49:05.060047Z","shell.execute_reply":"2022-08-17T07:49:55.396477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyvips\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:17.727652Z","iopub.execute_input":"2022-08-17T07:53:17.728192Z","iopub.status.idle":"2022-08-17T07:53:17.94073Z","shell.execute_reply.started":"2022-08-17T07:53:17.728142Z","shell.execute_reply":"2022-08-17T07:53:17.939629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"OPENCV_IO_MAX_IMAGE_PIXELS\"] = '5000000000'","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:19.386443Z","iopub.execute_input":"2022-08-17T07:53:19.386924Z","iopub.status.idle":"2022-08-17T07:53:19.3936Z","shell.execute_reply.started":"2022-08-17T07:53:19.386884Z","shell.execute_reply":"2022-08-17T07:53:19.39218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport glob\nfrom openslide import OpenSlide\nimport random\nimport gc\n\n\nImage.MAX_IMAGE_PIXELS = None","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:21.221752Z","iopub.execute_input":"2022-08-17T07:53:21.222636Z","iopub.status.idle":"2022-08-17T07:53:21.236331Z","shell.execute_reply.started":"2022-08-17T07:53:21.222583Z","shell.execute_reply":"2022-08-17T07:53:21.235331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:23.476416Z","iopub.execute_input":"2022-08-17T07:53:23.477269Z","iopub.status.idle":"2022-08-17T07:53:23.521319Z","shell.execute_reply.started":"2022-08-17T07:53:23.477224Z","shell.execute_reply":"2022-08-17T07:53:23.520244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")\ndisplay(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:25.808358Z","iopub.execute_input":"2022-08-17T07:53:25.808823Z","iopub.status.idle":"2022-08-17T07:53:25.826964Z","shell.execute_reply.started":"2022-08-17T07:53:25.808788Z","shell.execute_reply":"2022-08-17T07:53:25.826088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patients = len(train_data['patient_id'].value_counts())\ncenters = len(train_data['center_id'].value_counts())\n\nprint(\"No.of unique patients =\",patients)\nprint(\"No. of centers =\",centers)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:27.426435Z","iopub.execute_input":"2022-08-17T07:53:27.426926Z","iopub.status.idle":"2022-08-17T07:53:27.440625Z","shell.execute_reply.started":"2022-08-17T07:53:27.426888Z","shell.execute_reply":"2022-08-17T07:53:27.439174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = {'CE': len(train_data[train_data['label']=='CE']),'LAA':len(train_data[train_data['label']=='LAA'])}\nlabels = plot.keys()\nvalues = plot.values()\nprint(\" Total CE images =\",plot['CE'],'\\n','Total LAA images =',plot['LAA'])\nplt.bar(labels, values, width =0.6, color= 'orange')\nplt.xlabel(\"Type of Ischemic Stroke\")\nplt.ylabel(\"No. of Images\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:29.332381Z","iopub.execute_input":"2022-08-17T07:53:29.332829Z","iopub.status.idle":"2022-08-17T07:53:29.536439Z","shell.execute_reply.started":"2022-08-17T07:53:29.332795Z","shell.execute_reply":"2022-08-17T07:53:29.535569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['center_id'].value_counts().plot(kind='bar')\nplt.title('Center Representation')\nplt.xticks(rotation=360)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:31.567427Z","iopub.execute_input":"2022-08-17T07:53:31.567885Z","iopub.status.idle":"2022-08-17T07:53:31.807788Z","shell.execute_reply.started":"2022-08-17T07:53:31.567849Z","shell.execute_reply":"2022-08-17T07:53:31.806277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img = glob.glob(\"../input/mayo-clinic-strip-ai/train/*\")\ntrain_img_pd = pd.DataFrame(columns=['train_path'])\ntrain_img_pd['train_path'] = train_img\ntrain_img_pd['image_id'] = train_img_pd['train_path'].apply(lambda x: x.split('/')[-1].replace('.tif',''))\ndisplay(train_img_pd)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:33.955631Z","iopub.execute_input":"2022-08-17T07:53:33.956146Z","iopub.status.idle":"2022-08-17T07:53:34.069661Z","shell.execute_reply.started":"2022-08-17T07:53:33.956087Z","shell.execute_reply":"2022-08-17T07:53:34.068832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Adding Directory Paths to DF","metadata":{}},{"cell_type":"code","source":"train_data = train_data.merge(train_img_pd, on='image_id')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:38.377029Z","iopub.execute_input":"2022-08-17T07:53:38.378213Z","iopub.status.idle":"2022-08-17T07:53:38.408771Z","shell.execute_reply.started":"2022-08-17T07:53:38.378155Z","shell.execute_reply":"2022-08-17T07:53:38.407595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To check the maximum filesize","metadata":{}},{"cell_type":"code","source":"pix_list = []\npix_id = []\n\nfor i in range(len(train_data['patient_id'])):\n    img = Image.open(train_data[\"train_path\"][i])\n    ht,wt = img.size\n    pix = ht*wt\n    pix_id.append(train_data['image_id'][i])\n    pix_list.append(pix)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:40.752457Z","iopub.execute_input":"2022-08-17T07:53:40.752964Z","iopub.status.idle":"2022-08-17T07:53:57.044433Z","shell.execute_reply.started":"2022-08-17T07:53:40.752921Z","shell.execute_reply":"2022-08-17T07:53:57.043285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inx =  pix_list.index(max(pix_list))\nprint(\"Maximum Size of Image Data:\")\nprint('Index no:',inx)\nprint('ID:',pix_id[inx])\nmax_gb = max(pix_list)/1024/1024/1024\nprint('Size:',max_gb,'GB')","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:53:57.046137Z","iopub.execute_input":"2022-08-17T07:53:57.046513Z","iopub.status.idle":"2022-08-17T07:53:57.053881Z","shell.execute_reply.started":"2022-08-17T07:53:57.046481Z","shell.execute_reply":"2022-08-17T07:53:57.05287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying Image Using OpenSlide**:\nOpenSlide allows us to read Whole-Slide Images(WSI's)","metadata":{}},{"cell_type":"code","source":"img_path = train_data['train_path'][0]\n\nregion = (0, 0)\nlevel = 0\nsize = (10000, 10000)\n    \nplt.figure(figsize=(20,20))\nslide = OpenSlide(img_path)\nplt.imshow(slide.read_region(region, level, size))\nplt.show()\n   ","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:54:02.972301Z","iopub.execute_input":"2022-08-17T07:54:02.973312Z","iopub.status.idle":"2022-08-17T07:54:33.239731Z","shell.execute_reply.started":"2022-08-17T07:54:02.973256Z","shell.execute_reply":"2022-08-17T07:54:33.238628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying Image Using PIL**","metadata":{}},{"cell_type":"code","source":"def img_to_array(n):\n    image = Image.open(train_data['train_path'][n])\n    image = image.resize((300,300))\n    image = np.asarray(image)\n    image = image.astype(np.uint8)\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:54:33.242117Z","iopub.execute_input":"2022-08-17T07:54:33.242735Z","iopub.status.idle":"2022-08-17T07:54:33.248444Z","shell.execute_reply.started":"2022-08-17T07:54:33.242697Z","shell.execute_reply":"2022-08-17T07:54:33.247003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,15))   \nfor i in range(3):\n    img = img_to_array(i)\n    plt.subplot(1,3,i+1)\n    plt.title(train_data['image_id'][i])\n    plt.axis('off')\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:54:33.249953Z","iopub.execute_input":"2022-08-17T07:54:33.250292Z","iopub.status.idle":"2022-08-17T07:56:08.299735Z","shell.execute_reply.started":"2022-08-17T07:54:33.25026Z","shell.execute_reply":"2022-08-17T07:56:08.298389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying Images Using pyvips**: Due to limited RAM on Kaggle, pyvips is one of the memory efficient ways to read the huge .tif files","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15)) \nfor i in range(6):\n    n = random.randint(0,len(train_data))\n    img = pyvips.Image.thumbnail(train_data['train_path'][n],300,height=300,size='force')\n    plt.subplot(2,3,i+1)\n    plt.title(train_data['image_id'][i])\n    plt.axis('off')\n    plt.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:56:08.302151Z","iopub.execute_input":"2022-08-17T07:56:08.302571Z","iopub.status.idle":"2022-08-17T07:57:44.13078Z","shell.execute_reply.started":"2022-08-17T07:56:08.302533Z","shell.execute_reply":"2022-08-17T07:57:44.129665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}