{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom \nfrom pathlib import Path\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport cv2\nROOT_PATH=\"/kaggle/input/rsna-pneumonia-detection-challenge\"\nTEST_FOLDER=\"stage_2_test_images/\"\nTRAIN_FOLDER=\"stage_2_train_images/\"\nSAVE_PATH=Path(\"/kaggle/working/\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-09T09:26:02.380502Z","iopub.execute_input":"2024-02-09T09:26:02.380966Z","iopub.status.idle":"2024-02-09T09:26:02.389938Z","shell.execute_reply.started":"2024-02-09T09:26:02.380935Z","shell.execute_reply":"2024-02-09T09:26:02.388520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detailed_class_csv=pd.read_csv(os.path.join(ROOT_PATH,'stage_2_detailed_class_info.csv'))\ndetailed_class_csv.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:26:13.197634Z","iopub.execute_input":"2024-02-09T09:26:13.199042Z","iopub.status.idle":"2024-02-09T09:26:13.252149Z","shell.execute_reply.started":"2024-02-09T09:26:13.198985Z","shell.execute_reply":"2024-02-09T09:26:13.250890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=pd.read_csv(os.path.join(ROOT_PATH,'stage_2_train_labels.csv'))\nlabels.drop_duplicates(inplace=True)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:26:23.653462Z","iopub.execute_input":"2024-02-09T09:26:23.653896Z","iopub.status.idle":"2024-02-09T09:26:23.731363Z","shell.execute_reply.started":"2024-02-09T09:26:23.653862Z","shell.execute_reply":"2024-02-09T09:26:23.730096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(3,3,figsize=(10,8))\nlabel_dict={0:'No Pneumonia',1:'Pneumonia'}\nc=0\nfor i in range(3):\n    for j in range(3):\n        patient_id=labels.iloc[c]['patientId']\n        dcm_path=os.path.join(ROOT_PATH,TRAIN_FOLDER)+patient_id+'.dcm'\n        dcm_file=pydicom.read_file(dcm_path).pixel_array\n        ax[i][j].imshow(dcm_file,cmap='gray')\n        ax[i][j].set_title(label_dict[labels.iloc[c]['Target']])\n        ax[i][j].axis('off')\n        c+=1\n","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:26:37.833415Z","iopub.execute_input":"2024-02-09T09:26:37.834127Z","iopub.status.idle":"2024-02-09T09:26:39.757569Z","shell.execute_reply.started":"2024-02-09T09:26:37.834063Z","shell.execute_reply":"2024-02-09T09:26:39.756341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sums,sums_sq=0,0\nfor x ,pid in enumerate(tqdm(labels.patientId)):\n    dcm_path=os.path.join(ROOT_PATH,TRAIN_FOLDER)+pid+'.dcm'\n    dcm_file=pydicom.read_file(dcm_path).pixel_array/255\n    dcm_array=cv2.resize(dcm_file,(224,224)).astype(np.float16)\n    \n    label=labels.Target.iloc[x]\n    train_or_val='train' if x<28000 else 'val'\n    current_save_path=SAVE_PATH/train_or_val/str(label)\n    current_save_path.mkdir(parents=True,exist_ok=True)\n    np.save(current_save_path/pid,dcm_array)\n    \n    normalizer=224*224\n    if train_or_val=='train':\n        sums+=np.sum(dcm_array)/normalizer\n        sums_sq+=(dcm_array**2).sum() / normalizer","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:15.799031Z","iopub.execute_input":"2024-02-09T09:29:15.799525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean=sums/28000\nstd=np.sqrt((sums_sq/28000)-mean**2)\nprint(mean,std)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:28:46.529145Z","iopub.execute_input":"2024-02-09T09:28:46.530072Z","iopub.status.idle":"2024-02-09T09:28:46.536897Z","shell.execute_reply.started":"2024-02-09T09:28:46.530033Z","shell.execute_reply":"2024-02-09T09:28:46.535609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}