{"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":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import KFold\nfrom tqdm import tqdm\nimport pickle\n\nclass_names = ['Normal', 'Epidural', 'Intraparenchymal', 'Intraventricular', 'Subarachnoid', 'Subdural']","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:30:32.663096Z","iopub.execute_input":"2022-02-13T11:30:32.663458Z","iopub.status.idle":"2022-02-13T11:30:33.568106Z","shell.execute_reply.started":"2022-02-13T11:30:32.66337Z","shell.execute_reply":"2022-02-13T11:30:33.567448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_series = pd.read_csv('../input/rsna-abnormal-series-labels/RSNA_Series.csv')\nrsna_series = rsna_series.loc[rsna_series['Normal']==0]\nrsna_series.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:30:34.096202Z","iopub.execute_input":"2022-02-13T11:30:34.096986Z","iopub.status.idle":"2022-02-13T11:30:35.22094Z","shell.execute_reply.started":"2022-02-13T11:30:34.096953Z","shell.execute_reply":"2022-02-13T11:30:35.220108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df = pd.read_csv('../input/rsna-cq500-data-frames-png/RSNA.csv')\nrsna_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:30:37.560955Z","iopub.execute_input":"2022-02-13T11:30:37.56154Z","iopub.status.idle":"2022-02-13T11:30:39.368779Z","shell.execute_reply.started":"2022-02-13T11:30:37.561499Z","shell.execute_reply":"2022-02-13T11:30:39.368167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_path = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:30:39.370395Z","iopub.execute_input":"2022-02-13T11:30:39.370921Z","iopub.status.idle":"2022-02-13T11:30:39.375743Z","shell.execute_reply.started":"2022-02-13T11:30:39.370878Z","shell.execute_reply":"2022-02-13T11:30:39.374909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_ids = rsna_series['UID'].values\nrsna_dict = {}\nfor i in tqdm(range(len(rsna_ids))):\n    image_path = rsna_series.loc[rsna_series['UID']==rsna_ids[i]]['ImageList'].values[0]\n    image_path = image_path[1:-1].split(',')\n    image_path = [x.replace(' \\'', '').replace('\\'', '') for x in image_path]\n    image_df = rsna_df.loc[rsna_df['imgfile'].isin(image_path)]\n    image_labels = []\n    dicom_path = []\n    for j in range(len(image_path)):\n        image_labels.append(list(image_df.loc[image_df['imgfile']==image_path[j]][class_names].values[0]))\n        dicom_path.append(root_path+image_path[j].split('/')[-1].split('.')[0]+'.dcm')\n    rsna_dict[rsna_ids[i]] = [dicom_path, image_labels]","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:30:39.831533Z","iopub.execute_input":"2022-02-13T11:30:39.83181Z","iopub.status.idle":"2022-02-13T11:45:21.974924Z","shell.execute_reply.started":"2022-02-13T11:30:39.831783Z","shell.execute_reply":"2022-02-13T11:45:21.974041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('RSNA_Abnormal_Series_DICOM.pkl', 'wb') as f:\n    pickle.dump(rsna_dict, f)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:45:25.411985Z","iopub.execute_input":"2022-02-13T11:45:25.412259Z","iopub.status.idle":"2022-02-13T11:45:34.939619Z","shell.execute_reply.started":"2022-02-13T11:45:25.412231Z","shell.execute_reply":"2022-02-13T11:45:34.93884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import SimpleITK as sitk\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport random\nimport cv2\nimport tensorflow as tf\nimport shutil\nfrom skimage import morphology\nfrom scipy import ndimage\n\n\ndef window_img(img, img_min, img_max):\n    img = np.clip(img, img_min, img_max)\n    return img\n\ndef window_3d(image, window=None):\n    if window:\n        image_total = np.empty((len(image), 256, 256))\n        for i in range(len(image)):\n            image_brain =  window_img(image[i], 0, 80)\n            image_brain = (image_brain - 0) / 80\n            image_total[i] = cv2.resize(image_brain, dsize=(256, 256))\n    else:\n        image_total = np.empty((len(image), 256, 256, 3))\n        for i in range(len(image)):\n            image_brain =  window_img(image[i], 0, 80)\n            image_brain = (image_brain - 0) / 80\n            image_brain = np.expand_dims(image_brain, axis=-1)\n\n            image_subdural =  window_img(image[i], -20, 180)\n            image_subdural = (image_subdural - (-20)) / 200\n            image_subdural = np.expand_dims(image_subdural, axis=-1)\n\n            image_soft =  window_img(image[i], 20, 60)\n            image_soft = (image_soft - (20)) / 40\n            image_soft = np.expand_dims(image_soft, axis=-1)\n\n            image_total[i] = cv2.resize(np.concatenate([image_brain, image_subdural, image_soft], axis=-1), dsize=(256, 256))\n    return (image_total*255).astype(np.uint8)\n\n\ndef remove_noise(img):\n    image = []\n    for brain_image in img:\n        segmentation = morphology.dilation(brain_image, np.ones((1, 1)))\n        labels, label_nb = ndimage.label(segmentation)\n\n        label_count = np.bincount(labels.ravel().astype(np.uint8))\n        label_count[0] = 0\n\n        mask = labels == label_count.argmax()\n\n        # Improve the brain mask\n        mask = morphology.dilation(mask, np.ones((1, 1)))\n        mask = ndimage.morphology.binary_fill_holes(mask)\n        mask = morphology.dilation(mask, np.ones((3, 3)))\n        image.append(mask * brain_image)\n\n    return np.array(image)\n\ndef skull_strip(img_clipp, img_orig):\n    img_clipp[img_clipp == np.amax(img_clipp)] = 0\n    image = []\n    for hi in img_clipp:\n        labels, label_nb = ndimage.label(hi)\n        label_count = np.bincount(labels.ravel().astype(np.uint8))\n        label_count[0] = 0\n        mask = labels == label_count.argmax()\n        mask = morphology.dilation(mask, np.ones((1, 1)))\n        mask = ndimage.morphology.binary_fill_holes(mask)\n        mask = morphology.dilation(mask, np.ones((3, 3)))\n        mask = ndimage.morphology.binary_fill_holes(mask)\n        image.append(mask * hi)\n    image = np.array(image)\n    mask = (image > 0).astype(np.uint8)\n    mask = ndimage.binary_fill_holes(mask)\n    img_orig[mask==0] = 0\n    return img_orig\n\ndef crop_resize_image(image, label):\n    out_image = []\n    out_label = []\n    for i, img in enumerate(image):\n        mask = img == 0\n        coords = np.array(np.nonzero(~mask))\n        if coords.shape[1] > 100:\n            top_left = np.min(coords, axis=1)\n            bottom_right = np.max(coords, axis=1)\n            croped_image = img[top_left[0]:bottom_right[0],\n                            top_left[1]:bottom_right[1]]\n            if 0 in croped_image.shape:\n                continue\n            croped_image = np.expand_dims(croped_image, axis=-1)\n            resized= np.array(tf.image.resize_with_pad(croped_image,\n                                                    256, 256, method='bilinear',antialias=False))\n            resized = np.squeeze(resized)\n            out_image.append(resized)\n            out_label.append(label[i])\n    return np.array(out_image), out_label\n    \ndef _read_dicom(dicoms, labels):\n    image = []\n    for dicom in dicoms:\n        img = sitk.ReadImage(dicom)\n        img = np.array(sitk.GetArrayFromImage(img))[0]\n        image.append(img)\n    image = np.array(image)\n    img_clip = np.clip(image, 0, 80)\n    noise_removed = remove_noise(img_clip)\n    img_stripped = skull_strip(noise_removed, image)\n    img_resized, label = crop_resize_image(img_stripped, labels)\n    img_final = window_3d(img_resized)\n    return img_final, label","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:45:51.017852Z","iopub.execute_input":"2022-02-13T11:45:51.019119Z","iopub.status.idle":"2022-02-13T11:45:57.612037Z","shell.execute_reply.started":"2022-02-13T11:45:51.019079Z","shell.execute_reply":"2022-02-13T11:45:57.611362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nprint('creating archive')\nzf = zipfile.ZipFile('RSNA_Abnormal_Images.zip', mode='w')","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:45:34.971339Z","iopub.status.idle":"2022-02-13T11:45:34.971657Z","shell.execute_reply.started":"2022-02-13T11:45:34.971489Z","shell.execute_reply":"2022-02-13T11:45:34.971505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idds = list(rsna_dict.keys())\nrsna_png_dict = {}\nfor i in tqdm(range(len(idds))):\n    dicoms = rsna_dict[idds[i]][0]\n    labels = rsna_dict[idds[i]][1]\n    image_array, labels = _read_dicom(dicoms, labels)\n    png_series_name = []\n    for j in range(len(image_array)):\n        img_name = dicoms[j].split('/')[-1].split('.')[0]+'.png'\n        img_path = '{}/{}'.format(idds[i], img_name)\n        png_series_name.append(img_path)\n        cv2.imwrite(img_name, image_array[j])\n        zf.write(img_name, img_path, zipfile.ZIP_DEFLATED)\n        os.remove(img_name)\n    rsna_png_dict[idds[i]] = [png_series_name, labels]","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:46:02.675553Z","iopub.execute_input":"2022-02-13T11:46:02.67605Z","iopub.status.idle":"2022-02-13T11:46:06.280284Z","shell.execute_reply.started":"2022-02-13T11:46:02.675999Z","shell.execute_reply":"2022-02-13T11:46:06.279091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(image_array)):\n    plt.figure()\n    plt.imshow(image_array[i], cmap='bone')\n    plt.title(class_names[np.argmax(labels[i])])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T11:46:59.955637Z","iopub.execute_input":"2022-02-13T11:46:59.955949Z","iopub.status.idle":"2022-02-13T11:47:06.154142Z","shell.execute_reply.started":"2022-02-13T11:46:59.955916Z","shell.execute_reply":"2022-02-13T11:47:06.153353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zf.close()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T12:00:51.607391Z","iopub.execute_input":"2022-02-12T12:00:51.60776Z","iopub.status.idle":"2022-02-12T12:00:51.614291Z","shell.execute_reply.started":"2022-02-12T12:00:51.607705Z","shell.execute_reply":"2022-02-12T12:00:51.61307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('RSNA_Abnormal_Series_PNG.pkl', 'wb') as f:\n    pickle.dump(rsna_png_dict, f)","metadata":{},"execution_count":null,"outputs":[]}]}