{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np \nimport pandas as pd\nimport soundfile as sf\n%matplotlib inline\nplt.rcParams['figure.figsize'] = (12, 5);\nsns.set_style('whitegrid')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_fp=pd.read_csv(\"../input/rfcx-species-audio-detection/train_fp.csv\")\ntrain_tp=pd.read_csv(\"../input/rfcx-species-audio-detection/train_tp.csv\")\ntrain_tp.species_id.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_tp.shape, train_fp.shape\ntrain_tp.recording_id.nunique(), train_fp.recording_id.nunique()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# true_pos is showing if species are labeled correctly or not. \ntrain_fp[\"true_pos\"]=0\ntrain_tp[\"true_pos\"]=1\ntrain=pd.concat([train_fp, train_tp], ignore_index=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#no of common audio files\nlen(set(train_tp[\"recording_id\"]) & set(train_fp[\"recording_id\"]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#All audio have same number of frames i.e. 2880000.\ntrain_tp['nframes'] = train_tp['recording_id'].apply(lambda f: len(sf.read(\"../input/rfcx-species-audio-detection/train/\"+f+\".flac\")[0]))\ntrain_tp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"category_group = train[['recording_id','species_id', 'true_pos']].groupby(['species_id', 'true_pos']).count()\nplot = category_group.unstack().reindex(category_group.unstack().sum(axis=1).sort_values().index).plot(kind='bar', stacked=True, title=\"Number of Audio Samples per Category\", figsize=(12,5))\nplot.set_xlabel(\"Category\")\nplot.set_ylabel(\"Number of Samples\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reading first 100 frames of audio\ndata, rate = sf.read(\"../input/rfcx-species-audio-detection/train/00204008d.flac\",frames=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Specgram plot of first 100 frames.\n# Specgram computes the windowed discrete-time Fourier transform of a signal using a sliding window.\nPxx, freqs, bins, im = plt.specgram(data, Fs=rate)\n\n# add axis labels\nplt.ylabel('Frequency [Hz]')\nplt.xlabel('Time [sec]')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#plot of first 100 frames\nplt.plot(data,\"-g\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import IPython.display as ipd  # To play sound in the notebook\nfname=\"../input/rfcx-species-audio-detection/train/00204008d.flac\"\nipd.Audio(fname)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}