{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport pydicom","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dcm_paths = [\n    '../input/rsna-str-pulmonary-embolism-detection/train/00c07cd8129d/8877e4d12ce9/08700796d033.dcm',\n    '../input/rsna-str-pulmonary-embolism-detection/train/00c73e5a4e16/f41d8a527040/00eb856c55c2.dcm',\n    '../input/rsna-str-pulmonary-embolism-detection/train/010f10503133/0fb5dd84a89d/159172ebe2ef.dcm',\n    '../input/rsna-str-pulmonary-embolism-detection/train/01b3538d15d6/cd952ef9417e/01bbf9f5eafd.dcm',\n    '../input/rsna-str-pulmonary-embolism-detection/train/01d7afb6c23c/9ef17590cf19/1bba75d0969b.dcm',\n    '../input/rsna-str-pulmonary-embolism-detection/train/038c6bf912f4/c6d860f22aae/00cbd2da9ab5.dcm',\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs = [pydicom.dcmread(f) for f in dcm_paths]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pa = [img.pixel_array for img in imgs]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"row1 = np.concatenate([pa[0],pa[1],pa[2]], axis=1)\nrow2 = np.concatenate([pa[3],pa[4],pa[5]], axis=1)\nrows = np.concatenate([row1, row2], axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(20,10))\nplt.imshow(rows)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def set_outside_scanner_to_air(raw_pixelarrays):\n    # in OSIC we find outside-scanner-regions with raw-values of -2000. \n    # Let's threshold between air (0) and this default (-2000) using -1000\n    raw_pixelarrays[raw_pixelarrays <= -1000] = 0\n    return raw_pixelarrays","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def transform_to_hu(slices):\n    images = np.stack([file.pixel_array for file in slices])\n    images = images.astype(np.int16)\n\n    images = set_outside_scanner_to_air(images)\n    \n    # convert to HU\n    for n in range(len(slices)):\n        #continue\n        \n        intercept = slices[n].RescaleIntercept\n        slope = slices[n].RescaleSlope\n        \n        if slope != 1:\n            images[n] = slope * images[n].astype(np.float64)\n            images[n] = images[n].astype(np.int16)\n            \n        images[n] += np.int16(intercept)\n    \n    return np.array(images, dtype=np.int16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trans = [transform_to_hu([img])[0] for img in imgs]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"row1 = np.concatenate([trans[0],trans[1],trans[2]], axis=1)\nrow2 = np.concatenate([trans[3],trans[4],trans[5]], axis=1)\nrows = np.concatenate([row1, row2], axis=0)\nplt.figure(figsize=(20,10))\nplt.imshow(rows)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}