{"cells":[{"metadata":{"_uuid":"3d4586d9cd6a1cb886b69aa36037c7c38e43a9f3"},"cell_type":"markdown","source":"The original code is on colaboratory so the below will not run on Kaggle.\n\nI combined a few functions and some code from the earlier EDA kernels so if you see your code, just let me know and I'll source you.\n\nThis is my first public kernel so sorry for any obscurity.\n\nhttps://colab.research.google.com/drive/1reOc-bBi2CNBZ94rHsy86dMJbm9KwJGU"},{"metadata":{"id":"9HsJ7Yns8V1d","colab_type":"code","colab":{},"trusted":false,"_uuid":"2f48f4e912c473ebc404b61d79df027098809b75"},"cell_type":"code","source":"#!pip install -q pydicom\n#!pip3 install -q tqdm \n#!pip3 install -q imgaug\n#!pip3 install -q kaggle\n#!pip3 install -q pypng\n#!pip3 install -q pillow\n#!pip3 install PyDrive","execution_count":null,"outputs":[]},{"metadata":{"id":"gsO1ctyn8tV-","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"8f025afb-1792-4bfd-f4f4-5c7f369465e3","trusted":false,"_uuid":"410c692a834f5259923ab69ffbd616f34f0a4f88"},"cell_type":"code","source":"import os \nimport sys\nimport shutil\nimport glob\nimport png\nimport itertools\nimport pydicom # for reading dicom files\nimport os # for doing directory operations \nimport pandas as pd # for some simple data analysis (right now, just to load in the labels data and quickly reference it)\n\n%pylab inline\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimport numpy as np\nimport cv2\n","execution_count":null,"outputs":[]},{"metadata":{"id":"Uy3MBTLQ8w2q","colab_type":"code","colab":{},"trusted":false,"_uuid":"b1d9810a0987dd5244cffb6231e6927fcfe26e2c"},"cell_type":"code","source":"# enter your Kaggle credentionals here ,  you can create tokens from you profile page\nos.environ['KAGGLE_USERNAME']=\"username\"\nos.environ['KAGGLE_KEY']=\"key\"","execution_count":null,"outputs":[]},{"metadata":{"id":"1OmU-ZYP-HMQ","colab_type":"code","colab":{},"trusted":false,"_uuid":"58b68163b26f125419b4819cdfdd1e1153beb10c"},"cell_type":"code","source":"# run from 'content' directory\n# Root directory of the project\nos.mkdir('../content/RSNAdata')\nROOT_DIR = os.path.abspath('../content/RSNAdata')\nos.chdir(ROOT_DIR)","execution_count":null,"outputs":[]},{"metadata":{"id":"K5mXbN4L-LLt","colab_type":"code","colab":{},"trusted":false,"_uuid":"e80c5241ac7319f456bca72b0a18ccd6c2a96fd2"},"cell_type":"code","source":"#!kaggle competitions download -c rsna-pneumonia-detection-challenge","execution_count":null,"outputs":[]},{"metadata":{"id":"DScWIcGE-UtA","colab_type":"code","colab":{},"trusted":false,"_uuid":"a60dffd0e753bbefd9635c8eed769053b749a78c"},"cell_type":"code","source":"# unzipping takes a few minutes\n#!unzip -q -o stage_1_test_images.zip -d stage_1_test_images\n#!unzip -q -o stage_1_train_images.zip -d stage_1_train_images\n#!unzip -q -o stage_1_train_labels.csv.zip","execution_count":null,"outputs":[]},{"metadata":{"id":"k-bnr58T-5du","colab_type":"code","colab":{},"trusted":false,"_uuid":"985194ee75dad99291060d13fe3ead2cfb9bdc90"},"cell_type":"code","source":"os.chdir('../')\ndata_dir = '../content/RSNAdata/stage_1_train_images/'\npatients = os.listdir(data_dir)\nlabels_df = pd.read_csv('../content/RSNAdata/stage_1_train_labels.csv')\n\nlabels_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"QaKJ_-74o4QD","colab_type":"code","colab":{},"trusted":false,"_uuid":"53305d9c55cb688629bf807bc1394ef32177d838"},"cell_type":"code","source":"#merges the train labels and detailed class\n\nclass_info_df = pd.read_csv('../content/RSNAdata/stage_1_detailed_class_info.csv.zip')\ntrain_labels_df = pd.read_csv('../content/RSNAdata/stage_1_train_labels.csv')\n\ntrain_class_df = train_labels_df.merge(class_info_df, left_on='patientId', right_on='patientId', how='inner')\n\ntrain_class_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"n0qVhd8Z6QKN","colab_type":"code","colab":{},"trusted":false,"_uuid":"0ea088404c8e0dc32d969074e9b50474c6f69a63"},"cell_type":"code","source":"os.mkdir('../content/Target_0NN_PNGs')    #create folder to store png files, here 0NN = No Lung Opacity / Not Normal","execution_count":null,"outputs":[]},{"metadata":{"id":"PVsCzNtUNa06","colab_type":"code","colab":{},"trusted":false,"_uuid":"3603a8112bee080f031b2dabe624cf6879756265"},"cell_type":"code","source":"#batch conversion using PyPNG\n\ndef convertDCMtoPNG():\n\n    img_data = list(train_class_df.T.to_dict().values())\n     \n    for i ,data_row in enumerate(itertools.islice(img_data, 0, None)):\n      try:\n        if data_row['class']=='No Lung Opacity / Not Normal':\n            patientImage = data_row['patientId']+'.dcm'\n            imagePath = os.path.join(\"../content/RSNAdata/stage_1_train_images/\", patientImage)\n            data_row_img_data = pydicom.read_file(imagePath)\n            data_row_img = pydicom.dcmread(imagePath)\n            shape = data_row_img.pixel_array.shape\n        \n   # Convert to float to avoid overflow or underflow losses.\n            image_2d = data_row_img.pixel_array.astype(float)\n    \n       # Rescaling grey scale between 0-255\n            image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0\n\n    # Convert to uint\n            image_2d_scaled = np.uint8(image_2d_scaled)\n\n    # Write the PNG file\n            download_location = os.path.join('..content/Target_0NN_PNGs', data_row['patientId'] + '.png')\n            with  open(data_row['patientId'] + '.png','wb') as png_file:\n                  w = png.Writer(shape[1], shape[0], greyscale=True)\n                  w.write(png_file, image_2d_scaled)\n            shutil.move(data_row['patientId'] + '.png', '../content/Target_0NN_PNGs/')\n        else:\n            continue\n      except:\n        \n        if os.path.exists(download_location):\n            copy = False\n            continue","execution_count":null,"outputs":[]},{"metadata":{"id":"YFd0XcQEkxo-","colab_type":"code","colab":{},"trusted":false,"_uuid":"220f1fa389ebf34ddf6993615a614fd1c4acf851"},"cell_type":"code","source":"#convertDCMtoPNG()","execution_count":null,"outputs":[]},{"metadata":{"id":"Q41-ZDnAiary","colab_type":"code","colab":{},"cellView":"code","trusted":false,"_uuid":"77bf74b10c723698ff00115ac96f553c2c260135"},"cell_type":"code","source":"#adds a zip with all the pngs to your drive\n\nfilename = \"target_0_NN\" #@param {type:\"string\"}\nfolders_or_files_to_save = \"Target_0NN_PNGs\" #@param {type:\"string\"}\nfrom google.colab import files\nfrom google.colab import auth\nfrom googleapiclient.http import MediaFileUpload\nfrom googleapiclient.discovery import build\n\ndef save_file_to_drive(name, path):\n    file_metadata = {\n    'name': name,\n    'mimeType': 'application/octet-stream'\n    }\n\n    media = MediaFileUpload(path, \n                  mimetype='application/octet-stream',\n                  resumable=True)\n\n    created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()\n\n    print('File ID: {}'.format(created.get('id')))\n\n    return created\n\nextension_zip = \".zip\"\nzip_file = filename + extension_zip\n\n!zip -r $zip_file {folders_or_files_to_save} # FOLDERS TO SAVE INTO ZIP FILE\n\nauth.authenticate_user()\ndrive_service = build('drive', 'v3')\n\ndestination_name = zip_file\npath_to_file = zip_file\nsave_file_to_drive(destination_name, path_to_file)","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"dicom_png.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[],"toc_visible":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}