{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline\n", "from pyannote.core import notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# SlidingWindowFeature (`pyannote.core.feature.SlidingWindowFeature`)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from pyannote.core import SlidingWindowFeature, SlidingWindow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**SlidingWindowFeature** are used to manage feature vectors extracted on a sliding window (e.g. MFCC in audio processing)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# one 4-dimensional feature vector extracted every 100ms from a 200ms window\n", "frame = SlidingWindow(start=0.0, step=0.100, duration=0.200)\n", "\n", "# random for illustration purposes\n", "data = np.random.randn(100, 4)\n", "\n", "features = SlidingWindowFeature(data, frame)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cropping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may use `crop` to extract a temporal subset:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method crop in module pyannote.core.feature:\n", "\n", "crop(self, focus, mode=u'loose', fixed=None) method of pyannote.core.feature.SlidingWindowFeature instance\n", " Extract frames as numpy array\n", " \n", " Parameters\n", " ----------\n", " focus : Segment or Timeline\n", " mode : {'loose', 'strict', 'center', 'fixed'}, optional\n", " In 'strict' mode, only frames fully included in focus coverage are\n", " returned. In 'loose' mode, any intersecting frames are returned. In\n", " 'center' mode, first and last frames are chosen to be the positions\n", " whose centers are the closest to the focus start and end times.\n", " Defaults to 'loose'.\n", " fixed : float, optional\n", " When provided and mode is 'center', override focus duration to make\n", " sure two `focus` with the same duration always result in the same\n", " (fixed) number of frames being selected.\n", " \n", " Returns\n", " -------\n", " data : numpy array\n", " (nSamples, nFeatures) numpy array\n", " \n", " See also\n", " --------\n", " SlidingWindow.crop\n", "\n" ] } ], "source": [ "help(features.crop)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[-0.170939 , 0.7268368 , -1.11357192, 0.9102115 ],\n", " [ 0.75526988, 0.13631738, -1.12959046, 0.57945519],\n", " [-1.63332126, -1.05239927, -2.80404685, -3.19025518],\n", " [-1.2622323 , 0.80263563, 0.8912935 , 0.85251067],\n", " [-0.58865284, -1.05005899, 0.5468816 , 1.03976293],\n", " [ 2.99740924, 1.544513 , -2.18700361, -0.19051365],\n", " [-1.16020413, 2.75010658, -1.63448261, 0.96354481],\n", " [-1.09404058, -0.56915077, -0.73265453, -0.5412315 ],\n", " [-3.83225883, 2.48916336, -0.475149 , -0.56418448],\n", " [ 0.01583975, -1.41733235, -0.47454703, -1.18346613],\n", " [ 0.4350982 , -0.61821957, 0.0458578 , -0.66109202],\n", " [ 1.39603516, 1.00657646, 1.67931705, 0.01362444],\n", " [ 0.95027176, -0.02961472, -0.71587331, -0.04884979]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyannote.core import Segment\n", "features.crop(Segment(2, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Need help?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can always try the following... \n", "Who knows? It might give you the information you are looking for!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class SlidingWindowFeature in module pyannote.core.feature:\n", "\n", "class SlidingWindowFeature(__builtin__.object)\n", " | Periodic feature vectors\n", " | \n", " | Parameters\n", " | ----------\n", " | data : (nSamples, nFeatures) numpy array\n", " | \n", " | sliding_window : SlidingWindow\n", " | \n", " | Methods defined here:\n", " | \n", " | __getitem__(self, i)\n", " | Get ith feature vector\n", " | \n", " | __init__(self, data, sliding_window)\n", " | \n", " | crop(self, focus, mode=u'loose', fixed=None)\n", " | Extract frames as numpy array\n", " | \n", " | Parameters\n", " | ----------\n", " | focus : Segment or Timeline\n", " | mode : {'loose', 'strict', 'center', 'fixed'}, optional\n", " | In 'strict' mode, only frames fully included in focus coverage are\n", " | returned. In 'loose' mode, any intersecting frames are returned. In\n", " | 'center' mode, first and last frames are chosen to be the positions\n", " | whose centers are the closest to the focus start and end times.\n", " | Defaults to 'loose'.\n", " | fixed : float, optional\n", " | When provided and mode is 'center', override focus duration to make\n", " | sure two `focus` with the same duration always result in the same\n", " | (fixed) number of frames being selected.\n", " | \n", " | Returns\n", " | -------\n", " | data : numpy array\n", " | (nSamples, nFeatures) numpy array\n", " | \n", " | See also\n", " | --------\n", " | SlidingWindow.crop\n", " | \n", " | getDimension(self)\n", " | Dimension of feature vectors\n", " | \n", " | getExtent(self)\n", " | \n", " | getNumber(self)\n", " | Number of feature vectors\n", " | \n", " | iterfeatures(self, window=False)\n", " | Feature vector iterator\n", " | \n", " | Parameters\n", " | ----------\n", " | window : bool, optional\n", " | When True, yield both feature vector and corresponding window.\n", " | Default is to only yield feature vector\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "help(SlidingWindowFeature)" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }