{"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 SimpleITK as sitk\nimport os\nimport multiprocessing\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom tqdm import tqdm\n\n!mkdir img_256/ img_512/ img_1024/\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"3b7cc509-89e0-46d4-8234-35e1465364c8","_cell_guid":"6621d8b3-7642-4174-8eaa-b567acc8f597","execution":{"iopub.status.busy":"2022-12-18T10:56:12.20374Z","iopub.execute_input":"2022-12-18T10:56:12.204129Z","iopub.status.idle":"2022-12-18T10:56:13.227012Z","shell.execute_reply.started":"2022-12-18T10:56:12.204097Z","shell.execute_reply":"2022-12-18T10:56:13.225507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Runn!","metadata":{}},{"cell_type":"code","source":"def convert_image(input_file_name,output_file_name, new_width=None):\n    try:\n        image_file_reader = sitk.ImageFileReader()\n        image_file_reader.SetImageIO(\"GDCMImageIO\")\n        image_file_reader.SetFileName(input_file_name)\n        image_file_reader.ReadImageInformation()\n        image_size = list(image_file_reader.GetSize())\n        if len(image_size) == 3 and image_size[2] == 1:\n            image_size[2] = 0\n        image_file_reader.SetExtractSize(image_size)\n        image = image_file_reader.Execute()\n        if new_width:\n            original_size = image.GetSize()\n            original_spacing = image.GetSpacing()\n            new_spacing = [\n                (original_size[0] - 1) * original_spacing[0] / (new_width - 1)\n            ] * 2\n            new_size = [\n                new_width,\n                int(\n                    (original_size[1] - 1)\n                    * original_spacing[1]\n                    / new_spacing[1]\n                ),\n            ]\n            image = sitk.Resample(\n                image1=image,\n                size=new_size,\n                transform=sitk.Transform(),\n                interpolator=sitk.sitkLinear,\n                outputOrigin=image.GetOrigin(),\n                outputSpacing=new_spacing,\n                outputDirection=image.GetDirection(),\n                defaultPixelValue=0,\n                outputPixelType=image.GetPixelID(),\n            )\n        if image.GetNumberOfComponentsPerPixel() == 1:\n            image = sitk.RescaleIntensity(image, 0, 255)\n            if (\n                image_file_reader.GetMetaData(\"0028|0004\").strip() == \"MONOCHROME1\"\n            ):\n                image = sitk.InvertIntensity(image, maximum=255)\n            image = sitk.Cast(image, sitk.sitkUInt8)\n        sitk.WriteImage(image, output_file_name)\n        print(output_file_name)\n        return True\n    except BaseException:\n        return False","metadata":{"_uuid":"27c63b02-8987-4b97-b67e-c270fc972d4e","_cell_guid":"7625530b-3084-4c59-bf4c-51cc05a1e88a","scrolled":true,"execution":{"iopub.status.busy":"2022-12-18T10:56:13.403387Z","iopub.execute_input":"2022-12-18T10:56:13.40386Z","iopub.status.idle":"2022-12-18T10:56:13.417435Z","shell.execute_reply.started":"2022-12-18T10:56:13.403821Z","shell.execute_reply":"2022-12-18T10:56:13.416182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(row):\n    scale = 512\n    row = row[1]\n    patient_id = row[\"patient_id\"]\n    image_id = row[\"image_id\"]\n    input_file_name = f\"/kaggle/input/rsna-breast-cancer-detection/train_images/{patient_id}/{image_id}.dcm\"\n    output_file_name = f\"/kaggle/working/img_{scale}/{patient_id}_{image_id}.jpg\"\n    convert_image(input_file_name,output_file_name,scale)","metadata":{"_uuid":"814f4afd-7b87-4e45-85d8-65958eec4075","_cell_guid":"ca721049-2884-4a32-906f-71b20b9c7319","execution":{"iopub.status.busy":"2022-12-18T10:56:52.784955Z","iopub.execute_input":"2022-12-18T10:56:52.786209Z","iopub.status.idle":"2022-12-18T10:56:52.792294Z","shell.execute_reply.started":"2022-12-18T10:56:52.786154Z","shell.execute_reply":"2022-12-18T10:56:52.791324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-18T10:57:04.467735Z","iopub.execute_input":"2022-12-18T10:57:04.468878Z","iopub.status.idle":"2022-12-18T10:57:04.580178Z","shell.execute_reply.started":"2022-12-18T10:57:04.468826Z","shell.execute_reply":"2022-12-18T10:57:04.579044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:\n    list(tqdm(pool.imap_unordered(preprocess, df.iterrows()), total=len(df)))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T10:57:05.455558Z","iopub.execute_input":"2022-12-18T10:57:05.456846Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r img.zip img","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}