utils.ipynb 9.17 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/user/opt/anaconda3/envs/capstone/lib/python3.7/site-packages/librosa/util/decorators.py:9: NumbaDeprecationWarning: An import was requested from a module that has moved location.\n",
      "Import requested from: 'numba.decorators', please update to use 'numba.core.decorators' or pin to Numba version 0.48.0. This alias will not be present in Numba version 0.50.0.\n",
      "  from numba.decorators import jit as optional_jit\n",
      "/Users/user/opt/anaconda3/envs/capstone/lib/python3.7/site-packages/librosa/util/decorators.py:9: NumbaDeprecationWarning: An import was requested from a module that has moved location.\n",
      "Import of 'jit' requested from: 'numba.decorators', please update to use 'numba.core.decorators' or pin to Numba version 0.48.0. This alias will not be present in Numba version 0.50.0.\n",
      "  from numba.decorators import jit as optional_jit\n"
     ]
    }
   ],
   "source": [
    "import wave\n",
    "import numpy as np\n",
    "import os\n",
    "import scipy\n",
    "import matplotlib.pyplot as plt\n",
    "import sklearn\n",
    "import librosa\n",
    "import urllib\n",
    "import librosa.display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_upper_dir():\n",
    "    os.chdir('..')\n",
    "    return os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data_dir(project_path):\n",
    "    return os.path.join(project_path, 'data/output_noisy_file')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "PROJECT_DIR = get_upper_dir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = get_data_dir(PROJECT_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rename_data_file(data_path):\n",
    "    wave_file_list = os.listdir(DATA_DIR)\n",
    "    name_list = []\n",
    "    \n",
    "    for item in wave_file_list:\n",
    "        before = item[0:18]\n",
    "        after = item[21:]\n",
    "        name_list.append(before+'&'+after)\n",
    "    \n",
    "    name_list = list(set(name_list))\n",
    "    \n",
    "    for idx, name in enumerate(name_list):\n",
    "        file_name = name.split('&')\n",
    "        ch1_name = DATA_DIR + '/' + file_name[0] + 'ch1' + file_name[1]\n",
    "        ch2_name = DATA_DIR + '/' + file_name[0] + 'ch2' + file_name[1]\n",
    "        data_num = file_name[0][-7:-5]\n",
    "        angle = file_name[0][-4:-1]\n",
    "        new_ch1_name = DATA_DIR + '/' + 'data_' + str(idx) + '_' + str(data_num) \\\n",
    "                        + '_angle_' + angle + '_ch1' + '.wav'\n",
    "        new_ch2_name = DATA_DIR + '/' + 'data_' + str(idx) + '_' + str(data_num) \\\n",
    "                        + '_angle_' + angle + '_ch2' + '.wav'\n",
    "        os.rename(ch1_name,new_ch1_name)\n",
    "        os.rename(ch2_name,new_ch2_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "rename_data_file(DATA_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def get_feature_dir(project_path):\n",
    "    return os.path.join(project_path, 'data/feature/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_feature_dir(PROJECT_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def label_one_hot_encoder(label):\n",
    "    y_vec = np.zeros((len(y), 4), dtype=np.float)\n",
    "    for i, label in enumerate(y):\n",
    "        y_vec[i, int(y[i])] = 1.0\n",
    "    return y_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = os.path.join(PROJECT_DIR, 'data/rename')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rename_data_file(data_path):\n",
    "    wave_file_list = os.listdir(data_path)\n",
    "    name_list = []\n",
    "    \n",
    "    for item in wave_file_list:\n",
    "        name_list.append(item[:-7])\n",
    "    \n",
    "    name_list = list(set(name_list))\n",
    "    idx = 2250\n",
    "    for name in name_list:\n",
    "        ch1_name = data_path + '/' + name + 'ch1.wav'\n",
    "        ch2_name = data_path + '/' + name + 'ch2.wav'\n",
    "        data_num = name[8:10]\n",
    "        new_ch1_name = data_path + '/' + 'data_' + str(idx) + '_' + str(data_num) \\\n",
    "                        + '_angle_180' + '_ch1' + '.wav'\n",
    "        new_ch2_name = data_path + '/' + 'data_' + str(idx) + '_' + str(data_num) \\\n",
    "                        + '_angle_180' + '_ch2' + '.wav'\n",
    "        os.rename(ch1_name,new_ch1_name)\n",
    "        os.rename(ch2_name,new_ch2_name)\n",
    "        idx += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/user/git/2015104199/프로젝트/data/rename'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DATA_DIR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "wave_file_list = os.listdir(DATA_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'audio_0016_noisy_npark42_m20_ch2.wav'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wave_file_list[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'22'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wave_file_list[0][8:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'audio_0022_noisy_npark7_m20_'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wave_file_list[0][:-7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "ch1_name = DATA_DIR + '/' + wave_file_list[0][:-7] + 'ch1.wav'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/user/git/2015104199/프로젝트/data/rename/audio_0022_noisy_npark7_m20_ch1.wav'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ch1_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_ch1_name = DATA_DIR + '/' + 'data_' + str(1950) + '_' + str(22) \\\n",
    "                        + '_angle_060' + '_ch1' + '.wav'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/user/git/2015104199/프로젝트/data/rename/data_1950_22_angle_060_ch1.wav'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_ch1_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "rename_data_file(DATA_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.7.7"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}