{"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":"Hello to All.\n\nJust sharing a lite-spaghetti script below for visualizing the tiffs (kidney_1_dense) via loading it as nifti in ITKSNAP.\n\n<img src=\"https://i.imgur.com/dJhpz9r.png\">\n\n\nref. https://www.kaggle.com/code/sb0702/sennet-hoa-visualize-3d-slice-mask\n","metadata":{"execution":{"iopub.status.busy":"2023-11-10T18:29:51.778701Z","iopub.execute_input":"2023-11-10T18:29:51.779100Z","iopub.status.idle":"2023-11-10T18:29:51.784320Z","shell.execute_reply.started":"2023-11-10T18:29:51.779070Z","shell.execute_reply":"2023-11-10T18:29:51.782946Z"}}},{"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\ndirectory_path_train = '/kaggle/input/blood-vessel-segmentation/train'\ndef list_files_in_directory(directory_path = directory_path_train):\n    direc = []\n    for root, dirs, files in os.walk(directory_path):\n        for dire in dirs:\n            if dire in [\"labels\",\"images\"]:\n                continue \n            file_path = os.path.join(root, dire)\n            direc.append(file_path)\n            print(file_path)\n    return direc\n            \n\ntrain_folders = list_files_in_directory(directory_path_train)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-10T23:01:14.639596Z","iopub.execute_input":"2023-11-10T23:01:14.640204Z","iopub.status.idle":"2023-11-10T23:01:20.199228Z","shell.execute_reply.started":"2023-11-10T23:01:14.640171Z","shell.execute_reply":"2023-11-10T23:01:20.198316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folders","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:20.201122Z","iopub.execute_input":"2023-11-10T23:01:20.201628Z","iopub.status.idle":"2023-11-10T23:01:20.210615Z","shell.execute_reply.started":"2023-11-10T23:01:20.201598Z","shell.execute_reply":"2023-11-10T23:01:20.209447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n\"kidney_1_dense - The whole of a right kidney at 50um resolution. The entire 3D arterial vascular tree has been densely segmented, down to two generations from the glomeruli (i.e. the capillary bed). Uses beamline BM05.\"  from \"https://www.kaggle.com/competitions/blood-vessel-segmentation/data\"\n\nper above, likely isotropic voxel, spacing for all 3 axis 50um\n","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images | wc -l\n!ls /kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/labels | wc -l","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:20.212224Z","iopub.execute_input":"2023-11-10T23:01:20.213247Z","iopub.status.idle":"2023-11-10T23:01:20.876175Z","shell.execute_reply.started":"2023-11-10T23:01:20.213207Z","shell.execute_reply":"2023-11-10T23:01:20.874177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!free -mh","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:20.880534Z","iopub.execute_input":"2023-11-10T23:01:20.881859Z","iopub.status.idle":"2023-11-10T23:01:21.237636Z","shell.execute_reply.started":"2023-11-10T23:01:20.881799Z","shell.execute_reply":"2023-11-10T23:01:21.236137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nimport SimpleITK as sitk\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:21.238993Z","iopub.execute_input":"2023-11-10T23:01:21.239886Z","iopub.status.idle":"2023-11-10T23:01:21.706741Z","shell.execute_reply.started":"2023-11-10T23:01:21.239853Z","shell.execute_reply":"2023-11-10T23:01:21.704936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_spacing = 2279*0.05/512 # determine the target spacing\nprint(target_spacing)","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:21.708307Z","iopub.execute_input":"2023-11-10T23:01:21.708743Z","iopub.status.idle":"2023-11-10T23:01:21.715538Z","shell.execute_reply.started":"2023-11-10T23:01:21.708706Z","shell.execute_reply":"2023-11-10T23:01:21.714270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"blank = np.zeros((512,512,512)).astype(np.uint8)\norigin = (0,0,0)\ndirection = (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)\nspacing = (0.1,0.1,0.1)\ntgt_obj = sitk.GetImageFromArray(blank)\ntgt_obj.SetSpacing(spacing)\ntgt_obj.SetOrigin(origin)\ntgt_obj.SetDirection(direction)","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:21.717332Z","iopub.execute_input":"2023-11-10T23:01:21.717726Z","iopub.status.idle":"2023-11-10T23:01:21.953509Z","shell.execute_reply.started":"2023-11-10T23:01:21.717696Z","shell.execute_reply":"2023-11-10T23:01:21.952786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_folder = '/kaggle/input/blood-vessel-segmentation/train/'\ntrain_case_list = sorted(os.listdir(root_folder))\nfor x in train_case_list:\n    print(x)\n    print(os.listdir(os.path.join(root_folder,x)))\n    print('--')","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:21.954476Z","iopub.execute_input":"2023-11-10T23:01:21.955499Z","iopub.status.idle":"2023-11-10T23:01:21.963774Z","shell.execute_reply.started":"2023-11-10T23:01:21.955475Z","shell.execute_reply":"2023-11-10T23:01:21.962498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(tiff_folder,nifti_path):\n    if not os.path.exists(tiff_folder):\n        print('not found',tiff_folder)\n        return\n    myfile_list = sorted(os.listdir(tiff_folder))\n    myfile_list = myfile_list[::2] # to avoid OOM\n    mylist = []\n    print(f'loading {tiff_folder} ...')\n    \n    for basename in tqdm(myfile_list):\n        file_path = os.path.join(tiff_folder,basename)\n        arr = imageio.v2.imread(file_path)\n        arr = np.expand_dims(arr,axis=0)\n        mylist.append(arr)\n    arr = np.concatenate(mylist,axis=0)\n    print(arr.shape,arr.dtype)\n\n    origin = (0,0,0)\n    direction = (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)\n    ##spacing = (0.05,0.05,0.05)\n    spacing = (0.1,0.05,0.05) # unsure about z spacing \n    img_obj = sitk.GetImageFromArray(arr)\n    img_obj.SetSpacing(spacing)\n    img_obj.SetOrigin(origin)\n    img_obj.SetDirection(direction)\n\n    print('resampling...')\n    img_obj = sitk.Resample(img_obj, tgt_obj, sitk.Transform(), \n                  sitk.sitkNearestNeighbor, 0, img_obj.GetPixelID())\n    print(img_obj.GetSize())\n    print('writing...')\n    use_compression = True\n    writer = sitk.ImageFileWriter()    \n    writer.SetFileName(nifti_path)\n    writer.SetUseCompression(use_compression)\n    writer.Execute(img_obj)\n    print('done.')\n    del arr\n    del mylist\n    del img_obj\n    \nfor n,case_name in enumerate(train_case_list):\n    for kind in ['labels','images']:\n        tiff_folder = os.path.join(root_folder,case_name,kind)\n        nifti_path = f'{case_name}.{kind}.nii.gz'\n        process(tiff_folder,nifti_path)\n    #if n > 1:\n    #    break\n","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:01:25.763466Z","iopub.execute_input":"2023-11-10T23:01:25.763897Z","iopub.status.idle":"2023-11-10T23:05:34.912904Z","shell.execute_reply.started":"2023-11-10T23:01:25.763864Z","shell.execute_reply":"2023-11-10T23:05:34.911571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lha /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-11-10T23:05:34.979533Z","iopub.execute_input":"2023-11-10T23:05:34.980215Z","iopub.status.idle":"2023-11-10T23:05:35.308002Z","shell.execute_reply.started":"2023-11-10T23:05:34.980182Z","shell.execute_reply":"2023-11-10T23:05:35.305771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"+ download the nifti files, and view via ITKSnap!\n+ voxel most likely not isotropic.\n\n<img src=\"https://i.imgur.com/dJhpz9r.png\">\n","metadata":{}}]}