{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":99552,"databundleVersionId":13190393,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport pydicom\nimport numpy as np\nimport os\nimport glob\nfrom tqdm import tqdm\nimport gc\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.113071Z","iopub.execute_input":"2025-07-31T20:22:43.113459Z","iopub.status.idle":"2025-07-31T20:22:43.118514Z","shell.execute_reply.started":"2025-07-31T20:22:43.113433Z","shell.execute_reply":"2025-07-31T20:22:43.117585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv')\ntrain.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.1224Z","iopub.execute_input":"2025-07-31T20:22:43.122733Z","iopub.status.idle":"2025-07-31T20:22:43.170962Z","shell.execute_reply.started":"2025-07-31T20:22:43.12271Z","shell.execute_reply":"2025-07-31T20:22:43.170009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.groupby('Modality').count()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.172419Z","iopub.execute_input":"2025-07-31T20:22:43.172853Z","iopub.status.idle":"2025-07-31T20:22:43.189793Z","shell.execute_reply.started":"2025-07-31T20:22:43.172814Z","shell.execute_reply":"2025-07-31T20:22:43.188784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train[train['Aneurysm Present'] > 0]['SeriesInstanceUID'].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.190811Z","iopub.execute_input":"2025-07-31T20:22:43.191149Z","iopub.status.idle":"2025-07-31T20:22:43.214949Z","shell.execute_reply.started":"2025-07-31T20:22:43.191119Z","shell.execute_reply":"2025-07-31T20:22:43.214036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loc = pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train_localizers.csv')\ntrain_loc.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.217023Z","iopub.execute_input":"2025-07-31T20:22:43.217351Z","iopub.status.idle":"2025-07-31T20:22:43.249364Z","shell.execute_reply.started":"2025-07-31T20:22:43.217326Z","shell.execute_reply":"2025-07-31T20:22:43.248434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_loc.SeriesInstanceUID.unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.250169Z","iopub.execute_input":"2025-07-31T20:22:43.250454Z","iopub.status.idle":"2025-07-31T20:22:43.256305Z","shell.execute_reply.started":"2025-07-31T20:22:43.250432Z","shell.execute_reply":"2025-07-31T20:22:43.255509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path_series = '/kaggle/input/rsna-intracranial-aneurysm-detection/series/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.257516Z","iopub.execute_input":"2025-07-31T20:22:43.257775Z","iopub.status.idle":"2025-07-31T20:22:43.274718Z","shell.execute_reply.started":"2025-07-31T20:22:43.257756Z","shell.execute_reply":"2025-07-31T20:22:43.273756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import ast","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.275629Z","iopub.execute_input":"2025-07-31T20:22:43.276318Z","iopub.status.idle":"2025-07-31T20:22:43.293185Z","shell.execute_reply.started":"2025-07-31T20:22:43.276296Z","shell.execute_reply":"2025-07-31T20:22:43.292287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = train_loc.iloc[np.random.randint(len(train_loc))]\ndcm = pydicom.dcmread(path_series+sample.SeriesInstanceUID+'/'+sample.SOPInstanceUID+'.dcm')\n#ps = dcm.PixelSpacing\nxy = ast.literal_eval(sample.coordinates)\nx = np.rint(xy['x']).astype(int)\ny = np.rint(xy['y']).astype(int)\nz = dcm.InstanceNumber\nloc = sample.location\nprint(z,y,x)\nplt.imshow(dcm.pixel_array)\nplt.title(loc)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.294191Z","iopub.execute_input":"2025-07-31T20:22:43.294451Z","iopub.status.idle":"2025-07-31T20:22:43.612021Z","shell.execute_reply.started":"2025-07-31T20:22:43.29443Z","shell.execute_reply":"2025-07-31T20:22:43.611047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_slices = [pydicom.dcmread(path_series+sample.SeriesInstanceUID+'/'+dcm) for dcm in os.listdir(path_series+sample.SeriesInstanceUID)]\nall_slice_IN = [int(dcm.InstanceNumber) for dcm in all_slices]\nall_slices = [dcm.pixel_array for dcm in all_slices]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:43.613949Z","iopub.execute_input":"2025-07-31T20:22:43.614653Z","iopub.status.idle":"2025-07-31T20:22:44.350442Z","shell.execute_reply.started":"2025-07-31T20:22:43.614614Z","shell.execute_reply":"2025-07-31T20:22:44.349292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_slices = [x for _, x in sorted(zip(all_slice_IN, all_slices))]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.352931Z","iopub.execute_input":"2025-07-31T20:22:44.353229Z","iopub.status.idle":"2025-07-31T20:22:44.357822Z","shell.execute_reply.started":"2025-07-31T20:22:44.353205Z","shell.execute_reply":"2025-07-31T20:22:44.356759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = np.stack(all_slices)\nimage.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.358668Z","iopub.execute_input":"2025-07-31T20:22:44.358971Z","iopub.status.idle":"2025-07-31T20:22:44.382384Z","shell.execute_reply.started":"2025-07-31T20:22:44.358943Z","shell.execute_reply":"2025-07-31T20:22:44.381602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z,y,x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.383376Z","iopub.execute_input":"2025-07-31T20:22:44.383721Z","iopub.status.idle":"2025-07-31T20:22:44.389437Z","shell.execute_reply.started":"2025-07-31T20:22:44.383694Z","shell.execute_reply":"2025-07-31T20:22:44.388604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(y,x)\nplt.imshow(image[z])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.39036Z","iopub.execute_input":"2025-07-31T20:22:44.390641Z","iopub.status.idle":"2025-07-31T20:22:44.654943Z","shell.execute_reply.started":"2025-07-31T20:22:44.390619Z","shell.execute_reply":"2025-07-31T20:22:44.654016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(z,x)\nplt.imshow(image[:,y])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.656046Z","iopub.execute_input":"2025-07-31T20:22:44.656293Z","iopub.status.idle":"2025-07-31T20:22:44.81125Z","shell.execute_reply.started":"2025-07-31T20:22:44.656274Z","shell.execute_reply":"2025-07-31T20:22:44.810524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(z,y)\nplt.imshow(image[:,:,x])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.812082Z","iopub.execute_input":"2025-07-31T20:22:44.812328Z","iopub.status.idle":"2025-07-31T20:22:44.951376Z","shell.execute_reply.started":"2025-07-31T20:22:44.812307Z","shell.execute_reply":"2025-07-31T20:22:44.950432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = path_series+'1.2.826.0.1.3680043.8.498.99892390884723813599532075083872271516/'\nIN = []\nSL = []\nfor s in os.listdir(path):\n    image = pydicom.dcmread(path+s)\n    IN.append(image.InstanceNumber)\n    SL.append(image.SliceLocation)\n#   image.PixelSpacing)\n#   plt.imshow(image.pixel_array)\n#   plt.show()\n\nplt.plot(IN,SL,'.')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-31T20:22:44.952413Z","iopub.execute_input":"2025-07-31T20:22:44.952691Z","iopub.status.idle":"2025-07-31T20:22:45.758368Z","shell.execute_reply.started":"2025-07-31T20:22:44.95267Z","shell.execute_reply":"2025-07-31T20:22:45.757385Z"}},"outputs":[],"execution_count":null}]}