{"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":"#preprocessing imports \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom datetime import datetime\nimport os, sys, cv2, glob, random ,ast, warnings, time\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom skimage.filters import unsharp_mask\nfrom skimage.exposure import  equalize_hist, equalize_adapthist\nimport cv2 \nimport sys\nwarnings.filterwarnings('ignore')\n\n#model imports \nimport tensorflow as tf \nfrom tensorflow import keras\nfrom sklearn.metrics import roc_auc_score, accuracy_score\nfrom sklearn.model_selection import StratifiedKFold\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-21T10:00:38.188722Z","iopub.execute_input":"2021-07-21T10:00:38.189114Z","iopub.status.idle":"2021-07-21T10:00:47.119903Z","shell.execute_reply.started":"2021-07-21T10:00:38.189083Z","shell.execute_reply":"2021-07-21T10:00:47.118886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#constant values (DO NOT CHANGRE OR U DIE)\nLABELS = ['Negative for Pneumonia','Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance']\nLR = 1e-4\nBATCH_SIZE = 32 #may change\nEPOCHS = 5 #will change ","metadata":{"execution":{"iopub.status.busy":"2021-07-21T10:00:47.121462Z","iopub.execute_input":"2021-07-21T10:00:47.121774Z","iopub.status.idle":"2021-07-21T10:00:47.127413Z","shell.execute_reply.started":"2021-07-21T10:00:47.12174Z","shell.execute_reply":"2021-07-21T10:00:47.125837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load the csv files as dataframes and make them usable as the csv files provided are shitty af(#be a datascientist lmao)\ndef preprocess_df(train = True):\n    \n    df_image = pd.read_csv(\"../input/siim-covid19-detection/train_study_level.csv\")\n    df_det = pd.read_csv(\"../input/siim-covid19-detection/train_image_level.csv\")\n    df_image['StudyInstanceUID'] = df_image['id'].apply(lambda x : x[:-6])\n    df = df_det.merge(df_image, on='StudyInstanceUID')\n    path = []\n    TRAIN_DIR = \"../input/siim-covid19-detection/train/\"\n    \n    for instance_id in df['StudyInstanceUID']:\n        path.append(glob.glob(os.path.join(TRAIN_DIR, instance_id +\"/*/*\"))[0])\n    df['path'] = path\n    \n    df = df.drop(['id_x', 'id_y'], axis=1)\n    return df\n\n\n#cover the thicc and heacy dicom images to some reasonable size so that my kernal does not die \ndef dcm2cv(path,voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    data = apply_voi_lut(dicom.pixel_array, dicom)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n    \ndef dcmfilters(path):\n    \n    image = pydicom.dcmread(path)\n    pixels = image.pixel_array\n\n    min_pixel = np.min(pixels)\n    max_pixel = np.max(pixels)\n\n    if image.PhotometricInterpretation == \"MONOCHROME1\":\n        pixels = max_pixel - pixels\n    else:\n        pixels = pixels\n        \n    pixels= equalize_hist(pixels, nbins=256, mask=None)\n    return pixels\n    \npath = '../input/siim-covid19-detection/train/00a76543ed93/4a223cccbe04/ad8d4a5ba8f0.dcm'\n\ndef plot_images(title, image, image_processed):\n    # Plot both images\n    fig, axes = plt.subplots(nrows=1, ncols=2,sharex=True, sharey=True, figsize=(12, 12))\n    ax = axes.ravel()\n    ax[0].imshow(image, cmap=plt.cm.gray)\n    ax[0].set_title('Original image')\n    ax[1].imshow(image_processed, cmap=plt.cm.gray)\n    ax[1].set_title(title)\n    for a in ax:\n        a.axis('off')\n    fig.tight_layout()\n    plt.show()\n    \nfilteredimage = dcmfilters(path)\nimage = dcm2cv(path)\nfilteredimage.shape\nplot_images('test1', image, filteredimage)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T10:00:47.130043Z","iopub.execute_input":"2021-07-21T10:00:47.130527Z","iopub.status.idle":"2021-07-21T10:00:52.75618Z","shell.execute_reply.started":"2021-07-21T10:00:47.130479Z","shell.execute_reply":"2021-07-21T10:00:52.754447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}