Skip to content

Instantly share code, notes, and snippets.

@jdfreder
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save jdfreder/b0c24b96602ffef6ddba to your computer and use it in GitHub Desktop.

Select an option

Save jdfreder/b0c24b96602ffef6ddba to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"codemirror_mode": {
"name": "python",
"version": 2
},
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"name": "",
"signature": "sha256:fcf669a6bb9b33d907a39d1cc1d097f5737f6fbb1cdf4fd2e2ab097a7780fbe0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 146
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = np.arange(0, 40, 2)\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 147,
"text": [
"array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,\n",
" 34, 36, 38])"
]
}
],
"prompt_number": 147
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = np.random.randint(-10, 10, 20)\n",
"a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 148,
"text": [
"array([ 6, -4, -7, -1, -9, 2, -8, 8, -6, 9, -5, -5, 7, -9, 4, 9, 4,\n",
" 0, 3, 5])"
]
}
],
"prompt_number": 148
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b = np.zeros(a.shape)\n",
"b[a > 0] = 1\n",
"b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 149,
"text": [
"array([ 1., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., 1.,\n",
" 0., 1., 1., 1., 0., 1., 1.])"
]
}
],
"prompt_number": 149
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c = np.roll(b, -1)\n",
"c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 150,
"text": [
"array([ 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., 1., 0.,\n",
" 1., 1., 1., 0., 1., 1., 1.])"
]
}
],
"prompt_number": 150
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d = b - c\n",
"d"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 151,
"text": [
"array([ 1., 0., 0., 0., -1., 1., -1., 1., -1., 1., 0., -1., 1.,\n",
" -1., 0., 0., 1., -1., 0., 0.])"
]
}
],
"prompt_number": 151
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"e = np.where(d != 0)[0]\n",
"if e[0] != 0:\n",
" e = np.insert(e, 0, 0)\n",
"e"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 152,
"text": [
"array([ 0, 4, 5, 6, 7, 8, 9, 11, 12, 13, 16, 17])"
]
}
],
"prompt_number": 152
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = np.roll(e, -1)\n",
"f[-1] = a.shape[0]\n",
"f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 153,
"text": [
"array([ 4, 5, 6, 7, 8, 9, 11, 12, 13, 16, 17, 20])"
]
}
],
"prompt_number": 153
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g = f - e\n",
"g"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 154,
"text": [
"array([4, 1, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3])"
]
}
],
"prompt_number": 154
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g.sum()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 155,
"text": [
"20"
]
}
],
"prompt_number": 155
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"h = np.repeat(e, g)\n",
"h"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 156,
"text": [
"array([ 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 9, 11, 12, 13, 13, 13, 16,\n",
" 17, 17, 17])"
]
}
],
"prompt_number": 156
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"i = h - np.arange(a.shape[0])\n",
"i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 161,
"text": [
"array([ 0, -1, -2, -3, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, -2, 0,\n",
" 0, -1, -2])"
]
}
],
"prompt_number": 161
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"start = t[i + np.arange(a.shape[0])]\n",
"start"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 162,
"text": [
"array([ 0, 0, 0, 0, 8, 10, 12, 14, 16, 18, 18, 22, 24, 26, 26, 26, 32,\n",
" 34, 34, 34])"
]
}
],
"prompt_number": 162
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"current = t\n",
"current"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 163,
"text": [
"array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,\n",
" 34, 36, 38])"
]
}
],
"prompt_number": 163
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"delta = current - start\n",
"delta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 164,
"text": [
"array([0, 2, 4, 6, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 4, 0, 0, 2, 4])"
]
}
],
"prompt_number": 164
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def fast_group(a, t):\n",
" # Anything greater than 0 should be 1.\n",
" b = np.zeros(a.shape)\n",
" b[a > 0.] = 1\n",
" \n",
" # Find leading and trailing edges.\n",
" d = b - np.roll(b, -1)\n",
" d = np.roll(d, 1)\n",
" \n",
" # Get indicies of the edges. Make sure 0 is an edge.\n",
" e = np.where(d != 0)[0]\n",
" if e[0] != 0:\n",
" e = np.insert(e, 0, 0)\n",
" \n",
" # Calc the length of each segment usings edge indicies.\n",
" f = np.roll(e, -1)\n",
" f[-1] = a.shape[0]\n",
" g = f - e\n",
" \n",
" # Repeat the edge indicies for the segment lengths.\n",
" h = np.repeat(e, g)\n",
" \n",
" # Get the segment start times\n",
" i = h - np.arange(a.shape[0])\n",
" start = t[i + np.arange(a.shape[0])]\n",
" \n",
" # Return the delta from the last start time.\n",
" return t - start"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 165
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = np.random.randint(-10, 10, 20)\n",
"t = np.arange(20)\n",
"fast_group(a,t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 218,
"text": [
"array([0, 0, 1, 2, 3, 0, 1, 2, 0, 1, 2, 3, 4, 0, 1, 0, 0, 0, 0, 0])"
]
}
],
"prompt_number": 218
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment