{"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 nb_black\n!pip install python-gdcm\n!pip install pylibjpeg pylibjpeg-libjpeg pydicom","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:22:29.460305Z","iopub.execute_input":"2022-12-04T03:22:29.460859Z","iopub.status.idle":"2022-12-04T03:23:08.922065Z","shell.execute_reply.started":"2022-12-04T03:22:29.46076Z","shell.execute_reply":"2022-12-04T03:23:08.920435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nfrom glob import glob\nimport pydicom as dicom\nfrom pydicom import dcmread\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:08.924442Z","iopub.execute_input":"2022-12-04T03:23:08.926292Z","iopub.status.idle":"2022-12-04T03:23:09.838881Z","shell.execute_reply.started":"2022-12-04T03:23:08.926246Z","shell.execute_reply":"2022-12-04T03:23:09.837569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = \"/kaggle/input/rsna-breast-cancer-detection/\"\nTRAIN_PATH = PATH + \"train_images\" \nTRAIN_META_DATA = PATH + \"train.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:09.840748Z","iopub.execute_input":"2022-12-04T03:23:09.841098Z","iopub.status.idle":"2022-12-04T03:23:09.847447Z","shell.execute_reply.started":"2022-12-04T03:23:09.841065Z","shell.execute_reply":"2022-12-04T03:23:09.84618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(TRAIN_META_DATA)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:09.850922Z","iopub.execute_input":"2022-12-04T03:23:09.851984Z","iopub.status.idle":"2022-12-04T03:23:10.020787Z","shell.execute_reply.started":"2022-12-04T03:23:09.851939Z","shell.execute_reply":"2022-12-04T03:23:10.01948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_train = train[train[\"cancer\"] == 1]","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:10.022102Z","iopub.execute_input":"2022-12-04T03:23:10.022472Z","iopub.status.idle":"2022-12-04T03:23:10.038413Z","shell.execute_reply.started":"2022-12-04T03:23:10.022428Z","shell.execute_reply":"2022-12-04T03:23:10.036894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_train","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:10.040396Z","iopub.execute_input":"2022-12-04T03:23:10.040815Z","iopub.status.idle":"2022-12-04T03:23:10.069024Z","shell.execute_reply.started":"2022-12-04T03:23:10.040778Z","shell.execute_reply":"2022-12-04T03:23:10.067852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_images = []\nfor i in range(10):\n    \n    cancer_images.append(TRAIN_PATH + \"/\" + str(cancer_train.iloc[i][\"patient_id\"]) + \\\n                         \"/\" + str(cancer_train.iloc[i][\"image_id\"]) + \".dcm\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:10.070314Z","iopub.execute_input":"2022-12-04T03:23:10.070677Z","iopub.status.idle":"2022-12-04T03:23:10.079224Z","shell.execute_reply.started":"2022-12-04T03:23:10.070644Z","shell.execute_reply":"2022-12-04T03:23:10.078222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"non_cancer_train = train[train[\"cancer\"] == 0]\nnon_cancer_images = []\nfor i in range(10):\n    \n    non_cancer_images.append(TRAIN_PATH + \"/\" + str(non_cancer_train.iloc[i][\"patient_id\"]) + \\\n                         \"/\" + str(non_cancer_train.iloc[i][\"image_id\"]) + \".dcm\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:10.080518Z","iopub.execute_input":"2022-12-04T03:23:10.081074Z","iopub.status.idle":"2022-12-04T03:23:10.101855Z","shell.execute_reply.started":"2022-12-04T03:23:10.081041Z","shell.execute_reply":"2022-12-04T03:23:10.100359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILES = glob(TRAIN_PATH + '/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:10.103517Z","iopub.execute_input":"2022-12-04T03:23:10.103855Z","iopub.status.idle":"2022-12-04T03:23:37.578221Z","shell.execute_reply.started":"2022-12-04T03:23:10.103825Z","shell.execute_reply":"2022-12-04T03:23:37.576962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILES[:10]","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:37.58362Z","iopub.execute_input":"2022-12-04T03:23:37.584368Z","iopub.status.idle":"2022-12-04T03:23:37.592344Z","shell.execute_reply.started":"2022-12-04T03:23:37.584295Z","shell.execute_reply":"2022-12-04T03:23:37.591026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = dicom.dcmread(FILES[0])","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:37.594602Z","iopub.execute_input":"2022-12-04T03:23:37.595068Z","iopub.status.idle":"2022-12-04T03:23:37.631869Z","shell.execute_reply.started":"2022-12-04T03:23:37.595025Z","shell.execute_reply":"2022-12-04T03:23:37.630361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:37.633772Z","iopub.execute_input":"2022-12-04T03:23:37.634251Z","iopub.status.idle":"2022-12-04T03:23:37.646314Z","shell.execute_reply.started":"2022-12-04T03:23:37.634203Z","shell.execute_reply":"2022-12-04T03:23:37.645478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = ds.pixel_array\nplt.figure(figsize=(15, 15))\nplt.imshow(arr,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:37.647529Z","iopub.execute_input":"2022-12-04T03:23:37.648619Z","iopub.status.idle":"2022-12-04T03:23:39.358239Z","shell.execute_reply.started":"2022-12-04T03:23:37.64856Z","shell.execute_reply":"2022-12-04T03:23:39.357091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_images[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:39.359543Z","iopub.execute_input":"2022-12-04T03:23:39.359947Z","iopub.status.idle":"2022-12-04T03:23:39.367399Z","shell.execute_reply.started":"2022-12-04T03:23:39.35991Z","shell.execute_reply":"2022-12-04T03:23:39.366563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds01 = dicom.dcmread(cancer_images[1])\nds01","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:39.369001Z","iopub.execute_input":"2022-12-04T03:23:39.369801Z","iopub.status.idle":"2022-12-04T03:23:39.47109Z","shell.execute_reply.started":"2022-12-04T03:23:39.369744Z","shell.execute_reply":"2022-12-04T03:23:39.470143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cancer Image 01","metadata":{}},{"cell_type":"code","source":"arr = ds01.pixel_array\nplt.figure(figsize=(15, 15))\nplt.imshow(arr,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:39.472648Z","iopub.execute_input":"2022-12-04T03:23:39.473497Z","iopub.status.idle":"2022-12-04T03:23:42.451035Z","shell.execute_reply.started":"2022-12-04T03:23:39.473451Z","shell.execute_reply":"2022-12-04T03:23:42.450135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cancer Image 02","metadata":{}},{"cell_type":"code","source":"ds01 = dicom.dcmread(cancer_images[3])\narr = ds01.pixel_array\nplt.figure(figsize=(15, 15))\nplt.imshow(arr,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:42.452514Z","iopub.execute_input":"2022-12-04T03:23:42.453111Z","iopub.status.idle":"2022-12-04T03:23:45.646308Z","shell.execute_reply.started":"2022-12-04T03:23:42.453072Z","shell.execute_reply":"2022-12-04T03:23:45.645185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Non Cancer Image 01","metadata":{}},{"cell_type":"code","source":"ds02 = dicom.dcmread(non_cancer_images[1])\narr = ds02.pixel_array\nplt.figure(figsize=(15, 15))\nplt.imshow(arr,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:45.647656Z","iopub.execute_input":"2022-12-04T03:23:45.648005Z","iopub.status.idle":"2022-12-04T03:23:51.647842Z","shell.execute_reply.started":"2022-12-04T03:23:45.647972Z","shell.execute_reply":"2022-12-04T03:23:51.646494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Non Cancer Image 02","metadata":{}},{"cell_type":"code","source":"ds02 = dicom.dcmread(non_cancer_images[2])\narr = ds02.pixel_array\nplt.figure(figsize=(15, 15))\nplt.imshow(arr,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:51.649357Z","iopub.execute_input":"2022-12-04T03:23:51.649763Z","iopub.status.idle":"2022-12-04T03:23:57.160611Z","shell.execute_reply.started":"2022-12-04T03:23:51.649728Z","shell.execute_reply":"2022-12-04T03:23:57.159322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PNG_PATH = '/kaggle/input/rsna-breast-cancer-512-pngs/'","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:57.162051Z","iopub.execute_input":"2022-12-04T03:23:57.162374Z","iopub.status.idle":"2022-12-04T03:23:57.167397Z","shell.execute_reply.started":"2022-12-04T03:23:57.162345Z","shell.execute_reply":"2022-12-04T03:23:57.166248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILES = glob(PNG_PATH + '*')","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:57.169314Z","iopub.execute_input":"2022-12-04T03:23:57.170501Z","iopub.status.idle":"2022-12-04T03:23:57.895877Z","shell.execute_reply.started":"2022-12-04T03:23:57.170463Z","shell.execute_reply":"2022-12-04T03:23:57.894749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILES[:5]","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:57.89734Z","iopub.execute_input":"2022-12-04T03:23:57.898365Z","iopub.status.idle":"2022-12-04T03:23:57.905991Z","shell.execute_reply.started":"2022-12-04T03:23:57.898326Z","shell.execute_reply":"2022-12-04T03:23:57.904515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = []\npatient_id = []\nfiles_list = []\nfor f in FILES:\n    x = f.split(\"/\")[-1]\n    x1 = x.split('_')[-1].split(\".\")[0]\n    x0 = x.split('_')[0] \n    image_id.append(int(x1))\n    patient_id.append(int(x0))\n    files_list.append(x) ","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:57.907624Z","iopub.execute_input":"2022-12-04T03:23:57.90854Z","iopub.status.idle":"2022-12-04T03:23:58.028564Z","shell.execute_reply.started":"2022-12-04T03:23:57.908493Z","shell.execute_reply":"2022-12-04T03:23:58.027499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_png = pd.DataFrame({\"patient_id\" : (patient_id),\"image_id\" :(image_id), 'file_name':files_list})","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.031472Z","iopub.execute_input":"2022-12-04T03:23:58.032617Z","iopub.status.idle":"2022-12-04T03:23:58.074263Z","shell.execute_reply.started":"2022-12-04T03:23:58.032577Z","shell.execute_reply":"2022-12-04T03:23:58.073159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_png.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.07555Z","iopub.execute_input":"2022-12-04T03:23:58.07588Z","iopub.status.idle":"2022-12-04T03:23:58.087388Z","shell.execute_reply.started":"2022-12-04T03:23:58.07585Z","shell.execute_reply":"2022-12-04T03:23:58.086126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.088756Z","iopub.execute_input":"2022-12-04T03:23:58.089111Z","iopub.status.idle":"2022-12-04T03:23:58.101159Z","shell.execute_reply.started":"2022-12-04T03:23:58.089079Z","shell.execute_reply":"2022-12-04T03:23:58.099805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_png = pd.merge(train, df_png, how=\"inner\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.102717Z","iopub.execute_input":"2022-12-04T03:23:58.103341Z","iopub.status.idle":"2022-12-04T03:23:58.163196Z","shell.execute_reply.started":"2022-12-04T03:23:58.10329Z","shell.execute_reply":"2022-12-04T03:23:58.162213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train.columns), len(train_png.columns)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.169175Z","iopub.execute_input":"2022-12-04T03:23:58.169555Z","iopub.status.idle":"2022-12-04T03:23:58.176747Z","shell.execute_reply.started":"2022-12-04T03:23:58.169522Z","shell.execute_reply":"2022-12-04T03:23:58.175608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_png.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.178282Z","iopub.execute_input":"2022-12-04T03:23:58.178768Z","iopub.status.idle":"2022-12-04T03:23:58.201491Z","shell.execute_reply.started":"2022-12-04T03:23:58.178725Z","shell.execute_reply":"2022-12-04T03:23:58.200183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib. image as mpimg\nimg = mpimg.imread(FILES[0])\nplt.figure(figsize=(15, 15))\nplt.imshow(img,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.202913Z","iopub.execute_input":"2022-12-04T03:23:58.203245Z","iopub.status.idle":"2022-12-04T03:23:58.744865Z","shell.execute_reply.started":"2022-12-04T03:23:58.203216Z","shell.execute_reply":"2022-12-04T03:23:58.743367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_png.to_csv(\"train_labels.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T03:23:58.746746Z","iopub.execute_input":"2022-12-04T03:23:58.747117Z","iopub.status.idle":"2022-12-04T03:23:59.004467Z","shell.execute_reply.started":"2022-12-04T03:23:58.747082Z","shell.execute_reply":"2022-12-04T03:23:59.00323Z"},"trusted":true},"execution_count":null,"outputs":[]}]}