{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"vscode":{"interpreter":{"hash":"267fdfe96fa7a504aacdc9335fe5a6b8d723b7b9c8037a0d00bde7f4e13d528f"}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install opensmile\nimport opensmile\nimport pandas as pd\nimport numpy as np\nimport glob\nimport os\nimport pickle\nimport random\nimport librosa\nfrom sklearn.preprocessing import MinMaxScaler, StandardScaler\nfrom sklearn.manifold import TSNE\nimport matplotlib.pyplot as plt\nsmile = opensmile.Smile(\n    feature_set=opensmile.FeatureSet.eGeMAPSv02,\n    feature_level=opensmile.FeatureLevel.Functionals,\n    num_workers=24,\n)\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T02:14:18.416639Z","iopub.execute_input":"2023-09-02T02:14:18.416952Z","iopub.status.idle":"2023-09-02T02:14:43.702901Z","shell.execute_reply.started":"2023-09-02T02:14:18.416926Z","shell.execute_reply":"2023-09-02T02:14:43.701835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load Data and Extract Feature","metadata":{}},{"cell_type":"code","source":"rangpur_data = glob.glob('/kaggle/input/rangpur-train-test/rangpur/*/*.wav')\ndf_rangpur = smile.process_files(rangpur_data)\n\n\nctg_data = glob.glob('/kaggle/input/chittagong-train-test/chittagong/*/*.wav')\ndf_ctg = smile.process_files(ctg_data)\n\n\nkishor_data = glob.glob('/kaggle/input/kishoreganj-train-test/kishoreganj/*/*.wav')\ndf_kishor = smile.process_files(kishor_data)\n\n\nnarail_data = glob.glob('/kaggle/input/narail-train-test/narail/*/*.wav')\ndf_narail = smile.process_files(narail_data)\n\nnarsh_data = glob.glob('/kaggle/input/narsingdi-train-test/narsingdi/*/*.wav')\ndf_narsh = smile.process_files(narsh_data)\n\nmacro_data = glob.glob('/kaggle/input/bengaliai-speech-wav-dataset-0/*/*.wav')[:10000]\ndf_macro = smile.process_files(macro_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T02:32:22.931147Z","iopub.execute_input":"2023-09-02T02:32:22.931534Z","iopub.status.idle":"2023-09-02T03:04:48.674770Z","shell.execute_reply.started":"2023-09-02T02:32:22.931506Z","shell.execute_reply":"2023-09-02T03:04:48.673641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Concat All Data","metadata":{}},{"cell_type":"code","source":"vertical_concat = pd.concat([df_rangpur, df_ctg, df_kishor, df_narail, df_narsh, df_macro], axis=0)\nvertical_concat = vertical_concat.dropna().drop_duplicates()\nprint(vertical_concat.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:04:48.678127Z","iopub.execute_input":"2023-09-02T03:04:48.678503Z","iopub.status.idle":"2023-09-02T03:04:48.849772Z","shell.execute_reply.started":"2023-09-02T03:04:48.678471Z","shell.execute_reply":"2023-09-02T03:04:48.848897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_columns = vertical_concat.columns.to_list()\nvertical_index = vertical_concat.index.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:04:48.850884Z","iopub.execute_input":"2023-09-02T03:04:48.851348Z","iopub.status.idle":"2023-09-02T03:04:48.862467Z","shell.execute_reply.started":"2023-09-02T03:04:48.851321Z","shell.execute_reply":"2023-09-02T03:04:48.861298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load GENEVA feature set .pkl file\ndomains = []\nfor f in vertical_index :\n    filename = f[0]\n    domain_name = filename.split(\"/\")[-3]\n    if domain_name == 'bengaliai-speech-wav-dataset-0':\n        domain_name = 'OOD_Speech'\n    domains.append(domain_name)\n#domains","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:04:48.865105Z","iopub.execute_input":"2023-09-02T03:04:48.865855Z","iopub.status.idle":"2023-09-02T03:04:48.884781Z","shell.execute_reply.started":"2023-09-02T03:04:48.865794Z","shell.execute_reply":"2023-09-02T03:04:48.883487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_concat['domain'] = domains\ndf_normalized =  pd.DataFrame(scaler.fit_transform(vertical_concat[vertical_columns]), columns=vertical_columns,)\ndf_normalized","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:04:48.886641Z","iopub.execute_input":"2023-09-02T03:04:48.887088Z","iopub.status.idle":"2023-09-02T03:04:48.951801Z","shell.execute_reply.started":"2023-09-02T03:04:48.887049Z","shell.execute_reply":"2023-09-02T03:04:48.950773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit and transform the dataframe using the scaler object\ndf_normalized = pd.DataFrame(scaler.fit_transform(vertical_concat[vertical_columns]), columns=vertical_columns,)\ndf_normalized[\"domains\"] = domains\n#df_normalized[\"domains\"] = df_normalized[\"domains\"].apply(lambda x: \"OpenSLR\" if x.startswith(\"0\") else x) \n#df_normalized[\"domains\"] = df_normalized[\"domains\"].apply(lambda x: domain_name_map[x] if x in domain_name_map.keys() else x)\n\ndf_normalized = df_normalized[df_normalized['domains']!='OOD_Speech']\n\nlabels = df_normalized[\"domains\"].unique().tolist()\ncolors = plt.cm.get_cmap('tab20').colors\ncolor_map = {label: colors[i % len(colors)] for i, label in enumerate(labels)}\ncolor_map[\"MaCro train\"] = (1,.25,.05)\ndomain_color = [color_map[x] for x in df_normalized['domains']]\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:33:13.571428Z","iopub.execute_input":"2023-09-02T03:33:13.571806Z","iopub.status.idle":"2023-09-02T03:33:13.619656Z","shell.execute_reply.started":"2023-09-02T03:33:13.571779Z","shell.execute_reply":"2023-09-02T03:33:13.618505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['figure.dpi'] = 600\nplt.rcParams['font.size'] = '12'\n# Perform t-SNE on the feature set\ntsne = TSNE(n_components=2, random_state=44, perplexity=30, metric=\"l2\", n_iter=5000, early_exaggeration=50.0)\ntsne_results = tsne.fit_transform(df_normalized[vertical_columns])\n# Plot t-SNE results\n# plt.scatter(tsne_results[:, 0], tsne_results[:, 1], s = .3, c=domain_color)\n# plt.axis('off')\n# plt.legend()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T03:56:47.906669Z","iopub.execute_input":"2023-09-02T03:56:47.907215Z","iopub.status.idle":"2023-09-02T04:00:20.143980Z","shell.execute_reply.started":"2023-09-02T03:56:47.907159Z","shell.execute_reply":"2023-09-02T04:00:20.143000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\ntsne_df   = pd.DataFrame(tsne_results)\nhtw = tsne_df.to_numpy(copy = True)\ntsne_df['tsne_x'] = htw[:, 0]\ntsne_df['tsne_y'] = htw[:, 1]\ntsne_df.head()\nmy_cmap = sns.color_palette(list(color_map.values()))\nplt.rcParams['figure.dpi'] = 600\n\nsns.scatterplot(\n      x       = \"tsne_x\",\n      y       = \"tsne_y\",\n      hue= df_normalized.domains.to_list(),\n      data    = tsne_df,\n      alpha   = 0.9,\n      s= 2,\n      palette = my_cmap\n   )\n\n\nplt.legend( loc = 'best', ncol= 4, fontsize= 6, bbox_to_anchor=(1.1,0), frameon=False)\nplt.axis('off')\n\nplt.savefig('sushmitSNE.png', bbox_inches='tight')\n\n#plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T04:00:20.145836Z","iopub.execute_input":"2023-09-02T04:00:20.146194Z","iopub.status.idle":"2023-09-02T04:00:23.153831Z","shell.execute_reply.started":"2023-09-02T04:00:20.146148Z","shell.execute_reply":"2023-09-02T04:00:23.152705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-01T22:03:32.664367Z","iopub.execute_input":"2023-09-01T22:03:32.664762Z","iopub.status.idle":"2023-09-01T22:03:32.670320Z","shell.execute_reply.started":"2023-09-01T22:03:32.664734Z","shell.execute_reply":"2023-09-01T22:03:32.669185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-01T22:03:57.859243Z","iopub.execute_input":"2023-09-01T22:03:57.859624Z","iopub.status.idle":"2023-09-01T22:03:57.878315Z","shell.execute_reply.started":"2023-09-01T22:03:57.859596Z","shell.execute_reply":"2023-09-01T22:03:57.876940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}