{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install nibabel","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:38:13.480703Z","iopub.execute_input":"2023-11-11T13:38:13.481192Z","iopub.status.idle":"2023-11-11T13:38:27.917947Z","shell.execute_reply.started":"2023-11-11T13:38:13.481152Z","shell.execute_reply":"2023-11-11T13:38:27.916637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Para borrar archivos\n# Clear output folder\nimport os\n\ndef remove_folder_contents(folder):\n    for the_file in os.listdir(folder):\n        file_path = os.path.join(folder, the_file)\n        try:\n            if os.path.isfile(file_path):\n                os.unlink(file_path)\n            elif os.path.isdir(file_path):\n                remove_folder_contents(file_path)\n                os.rmdir(file_path)\n        except Exception as e:\n            print(e)\n\nfolder_path = '/kaggle/working'\nremove_folder_contents(folder_path)\nos.rmdir(folder_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-11T12:09:10.840643Z","iopub.execute_input":"2023-11-11T12:09:10.841105Z","iopub.status.idle":"2023-11-11T12:09:13.498786Z","shell.execute_reply.started":"2023-11-11T12:09:10.841069Z","shell.execute_reply":"2023-11-11T12:09:13.496889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport nibabel as nib\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-11T13:38:27.920755Z","iopub.execute_input":"2023-11-11T13:38:27.9212Z","iopub.status.idle":"2023-11-11T13:38:28.73808Z","shell.execute_reply.started":"2023-11-11T13:38:27.921151Z","shell.execute_reply":"2023-11-11T13:38:28.737143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=[]\nids=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'):\n    for filename in filenames:\n        ids+=[filename[0:-4]]\n        paths+=[(os.path.join(dirname, filename))]","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:40:04.870905Z","iopub.execute_input":"2023-11-11T13:40:04.871373Z","iopub.status.idle":"2023-11-11T13:40:04.923065Z","shell.execute_reply.started":"2023-11-11T13:40:04.871335Z","shell.execute_reply":"2023-11-11T13:40:04.921884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(f'/kaggle/working/segment', exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:40:18.857468Z","iopub.execute_input":"2023-11-11T13:40:18.858099Z","iopub.status.idle":"2023-11-11T13:40:18.862592Z","shell.execute_reply.started":"2023-11-11T13:40:18.858066Z","shell.execute_reply":"2023-11-11T13:40:18.861685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_img = 0\nfor path in tqdm(paths):\n#     if path.split('/')[-1]=='10494.nii':\n        nii_img = nib.load(path)\n        data = nii_img.get_fdata()\n#         print(data.shape)\n        total_img += data.shape[2]\n        imagesi=[]\n        series_id = path.split('/')[-1][0:-4]\n        for i in range(data.shape[2]):\n            img=data[:,:,i]*51\n            if img.max()>0:\n                img=np.rot90(img)\n                newfile= '/kaggle/working/segment/'+series_id+'_'+str(i).zfill(4)+'.png'\n                cv2.imwrite(newfile,img)\n#                 imagesi+=[img]\n#         print(len(imagesi))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:40:34.18269Z","iopub.execute_input":"2023-11-11T13:40:34.183409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = '/kaggle/working/segment/'\nz + newfile","metadata":{"execution":{"iopub.status.busy":"2023-11-11T12:18:13.651665Z","iopub.execute_input":"2023-11-11T12:18:13.652149Z","iopub.status.idle":"2023-11-11T12:18:13.661052Z","shell.execute_reply.started":"2023-11-11T12:18:13.652112Z","shell.execute_reply":"2023-11-11T12:18:13.659778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_img","metadata":{"execution":{"iopub.status.busy":"2023-11-11T11:32:53.798548Z","iopub.execute_input":"2023-11-11T11:32:53.80019Z","iopub.status.idle":"2023-11-11T11:32:53.820381Z","shell.execute_reply.started":"2023-11-11T11:32:53.800139Z","shell.execute_reply":"2023-11-11T11:32:53.818993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r\"/kaggle/working/file.zip\")","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:34:43.996856Z","iopub.execute_input":"2023-11-11T14:34:43.997679Z","iopub.status.idle":"2023-11-11T14:34:44.011641Z","shell.execute_reply.started":"2023-11-11T14:34:43.997629Z","shell.execute_reply":"2023-11-11T14:34:44.009917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -zcvf file.zip /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-11-11T12:02:56.174397Z","iopub.execute_input":"2023-11-11T12:02:56.174979Z","iopub.status.idle":"2023-11-11T12:03:07.348641Z","shell.execute_reply.started":"2023-11-11T12:02:56.174928Z","shell.execute_reply":"2023-11-11T12:03:07.347031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r file.zip /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:56:17.578348Z","iopub.execute_input":"2023-11-11T13:56:17.578905Z","iopub.status.idle":"2023-11-11T13:56:50.148656Z","shell.execute_reply.started":"2023-11-11T13:56:17.578865Z","shell.execute_reply":"2023-11-11T13:56:50.147377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport subprocess\nfrom IPython.display import FileLink, display\n\ndef download_file(path, download_file_name):\n    os.chdir('/kaggle/working/')\n    zip_name = f\"/kaggle/working/{download_file_name}.zip\"\n    command = f\"zip {zip_name} {path} -r\"\n    result = subprocess.run(command, shell=True, capture_output=True, text=True)\n    if result.returncode != 0:\n        print(\"Unable to run zip command!\")\n        print(result.stderr)\n        return\n    display(FileLink(f'{download_file_name}.zip'))\ndownload_file('/kaggle/output/file', 'out')","metadata":{"execution":{"iopub.status.busy":"2023-11-11T11:51:28.630662Z","iopub.execute_input":"2023-11-11T11:51:28.631178Z","iopub.status.idle":"2023-11-11T11:51:28.646269Z","shell.execute_reply.started":"2023-11-11T11:51:28.631107Z","shell.execute_reply":"2023-11-11T11:51:28.644144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}