view notebooks/test_music_segments.ipynb @ 30:e8084526f7e5 branch-tests

additional test functions
author Maria Panteli <m.x.panteli@gmail.com>
date Wed, 13 Sep 2017 19:57:49 +0100
parents 46b2c713cc73
children e6e10013e11c
line wrap: on
line source
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filenames = ['/import/c4dm-04/mariap/train_data_melodia_8.pickle',\n",
    "             '/import/c4dm-04/mariap/val_data_melodia_8.pickle', \n",
    "             '/import/c4dm-04/mariap/test_data_melodia_8.pickle']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_Yaudio = []\n",
    "all_Y = []\n",
    "for filename in filenames:\n",
    "    _, Y, Yaudio = pickle.load(open(filename, 'rb'))\n",
    "    all_Yaudio.append(Yaudio)\n",
    "    all_Y.append(Y)\n",
    "all_Yaudio = np.concatenate(all_Yaudio)\n",
    "all_Y = np.concatenate(all_Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "uniq_audio, uniq_counts = np.unique(all_Yaudio, return_counts=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stats on audio files with very few music frames (after the speech/music discrimination)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "63 files out of 8200 have less than 10 frames\n"
     ]
    }
   ],
   "source": [
    "min_n_frames = 10\n",
    "short_files_idx = np.where(uniq_counts<min_n_frames)[0]\n",
    "print '%d files out of %d have less than %d frames' % (len(short_files_idx), len(uniq_counts), min_n_frames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Countries for tracks with less than 10 frames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Italy': 1, 'Peru': 1, 'Solomon Islands': 2, 'France': 2, 'Ethiopia': 1, 'Somalia': 1, 'Ireland': 1, 'Swaziland': 1, 'Argentina': 1, 'Norway': 1, 'Nigeria': 1, 'Algeria': 1, 'Germany': 1, 'Puerto Rico': 1, 'Dominican Republic': 1, 'Poland': 3, 'Spain': 2, 'Netherlands': 1, 'Uganda': 4, 'Western Sahara': 5, 'Gambia': 2, 'Philippines': 2, 'Trinidad and Tobago': 1, 'Latvia': 1, 'South Sudan': 3, 'Mali': 1, 'Russia': 1, 'Romania': 1, 'Portugal': 1, 'South Africa': 3, 'Egypt': 1, 'Sierra Leone': 1, 'United Kingdom': 4, 'Lesotho': 1, 'Senegal': 2, 'Colombia': 2, 'Japan': 2, 'Nicaragua': 1, 'Botswana': 1}\n"
     ]
    }
   ],
   "source": [
    "countries = np.array([all_Y[np.where(all_Yaudio==uniq_audio[audio_idx])[0][0]][0] for audio_idx in short_files_idx])\n",
    "unique, counts = np.unique(countries, return_counts=True)\n",
    "print dict(zip(unique, counts))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stats on average duration of the music segments for all tracks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean 65.750000\n",
      "median 44.000000\n",
      "std 45.947865\n",
      "mean duration 32.875000\n"
     ]
    }
   ],
   "source": [
    "sr = 2.0  # with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\n",
    "print 'mean %f' % np.mean(uniq_counts)\n",
    "print 'median %f' % np.median(uniq_counts)\n",
    "print 'std %f' % np.std(uniq_counts)\n",
    "print 'mean duration %f' % (np.mean(uniq_counts) / sr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stats on average duration of the music segments for the Smithsonian tracks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n tracks: 6132\n",
      "mean 42.618885\n",
      "median 44.000000\n",
      "std 4.804534\n",
      "mean duration 21.309442\n"
     ]
    }
   ],
   "source": [
    "#British library tracks start with 'D:/Audio/...'\n",
    "idx_SM_tracks = np.array([i for i in range(len(uniq_audio)) if len(uniq_audio[i].split('D:/'))==1])\n",
    "sr = 2.0  # with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\n",
    "print 'n tracks: %d' % len(idx_SM_tracks)\n",
    "print 'mean %f' % np.mean(uniq_counts[idx_SM_tracks])\n",
    "print 'median %f' % np.median(uniq_counts[idx_SM_tracks])\n",
    "print 'std %f' % np.std(uniq_counts[idx_SM_tracks])\n",
    "print 'mean duration %f' % (np.mean(uniq_counts[idx_SM_tracks]) / sr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stats on average duration of the music segments for the British Library tracks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n tracks: 2068\n",
      "mean 134.338008\n",
      "median 163.000000\n",
      "std 44.855790\n",
      "mean duration 67.169004\n"
     ]
    }
   ],
   "source": [
    "#British library tracks start with 'D:/Audio/...'\n",
    "idx_BL_tracks = np.array([i for i in range(len(uniq_audio)) if len(uniq_audio[i].split('D:/'))>1])\n",
    "sr = 2.0  # with 8-second window and 0.5-second hop size the sampling rate is 2 about 2 samples per second\n",
    "print 'n tracks: %d' % len(idx_BL_tracks)\n",
    "print 'mean %f' % np.mean(uniq_counts[idx_BL_tracks])\n",
    "print 'median %f' % np.median(uniq_counts[idx_BL_tracks])\n",
    "print 'std %f' % np.std(uniq_counts[idx_BL_tracks])\n",
    "print 'mean duration %f' % (np.mean(uniq_counts[idx_BL_tracks]) / sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}