{"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":"gpu","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport glob\nimport pydicom\nimport matplotlib.pyplot as plt\nimport xgboost as xgb","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:05.234518Z","iopub.execute_input":"2024-09-06T13:43:05.234821Z","iopub.status.idle":"2024-09-06T13:43:07.155894Z","shell.execute_reply.started":"2024-09-06T13:43:05.234788Z","shell.execute_reply":"2024-09-06T13:43:07.154893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import fft\nfrom scipy.signal import find_peaks\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.158069Z","iopub.execute_input":"2024-09-06T13:43:07.158640Z","iopub.status.idle":"2024-09-06T13:43:07.211402Z","shell.execute_reply.started":"2024-09-06T13:43:07.158594Z","shell.execute_reply":"2024-09-06T13:43:07.210466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainpath = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv\"\ntrains = pd.read_csv(trainpath)\ntrains = pd.melt(trains, id_vars=['study_id'], \n        value_vars=trains.columns[1:],\n        var_name='condition', \n        value_name='severity')","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.212675Z","iopub.execute_input":"2024-09-06T13:43:07.213185Z","iopub.status.idle":"2024-09-06T13:43:07.261736Z","shell.execute_reply.started":"2024-09-06T13:43:07.213138Z","shell.execute_reply":"2024-09-06T13:43:07.260775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainImgs = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"\ntestImgs = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_images\"","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.264077Z","iopub.execute_input":"2024-09-06T13:43:07.264395Z","iopub.status.idle":"2024-09-06T13:43:07.268600Z","shell.execute_reply.started":"2024-09-06T13:43:07.264362Z","shell.execute_reply":"2024-09-06T13:43:07.267501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loadImages(path, studyID):\n    lst = glob.glob(path+\"/{}/*/*.dcm\".format(studyID))\n    for i in range(len(lst)):\n        lst[i] = pydicom.dcmread(lst[i]).pixel_array\n    return lst","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.269980Z","iopub.execute_input":"2024-09-06T13:43:07.270421Z","iopub.status.idle":"2024-09-06T13:43:07.278592Z","shell.execute_reply.started":"2024-09-06T13:43:07.270378Z","shell.execute_reply":"2024-09-06T13:43:07.277725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getFouriers(path, h):\n    x=loadImages(path,h)\n    for i in range(len(x)):\n        fshift = fft.fftshift(fft.fft2(x[i])) \n        x[i] = 20*np.log(np.abs(fshift))\n    return x","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.279734Z","iopub.execute_input":"2024-09-06T13:43:07.280029Z","iopub.status.idle":"2024-09-06T13:43:07.288624Z","shell.execute_reply.started":"2024-09-06T13:43:07.279998Z","shell.execute_reply":"2024-09-06T13:43:07.287728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def createFeatrueVector(fo):\n    for i in range(len(fo)):\n        c=np.mean(fo[i],axis=0)\n        u,v= find_peaks(c,np.percentile(c,50))\n        fo[i] = sorted(v[\"peak_heights\"])[-3:]\n    return np.max(np.vstack(fo),axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.289844Z","iopub.execute_input":"2024-09-06T13:43:07.290141Z","iopub.status.idle":"2024-09-06T13:43:07.298014Z","shell.execute_reply.started":"2024-09-06T13:43:07.290109Z","shell.execute_reply":"2024-09-06T13:43:07.297061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get feature vectors\nfeatureVectors = {b:createFeatrueVector(getFouriers(trainImgs, b)) for b in trains[\"study_id\"].unique()}","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:07.301215Z","iopub.execute_input":"2024-09-06T13:43:07.301489Z","iopub.status.idle":"2024-09-06T13:43:12.460704Z","shell.execute_reply.started":"2024-09-06T13:43:07.301459Z","shell.execute_reply":"2024-09-06T13:43:12.458369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#assemble feature vectors into the dataframe\ndf=pd.DataFrame(np.vstack(list(featureVectors.values())))\ndf[\"study_id\"]=featureVectors.keys()","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.461573Z","iopub.status.idle":"2024-09-06T13:43:12.461897Z","shell.execute_reply.started":"2024-09-06T13:43:12.461733Z","shell.execute_reply":"2024-09-06T13:43:12.461750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#merge with the labels\ntrainingData = pd.merge(df,trains,on=\"study_id\")\ntrainingData.drop(columns=[\"study_id\"], inplace=True)\ntrainingData.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.463296Z","iopub.status.idle":"2024-09-06T13:43:12.463669Z","shell.execute_reply.started":"2024-09-06T13:43:12.463463Z","shell.execute_reply":"2024-09-06T13:43:12.463481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=trainingData.drop(columns=[\"severity\"])\ny=trainingData[\"severity\"]","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.464857Z","iopub.status.idle":"2024-09-06T13:43:12.465185Z","shell.execute_reply.started":"2024-09-06T13:43:12.465017Z","shell.execute_reply":"2024-09-06T13:43:12.465033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sst up label encoders\nl_severity = LabelEncoder()\nl_severity.fit(y)\n\nl_condition = LabelEncoder()\nl_condition.fit(X[\"condition\"])","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.466836Z","iopub.status.idle":"2024-09-06T13:43:12.467296Z","shell.execute_reply.started":"2024-09-06T13:43:12.467054Z","shell.execute_reply":"2024-09-06T13:43:12.467078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply label encoders\ny=l_severity.transform(y)\nX[\"condition\"]=l_condition.transform(X[\"condition\"])","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.468265Z","iopub.status.idle":"2024-09-06T13:43:12.468737Z","shell.execute_reply.started":"2024-09-06T13:43:12.468483Z","shell.execute_reply":"2024-09-06T13:43:12.468506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#separate train and validation data\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.470148Z","iopub.status.idle":"2024-09-06T13:43:12.470622Z","shell.execute_reply.started":"2024-09-06T13:43:12.470366Z","shell.execute_reply":"2024-09-06T13:43:12.470390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create xgboost classifier\nclf = xgb.XGBClassifier(tree_method=\"hist\", enable_categorical=True, device=\"cuda\")\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.472310Z","iopub.status.idle":"2024-09-06T13:43:12.472689Z","shell.execute_reply.started":"2024-09-06T13:43:12.472490Z","shell.execute_reply":"2024-09-06T13:43:12.472508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test against validation data\ny_pred = clf.predict(X_test)\nprint(round(accuracy_score(y_test, y_pred)*100,2))","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.473463Z","iopub.status.idle":"2024-09-06T13:43:12.473807Z","shell.execute_reply.started":"2024-09-06T13:43:12.473640Z","shell.execute_reply":"2024-09-06T13:43:12.473657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testPath = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_series_descriptions.csv\"\ntestFrame = pd.read_csv(testPath)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.474978Z","iopub.status.idle":"2024-09-06T13:43:12.475305Z","shell.execute_reply.started":"2024-09-06T13:43:12.475144Z","shell.execute_reply":"2024-09-06T13:43:12.475161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testIDs = testFrame[\"study_id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.476377Z","iopub.status.idle":"2024-09-06T13:43:12.476719Z","shell.execute_reply.started":"2024-09-06T13:43:12.476529Z","shell.execute_reply":"2024-09-06T13:43:12.476561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conditions=trains[\"condition\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.557135Z","iopub.status.idle":"2024-09-06T13:43:12.557527Z","shell.execute_reply.started":"2024-09-06T13:43:12.557345Z","shell.execute_reply":"2024-09-06T13:43:12.557363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"severity_level = l_severity.inverse_transform(clf.classes_)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.559686Z","iopub.status.idle":"2024-09-06T13:43:12.560045Z","shell.execute_reply.started":"2024-09-06T13:43:12.559872Z","shell.execute_reply":"2024-09-06T13:43:12.559891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss=\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/sample_submission.csv\"\nsub=pd.read_csv(ss)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.561204Z","iopub.status.idle":"2024-09-06T13:43:12.561538Z","shell.execute_reply.started":"2024-09-06T13:43:12.561371Z","shell.execute_reply":"2024-09-06T13:43:12.561389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preDict={}\nfor k in testFrame[\"study_id\"].unique():\n    for s in conditions:\n        a=np.append(createFeatrueVector(getFouriers(testImgs, k)),l_condition.transform([s]))\n        row=str(k)+\"_\"+s\n        arr=np.mean(clf.predict_proba(a),axis=0)\n        preDict[row]=arr","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.562966Z","iopub.status.idle":"2024-09-06T13:43:12.563285Z","shell.execute_reply.started":"2024-09-06T13:43:12.563125Z","shell.execute_reply":"2024-09-06T13:43:12.563142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in preDict.keys():\n    a,b,c = tuple(preDict[k])\n    sub.loc[sub.row_id == k, 'normal_mild'] = b\n    sub.loc[sub.row_id == k, 'moderate'] = a\n    sub.loc[sub.row_id == k, 'severe'] = c","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.564212Z","iopub.status.idle":"2024-09-06T13:43:12.564553Z","shell.execute_reply.started":"2024-09-06T13:43:12.564372Z","shell.execute_reply":"2024-09-06T13:43:12.564389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T13:43:12.565584Z","iopub.status.idle":"2024-09-06T13:43:12.565897Z","shell.execute_reply.started":"2024-09-06T13:43:12.565737Z","shell.execute_reply":"2024-09-06T13:43:12.565753Z"},"trusted":true},"execution_count":null,"outputs":[]}]}