{"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":"code","source":"!pip install python-gdcm","metadata":{"_uuid":"47890ced-2d6c-45c2-be51-71113a32db3d","_cell_guid":"92fe20a6-b7c3-4ac3-abd8-e9249cc9e727","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:04.824347Z","iopub.execute_input":"2022-12-10T08:36:04.824851Z","iopub.status.idle":"2022-12-10T08:36:21.190537Z","shell.execute_reply.started":"2022-12-10T08:36:04.824747Z","shell.execute_reply":"2022-12-10T08:36:21.18946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\n\nfrom pathlib import Path\nfrom PIL import Image\nfrom pydicom import dcmread\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_modality_lut, apply_voi_lut\n\nimport matplotlib.pyplot as plt\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport multiprocessing\nfrom joblib import Parallel, delayed\n\nimport torch\n\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"_uuid":"72848245-04e7-4bb2-a916-338f0d87e59c","_cell_guid":"8e4cbecd-45bc-4a6e-82e6-c5845197ccfb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:40.737792Z","iopub.execute_input":"2022-12-10T08:36:40.738608Z","iopub.status.idle":"2022-12-10T08:36:40.745931Z","shell.execute_reply.started":"2022-12-10T08:36:40.738559Z","shell.execute_reply":"2022-12-10T08:36:40.744853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAVE_PATH = Path('/kaggle/working/')","metadata":{"_uuid":"a1891a78-7ab9-4a1e-8756-ec7c4e2054b2","_cell_guid":"40ff5573-b20f-4467-968f-a0887760fdc1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.019992Z","iopub.execute_input":"2022-12-10T08:36:25.020579Z","iopub.status.idle":"2022-12-10T08:36:25.026299Z","shell.execute_reply.started":"2022-12-10T08:36:25.020542Z","shell.execute_reply":"2022-12-10T08:36:25.025235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = Path('/kaggle/input/rsna-breast-cancer-detection')\nTRAIN_DIR = ROOT_DIR / 'train_images'\nTEST_DIR = ROOT_DIR / 'test_images'\nTRAIN_CSV = ROOT_DIR /'train.csv'\nTEST_CSV = ROOT_DIR /'test.csv'","metadata":{"_uuid":"6498abd7-b8fa-4fc6-93b4-20fafb72884c","_cell_guid":"79c3e2f3-5b0c-475d-b5d1-5342b460a3ed","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.028522Z","iopub.execute_input":"2022-12-10T08:36:25.028985Z","iopub.status.idle":"2022-12-10T08:36:25.039872Z","shell.execute_reply.started":"2022-12-10T08:36:25.028953Z","shell.execute_reply":"2022-12-10T08:36:25.038709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(TRAIN_CSV)\ntest_df = pd.read_csv(TEST_CSV)","metadata":{"_uuid":"ad6ca009-177e-44b0-b6a0-967464c471c2","_cell_guid":"59e1134c-978a-45f0-80c1-5d727c451747","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.041175Z","iopub.execute_input":"2022-12-10T08:36:25.041525Z","iopub.status.idle":"2022-12-10T08:36:25.177763Z","shell.execute_reply.started":"2022-12-10T08:36:25.041477Z","shell.execute_reply":"2022-12-10T08:36:25.176473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"_uuid":"0b8f2fa6-4d9d-4ba4-b101-d484eb15dfc1","_cell_guid":"4706699d-943f-4b8e-9815-8bdc974f4cab","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.179536Z","iopub.execute_input":"2022-12-10T08:36:25.1799Z","iopub.status.idle":"2022-12-10T08:36:25.206783Z","shell.execute_reply.started":"2022-12-10T08:36:25.179867Z","shell.execute_reply":"2022-12-10T08:36:25.205544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_path(root_study_path, dicom_image, image_ext = '.dcm'):\n    image = dicom_image+image_ext\n    for root,_,files in os.walk(root_study_path):\n        if image in files:\n            return Path(root)/Path(image)","metadata":{"_uuid":"8cf782cd-57ca-4dc8-9f20-c1422c622999","_cell_guid":"e7b5c5c5-d98b-43d0-9410-c6ef1eeacceb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.208254Z","iopub.execute_input":"2022-12-10T08:36:25.209144Z","iopub.status.idle":"2022-12-10T08:36:25.2151Z","shell.execute_reply.started":"2022-12-10T08:36:25.209107Z","shell.execute_reply":"2022-12-10T08:36:25.213938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True, bits = (2**16 - 1)):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * bits).astype(np.uint16)\n        \n    return data","metadata":{"_uuid":"62db46ee-bea5-4dc8-98cc-08e7b514ee70","_cell_guid":"609f03c2-8c2e-4095-84cf-82d89b057b5a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.216731Z","iopub.execute_input":"2022-12-10T08:36:25.217339Z","iopub.status.idle":"2022-12-10T08:36:25.228062Z","shell.execute_reply.started":"2022-12-10T08:36:25.217308Z","shell.execute_reply":"2022-12-10T08:36:25.226623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(image,ax = None, title = None, figsize = (5,5), cmap = 'gray'):\n    '''shows a single image'''\n    if ax is None: _, ax = plt.subplots(figsize = figsize)\n    if title is not None : ax.set_title(title)\n    \n    ax.imshow(image,cmap = cmap)\n    ax.axis('off')","metadata":{"_uuid":"8a4fb5fa-e7b4-406d-baf4-7f2ee6fccca0","_cell_guid":"9e870d26-9999-4f83-8bed-7d7b2fa9913d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.229548Z","iopub.execute_input":"2022-12-10T08:36:25.229863Z","iopub.status.idle":"2022-12-10T08:36:25.239037Z","shell.execute_reply.started":"2022-12-10T08:36:25.229836Z","shell.execute_reply":"2022-12-10T08:36:25.238015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose(\n    [\n        A.ToFloat(max_value=65535.0),\n        A.SmallestMaxSize(max_size = 1024, interpolation =  1,  p=1), # initerpolation = 4 : LANCZOS\n        A.FromFloat(max_value=65535.0)\n    ], \n    )","metadata":{"_uuid":"3d1f4a51-96de-47be-9528-2cea919b4274","_cell_guid":"660d2d27-6917-4396-a280-a798bdae5abe","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.241913Z","iopub.execute_input":"2022-12-10T08:36:25.242236Z","iopub.status.idle":"2022-12-10T08:36:25.254225Z","shell.execute_reply.started":"2022-12-10T08:36:25.242207Z","shell.execute_reply":"2022-12-10T08:36:25.253242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = get_image_path(TRAIN_DIR, '1967300488')\nxray_image = read_xray(image_path)\n\nshow_image(xray_image)","metadata":{"_uuid":"9b399dd2-0885-48eb-b3f7-8b03373c83c0","_cell_guid":"70e64fb9-1e26-4c47-a612-02ebb5286ec1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:36:25.255857Z","iopub.execute_input":"2022-12-10T08:36:25.256378Z","iopub.status.idle":"2022-12-10T08:36:40.696889Z","shell.execute_reply.started":"2022-12-10T08:36:25.256346Z","shell.execute_reply":"2022-12-10T08:36:40.695551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convertDicomPng(idx):\n    image_path = get_image_path(image_root_dir, str(df.image_id.iloc[idx]))\n    xray_image = read_xray(image_path)\n\n    transformed_dict = transform(image = xray_image)\n    transformed_image = transformed_dict['image']\n    #show_image(xray_image)\n    #show_image(transformed_image)\n    t_image = Image.fromarray(transformed_image,  'I;16')\n\n    img_parent = SAVE_PATH / (set_type +'/' + str(df.patient_id.iloc[idx]))\n    if not os.path.exists(img_parent):\n        try:\n            os.makedirs(img_parent,)\n        except:\n            print(\"Directory already exists\")\n            \n    full_path =  img_parent / Path(str(df.image_id.iloc[idx])+'.png')\n    t_image.save(full_path)\n    \n    print(\"ID: \"+str(idx)+\" Processing Image ID: \"+ str(df.image_id.iloc[idx]))","metadata":{"_uuid":"84a3bf73-3ff7-4698-8e4d-cf18752894e6","_cell_guid":"d8d1e6a7-6dc6-4dbc-878a-2ea61125c910","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:38:43.04556Z","iopub.execute_input":"2022-12-10T08:38:43.045994Z","iopub.status.idle":"2022-12-10T08:38:43.056605Z","shell.execute_reply.started":"2022-12-10T08:38:43.045958Z","shell.execute_reply":"2022-12-10T08:38:43.055152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"int(len(train_df) / 3),int(len(train_df) / 3)+int(len(train_df) / 3), len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T08:38:43.321334Z","iopub.execute_input":"2022-12-10T08:38:43.321876Z","iopub.status.idle":"2022-12-10T08:38:43.331721Z","shell.execute_reply.started":"2022-12-10T08:38:43.321834Z","shell.execute_reply":"2022-12-10T08:38:43.330427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_cores = multiprocessing.cpu_count()","metadata":{"execution":{"iopub.status.busy":"2022-12-10T08:38:43.633075Z","iopub.execute_input":"2022-12-10T08:38:43.634163Z","iopub.status.idle":"2022-12-10T08:38:43.639022Z","shell.execute_reply.started":"2022-12-10T08:38:43.634103Z","shell.execute_reply":"2022-12-10T08:38:43.637872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_df\nimage_root_dir = TRAIN_DIR\nset_type = 'train_images'\n\n#args = [idx for idx in range(int(len(train_df) / 3), ),int(len(train_df) / 3)+int(len(train_df) / 3),)]\n\nParallel(n_jobs=num_cores)(delayed(convertDicomPng)(idx) for idx in range(int(len(train_df) / 3)+int(len(train_df) / 3), len(train_df)))\n","metadata":{"_uuid":"16b03558-1712-45fd-acde-383795196f4a","_cell_guid":"f8ae61ba-002f-4f22-92bd-826207daa978","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T08:40:38.355952Z","iopub.execute_input":"2022-12-10T08:40:38.35639Z","iopub.status.idle":"2022-12-10T08:41:20.676631Z","shell.execute_reply.started":"2022-12-10T08:40:38.356354Z","shell.execute_reply":"2022-12-10T08:41:20.675031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = test_df\nimage_root_dir = TEST_DIR\nset_type = 'test_images'\n\n#args = [idx for idx in range(len(train_df))]\n#with multiprocessing.pool.Pool(32) as pool:\n    # call the same function with different data in parallel\n #   for result in pool.imap(convertDicomPng, args):\n  #      print(\"Image Processed: \"+ str(result))","metadata":{"_uuid":"273c1f92-06ce-41a2-8700-9e79d7a9a4e1","_cell_guid":"cebd8b43-5024-43af-9bef-267ceb961534","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}