{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71698,"databundleVersionId":7906362,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tifffile\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.ndimage import zoom\nfrom glob import glob\nimport os","metadata":{"execution":{"iopub.status.busy":"2024-05-26T16:02:31.695882Z","iopub.execute_input":"2024-05-26T16:02:31.696285Z","iopub.status.idle":"2024-05-26T16:02:32.089313Z","shell.execute_reply.started":"2024-05-26T16:02:31.696253Z","shell.execute_reply":"2024-05-26T16:02:32.087687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change to ess or siz here\nmode = \"ess\"","metadata":{"execution":{"iopub.status.busy":"2024-05-26T16:02:32.091122Z","iopub.execute_input":"2024-05-26T16:02:32.091653Z","iopub.status.idle":"2024-05-26T16:02:32.095420Z","shell.execute_reply.started":"2024-05-26T16:02:32.091623Z","shell.execute_reply":"2024-05-26T16:02:32.094526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Even slice selection (ESS)\ndef change_depth_ess(img):\n    \n    '''ESS depth size is 32 as min depth size is 47, so we cannot evenly select 64 slices!'''\n\n    # Ignore volumes which have depth less than 64\n    target_depth = 64\n\n    #print(img.shape) \n\n    scan_depth = int(img.shape[0])\n#     print(scan_depth)\n    \n    # If depth is not 64, make it 64\n    if scan_depth < 64:\n        number = 64 - scan_depth\n        #print(number)\n        # take last image and stack\n        extend = [img[-1,:,:] for _ in range(number)]\n        extend = np.array(np.dstack(extend))\n        #print(extend.shape)\n        img = np.concatenate((img, extend), axis=2)\n    \n    else:\n        factor = int(np.floor(scan_depth / target_depth)) # min depth is 32\n        #print(factor) # floor and ceil result in different values\n        #print(scan_depth, factor)\n\n        flatten = []\n        idx = 0\n\n        for i in range(0, scan_depth, factor):\n          #print(idx)\n\n            if idx>=scan_depth:\n                break\n            else:\n                flatten.append(img[i,:,:])\n\n        img = np.array(np.dstack(flatten))\n\n    # hardcode from bottom, if not 64 slices\n    img = img[:target_depth,:,:]     \n\n    #print(\"Final shape: \", img.shape)\n    \n    # hardcode from bottom, if not 32 slices\n    img = img[:target_depth,:,:]\n\n    assert img.shape[-1] == target_depth , \"Error\"\n    return img\n\n\n# Spline interpolated zoom (SIZ)\ndef change_depth_siz(img):\n    desired_depth = 64\n    current_depth = img.shape[0]\n    depth = current_depth / desired_depth\n    depth_factor = 1 / depth\n    img_new = zoom(img, (depth_factor, 1, 1), mode='nearest')\n    return img_new\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-26T16:02:32.096634Z","iopub.execute_input":"2024-05-26T16:02:32.097104Z","iopub.status.idle":"2024-05-26T16:02:32.111324Z","shell.execute_reply.started":"2024-05-26T16:02:32.097076Z","shell.execute_reply":"2024-05-26T16:02:32.110152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mkdirs(\"ess\")\npaths = glob(\"/kaggle/input/bugnist2024fgvc/BugNIST_DATA/train/*\")\nfor folder_path in paths:\n    folder_name = folder_path.split(\"/\")[-1]\n    os.mkdir(folder_name)\n    \n    files = glob(f\"/kaggle/input/bugnist2024fgvc/BugNIST_DATA/train/{folder_name}/*.tif\")\n    \n    for file_path in files:\n        file_name = file_path.split(\"/\")[-1].split(\".\")[0]\n#         print(file_name)\n        input_image = tifffile.imread(file_path)\n        if mode == \"ess\":\n            img = change_depth_ess(input_image)\n        else:\n            img = change_depth_siz(input_image)\n#         print(img.shape)\n        output_file = f\"{folder_name}/{file_name}.tif\"\n        tifffile.imwrite(output_file, img.astype(np.uint16))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-05-26T16:02:32.112777Z","iopub.execute_input":"2024-05-26T16:02:32.113356Z","iopub.status.idle":"2024-05-26T16:02:32.668704Z","shell.execute_reply.started":"2024-05-26T16:02:32.113308Z","shell.execute_reply":"2024-05-26T16:02:32.667677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}