{"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":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-08T01:28:59.226328Z","iopub.execute_input":"2022-12-08T01:28:59.226791Z","iopub.status.idle":"2022-12-08T01:29:00.38596Z","shell.execute_reply.started":"2022-12-08T01:28:59.226698Z","shell.execute_reply":"2022-12-08T01:29:00.384805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:00.388265Z","iopub.execute_input":"2022-12-08T01:29:00.388727Z","iopub.status.idle":"2022-12-08T01:29:00.513427Z","shell.execute_reply.started":"2022-12-08T01:29:00.38869Z","shell.execute_reply":"2022-12-08T01:29:00.512527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:00.514795Z","iopub.execute_input":"2022-12-08T01:29:00.515422Z","iopub.status.idle":"2022-12-08T01:29:00.523366Z","shell.execute_reply.started":"2022-12-08T01:29:00.515385Z","shell.execute_reply":"2022-12-08T01:29:00.52245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:00.525861Z","iopub.execute_input":"2022-12-08T01:29:00.526524Z","iopub.status.idle":"2022-12-08T01:29:00.552261Z","shell.execute_reply.started":"2022-12-08T01:29:00.526487Z","shell.execute_reply":"2022-12-08T01:29:00.551368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install swifter\nimport swifter","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:00.553619Z","iopub.execute_input":"2022-12-08T01:29:00.554174Z","iopub.status.idle":"2022-12-08T01:29:19.91786Z","shell.execute_reply.started":"2022-12-08T01:29:00.55414Z","shell.execute_reply":"2022-12-08T01:29:19.916553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#swifter reference : https://towardsdatascience.com/do-you-use-apply-in-pandas-there-is-a-600x-faster-way-d2497facfa66","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:19.919675Z","iopub.execute_input":"2022-12-08T01:29:19.920157Z","iopub.status.idle":"2022-12-08T01:29:19.926254Z","shell.execute_reply.started":"2022-12-08T01:29:19.9201Z","shell.execute_reply":"2022-12-08T01:29:19.925351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dcm_finder(patient_id,image_id):\n    base_image_folder = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\n    return os.path.join(base_image_folder,str(patient_id),str(image_id)+\".dcm\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:19.927744Z","iopub.execute_input":"2022-12-08T01:29:19.928401Z","iopub.status.idle":"2022-12-08T01:29:19.942223Z","shell.execute_reply.started":"2022-12-08T01:29:19.928365Z","shell.execute_reply":"2022-12-08T01:29:19.941378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['dcm_path'] = df.swifter.apply(lambda x : dcm_finder(x['patient_id'],x['image_id']),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:19.94373Z","iopub.execute_input":"2022-12-08T01:29:19.944416Z","iopub.status.idle":"2022-12-08T01:29:21.410655Z","shell.execute_reply.started":"2022-12-08T01:29:19.944382Z","shell.execute_reply":"2022-12-08T01:29:21.409536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['dcm_path'][0]","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:21.412495Z","iopub.execute_input":"2022-12-08T01:29:21.412848Z","iopub.status.idle":"2022-12-08T01:29:21.420259Z","shell.execute_reply.started":"2022-12-08T01:29:21.412804Z","shell.execute_reply":"2022-12-08T01:29:21.418939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"read = pydicom.filereader.dcmread(df['dcm_path'][0])","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:29:21.423928Z","iopub.execute_input":"2022-12-08T01:29:21.424465Z","iopub.status.idle":"2022-12-08T01:29:21.489363Z","shell.execute_reply.started":"2022-12-08T01:29:21.424414Z","shell.execute_reply":"2022-12-08T01:29:21.488446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"read","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:32:38.229401Z","iopub.execute_input":"2022-12-08T01:32:38.230688Z","iopub.status.idle":"2022-12-08T01:32:38.240393Z","shell.execute_reply.started":"2022-12-08T01:32:38.230644Z","shell.execute_reply":"2022-12-08T01:32:38.239104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(read['WindowCenter'].value)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T01:32:44.651703Z","iopub.execute_input":"2022-12-08T01:32:44.652116Z","iopub.status.idle":"2022-12-08T01:32:44.659691Z","shell.execute_reply.started":"2022-12-08T01:32:44.652081Z","shell.execute_reply":"2022-12-08T01:32:44.658581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dcm_detailer(dcm):\n    read = pydicom.filereader.dcmread(dcm)\n    photo_inter = read['PhotometricInterpretation'].value\n    samples_per_pixel = read['SamplesPerPixel'].value\n    transfer_syntax_UID = read.file_meta['TransferSyntaxUID'].value\n    try:\n        window_width = list(read['WindowWidth'].value)\n    except:\n        window_width = read['WindowWidth'].value\n    try:\n        window_center = list(read['WindowCenter'].value)\n    except:\n        window_center = read['WindowCenter'].value\n    try:\n        intercept = read[('0028','1052')].value\n    except:\n        intercept = np.NaN\n    try:\n        slope = read[('0028','1053')].value\n    except:\n        slope = np.NaN\n    try:\n        number_of_windows = len(window_width)\n    except:\n        number_of_windows = 1\n    try:\n        body_part_thickness = read[('0018', '11a0')].value\n    except:\n        body_part_thickness = -1\n    try:\n        compression_force = read[('0018', '11a2')].value\n    except:\n        compression_force = -1\n    return photo_inter,samples_per_pixel,transfer_syntax_UID,window_width,window_center,intercept, \\\n            slope,number_of_windows,body_part_thickness,compression_force","metadata":{"execution":{"iopub.status.busy":"2022-12-04T08:58:31.062195Z","iopub.execute_input":"2022-12-04T08:58:31.062956Z","iopub.status.idle":"2022-12-04T08:58:31.071132Z","shell.execute_reply.started":"2022-12-04T08:58:31.06292Z","shell.execute_reply":"2022-12-04T08:58:31.069997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['photo_inter'],df['samples_per_pixel'],df['transfer_syntax_UID'], \\\ndf['window_width'],df['window_center'],df['intercept'],\\\ndf['slope'],df['number_of_windows'],df['body_part_thickness'],df['compression_force'] = zip(*df['dcm_path'].swifter.apply(dcm_detailer))","metadata":{"execution":{"iopub.status.busy":"2022-12-04T08:58:31.072713Z","iopub.execute_input":"2022-12-04T08:58:31.073147Z","iopub.status.idle":"2022-12-04T09:47:59.808752Z","shell.execute_reply.started":"2022-12-04T08:58:31.073115Z","shell.execute_reply":"2022-12-04T09:47:59.80634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['photo_inter'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T09:59:11.878244Z","iopub.execute_input":"2022-12-04T09:59:11.878767Z","iopub.status.idle":"2022-12-04T09:59:11.911282Z","shell.execute_reply.started":"2022-12-04T09:59:11.878719Z","shell.execute_reply":"2022-12-04T09:59:11.909948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-04T09:59:19.996696Z","iopub.execute_input":"2022-12-04T09:59:19.998029Z","iopub.status.idle":"2022-12-04T09:59:20.007413Z","shell.execute_reply.started":"2022-12-04T09:59:19.997955Z","shell.execute_reply":"2022-12-04T09:59:20.006039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['slope'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T09:59:37.297437Z","iopub.execute_input":"2022-12-04T09:59:37.297875Z","iopub.status.idle":"2022-12-04T09:59:37.310319Z","shell.execute_reply.started":"2022-12-04T09:59:37.297839Z","shell.execute_reply":"2022-12-04T09:59:37.309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['intercept'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T09:59:42.446922Z","iopub.execute_input":"2022-12-04T09:59:42.447401Z","iopub.status.idle":"2022-12-04T09:59:42.45835Z","shell.execute_reply.started":"2022-12-04T09:59:42.447361Z","shell.execute_reply":"2022-12-04T09:59:42.45707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['number_of_windows'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T09:59:51.961947Z","iopub.execute_input":"2022-12-04T09:59:51.962444Z","iopub.status.idle":"2022-12-04T09:59:51.972781Z","shell.execute_reply.started":"2022-12-04T09:59:51.962408Z","shell.execute_reply":"2022-12-04T09:59:51.971376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['samples_per_pixel'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T10:00:08.567622Z","iopub.execute_input":"2022-12-04T10:00:08.56895Z","iopub.status.idle":"2022-12-04T10:00:08.580739Z","shell.execute_reply.started":"2022-12-04T10:00:08.568888Z","shell.execute_reply":"2022-12-04T10:00:08.579204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['machine_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T10:00:26.360432Z","iopub.execute_input":"2022-12-04T10:00:26.360998Z","iopub.status.idle":"2022-12-04T10:00:26.372365Z","shell.execute_reply.started":"2022-12-04T10:00:26.360919Z","shell.execute_reply":"2022-12-04T10:00:26.370921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['transfer_syntax_UID'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T10:00:42.338056Z","iopub.execute_input":"2022-12-04T10:00:42.338494Z","iopub.status.idle":"2022-12-04T10:00:42.360586Z","shell.execute_reply.started":"2022-12-04T10:00:42.338457Z","shell.execute_reply":"2022-12-04T10:00:42.359062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('basic_df.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-04T10:10:00.872627Z","iopub.execute_input":"2022-12-04T10:10:00.873229Z","iopub.status.idle":"2022-12-04T10:10:00.939647Z","shell.execute_reply.started":"2022-12-04T10:10:00.87318Z","shell.execute_reply":"2022-12-04T10:10:00.938663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}