Mercurial > hg > plosone_underreview
comparison notebooks/test_music_segments.ipynb @ 6:a35bd818d8e9 branch-tests
notebook to test music segments
author | Maria Panteli <m.x.panteli@gmail.com> |
---|---|
date | Mon, 11 Sep 2017 14:22:17 +0100 |
parents | |
children | 46b2c713cc73 |
comparison
equal
deleted
inserted
replaced
5:543744ed1ae7 | 6:a35bd818d8e9 |
---|---|
1 { | |
2 "cells": [ | |
3 { | |
4 "cell_type": "code", | |
5 "execution_count": 1, | |
6 "metadata": { | |
7 "collapsed": true | |
8 }, | |
9 "outputs": [], | |
10 "source": [ | |
11 "import numpy as np\n", | |
12 "import pickle" | |
13 ] | |
14 }, | |
15 { | |
16 "cell_type": "code", | |
17 "execution_count": 2, | |
18 "metadata": { | |
19 "collapsed": true | |
20 }, | |
21 "outputs": [], | |
22 "source": [ | |
23 "filenames = ['/import/c4dm-04/mariap/train_data_melodia_8.pickle', \n", | |
24 " '/import/c4dm-04/mariap/val_data_melodia_8.pickle', \n", | |
25 " '/import/c4dm-04/mariap/test_data_melodia_8.pickle']" | |
26 ] | |
27 }, | |
28 { | |
29 "cell_type": "code", | |
30 "execution_count": null, | |
31 "metadata": { | |
32 "collapsed": true | |
33 }, | |
34 "outputs": [], | |
35 "source": [ | |
36 "all_Yaudio = []\n", | |
37 "for filename in filenames:\n", | |
38 " _, Y, Yaudio = pickle.load(open(filename), 'rb')\n", | |
39 " all_Yaudio.append(Yaudio)\n", | |
40 "all_Yaudio = np.concatenate(all_Yaudio)" | |
41 ] | |
42 }, | |
43 { | |
44 "cell_type": "code", | |
45 "execution_count": 3, | |
46 "metadata": { | |
47 "collapsed": false | |
48 }, | |
49 "outputs": [ | |
50 { | |
51 "ename": "NameError", | |
52 "evalue": "name 'all_Yaudio' is not defined", | |
53 "output_type": "error", | |
54 "traceback": [ | |
55 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
56 "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
57 "\u001b[0;32m<ipython-input-3-4107ada442c0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0muniq_audio\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muniq_counts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munique\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mall_Yaudio\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_counts\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
58 "\u001b[0;31mNameError\u001b[0m: name 'all_Yaudio' is not defined" | |
59 ] | |
60 } | |
61 ], | |
62 "source": [ | |
63 "uniq_audio, uniq_counts = np.unique(all_Yaudio, return_counts=True)" | |
64 ] | |
65 }, | |
66 { | |
67 "cell_type": "markdown", | |
68 "metadata": {}, | |
69 "source": [ | |
70 "## Stats on audio files with very few music frames (after the speech/music discrimination)" | |
71 ] | |
72 }, | |
73 { | |
74 "cell_type": "code", | |
75 "execution_count": 4, | |
76 "metadata": { | |
77 "collapsed": false | |
78 }, | |
79 "outputs": [ | |
80 { | |
81 "ename": "NameError", | |
82 "evalue": "name 'uniq_counts' is not defined", | |
83 "output_type": "error", | |
84 "traceback": [ | |
85 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
86 "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
87 "\u001b[0;32m<ipython-input-4-700ed156399c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmin_n_frames\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mn_short_files\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_counts\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mmin_n_frames\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'%d files out of %d have less than %d frames'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mn_short_files\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_counts\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_n_frames\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
88 "\u001b[0;31mNameError\u001b[0m: name 'uniq_counts' is not defined" | |
89 ] | |
90 } | |
91 ], | |
92 "source": [ | |
93 "min_n_frames = 10\n", | |
94 "n_short_files = np.where(uniq_counts<min_n_frames)[0].shape\n", | |
95 "print '%d files out of %d have less than %d frames' % (n_short_files, len(uniq_counts), min_n_frames)" | |
96 ] | |
97 }, | |
98 { | |
99 "cell_type": "markdown", | |
100 "metadata": {}, | |
101 "source": [ | |
102 "## Stats on average duration of the music segments for all tracks" | |
103 ] | |
104 }, | |
105 { | |
106 "cell_type": "code", | |
107 "execution_count": 5, | |
108 "metadata": { | |
109 "collapsed": false | |
110 }, | |
111 "outputs": [ | |
112 { | |
113 "ename": "SyntaxError", | |
114 "evalue": "invalid syntax (<ipython-input-5-2c4ab0e943a6>, line 1)", | |
115 "output_type": "error", | |
116 "traceback": [ | |
117 "\u001b[0;36m File \u001b[0;32m\"<ipython-input-5-2c4ab0e943a6>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print 'mean %f' np.mean(uniq_counts)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" | |
118 ] | |
119 } | |
120 ], | |
121 "source": [ | |
122 "sr = 2.0 # with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\n", | |
123 "print 'mean %f' % np.mean(uniq_counts)\n", | |
124 "print 'median %f' % np.median(uniq_counts)\n", | |
125 "print 'std %f' % np.std(uniq_counts)\n", | |
126 "print 'mean duration %f' % (np.mean(uniq_counts) / sr)" | |
127 ] | |
128 }, | |
129 { | |
130 "cell_type": "markdown", | |
131 "metadata": {}, | |
132 "source": [ | |
133 "## Stats on average duration of the music segments for the British Library tracks" | |
134 ] | |
135 }, | |
136 { | |
137 "cell_type": "code", | |
138 "execution_count": 7, | |
139 "metadata": { | |
140 "collapsed": false | |
141 }, | |
142 "outputs": [ | |
143 { | |
144 "ename": "NameError", | |
145 "evalue": "name 'uniq_audio' is not defined", | |
146 "output_type": "error", | |
147 "traceback": [ | |
148 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
149 "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
150 "\u001b[0;32m<ipython-input-7-4ebf50436e4a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#British library tracks start with 'D:/Audio/...'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0midx_BL_tracks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_audio\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_audio\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'D:/'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0msr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2.0\u001b[0m \u001b[0;31m# with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'mean %f'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_counts\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0midx_BL_tracks\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'median %f'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmedian\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muniq_counts\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0midx_BL_tracks\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
151 "\u001b[0;31mNameError\u001b[0m: name 'uniq_audio' is not defined" | |
152 ] | |
153 } | |
154 ], | |
155 "source": [ | |
156 "#British library tracks start with 'D:/Audio/...'\n", | |
157 "idx_BL_tracks = np.array([i for i in range(len(uniq_audio)) if len(uniq_audio[i].split('D:/'))>1])\n", | |
158 "sr = 2.0 # with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\n", | |
159 "print 'mean %f' % np.mean(uniq_counts[idx_BL_tracks])\n", | |
160 "print 'median %f' % np.median(uniq_counts[idx_BL_tracks])\n", | |
161 "print 'std %f' % np.std(uniq_counts[idx_BL_tracks])\n", | |
162 "print 'mean duration %f' % (np.mean(uniq_counts[idx_BL_tracks]) / sr)" | |
163 ] | |
164 }, | |
165 { | |
166 "cell_type": "code", | |
167 "execution_count": null, | |
168 "metadata": { | |
169 "collapsed": true | |
170 }, | |
171 "outputs": [], | |
172 "source": [] | |
173 } | |
174 ], | |
175 "metadata": { | |
176 "kernelspec": { | |
177 "display_name": "Python 2", | |
178 "language": "python", | |
179 "name": "python2" | |
180 }, | |
181 "language_info": { | |
182 "codemirror_mode": { | |
183 "name": "ipython", | |
184 "version": 2 | |
185 }, | |
186 "file_extension": ".py", | |
187 "mimetype": "text/x-python", | |
188 "name": "python", | |
189 "nbconvert_exporter": "python", | |
190 "pygments_lexer": "ipython2", | |
191 "version": "2.7.12" | |
192 } | |
193 }, | |
194 "nbformat": 4, | |
195 "nbformat_minor": 0 | |
196 } |