{"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 numpy as np\nimport pandas as pd\nimport os\nimport glob","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:56:37.7503Z","iopub.execute_input":"2023-02-15T15:56:37.750714Z","iopub.status.idle":"2023-02-15T15:56:37.770803Z","shell.execute_reply.started":"2023-02-15T15:56:37.750616Z","shell.execute_reply":"2023-02-15T15:56:37.769328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"!pip install -U  pylibjpeg pylibjpeg-libjpeg pydicom pylibjpeg-openjpeg python-gdcm\n# !pip install pydicom[gdcm]\n!pip install pillow\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:56:37.77246Z","iopub.execute_input":"2023-02-15T15:56:37.772854Z","iopub.status.idle":"2023-02-15T15:57:01.056498Z","shell.execute_reply.started":"2023-02-15T15:56:37.772821Z","shell.execute_reply":"2023-02-15T15:57:01.055004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:01.058324Z","iopub.execute_input":"2023-02-15T15:57:01.058727Z","iopub.status.idle":"2023-02-15T15:57:01.144925Z","shell.execute_reply.started":"2023-02-15T15:57:01.058687Z","shell.execute_reply":"2023-02-15T15:57:01.143943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:01.148184Z","iopub.execute_input":"2023-02-15T15:57:01.148581Z","iopub.status.idle":"2023-02-15T15:57:01.158666Z","shell.execute_reply.started":"2023-02-15T15:57:01.148522Z","shell.execute_reply":"2023-02-15T15:57:01.157321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('cancer').count()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:01.160522Z","iopub.execute_input":"2023-02-15T15:57:01.160992Z","iopub.status.idle":"2023-02-15T15:57:01.206028Z","shell.execute_reply.started":"2023-02-15T15:57:01.160958Z","shell.execute_reply":"2023-02-15T15:57:01.204845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = df.cancer == 1\ndf2 = df[mask]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:01.20731Z","iopub.execute_input":"2023-02-15T15:57:01.207669Z","iopub.status.idle":"2023-02-15T15:57:01.215255Z","shell.execute_reply.started":"2023-02-15T15:57:01.207639Z","shell.execute_reply":"2023-02-15T15:57:01.214068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Seperate cancer labeled dcm.","metadata":{}},{"cell_type":"code","source":"base_dir = \"/kaggle/input/rsna-breast-cancer-detection/train_images\"\ncancer1_images = []\n\ndef get_dcm_file(row):\n    image_path = os.path.join(base_dir, str(row.patient_id), str(row.image_id))\n    cancer1_images.append(image_path+\".dcm\")\n    \nfor i, row in df2.iterrows():\n    get_dcm_file(row)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:01.216784Z","iopub.execute_input":"2023-02-15T15:57:01.217219Z","iopub.status.idle":"2023-02-15T15:57:01.314865Z","shell.execute_reply.started":"2023-02-15T15:57:01.217175Z","shell.execute_reply":"2023-02-15T15:57:01.313649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"working_dir = \"/kaggle/working/\"\nos.mkdir(working_dir+\"cancer\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:11:13.435926Z","iopub.execute_input":"2023-02-15T16:11:13.436335Z","iopub.status.idle":"2023-02-15T16:11:13.442511Z","shell.execute_reply.started":"2023-02-15T16:11:13.436301Z","shell.execute_reply":"2023-02-15T16:11:13.441404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom PIL import Image\nfrom pydicom import dcmread\nimport pylibjpeg\n\nfrom matplotlib import pyplot as plt\n\n\n\ndef dcm_to_png(path:str):\n    # Load the DICOM file\n    dcm_file = pydicom.dcmread(path)\n    # Get the pixel array from the DICOM file\n    pixel_array = dcm_file.pixel_array\n    # Resize the pixel array to the desired size using the Pillow library\n    resized_array = Image.fromarray(pixel_array).resize((256, 256))\n#     plt.imshow(resized_array, cmap='gray')\n    basename, _ = os.path.splitext(path)\n    print(basename)\n    p = os.path.join(working_dir, \"cancer\" ,os.path.basename(basename))\n    print(p+\".png\")\n    # Save the resized image as a PNG file\n    resized_array.save(p+\".png\")\n                                    \n\nfor img in cancer1_images:\n    dcm_to_png(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = df[~mask]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:36:02.208575Z","iopub.execute_input":"2023-02-15T16:36:02.211221Z","iopub.status.idle":"2023-02-15T16:36:02.242849Z","shell.execute_reply.started":"2023-02-15T16:36:02.211121Z","shell.execute_reply":"2023-02-15T16:36:02.241438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:36:14.786255Z","iopub.execute_input":"2023-02-15T16:36:14.786882Z","iopub.status.idle":"2023-02-15T16:36:14.798182Z","shell.execute_reply.started":"2023-02-15T16:36:14.78683Z","shell.execute_reply":"2023-02-15T16:36:14.796849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer0_images = []\n\ndef get_dcm_file2(row):\n    image_path = os.path.join(base_dir, str(row.patient_id), str(row.image_id))\n    cancer0_images.append(image_path+\".dcm\")\n\nfor index, row in df3.iterrows():\n    if index >= 1200:\n        break  \n    get_dcm_file2(row)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:41:57.215821Z","iopub.execute_input":"2023-02-15T16:41:57.217093Z","iopub.status.idle":"2023-02-15T16:41:57.338974Z","shell.execute_reply.started":"2023-02-15T16:41:57.217043Z","shell.execute_reply":"2023-02-15T16:41:57.337362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(working_dir+\"no_cancer\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:43:46.513998Z","iopub.execute_input":"2023-02-15T16:43:46.515174Z","iopub.status.idle":"2023-02-15T16:43:46.520101Z","shell.execute_reply.started":"2023-02-15T16:43:46.515131Z","shell.execute_reply":"2023-02-15T16:43:46.518963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dcm_to_png2(path:str):\n    # Load the DICOM file\n    dcm_file = pydicom.dcmread(path)\n    # Get the pixel array from the DICOM file\n    pixel_array = dcm_file.pixel_array\n    # Resize the pixel array to the desired size using the Pillow library\n    resized_array = Image.fromarray(pixel_array).resize((256, 256))\n#     plt.imshow(resized_array, cmap='gray')\n    basename, _ = os.path.splitext(path)\n    print(basename)\n    p = os.path.join(working_dir, \"no_cancer\" ,os.path.basename(basename))\n    print(p+\".png\")\n    # Save the resized image as a PNG file\n    resized_array.save(p+\".png\")\n                                    \n\nfor img in cancer0_images:\n    dcm_to_png2(img)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:44:40.697722Z","iopub.execute_input":"2023-02-15T16:44:40.698188Z","iopub.status.idle":"2023-02-15T16:59:48.93245Z","shell.execute_reply.started":"2023-02-15T16:44:40.698154Z","shell.execute_reply":"2023-02-15T16:59:48.931064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(cancer0_images)\ncancer0_images","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/cancer/","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:15:48.796196Z","iopub.execute_input":"2023-02-15T16:15:48.79745Z","iopub.status.idle":"2023-02-15T16:15:49.917063Z","shell.execute_reply.started":"2023-02-15T16:15:48.797396Z","shell.execute_reply":"2023-02-15T16:15:49.915622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\n# Create a list of items to iterate over\nitems = range(100)\n\n# Wrap the for loop with tqdm\nfor item in tqdm(items):\n    # Do some work with the item\n    # ...\n    1+1\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink -> FileLink(r'/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:04:18.980085Z","iopub.execute_input":"2023-02-15T17:04:18.981444Z","iopub.status.idle":"2023-02-15T17:04:18.988635Z","shell.execute_reply.started":"2023-02-15T17:04:18.981384Z","shell.execute_reply":"2023-02-15T17:04:18.987456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_array = plt.imread(\"/kaggle/working/cancer/388811999.png\")\n\n# Display the image using imshow()\nplt.imshow(image_array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T16:14:33.489004Z","iopub.execute_input":"2023-02-15T16:14:33.489402Z","iopub.status.idle":"2023-02-15T16:14:33.667963Z","shell.execute_reply.started":"2023-02-15T16:14:33.489371Z","shell.execute_reply":"2023-02-15T16:14:33.66663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntxt_files = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\n\nlen(txt_files)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:02.173877Z","iopub.status.idle":"2023-02-15T15:57:02.17432Z","shell.execute_reply.started":"2023-02-15T15:57:02.174108Z","shell.execute_reply":"2023-02-15T15:57:02.174135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom PIL import Image\n\n# Load the DICOM file\ndcm_file = pydicom.dcmread(\"path/to/dcm/file.dcm\")\n\n# Get the pixel array from the DICOM file\npixel_array = dcm_file.pixel_array\n\n# Resize the pixel array to the desired size using the Pillow library\nresized_array = Image.fromarray(pixel_array).resize((256, 256))\n\n# Save the resized image as a PNG file\nresized_array.save(\"path/to/resized/image.png\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T15:57:02.175889Z","iopub.status.idle":"2023-02-15T15:57:02.176332Z","shell.execute_reply.started":"2023-02-15T15:57:02.17613Z","shell.execute_reply":"2023-02-15T15:57:02.17615Z"},"trusted":true},"execution_count":null,"outputs":[]}]}