{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":9435964,"sourceType":"datasetVersion","datasetId":5733406}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom\nimport cv2\nfrom glob import glob\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-19T14:35:21.829785Z","iopub.execute_input":"2024-09-19T14:35:21.830206Z","iopub.status.idle":"2024-09-19T14:35:23.489381Z","shell.execute_reply.started":"2024-09-19T14:35:21.830163Z","shell.execute_reply":"2024-09-19T14:35:23.488318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/rsna0919data1/tmp1_new.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:35:40.691631Z","iopub.execute_input":"2024-09-19T14:35:40.692380Z","iopub.status.idle":"2024-09-19T14:35:40.777742Z","shell.execute_reply.started":"2024-09-19T14:35:40.692328Z","shell.execute_reply":"2024-09-19T14:35:40.776581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_df = data.groupby(['study_id', 'series_id']).apply(lambda x: x.sort_values(by='level')).reset_index(drop=True)\nsorted_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:37:43.802163Z","iopub.execute_input":"2024-09-19T14:37:43.803035Z","iopub.status.idle":"2024-09-19T14:37:44.968206Z","shell.execute_reply.started":"2024-09-19T14:37:43.802987Z","shell.execute_reply":"2024-09-19T14:37:44.967023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = sorted_df.groupby(['study_id'])['level'].count().reset_index()\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:40:18.299400Z","iopub.execute_input":"2024-09-19T14:40:18.300095Z","iopub.status.idle":"2024-09-19T14:40:18.311251Z","shell.execute_reply.started":"2024-09-19T14:40:18.300049Z","shell.execute_reply":"2024-09-19T14:40:18.310057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = []\nfor study_id in df['study_id'].tolist():\n    tmp = sorted_df.loc[sorted_df['study_id']==study_id].copy()\n    tmp['next_x'] = tmp['x'].shift(-1)\n    tmp['next_y'] = tmp['y'].shift(-1)\n    tmp = tmp.dropna()\n    tmp['distance'] = np.sqrt((tmp['next_x'] - tmp['x'])**2 + (tmp['next_y'] - tmp['y'])**2)\n    variance = tmp['distance'].var()\n    res.append(variance)  ","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:48:36.192421Z","iopub.execute_input":"2024-09-19T14:48:36.193357Z","iopub.status.idle":"2024-09-19T14:48:42.146015Z","shell.execute_reply.started":"2024-09-19T14:48:36.193307Z","shell.execute_reply":"2024-09-19T14:48:42.144903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['var'] = res","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:49:09.557695Z","iopub.execute_input":"2024-09-19T14:49:09.558133Z","iopub.status.idle":"2024-09-19T14:49:09.564489Z","shell.execute_reply.started":"2024-09-19T14:49:09.558092Z","shell.execute_reply":"2024-09-19T14:49:09.563318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['var'].describe()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:49:17.543689Z","iopub.execute_input":"2024-09-19T14:49:17.544094Z","iopub.status.idle":"2024-09-19T14:49:17.557091Z","shell.execute_reply.started":"2024-09-19T14:49:17.544058Z","shell.execute_reply":"2024-09-19T14:49:17.555915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.sort_values(by=['var'], ascending=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:50:34.118506Z","iopub.execute_input":"2024-09-19T14:50:34.118934Z","iopub.status.idle":"2024-09-19T14:50:34.131301Z","shell.execute_reply.started":"2024-09-19T14:50:34.118896Z","shell.execute_reply":"2024-09-19T14:50:34.130283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  遍历\npath = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\ni=0\nfor study_id in df['study_id'].tolist():\n    tmp = sorted_df.loc[sorted_df['study_id']==study_id].copy()\n    study_id, series_id, instance_number = tmp['study_id'].iloc[0], tmp['series_id'].iloc[0], tmp['instance_number'].iloc[0]\n    img_path = os.path.join(path, str(study_id), str(series_id),str(instance_number)+'.dcm')\n    dicom = pydicom.dcmread(img_path)\n    img = dicom.pixel_array\n    plt.grid(False)\n    plt.imshow(img, cmap=\"gray\")\n    \n    print(img_path)\n    x_coords = tmp['x'].tolist()\n    y_coords = tmp['y'].tolist()\n    plt.scatter(x_coords, y_coords, color='red')\n    \n    # 给每个点添加标签\n    labels = tmp['combine'].tolist()\n    for x, y, label in zip(x_coords, y_coords, labels):\n        plt.text(x, y, label, color='red', fontsize=12, ha='left', va='bottom')\n        \n    plt.show()\n    i+=1\n    if i>30:\n        break","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:17:34.808225Z","iopub.execute_input":"2024-09-19T15:17:34.809271Z","iopub.status.idle":"2024-09-19T15:17:44.531777Z","shell.execute_reply.started":"2024-09-19T15:17:34.809223Z","shell.execute_reply":"2024-09-19T15:17:44.530641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}