{"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":"!pip install -qU python-gdcm pydicom pylibjpeg\n!mkdir cancer\n!mkdir nocancer\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport tensorflow as tf\nimport cv2\nimport pydicom\nimport pylibjpeg\nimport gdcm\nimport random\nimport matplotlib as plt\n\nfrom joblib import Parallel, delayed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\nprint(df.groupby(['cancer'])['image_id'].count())\nprint('\\n')\nprint(df.groupby(['cancer', 'laterality'])['image_id'].count())\nprint('\\n')\nprint(df.groupby(['cancer', 'laterality', 'implant'])['image_id'].count())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noCancer = df[(df['cancer'] == 0) & (df['implant'] == 0)].reset_index()\nnoCancer.describe()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 1280\nWIDTH = 720\nTRAIN_FOLDER = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\nNO_CANCER_FOLDER = '/kaggle/working/nocancer/'\nUPSCALE_COUNT = 25000\n\ndef saveImg(index):\n    \n    patientID = str(noCancer.loc[index, 'patient_id'])\n    imageID = str(noCancer.loc[index, 'image_id'])\n    \n    dcm = pydicom.dcmread(TRAIN_FOLDER + patientID + '/' + imageID + '.dcm')\n    pixelArray = dcm.pixel_array\n    \n    equal_columns = np.all(pixelArray == pixelArray[0], axis=0)\n    image = pixelArray[:, ~equal_columns]\n    \n    if dcm.PhotometricInterpretation == 'MONOCHROME1':\n        image = 1- image\n\n    image = np.reshape(image, (image.shape[0], image.shape[1], 1))\n    image = tf.convert_to_tensor(image)\n    image = tf.image.resize_with_pad(image, HEIGHT, WIDTH)\n    \n    if noCancer.loc[index, 'laterality'] == \"R\":\n        image = tf.image.flip_left_right(image)\n    \n    tf.keras.utils.save_img(NO_CANCER_FOLDER + patientID + '_' + imageID + '_' + str(index) + '.jpg', image)\n\nParallel()(delayed(saveImg)(i) for i in range(UPSCALE_COUNT))","metadata":{},"execution_count":null,"outputs":[]}]}