{"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":"# Dicomsdl Pydicom Speed Testing","metadata":{}},{"cell_type":"markdown","source":"**This code is for Test the speed of dicomsdl and pydicom in reading dicom files**","metadata":{}},{"cell_type":"code","source":"!pip install dicomsdl","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:06:52.643681Z","iopub.execute_input":"2023-02-07T16:06:52.644792Z","iopub.status.idle":"2023-02-07T16:07:07.033324Z","shell.execute_reply.started":"2023-02-07T16:06:52.644687Z","shell.execute_reply":"2023-02-07T16:07:07.031634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicomsdl\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:07:07.035922Z","iopub.execute_input":"2023-02-07T16:07:07.036302Z","iopub.status.idle":"2023-02-07T16:07:07.169282Z","shell.execute_reply.started":"2023-02-07T16:07:07.036266Z","shell.execute_reply":"2023-02-07T16:07:07.168008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path  = '/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm'","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:07:07.170845Z","iopub.execute_input":"2023-02-07T16:07:07.17121Z","iopub.status.idle":"2023-02-07T16:07:07.175603Z","shell.execute_reply.started":"2023-02-07T16:07:07.171176Z","shell.execute_reply":"2023-02-07T16:07:07.174695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"read_func = True\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\ndicom_dcmread = read_funcs(path)\ndicom1 = dicom_dcmread.pixelData()\n# dicom2 = dicom_dcmread.pixel_array   # 'DataSet' object has no attribute 'pixel_array'\n\n# assert np.array_equal(dicom1, dicom2)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:07:07.177697Z","iopub.execute_input":"2023-02-07T16:07:07.178068Z","iopub.status.idle":"2023-02-07T16:07:08.56008Z","shell.execute_reply.started":"2023-02-07T16:07:07.178026Z","shell.execute_reply":"2023-02-07T16:07:08.558589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **dicomsdl** is with `.pixelData()` fuction not  `.pixel_array` property","metadata":{}},{"cell_type":"code","source":"read_func = False\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\ndicom_dicomsdl = read_funcs(path)\n# dicom1 = dicom_dicomsdl.pixelData()  #'FileDataset' object has no attribute 'pixelData'\ndicom2 = dicom_dicomsdl.pixel_array\n\n# assert np.array_equal(dicom1, dicom2)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:14:46.951935Z","iopub.execute_input":"2023-02-07T12:14:46.95235Z","iopub.status.idle":"2023-02-07T12:14:48.462393Z","shell.execute_reply.started":"2023-02-07T12:14:46.952315Z","shell.execute_reply":"2023-02-07T12:14:48.461374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **pydicom** is with`.pixel_array` property not  `.pixelData()` fuction ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nprint(np.array_equal(dicom1, dicom2))\nassert np.array_equal(dicom1, dicom2)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:21:21.53567Z","iopub.execute_input":"2023-02-07T12:21:21.536074Z","iopub.status.idle":"2023-02-07T12:21:21.584708Z","shell.execute_reply.started":"2023-02-07T12:21:21.536038Z","shell.execute_reply":"2023-02-07T12:21:21.583673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## the data of these two methods is the **same**","metadata":{}},{"cell_type":"code","source":"assert np.array_equal(1, 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:15:48.483804Z","iopub.execute_input":"2023-02-07T12:15:48.48422Z","iopub.status.idle":"2023-02-07T12:15:48.489022Z","shell.execute_reply.started":"2023-02-07T12:15:48.484181Z","shell.execute_reply":"2023-02-07T12:15:48.48805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert np.array_equal(1, 2)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:15:50.569019Z","iopub.execute_input":"2023-02-07T12:15:50.569412Z","iopub.status.idle":"2023-02-07T12:15:50.585747Z","shell.execute_reply.started":"2023-02-07T12:15:50.569378Z","shell.execute_reply":"2023-02-07T12:15:50.584153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom1.shape,dicom2.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:21:48.879401Z","iopub.execute_input":"2023-02-07T12:21:48.879775Z","iopub.status.idle":"2023-02-07T12:21:48.886776Z","shell.execute_reply.started":"2023-02-07T12:21:48.879745Z","shell.execute_reply":"2023-02-07T12:21:48.885714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I don't know why `%time` is not work for me,there is no big difference even I change the number of running times","metadata":{}},{"cell_type":"code","source":"%time\nread_func = True\nfor i in range(10):\n    read_funcs = dicomsdl.open if read_func else pydicom.dcmread\n    dicom_dcmread = read_funcs(path)\n    dicom1 = dicom_dcmread.pixelData()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:23:01.679107Z","iopub.execute_input":"2023-02-07T12:23:01.679551Z","iopub.status.idle":"2023-02-07T12:23:14.28294Z","shell.execute_reply.started":"2023-02-07T12:23:01.679515Z","shell.execute_reply":"2023-02-07T12:23:14.281412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nread_func = False\nfor i in range(10):\n    read_funcs = dicomsdl.open if read_func else pydicom.dcmread\n    dicom_dcmread = read_funcs(path)\n    dicom2 = dicom_dicomsdl.pixel_array\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:23:43.116321Z","iopub.execute_input":"2023-02-07T12:23:43.11747Z","iopub.status.idle":"2023-02-07T12:23:43.155471Z","shell.execute_reply.started":"2023-02-07T12:23:43.117422Z","shell.execute_reply":"2023-02-07T12:23:43.154135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### So I use **time** library to do the experiment again","metadata":{}},{"cell_type":"code","source":"import time","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:12:32.870526Z","iopub.execute_input":"2023-02-07T16:12:32.871495Z","iopub.status.idle":"2023-02-07T16:12:32.877275Z","shell.execute_reply.started":"2023-02-07T16:12:32.871449Z","shell.execute_reply":"2023-02-07T16:12:32.8756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\n\nread_func = True\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(10):\n    dicom_dcmread = read_funcs(path)\n#     dicom1 = dicom_dcmread.pixelData()\n    \nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:02.609727Z","iopub.execute_input":"2023-02-07T16:13:02.610178Z","iopub.status.idle":"2023-02-07T16:13:02.644518Z","shell.execute_reply.started":"2023-02-07T16:13:02.610121Z","shell.execute_reply":"2023-02-07T16:13:02.643136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\nread_func = False\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(10): \n    dicom_dcmread = read_funcs(path)\n#     dicom2 = dicom_dicomsdl.pixel_array\nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:04.998797Z","iopub.execute_input":"2023-02-07T16:13:04.999191Z","iopub.status.idle":"2023-02-07T16:13:05.035422Z","shell.execute_reply.started":"2023-02-07T16:13:04.999157Z","shell.execute_reply":"2023-02-07T16:13:05.034329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"0.028 vs 0.031, just a small difference","metadata":{}},{"cell_type":"code","source":"t = time.time()\n\nread_func = True\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(100):\n    dicom_dcmread = read_funcs(path)\n#     dicom1 = dicom_dcmread.pixelData()\n    \nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:06.664588Z","iopub.execute_input":"2023-02-07T16:13:06.665033Z","iopub.status.idle":"2023-02-07T16:13:06.811Z","shell.execute_reply.started":"2023-02-07T16:13:06.664994Z","shell.execute_reply":"2023-02-07T16:13:06.810097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\nread_func = False\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(100):\n    dicom_dcmread = read_funcs(path)\n#     dicom2 = dicom_dicomsdl.pixel_array\nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:08.768971Z","iopub.execute_input":"2023-02-07T16:13:08.769447Z","iopub.status.idle":"2023-02-07T16:13:09.066957Z","shell.execute_reply.started":"2023-02-07T16:13:08.769404Z","shell.execute_reply":"2023-02-07T16:13:09.065057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\n\nread_func = True\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(1000):\n    dicom_dcmread = read_funcs(path)\n#     dicom1 = dicom_dcmread.pixelData()\n    \nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:10.334544Z","iopub.execute_input":"2023-02-07T16:13:10.334991Z","iopub.status.idle":"2023-02-07T16:13:11.728421Z","shell.execute_reply.started":"2023-02-07T16:13:10.334953Z","shell.execute_reply":"2023-02-07T16:13:11.727131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\nread_func = False\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(1000):\n    dicom_dcmread = read_funcs(path)\n#     dicom2 = dicom_dicomsdl.pixel_array\nprint(f'Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:13:13.896079Z","iopub.execute_input":"2023-02-07T16:13:13.896473Z","iopub.status.idle":"2023-02-07T16:13:16.758432Z","shell.execute_reply.started":"2023-02-07T16:13:13.896441Z","shell.execute_reply":"2023-02-07T16:13:16.757368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## but if we get the data, then it shows interesting results","metadata":{}},{"cell_type":"code","source":"t = time.time()\n\nread_func = True\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(20):\n    dicom_dcmread1 = read_funcs(path)\n    dicom1 = dicom_dcmread1.pixelData()\n    \nprint(f'dicomsdl Time difference:{time.time() - t:.3f}s')\n\nt = time.time()\nread_func = False\nread_funcs = dicomsdl.open if read_func else pydicom.dcmread\nfor i in range(20):\n    dicom_dcmread2 = read_funcs(path)\n    dicom2 = dicom_dcmread2.pixel_array\nprint(f'pydicom Time difference:{time.time() - t:.3f}s')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:19:26.852723Z","iopub.execute_input":"2023-02-07T16:19:26.853117Z","iopub.status.idle":"2023-02-07T16:20:21.531186Z","shell.execute_reply.started":"2023-02-07T16:19:26.853077Z","shell.execute_reply":"2023-02-07T16:20:21.529991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_dcmread1","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:20:36.44549Z","iopub.execute_input":"2023-02-07T16:20:36.446333Z","iopub.status.idle":"2023-02-07T16:20:36.455261Z","shell.execute_reply.started":"2023-02-07T16:20:36.446293Z","shell.execute_reply":"2023-02-07T16:20:36.454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# But pydicom read methods contains more information","metadata":{}},{"cell_type":"code","source":"dicom_dcmread2","metadata":{"execution":{"iopub.status.busy":"2023-02-07T16:20:48.688849Z","iopub.execute_input":"2023-02-07T16:20:48.689253Z","iopub.status.idle":"2023-02-07T16:20:48.698669Z","shell.execute_reply.started":"2023-02-07T16:20:48.689221Z","shell.execute_reply":"2023-02-07T16:20:48.697343Z"},"trusted":true},"execution_count":null,"outputs":[]}]}