{"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":"# DICOM Search Suitable Range for Detection\nThis notebook referred to the following notebook.<br/>\nhttps://www.kaggle.com/code/mpwolke/cervical-fractures-pydicom-fastai","metadata":{}},{"cell_type":"code","source":"!pip install -U pydicom \n!pip install -U dicomsdl\n!pip install -U pylibjpeg \n!pip install -U pylibjpeg-openjpeg \n!pip install -U pylibjpeg-libjpeg \n!pip install -U python-gdcm","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:00:18.108746Z","iopub.execute_input":"2023-01-28T10:00:18.109782Z","iopub.status.idle":"2023-01-28T10:01:17.074162Z","shell.execute_reply.started":"2023-01-28T10:00:18.109749Z","shell.execute_reply":"2023-01-28T10:01:17.072949Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys \nimport cv2\nimport json\nimport glob\nimport random\nimport collections\nfrom tqdm import tqdm\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport seaborn as sns\nsns.set(style=\"whitegrid\")\nsns.set_context(\"paper\")\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\nimport nibabel as nib\nimport SimpleITK as sitk\nimport pylibjpeg\nfrom libjpeg import decode\nimport dicomsdl         \nimport pydicom as dicom \nfrom pydicom import dcmread\nfrom pydicom.data import get_testdata_file\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport skimage\nfrom skimage.transform import resize\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:17.077004Z","iopub.execute_input":"2023-01-28T10:01:17.077427Z","iopub.status.idle":"2023-01-28T10:01:17.089515Z","shell.execute_reply.started":"2023-01-28T10:01:17.077387Z","shell.execute_reply":"2023-01-28T10:01:17.088521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Collect positives ","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\nprint(train.columns.tolist())\ntrain2=train[['patient_id','image_id','cancer']][train['cancer']==1]\n#display(train2)\ndir0='/kaggle/input/rsna-breast-cancer-detection/train_images'\ntrain2=train2.reset_index(drop=True)\nfor i in range(len(train2)):\n    train2.loc[i,'path']=os.path.join(dir0,train2.loc[i,'patient_id'].astype(str),train2.loc[i,'image_id'].astype(str)+'.dcm')\ndisplay(train2)\npaths=train2['path'].tolist()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:17.090833Z","iopub.execute_input":"2023-01-28T10:01:17.091192Z","iopub.status.idle":"2023-01-28T10:01:17.601513Z","shell.execute_reply.started":"2023-01-28T10:01:17.091157Z","shell.execute_reply":"2023-01-28T10:01:17.600601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Positive sample image","metadata":{}},{"cell_type":"code","source":"dcm=dicom.dcmread(paths[0],force=True)\npip=dcm.PhotometricInterpretation \ntsu=dcm.file_meta.TransferSyntaxUID\nprint(pip,tsu)\ndcm.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:17.604651Z","iopub.execute_input":"2023-01-28T10:01:17.605176Z","iopub.status.idle":"2023-01-28T10:01:20.082376Z","shell.execute_reply.started":"2023-01-28T10:01:17.605135Z","shell.execute_reply":"2023-01-28T10:01:20.081445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dcm.pixels.flatten().numpy());","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-01-28T10:01:20.083777Z","iopub.execute_input":"2023-01-28T10:01:20.084402Z","iopub.status.idle":"2023-01-28T10:01:20.498133Z","shell.execute_reply.started":"2023-01-28T10:01:20.084365Z","shell.execute_reply":"2023-01-28T10:01:20.497283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dcm.scaled_px.flatten().numpy());","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:20.499598Z","iopub.execute_input":"2023-01-28T10:01:20.499933Z","iopub.status.idle":"2023-01-28T10:01:20.999984Z","shell.execute_reply.started":"2023-01-28T10:01:20.499906Z","shell.execute_reply":"2023-01-28T10:01:20.999042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1. Default","metadata":{}},{"cell_type":"code","source":"data_scaled0 = dcm.hist_scaled()\nplt.imshow(data_scaled0, cmap=plt.cm.bone);","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-01-28T10:01:21.001594Z","iopub.execute_input":"2023-01-28T10:01:21.002196Z","iopub.status.idle":"2023-01-28T10:01:23.143256Z","shell.execute_reply.started":"2023-01-28T10:01:21.002156Z","shell.execute_reply":"2023-01-28T10:01:23.142191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Restricted range of px","metadata":{}},{"cell_type":"code","source":"data_scaled1 =dcm.hist_scaled(min_px=1000, max_px=1500)\nplt.imshow(data_scaled1, cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:23.144554Z","iopub.execute_input":"2023-01-28T10:01:23.144938Z","iopub.status.idle":"2023-01-28T10:01:24.997114Z","shell.execute_reply.started":"2023-01-28T10:01:23.1449Z","shell.execute_reply":"2023-01-28T10:01:24.996173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Search suitable range of px for cancer detection","metadata":{}},{"cell_type":"code","source":"images=[]\nfor i in range(15):\n    j1=i*250\n    j2=(i+2)*250\n    data_scaledi =dcm.hist_scaled(min_px=j1, max_px=j2)\n    print(j1,'-',j2)\n    plt.imshow(data_scaledi, cmap=plt.cm.bone)\n    plt.show()\n    images+=[data_scaledi]","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:13:44.019223Z","iopub.execute_input":"2023-01-28T10:13:44.019687Z","iopub.status.idle":"2023-01-28T10:13:44.132636Z","shell.execute_reply.started":"2023-01-28T10:13:44.019575Z","shell.execute_reply":"2023-01-28T10:13:44.131317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    fig=plt.figure(figsize=(5,5))\n    plt.axis('off')\n    im=plt.imshow(ims[0],cmap=\"gray\")\n    plt.close()\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//5)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:38.097266Z","iopub.execute_input":"2023-01-28T10:01:38.097616Z","iopub.status.idle":"2023-01-28T10:01:38.104266Z","shell.execute_reply.started":"2023-01-28T10:01:38.09758Z","shell.execute_reply":"2023-01-28T10:01:38.103129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:38.105704Z","iopub.execute_input":"2023-01-28T10:01:38.106423Z","iopub.status.idle":"2023-01-28T10:01:44.129251Z","shell.execute_reply.started":"2023-01-28T10:01:38.106388Z","shell.execute_reply":"2023-01-28T10:01:44.128295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Various positive smaples","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(15,4, figsize=(12,45))\nfor i in range(60):\n    c=i%4\n    r=i//4\n    path=paths[r]\n    dcm=dicom.dcmread(path,force=True)\n    pip=dcm.PhotometricInterpretation \n    tsu=dcm.file_meta.TransferSyntaxUID\n    #print(r,pip,tsu)\n    scaled0 = dcm.hist_scaled() \n    scaled1 = dcm.hist_scaled(min_px=0, max_px=500) \n    scaled2 = dcm.hist_scaled(min_px=750, max_px=1250) \n    scaled3 = dcm.hist_scaled(min_px=1500, max_px=2000) \n    data_scaled=[scaled0,scaled1,scaled2,scaled3]\n    ax[r][c].imshow(data_scaled[c],  cmap=plt.cm.bone)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T10:01:46.222691Z","iopub.execute_input":"2023-01-28T10:01:46.223368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}