{"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":"markdown","source":"# A helpful function for converting DICOM to NUMPY by VTK","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:24:42.426276Z","iopub.execute_input":"2021-07-16T15:24:42.42662Z","iopub.status.idle":"2021-07-16T15:24:42.430567Z","shell.execute_reply.started":"2021-07-16T15:24:42.426589Z","shell.execute_reply":"2021-07-16T15:24:42.429603Z"}}},{"cell_type":"code","source":"import keras\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport seaborn as sns\n%matplotlib inline\nimport vtk\nfrom vtk.util import numpy_support\nimport os\nimport numpy\nfrom matplotlib import cm\n%pylab inline","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:21:52.558432Z","iopub.execute_input":"2021-07-16T15:21:52.558795Z","iopub.status.idle":"2021-07-16T15:21:52.564879Z","shell.execute_reply.started":"2021-07-16T15:21:52.558761Z","shell.execute_reply":"2021-07-16T15:21:52.563815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.path.join('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train')\ntest_dir = os.path.join('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test')\ntrain_label=os.path.join('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\nprint('total training cases:', len(os.listdir(train_dir)))\nprint('total test cases:', len(os.listdir(test_dir)))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:12:31.034418Z","iopub.execute_input":"2021-07-16T15:12:31.034882Z","iopub.status.idle":"2021-07-16T15:12:31.092955Z","shell.execute_reply.started":"2021-07-16T15:12:31.034837Z","shell.execute_reply":"2021-07-16T15:12:31.091963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting Function","metadata":{}},{"cell_type":"code","source":"def dcm_to_npy(path_dir=train_dir,case_number='00000',mode='FLAIR'):\n    PathDicom = f\"{path_dir}\"+\"/\"+f\"{case_number}\"+\"/\"+f\"{mode}\"\n    reader = vtk.vtkDICOMImageReader()\n    reader.SetDirectoryName(PathDicom)\n    reader.Update()\n    _extent = reader.GetDataExtent()\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1]\n    ConstPixelSpacing = reader.GetPixelSpacing()\n    x = numpy.arange(0.0, (ConstPixelDims[0]+1)*ConstPixelSpacing[0], ConstPixelSpacing[0])\n    y = numpy.arange(0.0, (ConstPixelDims[1]+1)*ConstPixelSpacing[1], ConstPixelSpacing[1])\n    z = numpy.arange(0.0, (ConstPixelDims[2]+1)*ConstPixelSpacing[2], ConstPixelSpacing[2])\n    imageData = reader.GetOutput()\n    pointData = imageData.GetPointData()\n    assert (pointData.GetNumberOfArrays()==1)\n    arrayData = pointData.GetArray(0)\n\n    ArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n    ArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\n    return ArrayDicom\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:32:13.935854Z","iopub.execute_input":"2021-07-16T15:32:13.936245Z","iopub.status.idle":"2021-07-16T15:32:13.943666Z","shell.execute_reply.started":"2021-07-16T15:32:13.936208Z","shell.execute_reply":"2021-07-16T15:32:13.942982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Example ","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:32:31.594687Z","iopub.execute_input":"2021-07-16T15:32:31.595244Z","iopub.status.idle":"2021-07-16T15:32:31.599276Z","shell.execute_reply.started":"2021-07-16T15:32:31.595196Z","shell.execute_reply":"2021-07-16T15:32:31.598463Z"}}},{"cell_type":"code","source":"Case_100_T1w=dcm_to_npy(path_dir=train_dir,case_number='00100',mode='T1w')\nCase_100_T1w.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:17:55.015714Z","iopub.execute_input":"2021-07-16T15:17:55.016084Z","iopub.status.idle":"2021-07-16T15:17:55.248655Z","shell.execute_reply.started":"2021-07-16T15:17:55.016051Z","shell.execute_reply":"2021-07-16T15:17:55.247945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:32:57.507154Z","iopub.execute_input":"2021-07-16T15:32:57.507632Z","iopub.status.idle":"2021-07-16T15:32:57.511358Z","shell.execute_reply.started":"2021-07-16T15:32:57.507601Z","shell.execute_reply":"2021-07-16T15:32:57.510508Z"}}},{"cell_type":"code","source":"def visualize(path_dir=train_dir,case_number='00000',mode='FLAIR',slice_index=0):\n    ArrayDicom=dcm_to_npy(path_dir,case_number,mode)\n    plt.figure(figsize=(5, 5))\n    pyplot.set_cmap(pyplot.gray())\n    plt.imshow(numpy.rot90(ArrayDicom[:, :, slice_index]))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:19:52.548751Z","iopub.execute_input":"2021-07-16T15:19:52.549076Z","iopub.status.idle":"2021-07-16T15:19:52.554095Z","shell.execute_reply.started":"2021-07-16T15:19:52.549049Z","shell.execute_reply":"2021-07-16T15:19:52.553313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(path_dir=train_dir,case_number='00100',mode='T1w',slice_index=10)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:20:03.919141Z","iopub.execute_input":"2021-07-16T15:20:03.919471Z","iopub.status.idle":"2021-07-16T15:20:04.111206Z","shell.execute_reply.started":"2021-07-16T15:20:03.91944Z","shell.execute_reply":"2021-07-16T15:20:04.110081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(path_dir=train_dir,case_number='00100',mode='FLAIR',slice_index=10)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:29:27.213564Z","iopub.execute_input":"2021-07-16T15:29:27.21405Z","iopub.status.idle":"2021-07-16T15:29:27.545191Z","shell.execute_reply.started":"2021-07-16T15:29:27.214019Z","shell.execute_reply":"2021-07-16T15:29:27.544216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}