{"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 cv2 as cv\nimport skimage\nfrom skimage.filters import sobel\nfrom skimage import segmentation\nfrom skimage.color import label2rgb\nfrom skimage.color import rgb2hed, hed2rgb\nfrom skimage.exposure import rescale_intensity\nimport tifffile as tifi\nfrom sklearn.preprocessing import StandardScaler\nfrom skimage.measure import regionprops, regionprops_table\nfrom scipy import ndimage as ndi\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:02:07.259773Z","iopub.execute_input":"2022-11-29T10:02:07.260779Z","iopub.status.idle":"2022-11-29T10:02:08.212302Z","shell.execute_reply.started":"2022-11-29T10:02:07.260745Z","shell.execute_reply":"2022-11-29T10:02:08.210816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\nsample_sub = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv\")\ntrain = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:04:16.240298Z","iopub.execute_input":"2022-11-29T10:04:16.240715Z","iopub.status.idle":"2022-11-29T10:04:16.413314Z","shell.execute_reply.started":"2022-11-29T10:04:16.240681Z","shell.execute_reply":"2022-11-29T10:04:16.412322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:04:51.368966Z","iopub.execute_input":"2022-11-29T10:04:51.369338Z","iopub.status.idle":"2022-11-29T10:04:51.395028Z","shell.execute_reply.started":"2022-11-29T10:04:51.369306Z","shell.execute_reply":"2022-11-29T10:04:51.393878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:04:53.466665Z","iopub.execute_input":"2022-11-29T10:04:53.467127Z","iopub.status.idle":"2022-11-29T10:04:53.497853Z","shell.execute_reply.started":"2022-11-29T10:04:53.467089Z","shell.execute_reply":"2022-11-29T10:04:53.496787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:04:56.356055Z","iopub.execute_input":"2022-11-29T10:04:56.356432Z","iopub.status.idle":"2022-11-29T10:04:56.369053Z","shell.execute_reply.started":"2022-11-29T10:04:56.356399Z","shell.execute_reply":"2022-11-29T10:04:56.367661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:06:02.391626Z","iopub.execute_input":"2022-11-29T10:06:02.391997Z","iopub.status.idle":"2022-11-29T10:06:02.412125Z","shell.execute_reply.started":"2022-11-29T10:06:02.391965Z","shell.execute_reply":"2022-11-29T10:06:02.410857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:06:11.463311Z","iopub.execute_input":"2022-11-29T10:06:11.463742Z","iopub.status.idle":"2022-11-29T10:06:11.498452Z","shell.execute_reply.started":"2022-11-29T10:06:11.463706Z","shell.execute_reply":"2022-11-29T10:06:11.49729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:07:04.921498Z","iopub.execute_input":"2022-11-29T10:07:04.922001Z","iopub.status.idle":"2022-11-29T10:07:04.940001Z","shell.execute_reply.started":"2022-11-29T10:07:04.921959Z","shell.execute_reply":"2022-11-29T10:07:04.938882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:06:32.265731Z","iopub.execute_input":"2022-11-29T10:06:32.266109Z","iopub.status.idle":"2022-11-29T10:06:33.672423Z","shell.execute_reply.started":"2022-11-29T10:06:32.266078Z","shell.execute_reply":"2022-11-29T10:06:33.671407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['BIRADS','density'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:15:45.146239Z","iopub.execute_input":"2022-11-29T10:15:45.146626Z","iopub.status.idle":"2022-11-29T10:15:45.156431Z","shell.execute_reply.started":"2022-11-29T10:15:45.14658Z","shell.execute_reply":"2022-11-29T10:15:45.155247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['age']=train['age'].fillna(train['age'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:17:12.364711Z","iopub.execute_input":"2022-11-29T10:17:12.365122Z","iopub.status.idle":"2022-11-29T10:17:12.373704Z","shell.execute_reply.started":"2022-11-29T10:17:12.365088Z","shell.execute_reply":"2022-11-29T10:17:12.372128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:17:14.832531Z","iopub.execute_input":"2022-11-29T10:17:14.833268Z","iopub.status.idle":"2022-11-29T10:17:15.79396Z","shell.execute_reply.started":"2022-11-29T10:17:14.833233Z","shell.execute_reply":"2022-11-29T10:17:15.79275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(test.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:06:45.252035Z","iopub.execute_input":"2022-11-29T10:06:45.252431Z","iopub.status.idle":"2022-11-29T10:06:45.448821Z","shell.execute_reply.started":"2022-11-29T10:06:45.252399Z","shell.execute_reply":"2022-11-29T10:06:45.447915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:07:51.786719Z","iopub.execute_input":"2022-11-29T10:07:51.787096Z","iopub.status.idle":"2022-11-29T10:07:51.793302Z","shell.execute_reply.started":"2022-11-29T10:07:51.787065Z","shell.execute_reply":"2022-11-29T10:07:51.791918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['cancer'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:10:26.047774Z","iopub.execute_input":"2022-11-29T10:10:26.04818Z","iopub.status.idle":"2022-11-29T10:10:26.059767Z","shell.execute_reply.started":"2022-11-29T10:10:26.048147Z","shell.execute_reply":"2022-11-29T10:10:26.058553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['0', '1']\ncounts = [547, 207]\nexplode = (0.1, 0)\nplt.pie(counts, explode=explode, labels=labels, autopct='%1.1f%%',\n        shadow=True, startangle=60) \nplt.legend()\nplt.title('Cancer')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:11:24.551151Z","iopub.execute_input":"2022-11-29T10:11:24.551532Z","iopub.status.idle":"2022-11-29T10:11:24.827578Z","shell.execute_reply.started":"2022-11-29T10:11:24.551498Z","shell.execute_reply":"2022-11-29T10:11:24.825633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:19:12.955247Z","iopub.execute_input":"2022-11-29T10:19:12.956219Z","iopub.status.idle":"2022-11-29T10:19:12.989997Z","shell.execute_reply.started":"2022-11-29T10:19:12.95617Z","shell.execute_reply":"2022-11-29T10:19:12.98861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Analysis**","metadata":{}},{"cell_type":"code","source":"sns.displot(data=train, x=\"age\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:29:18.770088Z","iopub.execute_input":"2022-11-29T10:29:18.770451Z","iopub.status.idle":"2022-11-29T10:29:19.191363Z","shell.execute_reply.started":"2022-11-29T10:29:18.77042Z","shell.execute_reply":"2022-11-29T10:29:19.190241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.cancer.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:29:41.716674Z","iopub.execute_input":"2022-11-29T10:29:41.717026Z","iopub.status.idle":"2022-11-29T10:29:41.725345Z","shell.execute_reply.started":"2022-11-29T10:29:41.716997Z","shell.execute_reply":"2022-11-29T10:29:41.724302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train, x='cancer')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:30:10.617223Z","iopub.execute_input":"2022-11-29T10:30:10.617594Z","iopub.status.idle":"2022-11-29T10:30:10.787934Z","shell.execute_reply.started":"2022-11-29T10:30:10.617544Z","shell.execute_reply":"2022-11-29T10:30:10.786836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.laterality.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:31:16.159882Z","iopub.execute_input":"2022-11-29T10:31:16.160542Z","iopub.status.idle":"2022-11-29T10:31:16.17491Z","shell.execute_reply.started":"2022-11-29T10:31:16.160501Z","shell.execute_reply":"2022-11-29T10:31:16.173164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.view.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:31:39.035813Z","iopub.execute_input":"2022-11-29T10:31:39.036467Z","iopub.status.idle":"2022-11-29T10:31:39.0585Z","shell.execute_reply.started":"2022-11-29T10:31:39.036422Z","shell.execute_reply":"2022-11-29T10:31:39.057439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport os\nfrom pathlib import Path\nimport glob\ndef show_images_for_patient(patient_id):\n    patient_dir = os.path.join('../input/rsna-breast-cancer-detection/train_images', str(patient_id))\n    num_images = len(glob.glob(f\"{patient_dir}/*\"))\n    print(f\"Number of images for patient: {num_images}\")\n    fig, axs = plt.subplots(2, 2, figsize=(24,15))\n    axs = axs.flatten()\n    for i, img_path in enumerate(list(Path(patient_dir).iterdir())):\n        ds = pydicom.dcmread(img_path)\n        axs[i].imshow(rescale_img_to_hu(ds), cmap=\"bone\")","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:25:09.396431Z","iopub.execute_input":"2022-11-29T10:25:09.397137Z","iopub.status.idle":"2022-11-29T10:25:09.518477Z","shell.execute_reply.started":"2022-11-29T10:25:09.397081Z","shell.execute_reply":"2022-11-29T10:25:09.51752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = '../input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm'\npydicom.dcmread(example)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:25:59.677454Z","iopub.execute_input":"2022-11-29T10:25:59.678451Z","iopub.status.idle":"2022-11-29T10:25:59.741306Z","shell.execute_reply.started":"2022-11-29T10:25:59.678411Z","shell.execute_reply":"2022-11-29T10:25:59.740139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_img_to_hu(dcm_ds):\n    return dcm_ds.pixel_array * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:26:25.14594Z","iopub.execute_input":"2022-11-29T10:26:25.146323Z","iopub.status.idle":"2022-11-29T10:26:25.151482Z","shell.execute_reply.started":"2022-11-29T10:26:25.146291Z","shell.execute_reply":"2022-11-29T10:26:25.150217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images_for_patient(10006)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:26:26.758701Z","iopub.execute_input":"2022-11-29T10:26:26.759076Z","iopub.status.idle":"2022-11-29T10:26:45.194721Z","shell.execute_reply.started":"2022-11-29T10:26:26.759045Z","shell.execute_reply":"2022-11-29T10:26:45.193482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape[0], train.patient_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:40:40.095721Z","iopub.execute_input":"2022-11-29T10:40:40.096106Z","iopub.status.idle":"2022-11-29T10:40:40.105548Z","shell.execute_reply.started":"2022-11-29T10:40:40.096074Z","shell.execute_reply":"2022-11-29T10:40:40.104625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(data={'prediction_id': test['prediction_id'], 'cancer': np.random.rand(test.shape[0])}).drop_duplicates(subset='prediction_id')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:41:07.787283Z","iopub.execute_input":"2022-11-29T10:41:07.787697Z","iopub.status.idle":"2022-11-29T10:41:07.803135Z","shell.execute_reply.started":"2022-11-29T10:41:07.787662Z","shell.execute_reply":"2022-11-29T10:41:07.801845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T10:41:40.352428Z","iopub.execute_input":"2022-11-29T10:41:40.353124Z","iopub.status.idle":"2022-11-29T10:41:40.364049Z","shell.execute_reply.started":"2022-11-29T10:41:40.35309Z","shell.execute_reply":"2022-11-29T10:41:40.362727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}