{"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":"# 🔬Stroke Blood Clot Origin: 🔎Image conversion\n\nNOTE: this is simplified version of https://www.kaggle.com/code/jirkaborovec/bloodclots-eda-load-crop-images-w-o-bg","metadata":{}},{"cell_type":"code","source":"import os, glob\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nDATASET_FOLDER = \"/kaggle/input/mayo-clinic-strip-ai/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T06:58:18.632085Z","iopub.execute_input":"2022-07-25T06:58:18.632508Z","iopub.status.idle":"2022-07-25T06:58:18.637677Z","shell.execute_reply.started":"2022-07-25T06:58:18.632448Z","shell.execute_reply":"2022-07-25T06:58:18.636606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install ../input/conda-pkg-pyvips-download-4-offline/conda-pkgs/*.tar.bz2 --offline\n\nos.environ['VIPS_CONCURRENCY'] = '4'\nos.environ['VIPS_DISC_THRESHOLD'] = '8gb'\n\n!mkdir -p train_images","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-25T06:40:36.732913Z","iopub.execute_input":"2022-07-25T06:40:36.733319Z","iopub.status.idle":"2022-07-25T06:40:37.525597Z","shell.execute_reply.started":"2022-07-25T06:40:36.733284Z","shell.execute_reply":"2022-07-25T06:40:37.523944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Export 🤖 images w/ pyvips","metadata":{}},{"cell_type":"code","source":"ls_imgs = glob.glob(os.path.join(DATASET_FOLDER, \"train\", \"*.tif\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T06:40:37.528113Z","iopub.execute_input":"2022-07-25T06:40:37.528538Z","iopub.status.idle":"2022-07-25T06:40:37.53834Z","shell.execute_reply.started":"2022-07-25T06:40:37.528501Z","shell.execute_reply":"2022-07-25T06:40:37.537122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport pyvips\nfrom tqdm.auto import tqdm\n\nfor tif_path in tqdm(ls_imgs):\n    # img = Image.open(tif_path)\n    name, _ = os.path.splitext(os.path.basename(tif_path))\n    png_path = os.path.join(\"./train_images\", f\"{name}.png\")\n    pyvips.Image.thumbnail(tif_path, 2000).write_to_file(png_path)\n    gc.collect()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-25T06:40:37.539748Z","iopub.execute_input":"2022-07-25T06:40:37.540317Z","iopub.status.idle":"2022-07-25T06:55:45.415214Z","shell.execute_reply.started":"2022-07-25T06:40:37.54028Z","shell.execute_reply":"2022-07-25T06:55:45.411137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show 🔎 few images","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(os.path.join(DATASET_FOLDER, \"train.csv\"))\nlabels = list(df_train[\"label\"].unique())\ndisplay(df_train.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-25T06:58:23.18716Z","iopub.execute_input":"2022-07-25T06:58:23.187578Z","iopub.status.idle":"2022-07-25T06:58:23.229151Z","shell.execute_reply.started":"2022-07-25T06:58:23.187542Z","shell.execute_reply":"2022-07-25T06:58:23.228055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=5, ncols=2, figsize=(16, 20))\nfor lb, dfg in df_train.groupby(\"label\"):\n    j = labels.index(lb)\n    for i, name in enumerate(dfg[\"image_id\"].sample(5)):\n        # print(f\"{lb}: {name}\")\n        img_path = os.path.join(\"train_images\", f\"{name}.png\")\n        axes[i, j].imshow(plt.imread(img_path))\n        axes[i, j].set_title(f\"{lb}: {name}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T06:58:26.384153Z","iopub.execute_input":"2022-07-25T06:58:26.384536Z","iopub.status.idle":"2022-07-25T06:58:27.517758Z","shell.execute_reply.started":"2022-07-25T06:58:26.384505Z","shell.execute_reply":"2022-07-25T06:58:27.51653Z"},"trusted":true},"execution_count":null,"outputs":[]}]}