{"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":"<a id=\"table\"></a>\n<h1 style=\"background-color:lightpink;font-family:newtimeroman;font-size:350%;text-align:center;border-radius: 15px 50px;\">Table of Content</h1>\n\n* [1. IMPORTING LIBRARIES](#1)\n\n* [2. CONFIG](#2)    \n\n* [3. LOADING DATASET](#3)\n  \n* [4. CONVERT DCM TO PNG (USE ROI)](#4)\n\n* [5. EXAMPLE IMAGE SAMPLES](#5)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Importing Libraries</p>","metadata":{}},{"cell_type":"code","source":"! pip install dicomsdl","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:34:48.782791Z","iopub.execute_input":"2022-12-13T09:34:48.783234Z","iopub.status.idle":"2022-12-13T09:35:04.970734Z","shell.execute_reply.started":"2022-12-13T09:34:48.783199Z","shell.execute_reply":"2022-12-13T09:35:04.969261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport dicomsdl\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom joblib import Parallel, delayed\nfrom matplotlib import pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:35:04.973385Z","iopub.execute_input":"2022-12-13T09:35:04.973798Z","iopub.status.idle":"2022-12-13T09:35:05.294163Z","shell.execute_reply.started":"2022-12-13T09:35:04.97376Z","shell.execute_reply":"2022-12-13T09:35:05.293011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">CONFIG</p>","metadata":{}},{"cell_type":"code","source":"class CFG:\n    class data:\n        resize_dim=1024\n        aspect_ratio=True\n        img_size=(1024, 512)\n        \n        image_dir='/kaggle/working'\n        base_path = '/kaggle/input/rsna-breast-cancer-detection'\n        path_to_train=\"/kaggle/input/rsna-breast-cancer-detection/train.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:35:05.295718Z","iopub.execute_input":"2022-12-13T09:35:05.296195Z","iopub.status.idle":"2022-12-13T09:35:05.302786Z","shell.execute_reply.started":"2022-12-13T09:35:05.296126Z","shell.execute_reply":"2022-12-13T09:35:05.301542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">LOADING DATASET</p>","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(CFG.data.path_to_train).head(100)\nprint(f\"train.shape = {train_df.shape}\")\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:07.669081Z","iopub.execute_input":"2022-12-13T09:36:07.669563Z","iopub.status.idle":"2022-12-13T09:36:07.748953Z","shell.execute_reply.started":"2022-12-13T09:36:07.669524Z","shell.execute_reply":"2022-12-13T09:36:07.747839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['dicom_path'] = f'{CFG.data.base_path}/train_images'\\\n                    + '/' + train_df.patient_id.astype(str)\\\n                    + '/' + train_df.image_id.astype(str)\\\n                    + '.dcm'\ntrain_df['image_path'] = train_df.dicom_path.str.replace('.dcm','.png').str.replace(CFG.data.base_path, CFG.data.image_dir)\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:08.661642Z","iopub.execute_input":"2022-12-13T09:36:08.66205Z","iopub.status.idle":"2022-12-13T09:36:08.687767Z","shell.execute_reply.started":"2022-12-13T09:36:08.662017Z","shell.execute_reply":"2022-12-13T09:36:08.68644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Convert DCM to PNG (use ROI)</p>","metadata":{}},{"cell_type":"code","source":"os.makedirs(CFG.data.image_dir, exist_ok = True)\n\n! ls /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:09.104221Z","iopub.execute_input":"2022-12-13T09:36:09.105257Z","iopub.status.idle":"2022-12-13T09:36:10.147045Z","shell.execute_reply.started":"2022-12-13T09:36:09.105211Z","shell.execute_reply":"2022-12-13T09:36:10.145839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ROI\ndef img2roi(img):\n    # Binarize the image\n    bin_img = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY)[1]\n\n    # Make contours around the binarized image, keep only the largest contour\n    contours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n    contour = max(contours, key=cv2.contourArea)\n\n    # Find ROI from largest contour\n    ys = contour.squeeze()[:, 0]\n    xs = contour.squeeze()[:, 1]\n    roi =  img[np.min(xs):np.max(xs), np.min(ys):np.max(ys)]\n    \n    return roi","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:10.149749Z","iopub.execute_input":"2022-12-13T09:36:10.150155Z","iopub.status.idle":"2022-12-13T09:36:10.158875Z","shell.execute_reply.started":"2022-12-13T09:36:10.150111Z","shell.execute_reply":"2022-12-13T09:36:10.157662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DCM \ndef read_xray(path, fix_monochrome = True):\n    dicom = dicomsdl.open(path)\n    data = dicom.pixelData(storedvalue=False)  # storedvalue = True for int16 return otherwise float32\n    data = data - np.min(data)\n    data = data / np.max(data)\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = 1.0 - data\n    return data\n\ndef resize_and_save(file_path):\n    img = read_xray(file_path)\n    h, w = img.shape[:2]  # orig hw\n    if CFG.data.aspect_ratio:\n        r = CFG.data.resize_dim / max(h, w)  # resize image to img_size\n        interp = cv2.INTER_LINEAR\n        if r != 1:  # always resize down, only resize up if training with augmentation\n            img = cv2.resize(img, (int(w * r), int(h * r)), interpolation=interp)\n    else:\n        img = cv2.resize(img, (CFG.resize_dim, CFG.resize_dim), cv2.INTER_LINEAR)\n    \n    img = (img * 255).astype(np.uint8)\n    img = img2roi(img)\n    img = cv2.resize(img, CFG.data.img_size[::-1], cv2.INTER_LINEAR)\n    \n    sub_path = file_path.split(\"/\",4)[-1].split('.dcm')[0] + '.png'\n    infos = sub_path.split('/')\n    pid = infos[-2]\n    iid = infos[-1]; iid = iid.replace('.png', '')\n    new_path = os.path.join(CFG.data.image_dir, sub_path)\n    \n    os.makedirs(new_path.rsplit('/',1)[0], exist_ok=True)\n    cv2.imwrite(new_path, img)\n    \n    return pid, iid , w, h","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:10.160686Z","iopub.execute_input":"2022-12-13T09:36:10.161364Z","iopub.status.idle":"2022-12-13T09:36:10.175992Z","shell.execute_reply.started":"2022-12-13T09:36:10.161322Z","shell.execute_reply":"2022-12-13T09:36:10.174629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_paths = train_df.dicom_path.tolist()\nlen(file_paths)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:10.178504Z","iopub.execute_input":"2022-12-13T09:36:10.178921Z","iopub.status.idle":"2022-12-13T09:36:10.193354Z","shell.execute_reply.started":"2022-12-13T09:36:10.178882Z","shell.execute_reply":"2022-12-13T09:36:10.192256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\nimport numpy as np\nimport cupy as cp\n\ndef apply_device(state, index, nqubits, device):\n    with cp.cuda.Device(device):\n        # transfer from host to device\n        piece = cp.asarray(state[index])\n    # transfer back from device to host\n    state[index] = piece.get()\n    # delete vector from device memory\n    del(piece)\n\ndef context_init(device):\n    with cp.cuda.Device(device):\n        c = cp.ones(10, dtype='complex128')\n\ndef main():\n    nqubits = 28\n    state = np.zeros((4, 2 ** (nqubits - 1)), dtype='complex128')\n    state[0, 0] = 1    \n\n    pool = joblib.Parallel(n_jobs=2, prefer=\"threads\", verbose=1)    \n    pool(joblib.delayed(context_init)(device) for device in [0, 1])\n\n    for i in range(5):\n        pool(joblib.delayed(apply_device)(state, index, nqubits, device)\n             for index, device in [(0, 0), (1, 1)])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgsize = Parallel(n_jobs=2, backend='threading', verbose=1)(delayed(resize_and_save)(file_path) for file_path in tqdm(file_paths, leave=True, position=0))\nlen(imgsize)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:36:10.195385Z","iopub.execute_input":"2022-12-13T09:36:10.195913Z","iopub.status.idle":"2022-12-13T09:37:32.970424Z","shell.execute_reply.started":"2022-12-13T09:36:10.195867Z","shell.execute_reply":"2022-12-13T09:37:32.96897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Example image samples</p>","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 15))\ngrid = ImageGrid(fig, 111,\n                 nrows_ncols=(2, 5),\n                 axes_pad=0.25\n)\n\nfor ax, img_path, label in zip(grid, train_df.image_path[:10].values, train_df.cancer[:10].values):\n    ax.imshow(cv2.imread(img_path))\n    ax.set_title(label)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T09:35:51.665142Z","iopub.status.idle":"2022-12-13T09:35:51.666357Z","shell.execute_reply.started":"2022-12-13T09:35:51.666096Z","shell.execute_reply":"2022-12-13T09:35:51.66612Z"},"trusted":true},"execution_count":null,"outputs":[]}]}