{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":99552,"databundleVersionId":13762876,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## DCM - read-speed\nI am running into time issues when loading all the raw slices in each epoch in a per-slice approach. I found the package `dicomsdl`, which seems to be much faster than `pydicom` at reading the pixel data. Unfortunately this is not installed in the standard container and hence cannot be used for inference, where the internet has to be turned off.\n\nMaybe this could be included?","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-09-24T12:16:16.754083Z","iopub.execute_input":"2025-09-24T12:16:16.754441Z","iopub.status.idle":"2025-09-24T12:16:19.779504Z","shell.execute_reply.started":"2025-09-24T12:16:16.754408Z","shell.execute_reply":"2025-09-24T12:16:19.778464Z"}}},{"cell_type":"code","source":"!pip install dicomsdl","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:16:36.07076Z","iopub.execute_input":"2025-09-24T12:16:36.071609Z","iopub.status.idle":"2025-09-24T12:16:42.6407Z","shell.execute_reply.started":"2025-09-24T12:16:36.07158Z","shell.execute_reply":"2025-09-24T12:16:42.639449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import dicomsdl\nimport pydicom\nimport os\nimport pandas\nfrom random import choice\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:24:42.482463Z","iopub.execute_input":"2025-09-24T12:24:42.48283Z","iopub.status.idle":"2025-09-24T12:24:42.487868Z","shell.execute_reply.started":"2025-09-24T12:24:42.482804Z","shell.execute_reply":"2025-09-24T12:24:42.486722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = '/kaggle/input/rsna-intracranial-aneurysm-detection'\nbase_series_dir = os.path.join(data_dir,'series')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:18:19.268056Z","iopub.execute_input":"2025-09-24T12:18:19.268417Z","iopub.status.idle":"2025-09-24T12:18:19.274596Z","shell.execute_reply.started":"2025-09-24T12:18:19.268392Z","shell.execute_reply":"2025-09-24T12:18:19.2729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_series = choice(os.listdir(base_series_dir))\nseries_dir = os.path.join(base_series_dir,sample_series)\nsample_file = os.path.join(series_dir,choice(os.listdir(series_dir)))\nprint(sample_file)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:22:07.687391Z","iopub.execute_input":"2025-09-24T12:22:07.688081Z","iopub.status.idle":"2025-09-24T12:22:07.773931Z","shell.execute_reply.started":"2025-09-24T12:22:07.688051Z","shell.execute_reply":"2025-09-24T12:22:07.772749Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%timeit\nds = pydicom.dcmread(sample_file)\npixels_pydicom =ds.pixel_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:25:00.846438Z","iopub.execute_input":"2025-09-24T12:25:00.848032Z","iopub.status.idle":"2025-09-24T12:25:14.566695Z","shell.execute_reply.started":"2025-09-24T12:25:00.847987Z","shell.execute_reply":"2025-09-24T12:25:14.56551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%timeit\nds = dicomsdl.open(sample_file)\npixels_dicomsdl = ds.pixelData()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:25:14.568623Z","iopub.execute_input":"2025-09-24T12:25:14.569338Z","iopub.status.idle":"2025-09-24T12:25:16.8779Z","shell.execute_reply.started":"2025-09-24T12:25:14.569274Z","shell.execute_reply":"2025-09-24T12:25:16.876372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds = pydicom.dcmread(sample_file)\npixels_pydicom =ds.pixel_array\nds = dicomsdl.open(sample_file)\npixels_dicomsdl = ds.pixelData()\n\nassert np.allclose(pixels_pydicom,pixels_dicomsdl)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T12:26:00.281997Z","iopub.execute_input":"2025-09-24T12:26:00.282376Z","iopub.status.idle":"2025-09-24T12:26:00.324038Z","shell.execute_reply.started":"2025-09-24T12:26:00.282351Z","shell.execute_reply":"2025-09-24T12:26:00.321074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}