{"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":87331,"databundleVersionId":9932741,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"## defining the path for parent directory\nDIR = '/kaggle/input/airs-ai-in-respiratory-sounds/'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:37:45.817279Z","iopub.execute_input":"2024-10-23T12:37:45.817684Z","iopub.status.idle":"2024-10-23T12:37:45.855490Z","shell.execute_reply.started":"2024-10-23T12:37:45.817646Z","shell.execute_reply":"2024-10-23T12:37:45.854301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom glob import glob\nimport json\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\nfrom mpl_toolkits.mplot3d import Axes3D\nimport plotly.graph_objs as go\n\n\npd.set_option('future.no_silent_downcasting', True)\n\nfor filenames in glob(DIR+'*'):\n    print(filenames)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:37:45.857576Z","iopub.execute_input":"2024-10-23T12:37:45.859254Z","iopub.status.idle":"2024-10-23T12:37:49.285795Z","shell.execute_reply.started":"2024-10-23T12:37:45.859165Z","shell.execute_reply":"2024-10-23T12:37:49.284426Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Loading and visualising training data","metadata":{}},{"cell_type":"code","source":"# defining all paths\ntr_path = '/kaggle/input/airs-ai-in-respiratory-sounds/train.csv'\nts_path = '/kaggle/input/airs-ai-in-respiratory-sounds/test.csv'\nfiles_path = '/kaggle/input/airs-ai-in-respiratory-sounds/sounds/sounds/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:37:49.287576Z","iopub.execute_input":"2024-10-23T12:37:49.288286Z","iopub.status.idle":"2024-10-23T12:37:49.294520Z","shell.execute_reply.started":"2024-10-23T12:37:49.288230Z","shell.execute_reply":"2024-10-23T12:37:49.292815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(tr_path)\ndf_test = pd.read_csv(ts_path)\n\ndf_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:37:49.297693Z","iopub.execute_input":"2024-10-23T12:37:49.298077Z","iopub.status.idle":"2024-10-23T12:37:49.354836Z","shell.execute_reply.started":"2024-10-23T12:37:49.298038Z","shell.execute_reply":"2024-10-23T12:37:49.353708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.groupby('disease').describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:37:49.356482Z","iopub.execute_input":"2024-10-23T12:37:49.356835Z","iopub.status.idle":"2024-10-23T12:37:49.463924Z","shell.execute_reply.started":"2024-10-23T12:37:49.356798Z","shell.execute_reply":"2024-10-23T12:37:49.462615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extracting only id and disease mapping to use vowel and sound embeddings\ndisease_map = {d['candidateID']: d['disease'] for d in df_train.to_dict('records')}\n\n# Assuming each cough bout as a unique sample and loading cough sound embeddings for each id (each individual coughed more than once)\nXtr = []\nytr = []\nXts = []\ntest_ids = []\n\n\nfor _id in glob(files_path+'*'):\n    emb_path = os.path.join(_id,'emb_cough.json')\n    cand_id = os.path.basename(_id)\n    if os.path.exists(emb_path):\n        embs = json.load(open(emb_path, 'r'))\n        if disease_map.get(cand_id) is not None:\n            for emb in embs:\n                Xtr.append(emb)\n                ytr.append(disease_map[cand_id])\n        else:\n            for emb in embs:\n                Xts.append(emb)\n                test_ids.append(cand_id)\n\n\nprint(len(Xtr), len(ytr), len(Xts))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:38:04.680694Z","iopub.execute_input":"2024-10-23T12:38:04.681245Z","iopub.status.idle":"2024-10-23T12:38:09.219471Z","shell.execute_reply.started":"2024-10-23T12:38:04.681196Z","shell.execute_reply":"2024-10-23T12:38:09.218067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# converting into numpy array\nXtr = np.array(Xtr)\nytr = np.array(ytr)\nXts = np.array(Xts)\n\nXtr.shape, ytr.shape, Xts.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:17.088631Z","iopub.execute_input":"2024-10-23T12:41:17.089106Z","iopub.status.idle":"2024-10-23T12:41:17.209583Z","shell.execute_reply.started":"2024-10-23T12:41:17.089045Z","shell.execute_reply":"2024-10-23T12:41:17.208248Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 3D Visaulization try using PCA and UMAP","metadata":{}},{"cell_type":"code","source":"def get_plot(embed, all_diseases, labels):\n    # Create the 3D scatter plot\n    fig = go.Figure(data=[go.Scatter3d(\n        x=embed[:, 0],\n        y=embed[:, 1],\n        z=embed[:, 2],\n        mode='markers',\n        marker=dict(\n            size=5,\n            color=[all_diseases.index(l) for l in labels],                # Color by labels\n            colorscale='Viridis',        # Choose a colorscale\n            opacity=0.8\n        ),\n        text=labels                      # Text to display on hover\n    )])\n    \n    # Update layout for better visualization\n    fig.update_layout(\n        scene=dict(\n            xaxis_title='PCA Component 1',\n            yaxis_title='PCA Component 2',\n            zaxis_title='PCA Component 3',\n        ),\n        title=f'3D PCA of Embeddings (Variance Explained: {cumulative_variance_explained_first_3:.2%})',\n        width=800,\n        height=600\n    )\n\n    return fig","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:18.022737Z","iopub.execute_input":"2024-10-23T12:41:18.023301Z","iopub.status.idle":"2024-10-23T12:41:18.034849Z","shell.execute_reply.started":"2024-10-23T12:41:18.023245Z","shell.execute_reply":"2024-10-23T12:41:18.033380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=10)\nreduced_embeddings = pca.fit_transform(Xtr)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:19.209926Z","iopub.execute_input":"2024-10-23T12:41:19.210378Z","iopub.status.idle":"2024-10-23T12:41:19.500807Z","shell.execute_reply.started":"2024-10-23T12:41:19.210336Z","shell.execute_reply":"2024-10-23T12:41:19.499797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n = 3\n\nexplained_variance = pca.explained_variance_ratio_\n\n# Calculate the cumulative variance explained by the first 3 components\ncumulative_variance_explained_first_3 = np.sum(explained_variance[:n])\n\nprint(f\"Cumulative Variance Explained by the first {n} components: {cumulative_variance_explained_first_3:.2%}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:20.435192Z","iopub.execute_input":"2024-10-23T12:41:20.435604Z","iopub.status.idle":"2024-10-23T12:41:20.443665Z","shell.execute_reply.started":"2024-10-23T12:41:20.435567Z","shell.execute_reply":"2024-10-23T12:41:20.441737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"get_plot(reduced_embeddings, list(np.unique(ytr)), ytr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:20.756520Z","iopub.execute_input":"2024-10-23T12:41:20.757415Z","iopub.status.idle":"2024-10-23T12:41:21.469682Z","shell.execute_reply.started":"2024-10-23T12:41:20.757361Z","shell.execute_reply":"2024-10-23T12:41:21.468458Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Preprocessing for machine learning tasks","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import f1_score\nfrom sklearn.ensemble import RandomForestClassifier\n\nscaler = StandardScaler()\nXtr = scaler.fit_transform(Xtr)\n\nXts = scaler.transform(Xts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:22.323139Z","iopub.execute_input":"2024-10-23T12:41:22.323600Z","iopub.status.idle":"2024-10-23T12:41:22.523458Z","shell.execute_reply.started":"2024-10-23T12:41:22.323559Z","shell.execute_reply":"2024-10-23T12:41:22.522146Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=500, random_state=42,max_depth=30, class_weight='balanced')\n\n# Train the model\nmodel.fit(Xtr, ytr)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:27.364646Z","iopub.execute_input":"2024-10-23T12:41:27.365145Z","iopub.status.idle":"2024-10-23T12:41:44.119333Z","shell.execute_reply.started":"2024-10-23T12:41:27.365103Z","shell.execute_reply":"2024-10-23T12:41:44.118052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on the test set\ny_pred = model.predict(Xts)\ny_pred_original = model.predict(Xtr)\n\n\nprint(\"\\nTrain Classification Report:\")\nprint(f1_score(ytr, y_pred_original, average='macro'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:41:44.121738Z","iopub.execute_input":"2024-10-23T12:41:44.122473Z","iopub.status.idle":"2024-10-23T12:41:44.475139Z","shell.execute_reply.started":"2024-10-23T12:41:44.122416Z","shell.execute_reply":"2024-10-23T12:41:44.473939Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Submit code and save submission","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(np.array([test_ids, y_pred]).T, columns=['candidateID', 'disease'])\nsubmission = pd.DataFrame(submission.groupby('candidateID')['disease'].agg(lambda x: np.bincount(x).argmax())).reset_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:43:17.033082Z","iopub.execute_input":"2024-10-23T12:43:17.033563Z","iopub.status.idle":"2024-10-23T12:43:17.065565Z","shell.execute_reply.started":"2024-10-23T12:43:17.033519Z","shell.execute_reply":"2024-10-23T12:43:17.064196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# since some of the embeddings are missing 32 predictions are missing in this submission. Therefore mapping all the remaining predictions to 1\nsubmission_map = {m['candidateID']: m['disease'] for m in submission.to_dict('records')}\n\ndf_test['disease'] = df_test['candidateID'].apply(lambda x:submission_map.get(x, 1))\ndf_test[['candidateID', 'disease']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:43:25.070250Z","iopub.execute_input":"2024-10-23T12:43:25.071175Z","iopub.status.idle":"2024-10-23T12:43:25.088760Z","shell.execute_reply.started":"2024-10-23T12:43:25.071124Z","shell.execute_reply":"2024-10-23T12:43:25.087573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.groupby('disease').describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:43:32.987393Z","iopub.execute_input":"2024-10-23T12:43:32.988637Z","iopub.status.idle":"2024-10-23T12:43:33.088658Z","shell.execute_reply.started":"2024-10-23T12:43:32.988579Z","shell.execute_reply":"2024-10-23T12:43:33.087293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test[['candidateID', 'disease']].to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T12:44:18.753881Z","iopub.execute_input":"2024-10-23T12:44:18.754297Z","iopub.status.idle":"2024-10-23T12:44:18.765787Z","shell.execute_reply.started":"2024-10-23T12:44:18.754260Z","shell.execute_reply":"2024-10-23T12:44:18.764204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}