Skip to content

Instantly share code, notes, and snippets.

@justinsalamon
Created May 26, 2016 20:19
Show Gist options
  • Save justinsalamon/a54be598dae3573d290ab2dba6b5f1dd to your computer and use it in GitHub Desktop.
Save justinsalamon/a54be598dae3573d290ab2dba6b5f1dd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import jams\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Generate an example event list</h2>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"events = pd.DataFrame(columns=['start_time', 'end_time', 'label', 'src_file', 'src_start', 'src_end', 'snr', 'role'])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# foreground events\n",
"events['start_time'] = np.arange(0, 7, 3)\n",
"events['end_time'] = np.arange(1, 8, 3)\n",
"events['label'] = ['car_honking', 'jackhammer_drilling', 'siren_wailing']\n",
"events['src_file'] = ['/sounds/a.wav', '/sounds/b.wav', '/sounds/c.wav']\n",
"events['src_start'] = np.zeros(3)\n",
"events['src_end'] = np.ones(3)\n",
"events['snr'] = [3, 0, -1]\n",
"events['role'] = ['event', 'event', 'event']\n",
"\n",
"# background \n",
"events.loc[len(events)] = [0, 7, 'park', '/backgrounds/park.wav', 5, 12, 0, 'background']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>start_time</th>\n",
" <th>end_time</th>\n",
" <th>label</th>\n",
" <th>src_file</th>\n",
" <th>src_start</th>\n",
" <th>src_end</th>\n",
" <th>snr</th>\n",
" <th>role</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>car_honking</td>\n",
" <td>/sounds/a.wav</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>3</td>\n",
" <td>event</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>jackhammer_drilling</td>\n",
" <td>/sounds/b.wav</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0</td>\n",
" <td>event</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>siren_wailing</td>\n",
" <td>/sounds/c.wav</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>-1</td>\n",
" <td>event</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>7</td>\n",
" <td>park</td>\n",
" <td>/backgrounds/park.wav</td>\n",
" <td>5.0</td>\n",
" <td>12.0</td>\n",
" <td>0</td>\n",
" <td>background</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" start_time end_time label src_file \\\n",
"0 0 1 car_honking /sounds/a.wav \n",
"1 3 4 jackhammer_drilling /sounds/b.wav \n",
"2 6 7 siren_wailing /sounds/c.wav \n",
"3 0 7 park /backgrounds/park.wav \n",
"\n",
" src_start src_end snr role \n",
"0 0.0 1.0 3 event \n",
"1 0.0 1.0 0 event \n",
"2 0.0 1.0 -1 event \n",
"3 5.0 12.0 0 background "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# lets see our events\n",
"events"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Create a JAMS file from this event list</h2>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scene_jam = jams.JAMS()\n",
"scene_ann = jams.Annotation(namespace='tag_open')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# everything other than the start time and end time (which translates to duraion)\n",
"# goes into the value field as a tuple\n",
"for ind, event in events.iterrows():\n",
" scene_ann.append(time=event['start_time'],\n",
" duration=event['end_time'] - event['start_time'],\n",
" value=(event['label'], event['src_file'], \n",
" event['src_start'], event['src_end'], \n",
" event['snr'], event['role']),\n",
" confidence=1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>time</th>\n",
" <th>duration</th>\n",
" <th>value</th>\n",
" <th>confidence</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>00:00:00</td>\n",
" <td>00:00:01</td>\n",
" <td>(car_honking, /sounds/a.wav, 0.0, 1.0, 3, event)</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>00:00:03</td>\n",
" <td>00:00:01</td>\n",
" <td>(jackhammer_drilling, /sounds/b.wav, 0.0, 1.0,...</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>00:00:06</td>\n",
" <td>00:00:01</td>\n",
" <td>(siren_wailing, /sounds/c.wav, 0.0, 1.0, -1, e...</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>00:00:00</td>\n",
" <td>00:00:07</td>\n",
" <td>(park, /backgrounds/park.wav, 5.0, 12.0, 0, ba...</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" time duration value \\\n",
"0 00:00:00 00:00:01 (car_honking, /sounds/a.wav, 0.0, 1.0, 3, event) \n",
"1 00:00:03 00:00:01 (jackhammer_drilling, /sounds/b.wav, 0.0, 1.0,... \n",
"2 00:00:06 00:00:01 (siren_wailing, /sounds/c.wav, 0.0, 1.0, -1, e... \n",
"3 00:00:00 00:00:07 (park, /backgrounds/park.wav, 5.0, 12.0, 0, ba... \n",
"\n",
" confidence \n",
"0 1.0 \n",
"1 1.0 \n",
"2 1.0 \n",
"3 1.0 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let's see what the annotation looks like:\n",
"scene_ann.data"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# let's add this scene to the jam\n",
"scene_jam.annotations.append(scene_ann)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Finally we need to populate the annotation_metadata with all the parameter values\n",
"# used to generate this scene. I think they can all go into the sandbox where you can\n",
"# create a custom dictionary structure."
]
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment