{"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":"markdown","source":"# Useful Histopathology Libraries\n\n* [HI-ML-Histopathology](https://github.com/microsoft/hi-ml/tree/main/hi-ml-histopathology)\n* [OpenSlide](https://openslide.org/api/python/)\n* [cuCIM](https://github.com/rapidsai/cucim)\n* [HistoQC](https://github.com/choosehappy/HistoQC)\n* [Digital Slide Archive](https://digitalslidearchive.github.io/digital_slide_archive/)\n* [MONAI](https://github.com/Project-MONAI/MONAI)","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom glob import glob\nfrom openslide import OpenSlide\nfrom pprint import pprint\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-06T23:07:50.258176Z","iopub.execute_input":"2022-07-06T23:07:50.258569Z","iopub.status.idle":"2022-07-06T23:07:50.265016Z","shell.execute_reply.started":"2022-07-06T23:07:50.258538Z","shell.execute_reply":"2022-07-06T23:07:50.263725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/train/*\")\nlen(train_images)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:07:02.37261Z","iopub.execute_input":"2022-07-06T23:07:02.37334Z","iopub.status.idle":"2022-07-06T23:07:02.389994Z","shell.execute_reply.started":"2022-07-06T23:07:02.373303Z","shell.execute_reply":"2022-07-06T23:07:02.388854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Open whole slide images","metadata":{}},{"cell_type":"code","source":"img_path = train_images[2]\n\nprint(img_path)\nslide = OpenSlide(img_path)\n\nprint(slide.dimensions)\nprint(slide.level_count)\nprint(slide.level_dimensions)\npprint(dict(slide.properties))\nprint(f\"file size: {round(os.path.getsize(img_path) / 1024 ** 2, 2)} MB\")","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:07:02.391822Z","iopub.execute_input":"2022-07-06T23:07:02.392189Z","iopub.status.idle":"2022-07-06T23:07:02.410427Z","shell.execute_reply.started":"2022-07-06T23:07:02.392156Z","shell.execute_reply":"2022-07-06T23:07:02.409262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"region = (0, 0)\nlevel = 0\nsize = (5000, 5000)\nregion = slide.read_region(region, level, size)\nplt.figure(figsize=(20, 20))\nplt.imshow(region)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:07:02.411602Z","iopub.execute_input":"2022-07-06T23:07:02.4119Z","iopub.status.idle":"2022-07-06T23:07:08.559788Z","shell.execute_reply.started":"2022-07-06T23:07:02.411872Z","shell.execute_reply":"2022-07-06T23:07:08.558372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scale = 1024 / max(slide.dimensions)\n# print(f\"scale: {scale}\")\n# print([int(m * scale) for m in slide.dimensions])\n\n# plt.imshow(slide.get_thumbnail([int(m * scale) for m in slide.dimensions]))\n#\n# Somehow this is very slow","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:07:08.56221Z","iopub.execute_input":"2022-07-06T23:07:08.562548Z","iopub.status.idle":"2022-07-06T23:07:08.566844Z","shell.execute_reply.started":"2022-07-06T23:07:08.562517Z","shell.execute_reply":"2022-07-06T23:07:08.565654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train labels","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:08:20.468807Z","iopub.execute_input":"2022-07-06T23:08:20.46922Z","iopub.status.idle":"2022-07-06T23:08:20.492344Z","shell.execute_reply.started":"2022-07-06T23:08:20.469187Z","shell.execute_reply":"2022-07-06T23:08:20.49132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = train_df.iloc[0]\nr","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:11:11.663701Z","iopub.execute_input":"2022-07-06T23:11:11.664279Z","iopub.status.idle":"2022-07-06T23:11:11.675539Z","shell.execute_reply.started":"2022-07-06T23:11:11.664225Z","shell.execute_reply":"2022-07-06T23:11:11.674135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = f\"/kaggle/input/mayo-clinic-strip-ai/train/{r.image_id}.tif\"\nprint(img_path)\nslide = OpenSlide(img_path)\n\nprint(slide.dimensions)\nprint(slide.level_count)\nprint(slide.level_dimensions)\npprint(dict(slide.properties))\nprint(f\"file size: {round(os.path.getsize(img_path) / 1024 ** 2, 2)} MB\")","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:11:12.980295Z","iopub.execute_input":"2022-07-06T23:11:12.980689Z","iopub.status.idle":"2022-07-06T23:11:13.037639Z","shell.execute_reply.started":"2022-07-06T23:11:12.980655Z","shell.execute_reply":"2022-07-06T23:11:13.036413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"region = (0, 0)\nlevel = 0\nsize = (5000, 5000)\nregion = slide.read_region(region, level, size)\nplt.figure(figsize=(20, 20))\nplt.imshow(region)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T23:11:13.70728Z","iopub.execute_input":"2022-07-06T23:11:13.707965Z","iopub.status.idle":"2022-07-06T23:11:20.621902Z","shell.execute_reply.started":"2022-07-06T23:11:13.707927Z","shell.execute_reply":"2022-07-06T23:11:20.620546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}