Guest User

Untitled

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "%load_ext autoreload\n",
  10. "%autoreload 2"
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": 2,
  16. "metadata": {},
  17. "outputs": [],
  18. "source": [
  19. "import numpy as np\n",
  20. "from numba import jit\n",
  21. "import librosa"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 3,
  27. "metadata": {},
  28. "outputs": [],
  29. "source": [
  30. "from_arr = np.array([[1,3],[5,7],[5,8]])"
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": 4,
  36. "metadata": {},
  37. "outputs": [],
  38. "source": [
  39. "from_arr_2 = np.array([[1,3],[7,5],[5,8]])"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 5,
  45. "metadata": {},
  46. "outputs": [],
  47. "source": [
  48. "to_arr = np.array([[1,2],[4,8]])"
  49. ]
  50. },
  51. {
  52. "cell_type": "code",
  53. "execution_count": 6,
  54. "metadata": {},
  55. "outputs": [
  56. {
  57. "data": {
  58. "text/plain": [
  59. "array([[1, 2],\n",
  60. " [4, 8]])"
  61. ]
  62. },
  63. "execution_count": 6,
  64. "metadata": {},
  65. "output_type": "execute_result"
  66. }
  67. ],
  68. "source": [
  69. "to_arr"
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": 7,
  75. "metadata": {},
  76. "outputs": [
  77. {
  78. "data": {
  79. "text/plain": [
  80. "array([[1, 3],\n",
  81. " [5, 7],\n",
  82. " [5, 8]])"
  83. ]
  84. },
  85. "execution_count": 7,
  86. "metadata": {},
  87. "output_type": "execute_result"
  88. }
  89. ],
  90. "source": [
  91. "from_arr"
  92. ]
  93. },
  94. {
  95. "cell_type": "code",
  96. "execution_count": 12,
  97. "metadata": {},
  98. "outputs": [
  99. {
  100. "data": {
  101. "text/plain": [
  102. "array([0, 0, 1])"
  103. ]
  104. },
  105. "execution_count": 12,
  106. "metadata": {},
  107. "output_type": "execute_result"
  108. }
  109. ],
  110. "source": [
  111. "librosa.util.match_intervals(from_arr_2, to_arr)"
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "execution_count": 15,
  117. "metadata": {
  118. "scrolled": false
  119. },
  120. "outputs": [
  121. {
  122. "data": {
  123. "text/plain": [
  124. "array([[0, 0],\n",
  125. " [1, 1],\n",
  126. " [0, 3]])"
  127. ]
  128. },
  129. "execution_count": 15,
  130. "metadata": {},
  131. "output_type": "execute_result"
  132. }
  133. ],
  134. "source": [
  135. "match_intervals(from_arr, to_arr)"
  136. ]
  137. },
  138. {
  139. "cell_type": "code",
  140. "execution_count": 14,
  141. "metadata": {},
  142. "outputs": [
  143. {
  144. "data": {
  145. "text/plain": [
  146. "array([1, 2, 3, 4, 5, 7, 8])"
  147. ]
  148. },
  149. "execution_count": 14,
  150. "metadata": {},
  151. "output_type": "execute_result"
  152. }
  153. ],
  154. "source": [
  155. "__find_union(from_arr, to_arr)"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 16,
  161. "metadata": {},
  162. "outputs": [],
  163. "source": [
  164. "def match_intervals(intervals_from, intervals_to, strict=True):\n",
  165. " if len(intervals_from) == 0 or len(intervals_to) == 0:\n",
  166. " raise ParameterError('Attempting to match empty interval list')\n",
  167. "\n",
  168. " valid_intervals(intervals_from)\n",
  169. " valid_intervals(intervals_to)\n",
  170. " \n",
  171. " # instantiate empty_like list\n",
  172. " output = np.empty_like(intervals_from, dtype=np.int)\n",
  173. " \n",
  174. " # for optimizing nested for-loop\n",
  175. " to_start = np.empty_like(intervals_to, dtype=np.int)\n",
  176. " to_end = np.empty_like(intervals_to, dtype=np.int)\n",
  177. " from_start = np.empty_like(intervals_from, dtype=np.int)\n",
  178. " \n",
  179. " return __match_intervals(intervals_from, intervals_to, output, strict)\n",
  180. "\n",
  181. "\n",
  182. "\n",
  183. "@jit\n",
  184. "def __match_intervals(intervals_from, intervals_to, output, strict):\n",
  185. " union = np.intersect1d(intervals_from, intervals_to)\n",
  186. " intersection = np.intersect1d(intervals_from, intervals_to)\n",
  187. " diff = 0\n",
  188. " \n",
  189. " '''\n",
  190. " # Optimizing nested for-loop\n",
  191. " # add end to-intervals to to_end array\n",
  192. " for to_ind, to_interval in enumerate(intervals_to):\n",
  193. " to_end[to_ind] = to_interval[0]\n",
  194. " \n",
  195. " # sort to_end array\n",
  196. " to_end_idx = np.argsort(to_end)\n",
  197. " sorted_to_end = events_to[to_end_idx]\n",
  198. " '''\n",
  199. " \n",
  200. " # find a possible to-interval to insert into\n",
  201. " for from_ind, from_interval in enumerate(intervals_from):\n",
  202. " from_start[from_ind] = np.searchsorted(sorted_to_end, from_interval[0])\n",
  203. "\n",
  204. " matching_to_ind = None\n",
  205. " is_matched = False\n",
  206. "\n",
  207. " for t_ind, t_interval in enumerate(intervals_to):\n",
  208. " # left and right are initialized\n",
  209. " left = f_interval[0] \n",
  210. " right = f_interval[1]\n",
  211. "\n",
  212. " # check if the to-intervals are a better fit than previous\n",
  213. " if left < t_interval[0]:\n",
  214. " left = t_interval[0]\n",
  215. " if right > t_interval[1]:\n",
  216. " right = t_interval[1]\n",
  217. "\n",
  218. " # test for a valid match\n",
  219. " if diff < (right - left):\n",
  220. " diff = right - left\n",
  221. " matching_to_ind = t_ind\n",
  222. " is_matched = True\n",
  223. "\n",
  224. " # only put a number down if there is a match\n",
  225. " if is_matched:\n",
  226. " intersection[f_ind] = matching_to_ind\n",
  227. " else:\n",
  228. " # find jaccard or hybrid distance\n",
  229. " pass\n",
  230. "\n",
  231. " \n",
  232. " return output"
  233. ]
  234. },
  235. {
  236. "cell_type": "code",
  237. "execution_count": 14,
  238. "metadata": {},
  239. "outputs": [],
  240. "source": [
  241. "def valid_intervals(intervals):\n",
  242. " '''Ensure that an array is a valid representation of time intervals:\n",
  243. "\n",
  244. " - intervals.ndim == 2\n",
  245. " - intervals.shape[1] == 2\n",
  246. "\n",
  247. " Parameters\n",
  248. " ----------\n",
  249. " intervals : np.ndarray [shape=(n, 2)]\n",
  250. " set of time intervals\n",
  251. "\n",
  252. " Returns\n",
  253. " -------\n",
  254. " valid : bool\n",
  255. " True if `intervals` passes validation.\n",
  256. " '''\n",
  257. "\n",
  258. " if intervals.ndim != 2 or intervals.shape[-1] != 2:\n",
  259. " raise ParameterError('intervals must have shape (n, 2)')\n",
  260. "\n",
  261. " return True"
  262. ]
  263. },
  264. {
  265. "cell_type": "code",
  266. "execution_count": null,
  267. "metadata": {},
  268. "outputs": [],
  269. "source": []
  270. }
  271. ],
  272. "metadata": {
  273. "kernelspec": {
  274. "display_name": "Python [conda env:env1]",
  275. "language": "python",
  276. "name": "conda-env-env1-py"
  277. },
  278. "language_info": {
  279. "codemirror_mode": {
  280. "name": "ipython",
  281. "version": 3
  282. },
  283. "file_extension": ".py",
  284. "mimetype": "text/x-python",
  285. "name": "python",
  286. "nbconvert_exporter": "python",
  287. "pygments_lexer": "ipython3",
  288. "version": "3.5.4"
  289. }
  290. },
  291. "nbformat": 4,
  292. "nbformat_minor": 2
  293. }
Add Comment
Please, Sign In to add comment