{"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":"%%capture\n!pip install audiomentations","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:13:51.414355Z","iopub.execute_input":"2024-10-26T17:13:51.414853Z","iopub.status.idle":"2024-10-26T17:14:17.187626Z","shell.execute_reply.started":"2024-10-26T17:13:51.414803Z","shell.execute_reply":"2024-10-26T17:14:17.186133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport shutil\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom pydub import AudioSegment\n\nimport torch\nimport torchaudio\nimport torchaudio.functional as F\n\nimport matplotlib.pyplot as plt\n\nimport soundfile as sf\nfrom scipy.signal import resample\n\nfrom audiomentations import BandPassFilter\nfrom audiomentations import TimeMask\nfrom audiomentations import AddGaussianSNR\nfrom audiomentations import ApplyImpulseResponse","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-26T18:12:48.201513Z","iopub.execute_input":"2024-10-26T18:12:48.201997Z","iopub.status.idle":"2024-10-26T18:12:48.210336Z","shell.execute_reply.started":"2024-10-26T18:12:48.201951Z","shell.execute_reply":"2024-10-26T18:12:48.208810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/airs-ai-in-respiratory-sounds/train.csv')\ntest = pd.read_csv('/kaggle/input/airs-ai-in-respiratory-sounds/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:17:06.305267Z","iopub.execute_input":"2024-10-26T17:17:06.305923Z","iopub.status.idle":"2024-10-26T17:17:06.343461Z","shell.execute_reply.started":"2024-10-26T17:17:06.305874Z","shell.execute_reply":"2024-10-26T17:17:06.342294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pydub import AudioSegment\nimport os\nimport shutil\n\n# Пути к исходной и рабочей папкам\nsource_dir = '/kaggle/input/airs-ai-in-respiratory-sounds/sounds/sounds/'\ndestination_dir = '/kaggle/working/sounds_original_6sec/'\n\n# Создаем новую папку для копирования файлов\nos.makedirs(destination_dir, exist_ok=True)\n\n# Функция для копирования, вырезки и генерации новых названий файлов\ndef copy_trim_and_rename_files(candidateID, source_dir, destination_dir):\n    candidate_folder = os.path.join(source_dir, candidateID)\n    new_cough_filename = None\n    new_vowel_filename = None\n    \n    # Путь к файлу cough.wav\n    cough_file = os.path.join(candidate_folder, 'cough.wav')\n    if os.path.exists(cough_file):\n        # Загружаем аудио и обрезаем отрезок со 2-й секунды (1000 мс) до 8-й (7000 мс)\n        audio = AudioSegment.from_wav(cough_file)\n        trimmed_audio = audio[1000:7000]  # обрезаем 6 секунд со 2-й секунды\n        \n        # Преобразуем аудио в моно и устанавливаем sample rate 16000\n        trimmed_audio = trimmed_audio.set_frame_rate(16000).set_channels(1)\n        \n        new_cough_filename = f\"{candidateID}_cough.wav\"\n        trimmed_audio.export(os.path.join(destination_dir, new_cough_filename), format=\"wav\")\n    \n    # Путь к файлу vowel.wav\n    vowel_file = os.path.join(candidate_folder, 'vowel.wav')\n    if os.path.exists(vowel_file):\n        # Загружаем аудио и обрезаем отрезок со 2-й секунды (1000 мс) до 8-й (7000 мс)\n        audio = AudioSegment.from_wav(vowel_file)\n        trimmed_audio = audio[1000:7000]  # обрезаем 6 секунд со 2-й секунды\n        \n        # Преобразуем аудио в моно и устанавливаем sample rate 16000\n        trimmed_audio = trimmed_audio.set_frame_rate(16000).set_channels(1)\n        \n        new_vowel_filename = f\"{candidateID}_vowel.wav\"\n        trimmed_audio.export(os.path.join(destination_dir, new_vowel_filename), format=\"wav\")\n    \n    return new_cough_filename, new_vowel_filename\n\n# Обновляем датафреймы train и test\ndef update_dataframe_with_sounds(df, source_dir, destination_dir):\n    df['sound_cough'] = None\n    df['sound_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        new_cough_filename, new_vowel_filename = copy_trim_and_rename_files(candidateID, source_dir, destination_dir)\n        \n        # Обновляем значения в датафрейме\n        df.at[idx, 'sound_cough'] = new_cough_filename\n        df.at[idx, 'sound_vowel'] = new_vowel_filename\n\n# Применяем к train и test\nupdate_dataframe_with_sounds(train, source_dir, destination_dir)\nupdate_dataframe_with_sounds(test, source_dir, destination_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:17:09.431846Z","iopub.execute_input":"2024-10-26T17:17:09.432287Z","iopub.status.idle":"2024-10-26T17:18:01.548396Z","shell.execute_reply.started":"2024-10-26T17:17:09.432244Z","shell.execute_reply":"2024-10-26T17:18:01.547063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:18:01.550393Z","iopub.execute_input":"2024-10-26T17:18:01.550804Z","iopub.status.idle":"2024-10-26T17:18:01.593143Z","shell.execute_reply.started":"2024-10-26T17:18:01.550739Z","shell.execute_reply":"2024-10-26T17:18:01.591272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:18:05.207056Z","iopub.execute_input":"2024-10-26T17:18:05.207468Z","iopub.status.idle":"2024-10-26T17:18:05.236668Z","shell.execute_reply.started":"2024-10-26T17:18:05.207428Z","shell.execute_reply":"2024-10-26T17:18:05.235307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv('train.csv', index=False)\ntest.to_csv('test.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:18:21.367708Z","iopub.execute_input":"2024-10-26T17:18:21.368195Z","iopub.status.idle":"2024-10-26T17:18:21.390159Z","shell.execute_reply.started":"2024-10-26T17:18:21.368150Z","shell.execute_reply":"2024-10-26T17:18:21.388997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reverse","metadata":{}},{"cell_type":"code","source":"input_folder = \"/kaggle/working/sounds_original_6sec\"\noutput_folder = \"/kaggle/working/sounds_reverse_6sec\"\n\n# Создаем папку для обратных файлов, если ее еще нет\nos.makedirs(output_folder, exist_ok=True)\n\n# Проходимся по всем аудиофайлам в исходной папке\nfor filename in os.listdir(input_folder):\n    if filename.endswith(\".wav\"):\n        # Загружаем аудиофайл\n        file_path = os.path.join(input_folder, filename)\n        audio = AudioSegment.from_wav(file_path)\n        \n        # Инвертируем аудиофайл\n        reversed_audio = audio.reverse()\n        \n        # Сохраняем в новую папку с добавлением \"_reverse\" к названию файла\n        new_filename = filename.replace(\".wav\", \"_reverse.wav\")\n        output_path = os.path.join(output_folder, new_filename)\n        reversed_audio.export(output_path, format=\"wav\")\n\nprint(\"Реверсированные аудиофайлы успешно созданы и сохранены.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:18:26.987172Z","iopub.execute_input":"2024-10-26T17:18:26.987593Z","iopub.status.idle":"2024-10-26T17:18:28.108545Z","shell.execute_reply.started":"2024-10-26T17:18:26.987550Z","shell.execute_reply":"2024-10-26T17:18:28.107078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## RIR ","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchaudio\nimport torchaudio.functional as F\n\nprint(torch.__version__)\nprint(torchaudio.__version__)\n\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:01.103672Z","iopub.execute_input":"2024-10-26T17:19:01.104162Z","iopub.status.idle":"2024-10-26T17:19:01.111509Z","shell.execute_reply.started":"2024-10-26T17:19:01.104107Z","shell.execute_reply":"2024-10-26T17:19:01.110184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Audio\nimport torchaudio\n\nfrom torchaudio.utils import download_asset\n\nSAMPLE_WAV = download_asset(\"tutorial-assets/steam-train-whistle-daniel_simon.wav\")\nSAMPLE_RIR = download_asset(\"tutorial-assets/Lab41-SRI-VOiCES-rm1-impulse-mc01-stu-clo-8000hz.wav\")\nSAMPLE_SPEECH = download_asset(\"tutorial-assets/Lab41-SRI-VOiCES-src-sp0307-ch127535-sg0042-8000hz.wav\")\nSAMPLE_NOISE = download_asset(\"tutorial-assets/Lab41-SRI-VOiCES-rm1-babb-mc01-stu-clo-8000hz.wav\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:05.610525Z","iopub.execute_input":"2024-10-26T17:19:05.610986Z","iopub.status.idle":"2024-10-26T17:19:06.906238Z","shell.execute_reply.started":"2024-10-26T17:19:05.610942Z","shell.execute_reply":"2024-10-26T17:19:06.905125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\nwaveform1, sample_rate = torchaudio.load(SAMPLE_WAV, channels_first=False)\n\n# Define effects\neffect = \",\".join(\n    [\n        \"lowpass=frequency=300:poles=1\",  # apply single-pole lowpass filter\n        \"atempo=0.8\",  # reduce the speed\n        \"aecho=in_gain=0.8:out_gain=0.9:delays=200:decays=0.3|delays=400:decays=0.3\"\n        # Applying echo gives some dramatic feeling\n    ],\n)\n\n\n# Apply effects\ndef apply_effect(waveform, sample_rate, effect):\n    effector = torchaudio.io.AudioEffector(effect=effect)\n    return effector.apply(waveform, sample_rate)\n\n\nwaveform2 = apply_effect(waveform1, sample_rate, effect)\n\nprint(waveform1.shape, sample_rate)\nprint(waveform2.shape, sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:15.028902Z","iopub.execute_input":"2024-10-26T17:19:15.029358Z","iopub.status.idle":"2024-10-26T17:19:15.220222Z","shell.execute_reply.started":"2024-10-26T17:19:15.029312Z","shell.execute_reply":"2024-10-26T17:19:15.218889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_waveform(waveform, sample_rate, title=\"Waveform\", xlim=None):\n    waveform = waveform.numpy()\n\n    num_channels, num_frames = waveform.shape\n    time_axis = torch.arange(0, num_frames) / sample_rate\n\n    figure, axes = plt.subplots(num_channels, 1)\n    if num_channels == 1:\n        axes = [axes]\n    for c in range(num_channels):\n        axes[c].plot(time_axis, waveform[c], linewidth=1)\n        axes[c].grid(True)\n        if num_channels > 1:\n            axes[c].set_ylabel(f\"Channel {c+1}\")\n        if xlim:\n            axes[c].set_xlim(xlim)\n    figure.suptitle(title)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:19.465251Z","iopub.execute_input":"2024-10-26T17:19:19.465702Z","iopub.status.idle":"2024-10-26T17:19:19.474572Z","shell.execute_reply.started":"2024-10-26T17:19:19.465657Z","shell.execute_reply":"2024-10-26T17:19:19.473217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_specgram(waveform, sample_rate, title=\"Spectrogram\", xlim=None):\n    waveform = waveform.numpy()\n\n    num_channels, _ = waveform.shape\n\n    figure, axes = plt.subplots(num_channels, 1)\n    if num_channels == 1:\n        axes = [axes]\n    for c in range(num_channels):\n        axes[c].specgram(waveform[c], Fs=sample_rate)\n        if num_channels > 1:\n            axes[c].set_ylabel(f\"Channel {c+1}\")\n        if xlim:\n            axes[c].set_xlim(xlim)\n    figure.suptitle(title)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:20.908050Z","iopub.execute_input":"2024-10-26T17:19:20.908598Z","iopub.status.idle":"2024-10-26T17:19:20.917092Z","shell.execute_reply.started":"2024-10-26T17:19:20.908549Z","shell.execute_reply":"2024-10-26T17:19:20.915807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rir_raw, sample_rate = torchaudio.load(SAMPLE_RIR)\nplot_waveform(rir_raw, sample_rate, title=\"Room Impulse Response (raw)\")\nplot_specgram(rir_raw, sample_rate, title=\"Room Impulse Response (raw)\")\nAudio(rir_raw, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:23.834723Z","iopub.execute_input":"2024-10-26T17:19:23.835142Z","iopub.status.idle":"2024-10-26T17:19:25.216589Z","shell.execute_reply.started":"2024-10-26T17:19:23.835102Z","shell.execute_reply":"2024-10-26T17:19:25.215355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rir = rir_raw[:, int(sample_rate * 1.01) : int(sample_rate * 1.3)]\nrir = rir / torch.linalg.vector_norm(rir, ord=2)\n\nplot_waveform(rir, sample_rate, title=\"Room Impulse Response\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:36.300344Z","iopub.execute_input":"2024-10-26T17:19:36.300843Z","iopub.status.idle":"2024-10-26T17:19:36.644699Z","shell.execute_reply.started":"2024-10-26T17:19:36.300776Z","shell.execute_reply":"2024-10-26T17:19:36.643524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"speech, sample_rate = torchaudio.load('/kaggle/working/sounds_original_6sec/e4ce0550fa7aa_cough.wav')\naugmented = F.fftconvolve(speech, rir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:19:52.150926Z","iopub.execute_input":"2024-10-26T17:19:52.152006Z","iopub.status.idle":"2024-10-26T17:19:52.272188Z","shell.execute_reply.started":"2024-10-26T17:19:52.151949Z","shell.execute_reply":"2024-10-26T17:19:52.271122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с оригинальными звуковыми файлами\ninput_folder = '/kaggle/working/sounds_original_6sec'\n# Путь к папке для сохранения измененных файлов\noutput_folder = '/kaggle/working/sounds_rir_6sec'\n\n# Убедитесь, что выходная папка существует\nos.makedirs(output_folder, exist_ok=True)\n\n# Загрузка и нормализация RIR\nrir_raw, sample_rate = torchaudio.load(SAMPLE_RIR)\nrir = rir_raw[:, int(sample_rate * 1.01):int(sample_rate * 1.3)]\nrir = rir / torch.linalg.vector_norm(rir, ord=2)\n\n# Перебираем все файлы в директории\nfor filename in os.listdir(input_folder):\n    if filename.endswith('.wav'):\n        # Загружаем аудиофайл\n        filepath = os.path.join(input_folder, filename)\n        try:\n            speech, sample_rate = torchaudio.load(filepath)\n            # Проверяем, что аудиофайл не пустой\n            if speech.numel() == 0:\n                print(f\"Пропуск пустого файла: {filename}\")\n                continue\n            \n            # Применяем свертку\n            augmented = F.fftconvolve(speech, rir)\n\n            # Сохраняем измененный файл\n            output_filename = filename.replace('.wav', '_rir.wav')\n            torchaudio.save(os.path.join(output_folder, output_filename), augmented, sample_rate)\n        \n        except RuntimeError as e:\n            print(f\"Ошибка при обработке {filename}: {e}\")\n\nprint(\"Обработка RIR завершена!\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:22:51.660663Z","iopub.execute_input":"2024-10-26T17:22:51.661148Z","iopub.status.idle":"2024-10-26T17:24:37.877825Z","shell.execute_reply.started":"2024-10-26T17:22:51.661100Z","shell.execute_reply":"2024-10-26T17:24:37.876621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(augmented, sample_rate, title=\"RIR Applied\")\nplot_specgram(augmented, sample_rate, title=\"RIR Applied\")\nAudio(augmented, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:25:40.237767Z","iopub.execute_input":"2024-10-26T17:25:40.238245Z","iopub.status.idle":"2024-10-26T17:25:43.312228Z","shell.execute_reply.started":"2024-10-26T17:25:40.238195Z","shell.execute_reply":"2024-10-26T17:25:43.310862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(speech, sample_rate, title=\"RIR NOT Applied\")\nplot_specgram(speech, sample_rate, title=\"RIR NOT Applied\")\nAudio(speech, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:26:04.336943Z","iopub.execute_input":"2024-10-26T17:26:04.337356Z","iopub.status.idle":"2024-10-26T17:26:07.280004Z","shell.execute_reply.started":"2024-10-26T17:26:04.337315Z","shell.execute_reply":"2024-10-26T17:26:07.278822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Backgorund Noise","metadata":{}},{"cell_type":"code","source":"SAMPLE_NOISE = download_asset(\"tutorial-assets/Lab41-SRI-VOiCES-rm1-babb-mc01-stu-clo-8000hz.wav\")\n\n# Загрузите аудиофайл\naudio_path = SAMPLE_NOISE\naudio, sr = librosa.load(audio_path, sr=None)\n\n# Рассчитайте коэффициент растяжения\noriginal_duration = librosa.get_duration(y=audio, sr=sr)\ntarget_duration = 6  # целевая длительность в секундах\ntarget_sample_count = int(target_duration * sr)\naudio_stretched = resample(audio, target_sample_count)\n\n# Сохраните измененный аудиофайл\noutput_path = \"Lab41-SRI-VOiCES-extended.wav\"\nsf.write(output_path, audio_stretched, sr)\n\nprint(f\"Измененный аудиофайл сохранен как {output_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:26:30.499378Z","iopub.execute_input":"2024-10-26T17:26:30.500731Z","iopub.status.idle":"2024-10-26T17:26:47.437457Z","shell.execute_reply.started":"2024-10-26T17:26:30.500678Z","shell.execute_reply":"2024-10-26T17:26:47.436174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAMPLE_NOISE = '/kaggle/working/Lab41-SRI-VOiCES-extended.wav'","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:06:31.630418Z","iopub.execute_input":"2024-10-26T19:06:31.630878Z","iopub.status.idle":"2024-10-26T19:06:31.636287Z","shell.execute_reply.started":"2024-10-26T19:06:31.630825Z","shell.execute_reply":"2024-10-26T19:06:31.634940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Загрузка оригинальных звуковых файлов\nspeech, sample_rate = torchaudio.load('/kaggle/working/sounds_original_6sec/e4ce0550fa7aa_cough.wav')\nnoise, sample_rate_noise = torchaudio.load(SAMPLE_NOISE)\n\n# Приведение частоты дискретизации шума к частоте речи\nif sample_rate != sample_rate_noise:\n    resampler = torchaudio.transforms.Resample(orig_freq=sample_rate_noise, new_freq=sample_rate)\n    noise = resampler(noise)\n\n# Обрезка шума до длины речи, если нужно\nif noise.shape[1] > speech.shape[1]:\n    noise = noise[:, :speech.shape[1]]\n\n# Теперь добавим шум\nsnr_dbs = torch.tensor([20, 10, 2])\nnoisy_speeches = F.add_noise(speech, noise, snr_dbs)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:26:47.928027Z","iopub.execute_input":"2024-10-26T17:26:47.928468Z","iopub.status.idle":"2024-10-26T17:26:48.078772Z","shell.execute_reply.started":"2024-10-26T17:26:47.928423Z","shell.execute_reply":"2024-10-26T17:26:48.077470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(noise, sample_rate, title=\"Background noise\")\nplot_specgram(noise, sample_rate, title=\"Background noise\")\nAudio(noise, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:26:53.886313Z","iopub.execute_input":"2024-10-26T17:26:53.887663Z","iopub.status.idle":"2024-10-26T17:26:57.654508Z","shell.execute_reply.started":"2024-10-26T17:26:53.887561Z","shell.execute_reply":"2024-10-26T17:26:57.653161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"snr_db, noisy_speech = snr_dbs[2], noisy_speeches[2:3]\nplot_waveform(noisy_speech, sample_rate, title=f\"SNR: {snr_db} [dB]\")\nplot_specgram(noisy_speech, sample_rate, title=f\"SNR: {snr_db} [dB]\")\nAudio(noisy_speech, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:27:09.771008Z","iopub.execute_input":"2024-10-26T17:27:09.771460Z","iopub.status.idle":"2024-10-26T17:27:13.363231Z","shell.execute_reply.started":"2024-10-26T17:27:09.771416Z","shell.execute_reply":"2024-10-26T17:27:13.361830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shutil.rmtree('/kaggle/working/sounds_noisy_6sec')","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:06:31.143942Z","iopub.execute_input":"2024-10-26T19:06:31.145536Z","iopub.status.idle":"2024-10-26T19:06:31.628617Z","shell.execute_reply.started":"2024-10-26T19:06:31.145469Z","shell.execute_reply":"2024-10-26T19:06:31.627028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с оригинальными звуковыми файлами\ninput_folder = '/kaggle/working/sounds_original_6sec'\n# Путь к папке для сохранения зашумленных файлов\noutput_folder = '/kaggle/working/sounds_noisy_6sec'\n\n\n# Убедитесь, что выходная папка существует\nos.makedirs(output_folder, exist_ok=True)\n\n# Загрузка шума\ntry:\n    noise, sample_rate_noise = torchaudio.load(SAMPLE_NOISE)\nexcept RuntimeError as e:\n    print(f\"Ошибка при загрузке файла с шумом: {e}\")\n    noise = None\n\n# Проверяем, что файл с шумом загружен успешно\nif noise is not None:\n    # Перебираем все файлы в директории\n    for filename in os.listdir(input_folder):\n        if filename.endswith('.wav'):\n            # Загружаем аудиофайл\n            filepath = os.path.join(input_folder, filename)\n            try:\n                speech, sample_rate = torchaudio.load(filepath)\n                # Проверяем, что аудиофайл не пустой\n                if speech.numel() == 0:\n                    print(f\"Пропуск пустого файла: {filename}\")\n                    continue\n                \n                # Приведение частоты дискретизации шума к частоте речи\n                if sample_rate != sample_rate_noise:\n                    resampler = torchaudio.transforms.Resample(orig_freq=sample_rate_noise, new_freq=sample_rate)\n                    noise_resampled = resampler(noise)\n                else:\n                    noise_resampled = noise\n\n                # Обрезка шума до длины речи\n                if noise_resampled.shape[1] > speech.shape[1]:\n                    noise_resampled = noise_resampled[:, :speech.shape[1]]\n\n                # Добавляем шум на разных уровнях SNR\n                snr_dbs = torch.tensor([2])\n                noisy_speeches = F.add_noise(speech, noise_resampled, snr_dbs)\n\n                # Сохраняем зашумленные версии файлов\n                for i, noisy_speech in enumerate(noisy_speeches):\n                    output_filename = filename.replace('.wav', f'_noise_snr{snr_dbs[i].item()}.wav')\n                    output_path = os.path.join(output_folder, output_filename)\n                    torchaudio.save(output_path, noisy_speech.unsqueeze(0), sample_rate)\n\n            except RuntimeError as e:\n                print(f\"Ошибка при обработке {filename}: {e}\")\n\nprint(\"Обработка шума завершена!\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:06:31.638324Z","iopub.execute_input":"2024-10-26T19:06:31.638720Z","iopub.status.idle":"2024-10-26T19:07:13.239648Z","shell.execute_reply.started":"2024-10-26T19:06:31.638677Z","shell.execute_reply":"2024-10-26T19:07:13.237236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Audiomentations","metadata":{}},{"cell_type":"code","source":"audio_file_path = \"/kaggle/working/sounds_original_6sec/e4ce0550fa7aa_cough.wav\"\naudio, sample_rate = librosa.load(audio_file_path, sr=None)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:35:31.922218Z","iopub.execute_input":"2024-10-26T17:35:31.922731Z","iopub.status.idle":"2024-10-26T17:35:31.930633Z","shell.execute_reply.started":"2024-10-26T17:35:31.922684Z","shell.execute_reply":"2024-10-26T17:35:31.929192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## BandPass","metadata":{}},{"cell_type":"code","source":"def plot_waveform(waveform, sample_rate, title=\"Waveform\", xlim=None):\n    # Check if waveform is a PyTorch tensor and convert to NumPy array if necessary\n    if isinstance(waveform, torch.Tensor):\n        waveform = waveform.numpy()\n\n    # Ensure waveform is a NumPy array\n    waveform = np.array(waveform)\n\n    # Handle mono and multi-channel cases\n    if waveform.ndim == 1:\n        # Mono case, reshape to (1, num_frames)\n        waveform = waveform[np.newaxis, :]\n    \n    num_channels, num_frames = waveform.shape\n    time_axis = np.arange(0, num_frames) / sample_rate\n\n    figure, axes = plt.subplots(num_channels, 1)\n\n    if num_channels == 1:\n        axes = [axes]\n    for c in range(num_channels):\n        axes[c].plot(time_axis, waveform[c], linewidth=1)\n        axes[c].grid(True)\n        if num_channels > 1:\n            axes[c].set_ylabel(f\"Channel {c + 1}\")\n        if xlim:\n            axes[c].set_xlim(xlim)\n\n    figure.suptitle(title)\n    plt.xlabel(\"Time (s)\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:34:43.042660Z","iopub.execute_input":"2024-10-26T17:34:43.043341Z","iopub.status.idle":"2024-10-26T17:34:43.056671Z","shell.execute_reply.started":"2024-10-26T17:34:43.043296Z","shell.execute_reply":"2024-10-26T17:34:43.055271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_specgram(waveform, sample_rate, title=\"Spectrogram\", xlim=None):\n    # Проверка, является ли waveform тензором PyTorch, и конвертация в NumPy массив, если это необходимо\n    if isinstance(waveform, torch.Tensor):\n        waveform = waveform.numpy()\n\n    # Убедитесь, что waveform — это NumPy массив\n    waveform = np.array(waveform)\n\n    # Обработка случаев с моно и многоканальными данными\n    if waveform.ndim == 1:\n        # Для моно случая, изменение формы на (1, num_frames)\n        waveform = waveform[np.newaxis, :]\n\n    num_channels, _ = waveform.shape\n\n    figure, axes = plt.subplots(num_channels, 1)\n    if num_channels == 1:\n        axes = [axes]\n    \n    for c in range(num_channels):\n        # Создание спектрограммы для каждого канала\n        axes[c].specgram(waveform[c], Fs=sample_rate)\n        if num_channels > 1:\n            axes[c].set_ylabel(f\"Channel {c + 1}\")\n        if xlim:\n            axes[c].set_xlim(xlim)\n\n    figure.suptitle(title)\n    plt.xlabel(\"Time (s)\")\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:34:46.438073Z","iopub.execute_input":"2024-10-26T17:34:46.438523Z","iopub.status.idle":"2024-10-26T17:34:46.449437Z","shell.execute_reply.started":"2024-10-26T17:34:46.438478Z","shell.execute_reply":"2024-10-26T17:34:46.448081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from audiomentations import BandPassFilter\n\naudio_file_path = \"/kaggle/working/sounds_original_6sec/e4ce0550fa7aa_cough.wav\"\naudio, sample_rate = librosa.load(audio_file_path, sr=None)\n\ntransform = BandPassFilter(min_center_freq=100.0, max_center_freq=8000, p=1.0)\n\naugmented_sound = transform(audio, sample_rate=16000)\n\n\nplot_waveform(augmented_sound, sample_rate, title=\"BandPass\")\nplot_specgram(augmented_sound, sample_rate, title=\"BandPass\")\nAudio(augmented_sound, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:44:56.810579Z","iopub.execute_input":"2024-10-26T17:44:56.811980Z","iopub.status.idle":"2024-10-26T17:44:57.623341Z","shell.execute_reply.started":"2024-10-26T17:44:56.811919Z","shell.execute_reply":"2024-10-26T17:44:57.622169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(augmented_sound, sample_rate, title=\"BandPass\")\nplot_specgram(augmented_sound, sample_rate, title=\"BandPass\")\nAudio(speech, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T17:35:40.213313Z","iopub.execute_input":"2024-10-26T17:35:40.213798Z","iopub.status.idle":"2024-10-26T17:35:41.031731Z","shell.execute_reply.started":"2024-10-26T17:35:40.213737Z","shell.execute_reply":"2024-10-26T17:35:41.030487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с оригинальными звуковыми файлами\ninput_folder = '/kaggle/working/sounds_original_6sec'\n# Путь к папке для сохранения преобразованных файлов\noutput_folder = '/kaggle/working/sounds_bandpass_6sec'\n\n# Убедитесь, что выходная папка существует\nos.makedirs(output_folder, exist_ok=True)\n\n# Инициализация BandPass фильтра\ntransform = BandPassFilter(min_center_freq=100.0, max_center_freq=8000, p=1.0)\n\n# Перебираем все файлы в директории\nfor filename in os.listdir(input_folder):\n    if filename.endswith('.wav'):\n        # Загружаем аудиофайл\n        filepath = os.path.join(input_folder, filename)\n        try:\n            audio, sample_rate = librosa.load(filepath, sr=None)\n            # Проверяем, что аудиофайл не пустой\n            if audio.size == 0:\n                print(f\"Пропуск пустого файла: {filename}\")\n                continue\n\n            # Применяем BandPass фильтр\n            augmented_sound = transform(audio, sample_rate=sample_rate)\n\n            # Сохраняем измененный файл\n            output_filename = filename.replace('.wav', '_bandpass.wav')\n            output_path = os.path.join(output_folder, output_filename)\n            sf.write(output_path, augmented_sound, sample_rate)\n\n        except Exception as e:\n            print(f\"Ошибка при обработке {filename}: {e}\")\n\nprint(\"Обработка BandPass фильтром завершена!\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:04:02.023628Z","iopub.execute_input":"2024-10-26T18:04:02.024134Z","iopub.status.idle":"2024-10-26T18:04:25.191228Z","shell.execute_reply.started":"2024-10-26T18:04:02.024086Z","shell.execute_reply":"2024-10-26T18:04:25.189868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Time Mask","metadata":{}},{"cell_type":"code","source":"from audiomentations import TimeMask\n\ntransform = TimeMask(\n    min_band_part=0.1,\n    max_band_part=0.15,\n    fade=True,\n    p=1.0,\n)\n\naugmented_sound = transform(audio, sample_rate=16000)\n\nAudio(augmented_sound, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T11:01:56.123852Z","iopub.execute_input":"2024-10-26T11:01:56.125337Z","iopub.status.idle":"2024-10-26T11:01:56.149789Z","shell.execute_reply.started":"2024-10-26T11:01:56.125257Z","shell.execute_reply":"2024-10-26T11:01:56.148595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(augmented_sound, sample_rate, title=\"BandPass\")\nplot_specgram(augmented_sound, sample_rate, title=\"BandPass\")\nAudio(augmented_sound, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T11:03:08.046023Z","iopub.execute_input":"2024-10-26T11:03:08.046479Z","iopub.status.idle":"2024-10-26T11:03:08.933841Z","shell.execute_reply.started":"2024-10-26T11:03:08.046434Z","shell.execute_reply":"2024-10-26T11:03:08.932599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с оригинальными звуковыми файлами\ninput_folder = '/kaggle/working/sounds_original_6sec'\n# Путь к папке для сохранения преобразованных файлов\noutput_folder = '/kaggle/working/sounds_timemask_6sec'\n\n# Убедитесь, что выходная папка существует\nos.makedirs(output_folder, exist_ok=True)\n\n# Инициализация BandPass фильтра\ntransform = TimeMask(\n    min_band_part=0.1,\n    max_band_part=0.15,\n    fade=True,\n    p=1.0,\n)\n\n# Перебираем все файлы в директории\nfor filename in os.listdir(input_folder):\n    if filename.endswith('.wav'):\n        # Загружаем аудиофайл\n        filepath = os.path.join(input_folder, filename)\n        try:\n            audio, sample_rate = librosa.load(filepath, sr=None)\n            # Проверяем, что аудиофайл не пустой\n            if audio.size == 0:\n                print(f\"Пропуск пустого файла: {filename}\")\n                continue\n\n            # Применяем BandPass фильтр\n            augmented_sound = transform(audio, sample_rate=sample_rate)\n\n            # Сохраняем измененный файл\n            output_filename = filename.replace('.wav', '_timemask.wav')\n            output_path = os.path.join(output_folder, output_filename)\n            sf.write(output_path, augmented_sound, sample_rate)\n\n        except Exception as e:\n            print(f\"Ошибка при обработке {filename}: {e}\")\n\nprint(\"Обработка TimeMask фильтром завершена!\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:07:50.144339Z","iopub.execute_input":"2024-10-26T18:07:50.144811Z","iopub.status.idle":"2024-10-26T18:08:08.240741Z","shell.execute_reply.started":"2024-10-26T18:07:50.144749Z","shell.execute_reply":"2024-10-26T18:08:08.239408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Gaussian SNR","metadata":{}},{"cell_type":"code","source":"from audiomentations import AddGaussianSNR\n\naudio_file_path = \"/kaggle/working/sounds_original_6sec/e4ce0550fa7aa_cough.wav\"\naudio, sample_rate = librosa.load(audio_file_path, sr=None)\n\ntransform = AddGaussianSNR(\n    min_snr_db=5.0,\n    max_snr_db=40.0,\n    p=1.0\n)\n\naugmented_sound = transform(audio, sample_rate=16000)\n\nAudio(augmented_sound, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:10:03.351391Z","iopub.execute_input":"2024-10-26T18:10:03.352025Z","iopub.status.idle":"2024-10-26T18:10:03.375745Z","shell.execute_reply.started":"2024-10-26T18:10:03.351959Z","shell.execute_reply":"2024-10-26T18:10:03.374395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(augmented_sound, sample_rate, title=\"GaussianSNR\")\nplot_specgram(augmented_sound, sample_rate, title=\"GaussianSNR\")\nAudio(augmented_sound, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:10:18.642346Z","iopub.execute_input":"2024-10-26T18:10:18.642844Z","iopub.status.idle":"2024-10-26T18:10:19.465737Z","shell.execute_reply.started":"2024-10-26T18:10:18.642767Z","shell.execute_reply":"2024-10-26T18:10:19.464556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с оригинальными звуковыми файлами\ninput_folder = '/kaggle/working/sounds_original_6sec'\n# Путь к папке для сохранения преобразованных файлов\noutput_folder = '/kaggle/working/sounds_gaussian_6sec'\n\n# Убедитесь, что выходная папка существует\nos.makedirs(output_folder, exist_ok=True)\n\n# Инициализация фильтра\ntransform = AddGaussianSNR(\n    min_snr_db=5.0,\n    max_snr_db=40.0,\n    p=1.0\n)\n\n# Перебираем все файлы в директории\nfor filename in os.listdir(input_folder):\n    if filename.endswith('.wav'):\n        # Загружаем аудиофайл\n        filepath = os.path.join(input_folder, filename)\n        try:\n            audio, sample_rate = librosa.load(filepath, sr=None)\n            # Проверяем, что аудиофайл не пустой\n            if audio.size == 0:\n                print(f\"Пропуск пустого файла: {filename}\")\n                continue\n\n            # Применяем BandPass фильтр\n            augmented_sound = transform(audio, sample_rate=sample_rate)\n\n            # Сохраняем измененный файл\n            output_filename = filename.replace('.wav', '_gaussian.wav')\n            output_path = os.path.join(output_folder, output_filename)\n            sf.write(output_path, augmented_sound, sample_rate)\n\n        except Exception as e:\n            print(f\"Ошибка при обработке {filename}: {e}\")\n\nprint(\"Обработка GaussianSNR фильтром завершена!\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:11:33.479769Z","iopub.execute_input":"2024-10-26T18:11:33.480324Z","iopub.status.idle":"2024-10-26T18:11:56.887462Z","shell.execute_reply.started":"2024-10-26T18:11:33.480273Z","shell.execute_reply":"2024-10-26T18:11:56.886145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Specs prep","metadata":{}},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_original_6sec'\nspecs_dir = '/kaggle/working/specs_original/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_original.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_original.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:39:38.383344Z","iopub.execute_input":"2024-10-26T18:39:38.383857Z","iopub.status.idle":"2024-10-26T18:44:23.013425Z","shell.execute_reply.started":"2024-10-26T18:39:38.383779Z","shell.execute_reply":"2024-10-26T18:44:23.011652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_reverse_6sec'\nspecs_dir = '/kaggle/working/specs_reverse/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_reverse.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_reverse.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_reverse.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_reverse.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T18:56:47.962553Z","iopub.execute_input":"2024-10-26T18:56:47.962997Z","iopub.status.idle":"2024-10-26T19:01:32.222350Z","shell.execute_reply.started":"2024-10-26T18:56:47.962951Z","shell.execute_reply":"2024-10-26T19:01:32.220347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_gaussian_6sec'\nspecs_dir = '/kaggle/working/specs_gaussian/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_gaussian.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_gaussian.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_gaussian.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_gaussian.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:01:32.229347Z","iopub.execute_input":"2024-10-26T19:01:32.230403Z","iopub.status.idle":"2024-10-26T19:06:31.140834Z","shell.execute_reply.started":"2024-10-26T19:01:32.230312Z","shell.execute_reply":"2024-10-26T19:06:31.139059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_noisy_6sec'\nspecs_dir = '/kaggle/working/specs_noisy/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_noise_snr2.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_noisy.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_noise_snr2.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_noisy.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:07:13.241576Z","iopub.execute_input":"2024-10-26T19:07:13.242034Z","iopub.status.idle":"2024-10-26T19:12:07.292385Z","shell.execute_reply.started":"2024-10-26T19:07:13.241988Z","shell.execute_reply":"2024-10-26T19:12:07.290900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_rir_6sec'\nspecs_dir = '/kaggle/working/specs_rir/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_rir.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_rir.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_rir.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_rir.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:12:07.296625Z","iopub.execute_input":"2024-10-26T19:12:07.297719Z","iopub.status.idle":"2024-10-26T19:17:07.442150Z","shell.execute_reply.started":"2024-10-26T19:12:07.297650Z","shell.execute_reply":"2024-10-26T19:17:07.440862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_bandpass_6sec'\nspecs_dir = '/kaggle/working/specs_bandpass/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_bandpass.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_bandpass.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_bandpass.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_bandpass.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:17:07.448330Z","iopub.execute_input":"2024-10-26T19:17:07.451490Z","iopub.status.idle":"2024-10-26T19:21:49.537388Z","shell.execute_reply.started":"2024-10-26T19:17:07.451420Z","shell.execute_reply":"2024-10-26T19:21:49.535628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пути к исходной папке с аудио и к новой папке для мелспектрограмм\ndestination_dir = '/kaggle/working/sounds_timemask_6sec'\nspecs_dir = '/kaggle/working/specs_timemask/'\n\n# Создаем папку для хранения мелспектрограмм\nos.makedirs(specs_dir, exist_ok=True)\n\n# Функция для сохранения мелспектрограммы с указанными параметрами для 6-секундного фрагмента\ndef save_mel_spectrogram(audio_file, output_file, duration=6):\n    y, sr = librosa.load(audio_file, sr=None)  # Загружаем аудиофайл с исходной частотой дискретизации\n    total_duration = librosa.get_duration(y=y, sr=sr)\n    \n#     # Находим центр аудиозаписи и вырезаем 10-секундный отрезок\n#     center = total_duration / 2\n#     start = max(center - duration / 2, 0)\n#     end = min(center + duration / 2, total_duration)\n    \n#     # Вырезаем 10-секундный фрагмент\n#     y_segment = y[int(start * sr):int(end * sr)]\n    \n    # Генерируем мелспектрограмму с указанными параметрами\n    S = librosa.feature.melspectrogram(\n        y=y, \n        sr=sr, \n        n_fft=1024, \n        hop_length=500, \n        n_mels=128, \n        fmin=40, \n        fmax = sr / 2, \n        power=2\n    )\n    \n    # Преобразуем мощность мелспектрограммы в децибелы\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    \n    # Сохраняем мелспектрограмму размером 240x240 пикселей без осей и надписей\n    plt.figure(figsize=(2.24, 2.24)) \n    plt.axis('off')  # Убираем оси\n    librosa.display.specshow(S_dB, sr=sr, hop_length=500, x_axis=None, y_axis=None, fmin=40, fmax=16000)\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0, dpi=100)  # Сохраняем изображение\n    plt.close()\n\n\n# Обновляем датафрейм и добавляем пути к мелспектрограммам\ndef update_dataframe_with_spectrograms(df, source_dir, specs_dir):\n    df['spec_cough'] = None\n    df['spec_vowel'] = None\n    \n    for idx, row in df.iterrows():\n        candidateID = row['candidateID']\n        \n        # Генерация и сохранение мелспектрограмм для cough.wav\n        cough_file = os.path.join(source_dir, f\"{candidateID}_cough_timemask.wav\")\n        if os.path.exists(cough_file):\n            cough_spec_filename = f\"{candidateID}_cough_spec_timemask.png\"\n            save_mel_spectrogram(cough_file, os.path.join(specs_dir, cough_spec_filename))\n            df.at[idx, 'spec_cough'] = cough_spec_filename\n        \n        # Генерация и сохранение мелспектрограмм для vowel.wav\n        vowel_file = os.path.join(source_dir, f\"{candidateID}_vowel_timemask.wav\")\n        if os.path.exists(vowel_file):\n            vowel_spec_filename = f\"{candidateID}_vowel_spec_timemask.png\"\n            save_mel_spectrogram(vowel_file, os.path.join(specs_dir, vowel_spec_filename))\n            df.at[idx, 'spec_vowel'] = vowel_spec_filename\n\n# Применяем к train и test\nupdate_dataframe_with_spectrograms(train, destination_dir, specs_dir)\nupdate_dataframe_with_spectrograms(test, destination_dir, specs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:21:49.539758Z","iopub.execute_input":"2024-10-26T19:21:49.540733Z","iopub.status.idle":"2024-10-26T19:26:33.235618Z","shell.execute_reply.started":"2024-10-26T19:21:49.540654Z","shell.execute_reply":"2024-10-26T19:26:33.234529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image, display\n\nimg_path = '/kaggle/working/specs_original/6078412191982_cough_spec_original.png'\ndisplay(Image(filename=img_path))\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:30:17.824018Z","iopub.execute_input":"2024-10-26T19:30:17.824509Z","iopub.status.idle":"2024-10-26T19:30:17.835059Z","shell.execute_reply.started":"2024-10-26T19:30:17.824463Z","shell.execute_reply":"2024-10-26T19:30:17.833765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image\nimport os\n\n# Пути к изображениям\nimg_paths = [\n    '/kaggle/working/specs_reverse/6078412191982_cough_spec_reverse.png',\n    '/kaggle/working/specs_gaussian/6078412191982_cough_spec_gaussian.png',\n    '/kaggle/working/specs_noisy/6078412191982_cough_spec_noisy.png',\n    '/kaggle/working/specs_rir/6078412191982_cough_spec_rir.png',\n    '/kaggle/working/specs_bandpass/6078412191982_cough_spec_bandpass.png',\n    '/kaggle/working/specs_timemask/6078412191982_cough_spec_timemask.png'\n]\n\n# Создаем фигуру с подписями и сеткой 2x3\nfig, axes = plt.subplots(2, 3, figsize=(8, 5))\n\n# Выводим изображения и добавляем подписи\nfor i, img_path in enumerate(img_paths):\n    img = Image.open(img_path)\n    ax = axes[i // 3, i % 3]  # Выбираем позицию в сетке\n    \n    # Отображаем изображение и добавляем подпись\n    ax.imshow(img)\n    suffix = os.path.splitext(img_path)[0].split('_')[-1]  # Извлекаем суффикс из имени файла\n    ax.set_title(f\"_{suffix}\", fontsize=12)\n    ax.axis('off')  # Убираем оси\n\n# Настраиваем и показываем\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T19:30:47.531062Z","iopub.execute_input":"2024-10-26T19:30:47.531526Z","iopub.status.idle":"2024-10-26T19:30:48.292237Z","shell.execute_reply.started":"2024-10-26T19:30:47.531479Z","shell.execute_reply":"2024-10-26T19:30:48.290880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Add info to train / test dataframes","metadata":{}},{"cell_type":"code","source":"train = train[['candidateID', 'age', 'gender', 'tbContactHistory', 'wheezingHistory',\n       'phlegmCough', 'familyAsthmaHistory', 'feverHistory', 'coldPresent',\n       'packYears', 'disease']]\n\ntest = test[['candidateID', 'age', 'gender', 'tbContactHistory', 'wheezingHistory',\n       'phlegmCough', 'familyAsthmaHistory', 'feverHistory', 'coldPresent',\n       'packYears']]","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:09:57.276029Z","iopub.execute_input":"2024-10-26T20:09:57.276512Z","iopub.status.idle":"2024-10-26T20:09:57.286327Z","shell.execute_reply.started":"2024-10-26T20:09:57.276466Z","shell.execute_reply":"2024-10-26T20:09:57.284976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к папке с папками (замените на актуальный путь)\nbase_dir = '/kaggle/working'\n\n# Список папок с аудио и изображениями\nfolders = [\n    'specs_original', 'specs_bandpass', 'specs_gaussian', 'specs_noisy', 'specs_reverse','specs_rir', 'specs_timemask', \n    'sounds_original_6sec', 'sounds_bandpass_6sec', 'sounds_gaussian_6sec', 'sounds_noisy_6sec','sounds_reverse_6sec', 'sounds_rir_6sec',  'sounds_timemask_6sec',      \n]\n\n# Функция для создания колонок на основе файлов в папках\ndef add_file_columns(df, folders, base_dir):\n    for folder in folders:\n        # Полный путь к папке\n        folder_path = os.path.join(base_dir, folder)\n        \n        # Проход по файлам в папке\n        for file_name in os.listdir(folder_path):\n            # Извлечение идентификатора candidateID и типа (cough или vowel) из имени файла\n            parts = file_name.split('_')\n            if len(parts) < 2:\n                continue\n            candidate_id = parts[0]\n            file_type = 'cough' if 'cough' in file_name else 'vowel' if 'vowel' in file_name else None\n            \n            # Если тип файла распознан, добавляем его в соответствующую колонку\n            if file_type:\n                col_name = f'{folder}_{file_type}'\n                \n                # Если колонки еще нет в датафрейме, создаем ее\n                if col_name not in df.columns:\n                    df[col_name] = None\n                \n                # Обновляем значение в колонке для строки с данным candidateID\n                df.loc[df['candidateID'] == candidate_id, col_name] = file_name\n\n# Применяем функцию для обоих датафреймов\nadd_file_columns(train, folders, base_dir)\nadd_file_columns(test, folders, base_dir)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:13:28.408046Z","iopub.execute_input":"2024-10-26T20:13:28.408585Z","iopub.status.idle":"2024-10-26T20:13:58.066969Z","shell.execute_reply.started":"2024-10-26T20:13:28.408535Z","shell.execute_reply":"2024-10-26T20:13:58.065770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:14:05.278120Z","iopub.execute_input":"2024-10-26T20:14:05.278607Z","iopub.status.idle":"2024-10-26T20:14:05.288227Z","shell.execute_reply.started":"2024-10-26T20:14:05.278561Z","shell.execute_reply":"2024-10-26T20:14:05.286752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:14:14.240812Z","iopub.execute_input":"2024-10-26T20:14:14.241283Z","iopub.status.idle":"2024-10-26T20:14:14.250276Z","shell.execute_reply.started":"2024-10-26T20:14:14.241237Z","shell.execute_reply":"2024-10-26T20:14:14.249092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:14:53.337671Z","iopub.execute_input":"2024-10-26T20:14:53.338342Z","iopub.status.idle":"2024-10-26T20:14:53.349231Z","shell.execute_reply.started":"2024-10-26T20:14:53.338282Z","shell.execute_reply":"2024-10-26T20:14:53.347473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv('train.csv', index=False)\ntest.to_csv('test.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:15:23.675565Z","iopub.execute_input":"2024-10-26T20:15:23.676509Z","iopub.status.idle":"2024-10-26T20:15:23.755666Z","shell.execute_reply.started":"2024-10-26T20:15:23.676455Z","shell.execute_reply":"2024-10-26T20:15:23.753920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.remove('/kaggle/working/Lab41-SRI-VOiCES-extended.wav')","metadata":{"execution":{"iopub.status.busy":"2024-10-26T20:16:11.694799Z","iopub.execute_input":"2024-10-26T20:16:11.696121Z","iopub.status.idle":"2024-10-26T20:16:11.701651Z","shell.execute_reply.started":"2024-10-26T20:16:11.696065Z","shell.execute_reply":"2024-10-26T20:16:11.700123Z"},"trusted":true},"execution_count":null,"outputs":[]}]}