{"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":"# PNG Search Suitable Range for Detection\nhttps://www.kaggle.com/stpeteishii/dicom-search-suitable-range-for-detection","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":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-02-04T16:11:38.735806Z","iopub.execute_input":"2023-02-04T16:11:38.736622Z","iopub.status.idle":"2023-02-04T16:13:12.866645Z","shell.execute_reply.started":"2023-02-04T16:11:38.736568Z","shell.execute_reply":"2023-02-04T16:13:12.865266Z"},"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-02-04T16:13:12.87293Z","iopub.execute_input":"2023-02-04T16:13:12.875342Z","iopub.status.idle":"2023-02-04T16:13:12.892986Z","shell.execute_reply.started":"2023-02-04T16:13:12.875295Z","shell.execute_reply":"2023-02-04T16:13:12.89172Z"},"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-02-04T16:13:12.898837Z","iopub.execute_input":"2023-02-04T16:13:12.901521Z","iopub.status.idle":"2023-02-04T16:13:13.631296Z","shell.execute_reply.started":"2023-02-04T16:13:12.901482Z","shell.execute_reply":"2023-02-04T16:13:13.630296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_img_to_hu(dcm_ds):\n    \"\"\"Rescales the image to Hounsfield unit.\"\"\"\n    data = dcm_ds.pixel_array\n    if dcm_ds.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    return data * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:13.635674Z","iopub.execute_input":"2023-02-04T16:13:13.637999Z","iopub.status.idle":"2023-02-04T16:13:13.645418Z","shell.execute_reply.started":"2023-02-04T16:13:13.637957Z","shell.execute_reply":"2023-02-04T16:13:13.644428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dicom Show","metadata":{}},{"cell_type":"code","source":"dcm=dicom.dcmread(paths[1],force=True)\npip=dcm.PhotometricInterpretation \ntsu=dcm.file_meta.TransferSyntaxUID\nprint(pip,' - ',tsu)\ndcm.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:13.649842Z","iopub.execute_input":"2023-02-04T16:13:13.652264Z","iopub.status.idle":"2023-02-04T16:13:18.499634Z","shell.execute_reply.started":"2023-02-04T16:13:13.652216Z","shell.execute_reply":"2023-02-04T16:13:18.498653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dcm.pixels.flatten().numpy());","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:18.505057Z","iopub.execute_input":"2023-02-04T16:13:18.507379Z","iopub.status.idle":"2023-02-04T16:13:19.091287Z","shell.execute_reply.started":"2023-02-04T16:13:18.507337Z","shell.execute_reply":"2023-02-04T16:13:19.090296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_scaled0 = dcm.hist_scaled()\nplt.imshow(data_scaled0, cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:19.09572Z","iopub.execute_input":"2023-02-04T16:13:19.098172Z","iopub.status.idle":"2023-02-04T16:13:22.009755Z","shell.execute_reply.started":"2023-02-04T16:13:19.09813Z","shell.execute_reply":"2023-02-04T16:13:22.008789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-02-04T16:13:22.014657Z","iopub.execute_input":"2023-02-04T16:13:22.016997Z","iopub.status.idle":"2023-02-04T16:13:25.230053Z","shell.execute_reply.started":"2023-02-04T16:13:22.016955Z","shell.execute_reply":"2023-02-04T16:13:25.225247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PNG Show\ndo it in png files","metadata":{}},{"cell_type":"code","source":"img0=rescale_img_to_hu(dcm)\nplt.imshow(img0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:25.235941Z","iopub.execute_input":"2023-02-04T16:13:25.238345Z","iopub.status.idle":"2023-02-04T16:13:26.697425Z","shell.execute_reply.started":"2023-02-04T16:13:25.238304Z","shell.execute_reply":"2023-02-04T16:13:26.696457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(img0.flatten())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:26.701855Z","iopub.execute_input":"2023-02-04T16:13:26.704183Z","iopub.status.idle":"2023-02-04T16:13:27.440927Z","shell.execute_reply.started":"2023-02-04T16:13:26.704141Z","shell.execute_reply":"2023-02-04T16:13:27.439743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def square_if_positive(x,L,H):\n    if L < x < H:\n        return x\n    else:\n        return 0\n    \nvectorized_func = np.vectorize(square_if_positive)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:27.445372Z","iopub.execute_input":"2023-02-04T16:13:27.448145Z","iopub.status.idle":"2023-02-04T16:13:27.455617Z","shell.execute_reply.started":"2023-02-04T16:13:27.448104Z","shell.execute_reply":"2023-02-04T16:13:27.454604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1=vectorized_func(img0,2000,2500)\nplt.imshow(img1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:13:27.461189Z","iopub.execute_input":"2023-02-04T16:13:27.463478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Search Suitable Range of PNG for Detection ","metadata":{}},{"cell_type":"code","source":"images=[]\nfor i in range(40):\n    j1=i*100\n    j2=j1+500\n    imgi=vectorized_func(img0,j1,j2)\n    print(j1,'-',j2)\n    plt.imshow(imgi, cmap=plt.cm.bone)\n    plt.show()\n    images+=[imgi]","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images)","metadata":{"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    img0=rescale_img_to_hu(dcm)\n    scaled0 = vectorized_func(img0,0,3000)\n    scaled1 = vectorized_func(img0,1000,1500)\n    scaled2 = vectorized_func(img0,1500,2000)\n    scaled3 = vectorized_func(img0,2000,2500)\n    data_scaled=[scaled0,scaled1,scaled2,scaled3]\n    ax[r][c].imshow(data_scaled[c],  cmap=plt.cm.bone)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}