Refactored paths

This commit is contained in:
zin
2026-05-06 18:22:54 +00:00
parent 4e192b7bc4
commit dd22ee09a4
8 changed files with 61 additions and 0 deletions
+146
View File
@@ -0,0 +1,146 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "83693ad7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "99850a99",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Читаем файл аннотаций: ../dataset/DEAM/DEAM_Annotations/annotations/annotations per each rater/song_level/static_annotations_songs_1_2000.csv\n"
]
}
],
"source": [
"# 1. Ищем файл (поднимаемся из src на уровень выше)\n",
"deam_root = Path(\"../dataset/DEAM\")\n",
"\n",
"# Ищем файл статичных аннотаций. Берем первый попавшийся.\n",
"csv_files = list(deam_root.rglob(\"*static_annotations*.csv\"))\n",
"if not csv_files:\n",
" # Если не нашел static, берем вообще любой csv с аннотациями\n",
" csv_files = list(deam_root.rglob(\"*.csv\"))\n",
"\n",
"if not csv_files:\n",
" # Если путь неверный или файлов нет, скрипт сразу скажет об этом и покажет полный путь\n",
" raise FileNotFoundError(f\"В папке {deam_root.resolve()} не найдено ни одного CSV файла! Проверьте пути.\")\n",
"\n",
"anno_path = csv_files[0]\n",
"print(f\"Читаем файл аннотаций: {anno_path}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5fbc493f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Оригинальные колонки в файле: ['workerID', ' SongId', ' Valence', ' Arousal']\n"
]
}
],
"source": [
"# 2. Загружаем и чистим колонки\n",
"df = pd.read_csv(anno_path)\n",
"print(\"Оригинальные колонки в файле:\", df.columns.tolist())\n",
"\n",
"# Сносим пробелы по краям и переводим в нижний регистр\n",
"df.columns = [str(c).strip().lower() for c in df.columns]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1e28fece",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Успешно найдены колонки -> ID: 'workerid', Valence: 'valence', Arousal: 'arousal'\n"
]
}
],
"source": [
"# 3. Умный поиск колонок\n",
"# Ищем первую колонку, где есть 'id' или 'song'\n",
"song_col = next((c for c in df.columns if 'song' in c or 'id' in c), df.columns[0])\n",
"# Ищем valence (желательно mean, но сойдет любой)\n",
"v_col = next((c for c in df.columns if 'valence' in c and 'mean' in c), \n",
" next((c for c in df.columns if 'valence' in c), None))\n",
"# Ищем arousal\n",
"a_col = next((c for c in df.columns if 'arousal' in c and 'mean' in c), \n",
" next((c for c in df.columns if 'arousal' in c), None))\n",
"\n",
"if not v_col or not a_col:\n",
" raise ValueError(f\"Не смог найти Valence или Arousal! Доступные колонки: {df.columns.tolist()}\")\n",
"\n",
"print(f\"Успешно найдены колонки -> ID: '{song_col}', Valence: '{v_col}', Arousal: '{a_col}'\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "469f651c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Готово! Музыкальная база сохранена: ../dataset/DEAM/music_db.csv\n"
]
}
],
"source": [
"# 4. Сохраняем результат\n",
"clean_df = df[[song_col, v_col, a_col]].copy()\n",
"clean_df.columns = ['song_id', 'valence', 'arousal']\n",
"\n",
"output_path = deam_root / \"music_db.csv\"\n",
"clean_df.to_csv(output_path, index=False)\n",
"print(f\"Готово! Музыкальная база сохранена: {output_path}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (my-python-project)",
"language": "python",
"name": "my-python-project"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}