{"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":"for disscusion at : https://www.kaggle.com/competitions/rsna-breast-cancer-detection/discussion/371033","metadata":{}},{"cell_type":"code","source":"! pip install -U dicomsdl\n! pip install -U pylibjpeg\n! pip install -U python-gdcm\n\n\nimport pydicom\nimport dicomsdl\n\ndef __dataset__to_numpy_image(self, index=0): \n    # https://stackoverflow.com/questions/44659924/returning-numpy-arrays-via-pybind11\n    info = self.getPixelDataInfo() \n    dtype = info['dtype']\n    if info['SamplesPerPixel'] != 1:\n        raise RuntimeError('SamplesPerPixel != 1')\n    else:\n        shape = [info['Rows'], info['Cols']]\n    \n    outarr = np.empty(shape, dtype=dtype)\n    self.copyFrameData(index, outarr)\n    \n    check = lambda x: x[index] if isinstance(x, list) else x \n    intercept = check(info['RescaleIntercept'])\n    intercept = intercept if intercept is not None else 0.0\n    slope = check(info['RescaleSlope'])\n    slope = slope if slope is not None else 1.0\n    # c = check(info['WindowCenter'])\n    # w = check(info['WindowWidth'])\n    xmin = outarr.min()\n    xmax = outarr.max()\n    \n    data8 = np.empty_like(outarr, dtype=np.uint8)\n    dicomsdl.util.convert_to_uint8(outarr, data8, xmin, xmax) \n    if info['PhotometricInterpretation'] == 'MONOCHROME1':\n        data8 = 255 - data8\n        \n    return data8\n\ndicomsdl._dicomsdl.DataSet.to_numpy_image = __dataset__to_numpy_image\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-10T08:40:18.430055Z","iopub.execute_input":"2022-12-10T08:40:18.430577Z","iopub.status.idle":"2022-12-10T08:40:46.676653Z","shell.execute_reply.started":"2022-12-10T08:40:18.430536Z","shell.execute_reply":"2022-12-10T08:40:46.675629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport timeit\nfrom glob import glob\n\n\ndcm_dir = '/kaggle/input/rsna-breast-cancer-detection/train_images'\n\ndcm_file = sorted(glob(f'{dcm_dir}/*/*.dcm'))\ndcm_file = dcm_file[:25]\nprint(len(dcm_file))\n","metadata":{"execution":{"iopub.status.busy":"2022-12-10T08:40:46.678765Z","iopub.execute_input":"2022-12-10T08:40:46.679134Z","iopub.status.idle":"2022-12-10T08:41:20.231503Z","shell.execute_reply.started":"2022-12-10T08:40:46.679079Z","shell.execute_reply":"2022-12-10T08:41:20.23047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef norm_data(image, invert):\n    min = image.min()\n    max = image.max()\n    image = (image - min) / (max - min + 1e-6)\n    if invert:\n        image = 1 - image\n    image = (image * 255).astype(np.uint8)\n    return image\n\n\ndef timer_function10():\n    image = []\n    for f in dcm_file:\n        dicom = pydicom.dcmread(f)\n        m = dicom.pixel_array\n        m = norm_data(m,  dicom.PhotometricInterpretation == 'MONOCHROME1')\n        image.append(m)\n    print(len(image))\n    return image\n\n\ndef timer_function7():\n    image = []\n    for f in dcm_file:\n        dicom = dicomsdl.open(f)\n        m = dicom.to_numpy_image()\n        image.append(m)\n    print(len(image))\n    return image\n\n#######################\nif 1:\n    print('*** start timer_function10()')\n    print(timeit.timeit('timer_function10()', globals=globals(), number=5))\n\n\n    print('*** start timer_function7()')\n    print(timeit.timeit('timer_function7()', globals=globals(), number=5))\n    #print(timeit.repeat('timer_function1()', globals=globals(), number=5, repeat=3))\n\n#######################\nif 1:\n    image1 = timer_function10()\n    image7 = timer_function7()\n\n    for m1,m7 in zip(image1,image7):\n        diff = m1.astype(np.float32)-m7.astype(np.float32)\n        print(diff.max(),diff.min(),diff.mean())\n#         image_show('m1',m1, resize=0.1)\n#         image_show('m7',m7, resize=0.1)\n#         image_show_norm('diff',diff, min=-1,max=1, resize=0.1)\n#       cv2.waitKey(0)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-10T08:46:44.3114Z","iopub.execute_input":"2022-12-10T08:46:44.31175Z","iopub.status.idle":"2022-12-10T08:50:45.163486Z","shell.execute_reply.started":"2022-12-10T08:46:44.31172Z","shell.execute_reply":"2022-12-10T08:50:45.162426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef timer_function1():\n    image = []\n    for f in dcm_file:\n        # https://github.com/tsangel/dicomsdl/blob/master/tutorials/timeit_test.ipynb\n        dicom0 = pydicom.dcmread(f)\n        image0 = dicom0.pixel_array\n        image.append(image0)\n    print(len(image))\n\ndef timer_function2():\n    image = []\n    for f in dcm_file:\n        # https://github.com/tsangel/dicomsdl/blob/master/tutorials/timeit_test.ipynb\n        dicom1 = dicomsdl.open(f)\n        image1 = dicom1.pixelData()\n        image.append(image1)\n    print(len(image))\n\n\n# print('*** start timer_function1()')\n# print(timeit.timeit('timer_function1()', globals=globals(), number=5))\n# #print(timeit.repeat('timer_function1()', globals=globals(), number=5, repeat=3))\n\n\n# print('*** start timer_function2()')\n# print(timeit.timeit('timer_function2()', globals=globals(), number=5))\n\n'''\n25\n*** start timer_function1()\n25\n25\n25\n25\n25\n57.333554964000086\n*** start timer_function2()\n25\n25\n25\n25\n25\n59.259146037999926\n\n'''","metadata":{"execution":{"iopub.status.busy":"2022-12-10T08:42:04.104279Z","iopub.status.idle":"2022-12-10T08:42:04.104997Z","shell.execute_reply.started":"2022-12-10T08:42:04.104739Z","shell.execute_reply":"2022-12-10T08:42:04.104764Z"},"trusted":true},"execution_count":null,"outputs":[]}]}