Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "%load_ext autoreload\n",
- "%autoreload 2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "import numpy as np\n",
- "from numba import jit\n",
- "import librosa"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "from_arr = np.array([[1,3],[5,7],[5,8]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "from_arr_2 = np.array([[1,3],[7,5],[5,8]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [],
- "source": [
- "to_arr = np.array([[1,2],[4,8]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 2],\n",
- " [4, 8]])"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "to_arr"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 3],\n",
- " [5, 7],\n",
- " [5, 8]])"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from_arr"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([0, 0, 1])"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "librosa.util.match_intervals(from_arr_2, to_arr)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[0, 0],\n",
- " [1, 1],\n",
- " [0, 3]])"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "match_intervals(from_arr, to_arr)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([1, 2, 3, 4, 5, 7, 8])"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "__find_union(from_arr, to_arr)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "def match_intervals(intervals_from, intervals_to, strict=True):\n",
- " if len(intervals_from) == 0 or len(intervals_to) == 0:\n",
- " raise ParameterError('Attempting to match empty interval list')\n",
- "\n",
- " valid_intervals(intervals_from)\n",
- " valid_intervals(intervals_to)\n",
- " \n",
- " # instantiate empty_like list\n",
- " output = np.empty_like(intervals_from, dtype=np.int)\n",
- " \n",
- " # for optimizing nested for-loop\n",
- " to_start = np.empty_like(intervals_to, dtype=np.int)\n",
- " to_end = np.empty_like(intervals_to, dtype=np.int)\n",
- " from_start = np.empty_like(intervals_from, dtype=np.int)\n",
- " \n",
- " return __match_intervals(intervals_from, intervals_to, output, strict)\n",
- "\n",
- "\n",
- "\n",
- "@jit\n",
- "def __match_intervals(intervals_from, intervals_to, output, strict):\n",
- " union = np.intersect1d(intervals_from, intervals_to)\n",
- " intersection = np.intersect1d(intervals_from, intervals_to)\n",
- " diff = 0\n",
- " \n",
- " '''\n",
- " # Optimizing nested for-loop\n",
- " # add end to-intervals to to_end array\n",
- " for to_ind, to_interval in enumerate(intervals_to):\n",
- " to_end[to_ind] = to_interval[0]\n",
- " \n",
- " # sort to_end array\n",
- " to_end_idx = np.argsort(to_end)\n",
- " sorted_to_end = events_to[to_end_idx]\n",
- " '''\n",
- " \n",
- " # find a possible to-interval to insert into\n",
- " for from_ind, from_interval in enumerate(intervals_from):\n",
- " from_start[from_ind] = np.searchsorted(sorted_to_end, from_interval[0])\n",
- "\n",
- " matching_to_ind = None\n",
- " is_matched = False\n",
- "\n",
- " for t_ind, t_interval in enumerate(intervals_to):\n",
- " # left and right are initialized\n",
- " left = f_interval[0] \n",
- " right = f_interval[1]\n",
- "\n",
- " # check if the to-intervals are a better fit than previous\n",
- " if left < t_interval[0]:\n",
- " left = t_interval[0]\n",
- " if right > t_interval[1]:\n",
- " right = t_interval[1]\n",
- "\n",
- " # test for a valid match\n",
- " if diff < (right - left):\n",
- " diff = right - left\n",
- " matching_to_ind = t_ind\n",
- " is_matched = True\n",
- "\n",
- " # only put a number down if there is a match\n",
- " if is_matched:\n",
- " intersection[f_ind] = matching_to_ind\n",
- " else:\n",
- " # find jaccard or hybrid distance\n",
- " pass\n",
- "\n",
- " \n",
- " return output"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [],
- "source": [
- "def valid_intervals(intervals):\n",
- " '''Ensure that an array is a valid representation of time intervals:\n",
- "\n",
- " - intervals.ndim == 2\n",
- " - intervals.shape[1] == 2\n",
- "\n",
- " Parameters\n",
- " ----------\n",
- " intervals : np.ndarray [shape=(n, 2)]\n",
- " set of time intervals\n",
- "\n",
- " Returns\n",
- " -------\n",
- " valid : bool\n",
- " True if `intervals` passes validation.\n",
- " '''\n",
- "\n",
- " if intervals.ndim != 2 or intervals.shape[-1] != 2:\n",
- " raise ParameterError('intervals must have shape (n, 2)')\n",
- "\n",
- " return True"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python [conda env:env1]",
- "language": "python",
- "name": "conda-env-env1-py"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.5.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment