{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nfrom matplotlib.patches import Rectangle\nimport librosa\nfrom librosa import display as ld\n\nsns.set_style(style=\"darkgrid\")\nmpl.rcParams.update({'figure.max_open_warning': 0})","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\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\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Datasets\n* train_tp.csv\n* train_fp.csv"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_tp=pd.read_csv('/kaggle/input/rfcx-species-audio-detection/train_tp.csv')\ndf_fp=pd.read_csv('/kaggle/input/rfcx-species-audio-detection/train_fp.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_tp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#0099c367b\ndf_tp.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_fp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_fp.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Unique species\nlen(df_tp['species_id'].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ploting chart1 function\ndef grafico_dispersao(df, titulo):\n    total = float(len(df))\n    f, ax = plt.subplots(1,1, figsize=(16,4))\n    ax = sns.countplot(x=\"species_id\", data=df, palette=\"Set3\")\n    plt.xticks(rotation=45, size=8)\n    for p in ax.patches:\n        height = p.get_height()\n        ax.text(p.get_x() + p.get_width() / 2., height + 3, '{:1.2f}%'.format(100 * height / total), ha=\"center\", rotation=45)\n    limite_y = ax.get_ylim()\n    ax.set_ylim(limite_y[0], limite_y[1]*1.15)\n    ax.set_title(titulo)\n    plt.plot()\n    \ngrafico_dispersao(df_tp, \"Dataset TP - Dispesão das amostras por espécie\")\ngrafico_dispersao(df_fp, \"Dataset FP - Dispesão das amostras por espécie\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true,"_kg_hide-input":false},"cell_type":"code","source":"estatistica = df_tp[['species_id', 'f_min', 'f_max']].groupby('species_id').agg(['describe'])\nestatistica[:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"sr = df_tp.set_index(['recording_id', 'species_id', 'songtype_id']).stack().rename_axis(index={None: 'indicator'}).rename('value')\nsr.index = sr.index.droplevel('recording_id')\nsr.index = sr.index.droplevel('songtype_id')\nsr.sort_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def violinplot_cutomize(sr, ax, species_id, indicators):\n    sns.violinplot(\n        ax = ax,\n        data = sr.loc[(species_id, indicators)].reset_index().sort_values(by='indicator', ascending=False), \n        y=\"value\", \n        x=\"indicator\",\n        split=True,\n        scale=\"count\", \n        inner=\"quartile\", \n        linewidth=1,\n        palette=\"Set3\")\n\n    \ndef kdeplot_customize(sr, ax, species_id, indicators):\n    sns.kdeplot(\n        ax = ax,\n        data = sr.loc[(species_id, indicators)].reset_index().sort_values(by='indicator', ascending=False), \n        #y=\"value\", \n        x=\"value\",\n        palette=\"Set3\")\n\ndef countplot_customize(sr, ax, species_id, indicators):\n    sns.countplot(\n        ax = ax,\n        data = sr.loc[(species_id, indicators)].reset_index().sort_values(by='indicator', ascending=False),\n        x=\"value\",\n        palette=\"Set3\")\n    plt.xlabel(indicators)\n\ndef species_chart(sr, indicators):\n    for species_id in sr.index.levels[0].values:\n        f, axs = plt.subplots(1,3, figsize=(16,4))\n        violinplot_cutomize(sr, axs[0], species_id, indicators)\n        countplot_customize(sr, axs[1], species_id, indicators[0])\n        countplot_customize(sr, axs[2], species_id, indicators[1])\n        f.suptitle('Specie #' + str(species_id), fontsize=16)\n        \nspecies_chart(sr, ['f_min', 'f_max'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import soundfile as sf\n\nexample = df_tp.iloc[3]\n\ndata, sf_rate = sf.read('../input/rfcx-species-audio-detection/train/'+example['recording_id']+'.flac')\n#data = cp.array(data)\n\n#varfft = cp.abs( cp.fft.fft(data)[:(len(data)//2)] )\n#    \n#return cp.asnumpy( varfft.reshape( (1000,1440) ).mean(axis=1) )\n\"\"\"\nFT = []\nfor fn in tqdm(traint.recording_id.to_array()):\n    FT.append( extract_fft( '../input/rfcx-species-audio-detection/train/'+fn+'.flac' ) )\nFT = np.stack(FT)\ngc.collect()\n\nFT.shape\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sftf = librosa.stft(data)\nsftf_xdb = librosa.amplitude_to_db(abs(sftf))\n\nplt.figure(figsize=(30, 10))\nld.specshow(sftf_xdb, sr=sf_rate, x_axis='time', y_axis='hz')\n\n# Add the audio position\nax = plt.gca()\n\naudio_position = Rectangle(\n    (example['t_min'],example['f_min']),\n    example['t_max'] - example['t_min'],\n    example['f_max'] - example['f_min'],\n    linewidth=10,\n    edgecolor='white',\n    facecolor='none'\n)\nax.add_patch(audio_position)\n\nplt.colorbar()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.close('all')\nfig, ax = plt.subplots(1, 1, figsize=(12, 10))\nld.specshow(sftf_xdb, sr=sf_rate, x_axis='time', y_axis='hz')\n\nax = plt.gca()\nax.set_xlim(left = example['t_min'], right = example['t_max'])\nax.set_ylim(bottom = example['f_min'], top = example['f_max'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import IPython.display as ipd\nipd.Audio('../input/rfcx-species-audio-detection/train/'+example['recording_id']+'.flac')","execution_count":null,"outputs":[]}],"metadata":{"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"}},"nbformat":4,"nbformat_minor":4}