{"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":"markdown","source":"# Train and Test DICOM Image Type Classify\nTransferSyntaxUID and PhotometricInterpretation of train and test data<br/> \nhttps://www.kaggle.com/stpeteishii/mammography-dicom-to-png-4-1","metadata":{}},{"cell_type":"code","source":"#https://www.kaggle.com/competitions/rsna-breast-cancer-detection/discussion/370045\n!pip install -U pylibjpeg pylibjpeg-openjpeg pylibjpeg-libjpeg pydicom python-gdcm","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-29T14:17:58.903374Z","iopub.execute_input":"2023-01-29T14:17:58.9045Z","iopub.status.idle":"2023-01-29T14:18:15.131822Z","shell.execute_reply.started":"2023-01-29T14:17:58.904374Z","shell.execute_reply":"2023-01-29T14:18:15.130765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pylibjpeg\nfrom libjpeg import decode\nimport pydicom as dicom\nfrom pydicom import dcmread","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":7.184525,"end_time":"2022-10-02T17:51:17.649864","exception":false,"start_time":"2022-10-02T17:51:10.465339","status":"completed"},"tags":[],"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-29T14:18:15.134194Z","iopub.execute_input":"2023-01-29T14:18:15.134624Z","iopub.status.idle":"2023-01-29T14:18:16.127237Z","shell.execute_reply.started":"2023-01-29T14:18:15.134589Z","shell.execute_reply":"2023-01-29T14:18:16.126097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=[]\nfiles=[]\nids=[]\ntraintest=[]\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if filename[-4:]!='.csv':\n            paths+=[(os.path.join(dirname, filename))]\n            files+=[filename[:-4]]\n            ids+=[dirname.split('/')[-1]]\n            traintest+=[dirname.split('/')[-2]]","metadata":{"papermill":{"duration":0.071711,"end_time":"2022-10-02T17:51:17.731873","exception":false,"start_time":"2022-10-02T17:51:17.660162","status":"completed"},"tags":[],"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-29T14:18:16.129006Z","iopub.execute_input":"2023-01-29T14:18:16.129392Z","iopub.status.idle":"2023-01-29T14:18:46.175512Z","shell.execute_reply.started":"2023-01-29T14:18:16.12936Z","shell.execute_reply":"2023-01-29T14:18:46.174643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0=pd.DataFrame(columns=['path','file','id','train/test'])\ndata0['path']=paths\ndata0['file']=files\ndata0['id']=ids\ndata0['train/test']=traintest\ndata0['newfile']=data0['id']+'_'+data0['file']+'.png'\ndisplay(data0)","metadata":{"papermill":{"duration":0.059019,"end_time":"2022-10-02T17:51:17.799988","exception":false,"start_time":"2022-10-02T17:51:17.740969","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-29T14:18:46.177767Z","iopub.execute_input":"2023-01-29T14:18:46.178366Z","iopub.status.idle":"2023-01-29T14:18:46.276625Z","shell.execute_reply.started":"2023-01-29T14:18:46.178333Z","shell.execute_reply":"2023-01-29T14:18:46.275722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIP=[]\nTSU=[]\nfor i,path in enumerate(paths):\n    if i%5000==0:\n        print(i)\n    dcm=dicom.dcmread(path,force=True)\n    pip=dcm.PhotometricInterpretation \n    tsu=dcm.file_meta.TransferSyntaxUID\n    PIP+=[pip]\n    TSU+=[tsu]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T14:18:46.277813Z","iopub.execute_input":"2023-01-29T14:18:46.278127Z","iopub.status.idle":"2023-01-29T14:18:46.979983Z","shell.execute_reply.started":"2023-01-29T14:18:46.278099Z","shell.execute_reply":"2023-01-29T14:18:46.97906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0['pip']=PIP\ndata0['tsu']=TSU\ndisplay(data0)\ndata0.to_csv('mammo.csv',index=False)","metadata":{"papermill":{"duration":0.488204,"end_time":"2022-10-02T17:51:36.326057","exception":false,"start_time":"2022-10-02T17:51:35.837853","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-29T14:18:46.982731Z","iopub.execute_input":"2023-01-29T14:18:46.983086Z","iopub.status.idle":"2023-01-29T14:18:47.242763Z","shell.execute_reply.started":"2023-01-29T14:18:46.983054Z","shell.execute_reply":"2023-01-29T14:18:47.241833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols=data0.columns.tolist()\nprint(cols)\nprint(len(cols))","metadata":{"papermill":{"duration":0.487082,"end_time":"2022-10-02T17:51:37.297263","exception":false,"start_time":"2022-10-02T17:51:36.810181","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-29T14:18:47.244216Z","iopub.execute_input":"2023-01-29T14:18:47.245082Z","iopub.status.idle":"2023-01-29T14:18:47.250893Z","shell.execute_reply.started":"2023-01-29T14:18:47.245048Z","shell.execute_reply":"2023-01-29T14:18:47.249891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(7,1,figsize=(16,84))\nfor i in tqdm(range(7)):\n    sns.histplot(data0[cols[i]], label=cols[i], ax=ax[i], color='C1', bins=40)\n    ax[i].legend()\n    ax[i].grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T14:28:51.484282Z","iopub.execute_input":"2023-01-29T14:28:51.485255Z","iopub.status.idle":"2023-01-29T14:28:59.304185Z","shell.execute_reply.started":"2023-01-29T14:28:51.485216Z","shell.execute_reply":"2023-01-29T14:28:59.303018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=data0[data0['train/test']=='train_images']\ntrain['count']=1\ntrain2=train.groupby(['pip','tsu'],as_index=False)['count'].sum()\ntrain3=train2.set_index(['pip','tsu']).unstack()\ndisplay(train3)\n\nplt.figure(figsize=(6,6))\nsns.heatmap(train3, cmap='YlGnBu',annot=True)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=data0[data0['train/test']=='test_images']\ntest['count']=1\ntest2=test.groupby(['pip','tsu'],as_index=False)['count'].sum()\ntest3=test2.set_index(['pip','tsu']).unstack()\ndisplay(test3)\n\nplt.figure(figsize=(6,6))\nsns.heatmap(test3, cmap='YlGnBu',annot=True)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}