Skip to content

Instantly share code, notes, and snippets.

@lagru
Last active March 22, 2018 19:36
Show Gist options
  • Save lagru/0cc494385c12bd7f21b9477a13c79329 to your computer and use it in GitHub Desktop.
Save lagru/0cc494385c12bd7f21b9477a13c79329 to your computer and use it in GitHub Desktop.
Evaluation of _select_by_peak_distance.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%load_ext Cython\n",
"%load_ext line_profiler\n",
"\n",
"import timeit\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import line_profiler\n",
"from scipy.signal import find_peaks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vectorized NumPy solution"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def select_by_peak_distance_py(peaks, priority, dmin):\n",
" \"\"\"\n",
" Evaluate which peaks fulfill the distance condition.\n",
"\n",
" Parameters\n",
" ----------\n",
" peaks : ndarray\n",
" Indices of peaks in `vector`.\n",
" priority : ndarray\n",
" An array with priorities matching `peaks` used to determine priority of\n",
" peaks. A peak with a higher priority value is kept over one with a lower\n",
" one.\n",
" dmin : number\n",
" Minimal distance that peaks must be spaced.\n",
"\n",
" Returns\n",
" -------\n",
" keep : ndarray[bool]\n",
" A boolean mask evaluating to true where `peaks` fulfill the distance\n",
" condition.\n",
"\n",
" Notes\n",
" -----\n",
"\n",
" .. versionadded:: 1.1.0\n",
" \"\"\"\n",
" # Peaks are evaluated by priority (larger first)\n",
" eval_peaks = peaks[np.argsort(priority)][::-1]\n",
"\n",
" # Flag peaks for deletion\n",
" del_flag = np.zeros(eval_peaks.size, dtype=bool)\n",
" for i in range(eval_peaks.size):\n",
" if not del_flag[i]:\n",
" # Flag peaks in intervall +-distance around current peak\n",
" del_flag |= (eval_peaks > eval_peaks[i] - dmin) \\\n",
" & (eval_peaks < eval_peaks[i] + dmin)\n",
" # Keep current peak\n",
" del_flag[i] = False\n",
"\n",
" keep = ~del_flag[np.argsort(eval_peaks)]\n",
"\n",
" return keep"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loop-based Cython solution"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html>\n",
"<!-- Generated by Cython 0.27.3 -->\n",
"<html>\n",
"<head>\n",
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n",
" <title>Cython: _cython_magic_4718562d00f3793c17382a068f2a3571.pyx</title>\n",
" <style type=\"text/css\">\n",
" \n",
"body.cython { font-family: courier; font-size: 12; }\n",
"\n",
".cython.tag { }\n",
".cython.line { margin: 0em }\n",
".cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }\n",
"\n",
".cython.line .run { background-color: #B0FFB0; }\n",
".cython.line .mis { background-color: #FFB0B0; }\n",
".cython.code.run { border-left: 8px solid #B0FFB0; }\n",
".cython.code.mis { border-left: 8px solid #FFB0B0; }\n",
"\n",
".cython.code .py_c_api { color: red; }\n",
".cython.code .py_macro_api { color: #FF7000; }\n",
".cython.code .pyx_c_api { color: #FF3000; }\n",
".cython.code .pyx_macro_api { color: #FF7000; }\n",
".cython.code .refnanny { color: #FFA000; }\n",
".cython.code .trace { color: #FFA000; }\n",
".cython.code .error_goto { color: #FFA000; }\n",
"\n",
".cython.code .coerce { color: #008000; border: 1px dotted #008000 }\n",
".cython.code .py_attr { color: #FF0000; font-weight: bold; }\n",
".cython.code .c_attr { color: #0000FF; }\n",
".cython.code .py_call { color: #FF0000; font-weight: bold; }\n",
".cython.code .c_call { color: #0000FF; }\n",
"\n",
".cython.score-0 {background-color: #FFFFff;}\n",
".cython.score-1 {background-color: #FFFFe7;}\n",
".cython.score-2 {background-color: #FFFFd4;}\n",
".cython.score-3 {background-color: #FFFFc4;}\n",
".cython.score-4 {background-color: #FFFFb6;}\n",
".cython.score-5 {background-color: #FFFFaa;}\n",
".cython.score-6 {background-color: #FFFF9f;}\n",
".cython.score-7 {background-color: #FFFF96;}\n",
".cython.score-8 {background-color: #FFFF8d;}\n",
".cython.score-9 {background-color: #FFFF86;}\n",
".cython.score-10 {background-color: #FFFF7f;}\n",
".cython.score-11 {background-color: #FFFF79;}\n",
".cython.score-12 {background-color: #FFFF73;}\n",
".cython.score-13 {background-color: #FFFF6e;}\n",
".cython.score-14 {background-color: #FFFF6a;}\n",
".cython.score-15 {background-color: #FFFF66;}\n",
".cython.score-16 {background-color: #FFFF62;}\n",
".cython.score-17 {background-color: #FFFF5e;}\n",
".cython.score-18 {background-color: #FFFF5b;}\n",
".cython.score-19 {background-color: #FFFF57;}\n",
".cython.score-20 {background-color: #FFFF55;}\n",
".cython.score-21 {background-color: #FFFF52;}\n",
".cython.score-22 {background-color: #FFFF4f;}\n",
".cython.score-23 {background-color: #FFFF4d;}\n",
".cython.score-24 {background-color: #FFFF4b;}\n",
".cython.score-25 {background-color: #FFFF48;}\n",
".cython.score-26 {background-color: #FFFF46;}\n",
".cython.score-27 {background-color: #FFFF44;}\n",
".cython.score-28 {background-color: #FFFF43;}\n",
".cython.score-29 {background-color: #FFFF41;}\n",
".cython.score-30 {background-color: #FFFF3f;}\n",
".cython.score-31 {background-color: #FFFF3e;}\n",
".cython.score-32 {background-color: #FFFF3c;}\n",
".cython.score-33 {background-color: #FFFF3b;}\n",
".cython.score-34 {background-color: #FFFF39;}\n",
".cython.score-35 {background-color: #FFFF38;}\n",
".cython.score-36 {background-color: #FFFF37;}\n",
".cython.score-37 {background-color: #FFFF36;}\n",
".cython.score-38 {background-color: #FFFF35;}\n",
".cython.score-39 {background-color: #FFFF34;}\n",
".cython.score-40 {background-color: #FFFF33;}\n",
".cython.score-41 {background-color: #FFFF32;}\n",
".cython.score-42 {background-color: #FFFF31;}\n",
".cython.score-43 {background-color: #FFFF30;}\n",
".cython.score-44 {background-color: #FFFF2f;}\n",
".cython.score-45 {background-color: #FFFF2e;}\n",
".cython.score-46 {background-color: #FFFF2d;}\n",
".cython.score-47 {background-color: #FFFF2c;}\n",
".cython.score-48 {background-color: #FFFF2b;}\n",
".cython.score-49 {background-color: #FFFF2b;}\n",
".cython.score-50 {background-color: #FFFF2a;}\n",
".cython.score-51 {background-color: #FFFF29;}\n",
".cython.score-52 {background-color: #FFFF29;}\n",
".cython.score-53 {background-color: #FFFF28;}\n",
".cython.score-54 {background-color: #FFFF27;}\n",
".cython.score-55 {background-color: #FFFF27;}\n",
".cython.score-56 {background-color: #FFFF26;}\n",
".cython.score-57 {background-color: #FFFF26;}\n",
".cython.score-58 {background-color: #FFFF25;}\n",
".cython.score-59 {background-color: #FFFF24;}\n",
".cython.score-60 {background-color: #FFFF24;}\n",
".cython.score-61 {background-color: #FFFF23;}\n",
".cython.score-62 {background-color: #FFFF23;}\n",
".cython.score-63 {background-color: #FFFF22;}\n",
".cython.score-64 {background-color: #FFFF22;}\n",
".cython.score-65 {background-color: #FFFF22;}\n",
".cython.score-66 {background-color: #FFFF21;}\n",
".cython.score-67 {background-color: #FFFF21;}\n",
".cython.score-68 {background-color: #FFFF20;}\n",
".cython.score-69 {background-color: #FFFF20;}\n",
".cython.score-70 {background-color: #FFFF1f;}\n",
".cython.score-71 {background-color: #FFFF1f;}\n",
".cython.score-72 {background-color: #FFFF1f;}\n",
".cython.score-73 {background-color: #FFFF1e;}\n",
".cython.score-74 {background-color: #FFFF1e;}\n",
".cython.score-75 {background-color: #FFFF1e;}\n",
".cython.score-76 {background-color: #FFFF1d;}\n",
".cython.score-77 {background-color: #FFFF1d;}\n",
".cython.score-78 {background-color: #FFFF1c;}\n",
".cython.score-79 {background-color: #FFFF1c;}\n",
".cython.score-80 {background-color: #FFFF1c;}\n",
".cython.score-81 {background-color: #FFFF1c;}\n",
".cython.score-82 {background-color: #FFFF1b;}\n",
".cython.score-83 {background-color: #FFFF1b;}\n",
".cython.score-84 {background-color: #FFFF1b;}\n",
".cython.score-85 {background-color: #FFFF1a;}\n",
".cython.score-86 {background-color: #FFFF1a;}\n",
".cython.score-87 {background-color: #FFFF1a;}\n",
".cython.score-88 {background-color: #FFFF1a;}\n",
".cython.score-89 {background-color: #FFFF19;}\n",
".cython.score-90 {background-color: #FFFF19;}\n",
".cython.score-91 {background-color: #FFFF19;}\n",
".cython.score-92 {background-color: #FFFF19;}\n",
".cython.score-93 {background-color: #FFFF18;}\n",
".cython.score-94 {background-color: #FFFF18;}\n",
".cython.score-95 {background-color: #FFFF18;}\n",
".cython.score-96 {background-color: #FFFF18;}\n",
".cython.score-97 {background-color: #FFFF17;}\n",
".cython.score-98 {background-color: #FFFF17;}\n",
".cython.score-99 {background-color: #FFFF17;}\n",
".cython.score-100 {background-color: #FFFF17;}\n",
".cython.score-101 {background-color: #FFFF16;}\n",
".cython.score-102 {background-color: #FFFF16;}\n",
".cython.score-103 {background-color: #FFFF16;}\n",
".cython.score-104 {background-color: #FFFF16;}\n",
".cython.score-105 {background-color: #FFFF16;}\n",
".cython.score-106 {background-color: #FFFF15;}\n",
".cython.score-107 {background-color: #FFFF15;}\n",
".cython.score-108 {background-color: #FFFF15;}\n",
".cython.score-109 {background-color: #FFFF15;}\n",
".cython.score-110 {background-color: #FFFF15;}\n",
".cython.score-111 {background-color: #FFFF15;}\n",
".cython.score-112 {background-color: #FFFF14;}\n",
".cython.score-113 {background-color: #FFFF14;}\n",
".cython.score-114 {background-color: #FFFF14;}\n",
".cython.score-115 {background-color: #FFFF14;}\n",
".cython.score-116 {background-color: #FFFF14;}\n",
".cython.score-117 {background-color: #FFFF14;}\n",
".cython.score-118 {background-color: #FFFF13;}\n",
".cython.score-119 {background-color: #FFFF13;}\n",
".cython.score-120 {background-color: #FFFF13;}\n",
".cython.score-121 {background-color: #FFFF13;}\n",
".cython.score-122 {background-color: #FFFF13;}\n",
".cython.score-123 {background-color: #FFFF13;}\n",
".cython.score-124 {background-color: #FFFF13;}\n",
".cython.score-125 {background-color: #FFFF12;}\n",
".cython.score-126 {background-color: #FFFF12;}\n",
".cython.score-127 {background-color: #FFFF12;}\n",
".cython.score-128 {background-color: #FFFF12;}\n",
".cython.score-129 {background-color: #FFFF12;}\n",
".cython.score-130 {background-color: #FFFF12;}\n",
".cython.score-131 {background-color: #FFFF12;}\n",
".cython.score-132 {background-color: #FFFF11;}\n",
".cython.score-133 {background-color: #FFFF11;}\n",
".cython.score-134 {background-color: #FFFF11;}\n",
".cython.score-135 {background-color: #FFFF11;}\n",
".cython.score-136 {background-color: #FFFF11;}\n",
".cython.score-137 {background-color: #FFFF11;}\n",
".cython.score-138 {background-color: #FFFF11;}\n",
".cython.score-139 {background-color: #FFFF11;}\n",
".cython.score-140 {background-color: #FFFF11;}\n",
".cython.score-141 {background-color: #FFFF10;}\n",
".cython.score-142 {background-color: #FFFF10;}\n",
".cython.score-143 {background-color: #FFFF10;}\n",
".cython.score-144 {background-color: #FFFF10;}\n",
".cython.score-145 {background-color: #FFFF10;}\n",
".cython.score-146 {background-color: #FFFF10;}\n",
".cython.score-147 {background-color: #FFFF10;}\n",
".cython.score-148 {background-color: #FFFF10;}\n",
".cython.score-149 {background-color: #FFFF10;}\n",
".cython.score-150 {background-color: #FFFF0f;}\n",
".cython.score-151 {background-color: #FFFF0f;}\n",
".cython.score-152 {background-color: #FFFF0f;}\n",
".cython.score-153 {background-color: #FFFF0f;}\n",
".cython.score-154 {background-color: #FFFF0f;}\n",
".cython.score-155 {background-color: #FFFF0f;}\n",
".cython.score-156 {background-color: #FFFF0f;}\n",
".cython.score-157 {background-color: #FFFF0f;}\n",
".cython.score-158 {background-color: #FFFF0f;}\n",
".cython.score-159 {background-color: #FFFF0f;}\n",
".cython.score-160 {background-color: #FFFF0f;}\n",
".cython.score-161 {background-color: #FFFF0e;}\n",
".cython.score-162 {background-color: #FFFF0e;}\n",
".cython.score-163 {background-color: #FFFF0e;}\n",
".cython.score-164 {background-color: #FFFF0e;}\n",
".cython.score-165 {background-color: #FFFF0e;}\n",
".cython.score-166 {background-color: #FFFF0e;}\n",
".cython.score-167 {background-color: #FFFF0e;}\n",
".cython.score-168 {background-color: #FFFF0e;}\n",
".cython.score-169 {background-color: #FFFF0e;}\n",
".cython.score-170 {background-color: #FFFF0e;}\n",
".cython.score-171 {background-color: #FFFF0e;}\n",
".cython.score-172 {background-color: #FFFF0e;}\n",
".cython.score-173 {background-color: #FFFF0d;}\n",
".cython.score-174 {background-color: #FFFF0d;}\n",
".cython.score-175 {background-color: #FFFF0d;}\n",
".cython.score-176 {background-color: #FFFF0d;}\n",
".cython.score-177 {background-color: #FFFF0d;}\n",
".cython.score-178 {background-color: #FFFF0d;}\n",
".cython.score-179 {background-color: #FFFF0d;}\n",
".cython.score-180 {background-color: #FFFF0d;}\n",
".cython.score-181 {background-color: #FFFF0d;}\n",
".cython.score-182 {background-color: #FFFF0d;}\n",
".cython.score-183 {background-color: #FFFF0d;}\n",
".cython.score-184 {background-color: #FFFF0d;}\n",
".cython.score-185 {background-color: #FFFF0d;}\n",
".cython.score-186 {background-color: #FFFF0d;}\n",
".cython.score-187 {background-color: #FFFF0c;}\n",
".cython.score-188 {background-color: #FFFF0c;}\n",
".cython.score-189 {background-color: #FFFF0c;}\n",
".cython.score-190 {background-color: #FFFF0c;}\n",
".cython.score-191 {background-color: #FFFF0c;}\n",
".cython.score-192 {background-color: #FFFF0c;}\n",
".cython.score-193 {background-color: #FFFF0c;}\n",
".cython.score-194 {background-color: #FFFF0c;}\n",
".cython.score-195 {background-color: #FFFF0c;}\n",
".cython.score-196 {background-color: #FFFF0c;}\n",
".cython.score-197 {background-color: #FFFF0c;}\n",
".cython.score-198 {background-color: #FFFF0c;}\n",
".cython.score-199 {background-color: #FFFF0c;}\n",
".cython.score-200 {background-color: #FFFF0c;}\n",
".cython.score-201 {background-color: #FFFF0c;}\n",
".cython.score-202 {background-color: #FFFF0c;}\n",
".cython.score-203 {background-color: #FFFF0b;}\n",
".cython.score-204 {background-color: #FFFF0b;}\n",
".cython.score-205 {background-color: #FFFF0b;}\n",
".cython.score-206 {background-color: #FFFF0b;}\n",
".cython.score-207 {background-color: #FFFF0b;}\n",
".cython.score-208 {background-color: #FFFF0b;}\n",
".cython.score-209 {background-color: #FFFF0b;}\n",
".cython.score-210 {background-color: #FFFF0b;}\n",
".cython.score-211 {background-color: #FFFF0b;}\n",
".cython.score-212 {background-color: #FFFF0b;}\n",
".cython.score-213 {background-color: #FFFF0b;}\n",
".cython.score-214 {background-color: #FFFF0b;}\n",
".cython.score-215 {background-color: #FFFF0b;}\n",
".cython.score-216 {background-color: #FFFF0b;}\n",
".cython.score-217 {background-color: #FFFF0b;}\n",
".cython.score-218 {background-color: #FFFF0b;}\n",
".cython.score-219 {background-color: #FFFF0b;}\n",
".cython.score-220 {background-color: #FFFF0b;}\n",
".cython.score-221 {background-color: #FFFF0b;}\n",
".cython.score-222 {background-color: #FFFF0a;}\n",
".cython.score-223 {background-color: #FFFF0a;}\n",
".cython.score-224 {background-color: #FFFF0a;}\n",
".cython.score-225 {background-color: #FFFF0a;}\n",
".cython.score-226 {background-color: #FFFF0a;}\n",
".cython.score-227 {background-color: #FFFF0a;}\n",
".cython.score-228 {background-color: #FFFF0a;}\n",
".cython.score-229 {background-color: #FFFF0a;}\n",
".cython.score-230 {background-color: #FFFF0a;}\n",
".cython.score-231 {background-color: #FFFF0a;}\n",
".cython.score-232 {background-color: #FFFF0a;}\n",
".cython.score-233 {background-color: #FFFF0a;}\n",
".cython.score-234 {background-color: #FFFF0a;}\n",
".cython.score-235 {background-color: #FFFF0a;}\n",
".cython.score-236 {background-color: #FFFF0a;}\n",
".cython.score-237 {background-color: #FFFF0a;}\n",
".cython.score-238 {background-color: #FFFF0a;}\n",
".cython.score-239 {background-color: #FFFF0a;}\n",
".cython.score-240 {background-color: #FFFF0a;}\n",
".cython.score-241 {background-color: #FFFF0a;}\n",
".cython.score-242 {background-color: #FFFF0a;}\n",
".cython.score-243 {background-color: #FFFF0a;}\n",
".cython.score-244 {background-color: #FFFF0a;}\n",
".cython.score-245 {background-color: #FFFF0a;}\n",
".cython.score-246 {background-color: #FFFF09;}\n",
".cython.score-247 {background-color: #FFFF09;}\n",
".cython.score-248 {background-color: #FFFF09;}\n",
".cython.score-249 {background-color: #FFFF09;}\n",
".cython.score-250 {background-color: #FFFF09;}\n",
".cython.score-251 {background-color: #FFFF09;}\n",
".cython.score-252 {background-color: #FFFF09;}\n",
".cython.score-253 {background-color: #FFFF09;}\n",
".cython.score-254 {background-color: #FFFF09;}\n",
".cython .hll { background-color: #ffffcc }\n",
".cython { background: #f8f8f8; }\n",
".cython .c { color: #408080; font-style: italic } /* Comment */\n",
".cython .err { border: 1px solid #FF0000 } /* Error */\n",
".cython .k { color: #008000; font-weight: bold } /* Keyword */\n",
".cython .o { color: #666666 } /* Operator */\n",
".cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
".cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
".cython .cp { color: #BC7A00 } /* Comment.Preproc */\n",
".cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
".cython .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
".cython .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
".cython .gd { color: #A00000 } /* Generic.Deleted */\n",
".cython .ge { font-style: italic } /* Generic.Emph */\n",
".cython .gr { color: #FF0000 } /* Generic.Error */\n",
".cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
".cython .gi { color: #00A000 } /* Generic.Inserted */\n",
".cython .go { color: #888888 } /* Generic.Output */\n",
".cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
".cython .gs { font-weight: bold } /* Generic.Strong */\n",
".cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
".cython .gt { color: #0044DD } /* Generic.Traceback */\n",
".cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
".cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
".cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
".cython .kp { color: #008000 } /* Keyword.Pseudo */\n",
".cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
".cython .kt { color: #B00040 } /* Keyword.Type */\n",
".cython .m { color: #666666 } /* Literal.Number */\n",
".cython .s { color: #BA2121 } /* Literal.String */\n",
".cython .na { color: #7D9029 } /* Name.Attribute */\n",
".cython .nb { color: #008000 } /* Name.Builtin */\n",
".cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
".cython .no { color: #880000 } /* Name.Constant */\n",
".cython .nd { color: #AA22FF } /* Name.Decorator */\n",
".cython .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
".cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
".cython .nf { color: #0000FF } /* Name.Function */\n",
".cython .nl { color: #A0A000 } /* Name.Label */\n",
".cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
".cython .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
".cython .nv { color: #19177C } /* Name.Variable */\n",
".cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
".cython .w { color: #bbbbbb } /* Text.Whitespace */\n",
".cython .mb { color: #666666 } /* Literal.Number.Bin */\n",
".cython .mf { color: #666666 } /* Literal.Number.Float */\n",
".cython .mh { color: #666666 } /* Literal.Number.Hex */\n",
".cython .mi { color: #666666 } /* Literal.Number.Integer */\n",
".cython .mo { color: #666666 } /* Literal.Number.Oct */\n",
".cython .sa { color: #BA2121 } /* Literal.String.Affix */\n",
".cython .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
".cython .sc { color: #BA2121 } /* Literal.String.Char */\n",
".cython .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
".cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
".cython .s2 { color: #BA2121 } /* Literal.String.Double */\n",
".cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
".cython .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
".cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
".cython .sx { color: #008000 } /* Literal.String.Other */\n",
".cython .sr { color: #BB6688 } /* Literal.String.Regex */\n",
".cython .s1 { color: #BA2121 } /* Literal.String.Single */\n",
".cython .ss { color: #19177C } /* Literal.String.Symbol */\n",
".cython .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
".cython .fm { color: #0000FF } /* Name.Function.Magic */\n",
".cython .vc { color: #19177C } /* Name.Variable.Class */\n",
".cython .vg { color: #19177C } /* Name.Variable.Global */\n",
".cython .vi { color: #19177C } /* Name.Variable.Instance */\n",
".cython .vm { color: #19177C } /* Name.Variable.Magic */\n",
".cython .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
" </style>\n",
" <script>\n",
" function toggleDiv(id) {\n",
" theDiv = id.nextElementSibling\n",
" if (theDiv.style.display != 'block') theDiv.style.display = 'block';\n",
" else theDiv.style.display = 'none';\n",
" }\n",
" </script>\n",
"</head>\n",
"<body class=\"cython\">\n",
"<p><span style=\"border-bottom: solid 1px grey;\">Generated by Cython 0.27.3</span></p>\n",
"<p>\n",
" <span style=\"background-color: #FFFF00\">Yellow lines</span> hint at Python interaction.<br />\n",
" Click on a line that starts with a \"<code>+</code>\" to see the C code that Cython generated for it.\n",
"</p>\n",
"<div class=\"cython\"><pre class=\"cython line score-0\">&#xA0;<span class=\"\">01</span>: </pre>\n",
"<pre class=\"cython line score-16\" onclick='toggleDiv(this)'>+<span class=\"\">02</span>: <span class=\"k\">import</span> <span class=\"nn\">numpy</span> <span class=\"k\">as</span> <span class=\"nn\">np</span></pre>\n",
"<pre class='cython code score-16 '> __pyx_t_1 = <span class='pyx_c_api'>__Pyx_Import</span>(__pyx_n_s_numpy, 0, 0);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" if (<span class='py_c_api'>PyDict_SetItem</span>(__pyx_d, __pyx_n_s_np, __pyx_t_1) &lt; 0) <span class='error_goto'>__PYX_ERR(0, 2, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
"/* … */\n",
" __pyx_t_1 = <span class='pyx_c_api'>__Pyx_PyDict_NewPresized</span>(0);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" if (<span class='py_c_api'>PyDict_SetItem</span>(__pyx_d, __pyx_n_s_test, __pyx_t_1) &lt; 0) <span class='error_goto'>__PYX_ERR(0, 2, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">03</span>: <span class=\"k\">import</span> <span class=\"nn\">cython</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">04</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">05</span>: <span class=\"k\">cimport</span> <span class=\"nn\">numpy</span> <span class=\"k\">as</span> <span class=\"nn\">np</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">06</span>: <span class=\"k\">from</span> <span class=\"nn\">libc.math</span> <span class=\"k\">cimport</span> <span class=\"n\">ceil</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">07</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">08</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">09</span>: <span class=\"nd\">@cython</span><span class=\"o\">.</span><span class=\"n\">wraparound</span><span class=\"p\">(</span><span class=\"bp\">False</span><span class=\"p\">)</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">10</span>: <span class=\"nd\">@cython</span><span class=\"o\">.</span><span class=\"n\">boundscheck</span><span class=\"p\">(</span><span class=\"bp\">False</span><span class=\"p\">)</span></pre>\n",
"<pre class=\"cython line score-79\" onclick='toggleDiv(this)'>+<span class=\"\">11</span>: <span class=\"k\">def</span> <span class=\"nf\">select_by_peak_distance_cy</span><span class=\"p\">(</span><span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">intp_t</span><span class=\"p\">[:]</span> <span class=\"n\">peaks</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">,</span></pre>\n",
"<pre class='cython code score-79 '>/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_4718562d00f3793c17382a068f2a3571_1select_by_peak_distance_cy(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static char __pyx_doc_46_cython_magic_4718562d00f3793c17382a068f2a3571_select_by_peak_distance_cy[] = \"\\n Evaluate which peaks fulfill the distance condition.\\n\\n Parameters\\n ----------\\n peaks : ndarray\\n Indices of peaks in `vector`.\\n priority : ndarray\\n An array matching `peaks` used to determine priority of each peak. A peak\\n with a higher priority value is kept over one with a lower one.\\n dmin : number\\n Minimal distance that peaks must be spaced.\\n\\n Returns\\n -------\\n keep : ndarray[bool]\\n A boolean mask evaluating to true where `peaks` fulfill the distance\\n condition.\\n\\n Notes\\n -----\\n\\n .. versionadded:: 1.1.0\\n \";\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_4718562d00f3793c17382a068f2a3571_1select_by_peak_distance_cy = {\"select_by_peak_distance_cy\", (PyCFunction)__pyx_pw_46_cython_magic_4718562d00f3793c17382a068f2a3571_1select_by_peak_distance_cy, METH_VARARGS|METH_KEYWORDS, __pyx_doc_46_cython_magic_4718562d00f3793c17382a068f2a3571_select_by_peak_distance_cy};\n",
"static PyObject *__pyx_pw_46_cython_magic_4718562d00f3793c17382a068f2a3571_1select_by_peak_distance_cy(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
" __Pyx_memviewslice __pyx_v_peaks = { 0, 0, { 0 }, { 0 }, { 0 } };\n",
" __Pyx_memviewslice __pyx_v_priority = { 0, 0, { 0 }, { 0 }, { 0 } };\n",
" __pyx_t_5numpy_float64_t __pyx_v_distance;\n",
" PyObject *__pyx_r = 0;\n",
" <span class='refnanny'>__Pyx_RefNannyDeclarations</span>\n",
" <span class='refnanny'>__Pyx_RefNannySetupContext</span>(\"select_by_peak_distance_cy (wrapper)\", 0);\n",
" {\n",
" static PyObject **__pyx_pyargnames[] = {&amp;__pyx_n_s_peaks,&amp;__pyx_n_s_priority,&amp;__pyx_n_s_distance,0};\n",
" PyObject* values[3] = {0,0,0};\n",
" if (unlikely(__pyx_kwds)) {\n",
" Py_ssize_t kw_args;\n",
" const Py_ssize_t pos_args = <span class='py_macro_api'>PyTuple_GET_SIZE</span>(__pyx_args);\n",
" switch (pos_args) {\n",
" case 3: values[2] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 2);\n",
" CYTHON_FALLTHROUGH;\n",
" case 2: values[1] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 1);\n",
" CYTHON_FALLTHROUGH;\n",
" case 1: values[0] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 0);\n",
" CYTHON_FALLTHROUGH;\n",
" case 0: break;\n",
" default: goto __pyx_L5_argtuple_error;\n",
" }\n",
" kw_args = <span class='py_c_api'>PyDict_Size</span>(__pyx_kwds);\n",
" switch (pos_args) {\n",
" case 0:\n",
" if (likely((values[0] = <span class='py_c_api'>PyDict_GetItem</span>(__pyx_kwds, __pyx_n_s_peaks)) != 0)) kw_args--;\n",
" else goto __pyx_L5_argtuple_error;\n",
" CYTHON_FALLTHROUGH;\n",
" case 1:\n",
" if (likely((values[1] = <span class='py_c_api'>PyDict_GetItem</span>(__pyx_kwds, __pyx_n_s_priority)) != 0)) kw_args--;\n",
" else {\n",
" <span class='pyx_c_api'>__Pyx_RaiseArgtupleInvalid</span>(\"select_by_peak_distance_cy\", 1, 3, 3, 1); <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L3_error)</span>\n",
" }\n",
" CYTHON_FALLTHROUGH;\n",
" case 2:\n",
" if (likely((values[2] = <span class='py_c_api'>PyDict_GetItem</span>(__pyx_kwds, __pyx_n_s_distance)) != 0)) kw_args--;\n",
" else {\n",
" <span class='pyx_c_api'>__Pyx_RaiseArgtupleInvalid</span>(\"select_by_peak_distance_cy\", 1, 3, 3, 2); <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L3_error)</span>\n",
" }\n",
" }\n",
" if (unlikely(kw_args &gt; 0)) {\n",
" if (unlikely(<span class='pyx_c_api'>__Pyx_ParseOptionalKeywords</span>(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"select_by_peak_distance_cy\") &lt; 0)) <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L3_error)</span>\n",
" }\n",
" } else if (<span class='py_macro_api'>PyTuple_GET_SIZE</span>(__pyx_args) != 3) {\n",
" goto __pyx_L5_argtuple_error;\n",
" } else {\n",
" values[0] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 0);\n",
" values[1] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 1);\n",
" values[2] = <span class='py_macro_api'>PyTuple_GET_ITEM</span>(__pyx_args, 2);\n",
" }\n",
" __pyx_v_peaks = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_intp_t(values[0]);<span class='error_goto'> if (unlikely(!__pyx_v_peaks.memview)) __PYX_ERR(0, 11, __pyx_L3_error)</span>\n",
" __pyx_v_priority = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_float64_t(values[1]);<span class='error_goto'> if (unlikely(!__pyx_v_priority.memview)) __PYX_ERR(0, 12, __pyx_L3_error)</span>\n",
" __pyx_v_distance = __pyx_<span class='py_c_api'>PyFloat_AsDouble</span>(values[2]); if (unlikely((__pyx_v_distance == ((npy_float64)-1)) &amp;&amp; <span class='py_c_api'>PyErr_Occurred</span>())) <span class='error_goto'>__PYX_ERR(0, 13, __pyx_L3_error)</span>\n",
" }\n",
" goto __pyx_L4_argument_unpacking_done;\n",
" __pyx_L5_argtuple_error:;\n",
" <span class='pyx_c_api'>__Pyx_RaiseArgtupleInvalid</span>(\"select_by_peak_distance_cy\", 1, 3, 3, <span class='py_macro_api'>PyTuple_GET_SIZE</span>(__pyx_args)); <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L3_error)</span>\n",
" __pyx_L3_error:;\n",
" <span class='pyx_c_api'>__Pyx_AddTraceback</span>(\"_cython_magic_4718562d00f3793c17382a068f2a3571.select_by_peak_distance_cy\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" <span class='refnanny'>__Pyx_RefNannyFinishContext</span>();\n",
" return NULL;\n",
" __pyx_L4_argument_unpacking_done:;\n",
" if (unlikely(((PyObject *)__pyx_v_peaks.memview) == Py_None)) {\n",
" <span class='py_c_api'>PyErr_Format</span>(PyExc_TypeError, \"Argument '%.200s' must not be None\", \"peaks\"); <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L1_error)</span>\n",
" }\n",
" if (unlikely(((PyObject *)__pyx_v_priority.memview) == Py_None)) {\n",
" <span class='py_c_api'>PyErr_Format</span>(PyExc_TypeError, \"Argument '%.200s' must not be None\", \"priority\"); <span class='error_goto'>__PYX_ERR(0, 12, __pyx_L1_error)</span>\n",
" }\n",
" __pyx_r = __pyx_pf_46_cython_magic_4718562d00f3793c17382a068f2a3571_select_by_peak_distance_cy(__pyx_self, __pyx_v_peaks, __pyx_v_priority, __pyx_v_distance);\n",
"\n",
" /* function exit code */\n",
" goto __pyx_L0;\n",
" __pyx_L1_error:;\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
" <span class='refnanny'>__Pyx_RefNannyFinishContext</span>();\n",
" return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_4718562d00f3793c17382a068f2a3571_select_by_peak_distance_cy(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_peaks, __Pyx_memviewslice __pyx_v_priority, __pyx_t_5numpy_float64_t __pyx_v_distance) {\n",
" __Pyx_memviewslice __pyx_v_priority_to_position = { 0, 0, { 0 }, { 0 }, { 0 } };\n",
" __Pyx_memviewslice __pyx_v_keep = { 0, 0, { 0 }, { 0 }, { 0 } };\n",
" __pyx_t_5numpy_intp_t __pyx_v_i;\n",
" __pyx_t_5numpy_intp_t __pyx_v_j;\n",
" __pyx_t_5numpy_intp_t __pyx_v_k;\n",
" __pyx_t_5numpy_intp_t __pyx_v_peaks_size;\n",
" __pyx_t_5numpy_intp_t __pyx_v_distance_;\n",
" PyObject *__pyx_r = NULL;\n",
" <span class='refnanny'>__Pyx_RefNannyDeclarations</span>\n",
" <span class='refnanny'>__Pyx_RefNannySetupContext</span>(\"select_by_peak_distance_cy\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L1_error:;\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_1);\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_2);\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_3);\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_4);\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_5);\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_t_6, 1);\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_t_7, 1);\n",
" <span class='pyx_c_api'>__Pyx_AddTraceback</span>(\"_cython_magic_4718562d00f3793c17382a068f2a3571.select_by_peak_distance_cy\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_v_priority_to_position, 1);\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_v_keep, 1);\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_v_peaks, 1);\n",
" __PYX_XDEC_MEMVIEW(&amp;__pyx_v_priority, 1);\n",
" <span class='refnanny'>__Pyx_XGIVEREF</span>(__pyx_r);\n",
" <span class='refnanny'>__Pyx_RefNannyFinishContext</span>();\n",
" return __pyx_r;\n",
"}\n",
"/* … */\n",
" __pyx_tuple__29 = <span class='py_c_api'>PyTuple_Pack</span>(10, __pyx_n_s_peaks, __pyx_n_s_priority, __pyx_n_s_distance, __pyx_n_s_priority_to_position, __pyx_n_s_keep, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_k, __pyx_n_s_peaks_size, __pyx_n_s_distance_2);<span class='error_goto'> if (unlikely(!__pyx_tuple__29)) __PYX_ERR(0, 11, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_tuple__29);\n",
" <span class='refnanny'>__Pyx_GIVEREF</span>(__pyx_tuple__29);\n",
"/* … */\n",
" __pyx_t_1 = PyCFunction_NewEx(&amp;__pyx_mdef_46_cython_magic_4718562d00f3793c17382a068f2a3571_1select_by_peak_distance_cy, NULL, __pyx_n_s_cython_magic_4718562d00f3793c17);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" if (<span class='py_c_api'>PyDict_SetItem</span>(__pyx_d, __pyx_n_s_select_by_peak_distance_cy, __pyx_t_1) &lt; 0) <span class='error_goto'>__PYX_ERR(0, 11, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" __pyx_codeobj__30 = (PyObject*)<span class='pyx_c_api'>__Pyx_PyCode_New</span>(3, 0, 10, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__29, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_lg_cache_ipython_cython__c, __pyx_n_s_select_by_peak_distance_cy, 11, __pyx_empty_bytes);<span class='error_goto'> if (unlikely(!__pyx_codeobj__30)) __PYX_ERR(0, 11, __pyx_L1_error)</span>\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">12</span>: <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">float64_t</span><span class=\"p\">[:]</span> <span class=\"n\">priority</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">,</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">13</span>: <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">float64_t</span> <span class=\"n\">distance</span><span class=\"p\">):</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">14</span>: <span class=\"sd\">&quot;&quot;&quot;</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">15</span>: <span class=\"sd\"> Evaluate which peaks fulfill the distance condition.</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">16</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">17</span>: <span class=\"sd\"> Parameters</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">18</span>: <span class=\"sd\"> ----------</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">19</span>: <span class=\"sd\"> peaks : ndarray</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">20</span>: <span class=\"sd\"> Indices of peaks in `vector`.</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">21</span>: <span class=\"sd\"> priority : ndarray</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">22</span>: <span class=\"sd\"> An array matching `peaks` used to determine priority of each peak. A peak</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">23</span>: <span class=\"sd\"> with a higher priority value is kept over one with a lower one.</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">24</span>: <span class=\"sd\"> dmin : number</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">25</span>: <span class=\"sd\"> Minimal distance that peaks must be spaced.</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">26</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">27</span>: <span class=\"sd\"> Returns</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">28</span>: <span class=\"sd\"> -------</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">29</span>: <span class=\"sd\"> keep : ndarray[bool]</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">30</span>: <span class=\"sd\"> A boolean mask evaluating to true where `peaks` fulfill the distance</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">31</span>: <span class=\"sd\"> condition.</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">32</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">33</span>: <span class=\"sd\"> Notes</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">34</span>: <span class=\"sd\"> -----</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">35</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">36</span>: <span class=\"sd\"> .. versionadded:: 1.1.0</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">37</span>: <span class=\"sd\"> &quot;&quot;&quot;</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">38</span>: <span class=\"k\">cdef</span><span class=\"p\">:</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">39</span>: <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">intp_t</span><span class=\"p\">[::</span><span class=\"mf\">1</span><span class=\"p\">]</span> <span class=\"n\">priority_to_position</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">40</span>: <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">int8_t</span><span class=\"p\">[::</span><span class=\"mf\">1</span><span class=\"p\">]</span> <span class=\"n\">keep</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">41</span>: <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">intp_t</span> <span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">j</span><span class=\"p\">,</span> <span class=\"n\">k</span><span class=\"p\">,</span> <span class=\"n\">peaks_size</span><span class=\"p\">,</span> <span class=\"n\">distance_</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">42</span>: </pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">43</span>: <span class=\"n\">peaks_size</span> <span class=\"o\">=</span> <span class=\"n\">peaks</span><span class=\"o\">.</span><span class=\"n\">shape</span><span class=\"p\">[</span><span class=\"mf\">0</span><span class=\"p\">]</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_peaks_size = (__pyx_v_peaks.shape[0]);\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">44</span>: <span class=\"c\"># Round up because actual peak distance can only be natural number</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">45</span>: <span class=\"n\">distance_</span> <span class=\"o\">=</span> <span class=\"o\">&lt;</span><span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">intp_t</span><span class=\"o\">&gt;</span><span class=\"n\">ceil</span><span class=\"p\">(</span><span class=\"n\">distance</span><span class=\"p\">)</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_distance_ = ((__pyx_t_5numpy_intp_t)ceil(__pyx_v_distance));\n",
"</pre><pre class=\"cython line score-32\" onclick='toggleDiv(this)'>+<span class=\"\">46</span>: <span class=\"n\">keep</span> <span class=\"o\">=</span> <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">ones</span><span class=\"p\">(</span><span class=\"n\">peaks_size</span><span class=\"p\">,</span> <span class=\"n\">dtype</span><span class=\"o\">=</span><span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">int8</span><span class=\"p\">)</span> <span class=\"c\"># Prepare array of flags</span></pre>\n",
"<pre class='cython code score-32 '> __pyx_t_1 = <span class='pyx_c_api'>__Pyx_GetModuleGlobalName</span>(__pyx_n_s_np);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" __pyx_t_2 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_1, __pyx_n_s_ones);<span class='error_goto'> if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_2);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" __pyx_t_1 = <span class='pyx_c_api'>__Pyx_PyInt_From_Py_intptr_t</span>(__pyx_v_peaks_size);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" __pyx_t_3 = <span class='py_c_api'>PyTuple_New</span>(1);<span class='error_goto'> if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_3);\n",
" <span class='refnanny'>__Pyx_GIVEREF</span>(__pyx_t_1);\n",
" <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_3, 0, __pyx_t_1);\n",
" __pyx_t_1 = 0;\n",
" __pyx_t_1 = <span class='pyx_c_api'>__Pyx_PyDict_NewPresized</span>(1);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" __pyx_t_4 = <span class='pyx_c_api'>__Pyx_GetModuleGlobalName</span>(__pyx_n_s_np);<span class='error_goto'> if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_4);\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_4, __pyx_n_s_int8);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_4); __pyx_t_4 = 0;\n",
" if (<span class='py_c_api'>PyDict_SetItem</span>(__pyx_t_1, __pyx_n_s_dtype, __pyx_t_5) &lt; 0) <span class='error_goto'>__PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyObject_Call</span>(__pyx_t_2, __pyx_t_3, __pyx_t_1);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_2); __pyx_t_2 = 0;\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_3); __pyx_t_3 = 0;\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" __pyx_t_6 = __Pyx_PyObject_to_MemoryviewSlice_dc_nn___pyx_t_5numpy_int8_t(__pyx_t_5);\n",
" if (unlikely(!__pyx_t_6.memview)) <span class='error_goto'>__PYX_ERR(0, 46, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n",
" __pyx_v_keep = __pyx_t_6;\n",
" __pyx_t_6.memview = NULL;\n",
" __pyx_t_6.data = NULL;\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">47</span>: </pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">48</span>: <span class=\"c\"># Create map from `i` (index for `peaks` sorted by `priority`) to `j` (index</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">49</span>: <span class=\"c\"># for `peaks` sorted by position). This allows to iterate `peaks` and `keep`</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">50</span>: <span class=\"c\"># with `j` by order of `priority` while still maintaining the ability to</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">51</span>: <span class=\"c\"># step to neighbouring peaks with (`j` + 1) or (`j` - 1).</span></pre>\n",
"<pre class=\"cython line score-45\" onclick='toggleDiv(this)'>+<span class=\"\">52</span>: <span class=\"n\">priority_to_position</span> <span class=\"o\">=</span> <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">argsort</span><span class=\"p\">(</span><span class=\"n\">priority</span><span class=\"p\">)</span></pre>\n",
"<pre class='cython code score-45 '> __pyx_t_1 = <span class='pyx_c_api'>__Pyx_GetModuleGlobalName</span>(__pyx_n_s_np);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" __pyx_t_3 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_1, __pyx_n_s_argsort);<span class='error_goto'> if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_3);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_priority, 1, (PyObject *(*)(char *)) __pyx_memview_get_nn___pyx_t_5numpy_float64_t, (int (*)(char *, PyObject *)) __pyx_memview_set_nn___pyx_t_5numpy_float64_t, 0);;<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" __pyx_t_2 = NULL;\n",
" if (CYTHON_UNPACK_METHODS &amp;&amp; unlikely(<span class='py_c_api'>PyMethod_Check</span>(__pyx_t_3))) {\n",
" __pyx_t_2 = <span class='py_macro_api'>PyMethod_GET_SELF</span>(__pyx_t_3);\n",
" if (likely(__pyx_t_2)) {\n",
" PyObject* function = <span class='py_macro_api'>PyMethod_GET_FUNCTION</span>(__pyx_t_3);\n",
" <span class='pyx_macro_api'>__Pyx_INCREF</span>(__pyx_t_2);\n",
" <span class='pyx_macro_api'>__Pyx_INCREF</span>(function);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF_SET</span>(__pyx_t_3, function);\n",
" }\n",
" }\n",
" if (!__pyx_t_2) {\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyObject_CallOneArg</span>(__pyx_t_3, __pyx_t_1);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" } else {\n",
" #if CYTHON_FAST_PYCALL\n",
" if (<span class='py_c_api'>PyFunction_Check</span>(__pyx_t_3)) {\n",
" PyObject *__pyx_temp[2] = {__pyx_t_2, __pyx_t_1};\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyFunction_FastCall</span>(__pyx_t_3, __pyx_temp+1-1, 1+1);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_2); __pyx_t_2 = 0;\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" } else\n",
" #endif\n",
" #if CYTHON_FAST_PYCCALL\n",
" if (<span class='pyx_c_api'>__Pyx_PyFastCFunction_Check</span>(__pyx_t_3)) {\n",
" PyObject *__pyx_temp[2] = {__pyx_t_2, __pyx_t_1};\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyCFunction_FastCall</span>(__pyx_t_3, __pyx_temp+1-1, 1+1);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_2); __pyx_t_2 = 0;\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" } else\n",
" #endif\n",
" {\n",
" __pyx_t_4 = <span class='py_c_api'>PyTuple_New</span>(1+1);<span class='error_goto'> if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_4);\n",
" <span class='refnanny'>__Pyx_GIVEREF</span>(__pyx_t_2); <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_4, 0, __pyx_t_2); __pyx_t_2 = NULL;\n",
" <span class='refnanny'>__Pyx_GIVEREF</span>(__pyx_t_1);\n",
" <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_4, 0+1, __pyx_t_1);\n",
" __pyx_t_1 = 0;\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyObject_Call</span>(__pyx_t_3, __pyx_t_4, NULL);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_4); __pyx_t_4 = 0;\n",
" }\n",
" }\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_3); __pyx_t_3 = 0;\n",
" __pyx_t_7 = __Pyx_PyObject_to_MemoryviewSlice_dc_nn___pyx_t_5numpy_intp_t(__pyx_t_5);\n",
" if (unlikely(!__pyx_t_7.memview)) <span class='error_goto'>__PYX_ERR(0, 52, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n",
" __pyx_v_priority_to_position = __pyx_t_7;\n",
" __pyx_t_7.memview = NULL;\n",
" __pyx_t_7.data = NULL;\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">53</span>: </pre>\n",
"<pre class=\"cython line score-4\" onclick='toggleDiv(this)'>+<span class=\"\">54</span>: <span class=\"k\">with</span> <span class=\"k\">nogil</span><span class=\"p\">:</span></pre>\n",
"<pre class='cython code score-4 '> {\n",
" #ifdef WITH_THREAD\n",
" PyThreadState *_save;\n",
" Py_UNBLOCK_THREADS\n",
" <span class='pyx_c_api'>__Pyx_FastGIL_Remember</span>();\n",
" #endif\n",
" /*try:*/ {\n",
"/* … */\n",
" /*finally:*/ {\n",
" /*normal exit:*/{\n",
" #ifdef WITH_THREAD\n",
" <span class='pyx_c_api'>__Pyx_FastGIL_Forget</span>();\n",
" Py_BLOCK_THREADS\n",
" #endif\n",
" goto __pyx_L5;\n",
" }\n",
" __pyx_L5:;\n",
" }\n",
" }\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">55</span>: <span class=\"c\"># Highest priority first -&gt; iterate in reverse order (decreasing)</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">56</span>: <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"n\">peaks_size</span> <span class=\"o\">-</span> <span class=\"mf\">1</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mf\">1</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mf\">1</span><span class=\"p\">):</span></pre>\n",
"<pre class='cython code score-0 '> for (__pyx_t_8 = (__pyx_v_peaks_size - 1); __pyx_t_8 &gt; -1L; __pyx_t_8-=1) {\n",
" __pyx_v_i = __pyx_t_8;\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">57</span>: <span class=\"c\"># &quot;Translate&quot; `i` to `j` which points to current peak whose</span></pre>\n",
"<pre class=\"cython line score-0\">&#xA0;<span class=\"\">58</span>: <span class=\"c\"># neighbours are to be evaluated</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">59</span>: <span class=\"n\">j</span> <span class=\"o\">=</span> <span class=\"n\">priority_to_position</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_t_9 = __pyx_v_i;\n",
" __pyx_v_j = (*((__pyx_t_5numpy_intp_t *) ( /* dim=0 */ ((char *) (((__pyx_t_5numpy_intp_t *) __pyx_v_priority_to_position.data) + __pyx_t_9)) )));\n",
"</pre><pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">60</span>: <span class=\"k\">if</span> <span class=\"n\">keep</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"mf\">0</span><span class=\"p\">:</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_t_10 = __pyx_v_j;\n",
" __pyx_t_11 = (((*((__pyx_t_5numpy_int8_t *) ( /* dim=0 */ ((char *) (((__pyx_t_5numpy_int8_t *) __pyx_v_keep.data) + __pyx_t_10)) ))) == 0) != 0);\n",
" if (__pyx_t_11) {\n",
"/* … */\n",
" }\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">61</span>: <span class=\"c\"># Skip evaluation for peak already marked as &quot;don&#39;t keep&quot;</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">62</span>: <span class=\"k\">continue</span></pre>\n",
"<pre class='cython code score-0 '> goto __pyx_L6_continue;\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">63</span>: </pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">64</span>: <span class=\"n\">k</span> <span class=\"o\">=</span> <span class=\"n\">j</span> <span class=\"o\">-</span> <span class=\"mf\">1</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_k = (__pyx_v_j - 1);\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">65</span>: <span class=\"c\"># Flag &quot;earlier&quot; peaks for removal until minimal distance is exceeded</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">66</span>: <span class=\"k\">while</span> <span class=\"mf\">0</span> <span class=\"o\">&lt;=</span> <span class=\"n\">k</span> <span class=\"ow\">and</span> <span class=\"n\">peaks</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"o\">-</span> <span class=\"n\">peaks</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">&lt;</span> <span class=\"n\">distance_</span><span class=\"p\">:</span></pre>\n",
"<pre class='cython code score-0 '> while (1) {\n",
" __pyx_t_12 = ((0 &lt;= __pyx_v_k) != 0);\n",
" if (__pyx_t_12) {\n",
" } else {\n",
" __pyx_t_11 = __pyx_t_12;\n",
" goto __pyx_L11_bool_binop_done;\n",
" }\n",
" __pyx_t_13 = __pyx_v_j;\n",
" __pyx_t_14 = __pyx_v_k;\n",
" __pyx_t_12 = ((((*((__pyx_t_5numpy_intp_t *) ( /* dim=0 */ (__pyx_v_peaks.data + __pyx_t_13 * __pyx_v_peaks.strides[0]) ))) - (*((__pyx_t_5numpy_intp_t *) ( /* dim=0 */ (__pyx_v_peaks.data + __pyx_t_14 * __pyx_v_peaks.strides[0]) )))) &lt; __pyx_v_distance_) != 0);\n",
" __pyx_t_11 = __pyx_t_12;\n",
" __pyx_L11_bool_binop_done:;\n",
" if (!__pyx_t_11) break;\n",
"</pre><pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">67</span>: <span class=\"n\">keep</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mf\">0</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_t_15 = __pyx_v_k;\n",
" *((__pyx_t_5numpy_int8_t *) ( /* dim=0 */ ((char *) (((__pyx_t_5numpy_int8_t *) __pyx_v_keep.data) + __pyx_t_15)) )) = 0;\n",
"</pre><pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">68</span>: <span class=\"n\">k</span> <span class=\"o\">-=</span> <span class=\"mf\">1</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_k = (__pyx_v_k - 1);\n",
" }\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">69</span>: </pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">70</span>: <span class=\"n\">k</span> <span class=\"o\">=</span> <span class=\"n\">j</span> <span class=\"o\">+</span> <span class=\"mf\">1</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_k = (__pyx_v_j + 1);\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">71</span>: <span class=\"c\"># Flag &quot;later&quot; peaks for removal until minimal distance is exceeded</span></pre>\n",
"<pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">72</span>: <span class=\"k\">while</span> <span class=\"n\">k</span> <span class=\"o\">&lt;</span> <span class=\"n\">peaks_size</span> <span class=\"ow\">and</span> <span class=\"n\">peaks</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">-</span> <span class=\"n\">peaks</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"o\">&lt;</span> <span class=\"n\">distance_</span><span class=\"p\">:</span></pre>\n",
"<pre class='cython code score-0 '> while (1) {\n",
" __pyx_t_12 = ((__pyx_v_k &lt; __pyx_v_peaks_size) != 0);\n",
" if (__pyx_t_12) {\n",
" } else {\n",
" __pyx_t_11 = __pyx_t_12;\n",
" goto __pyx_L15_bool_binop_done;\n",
" }\n",
" __pyx_t_16 = __pyx_v_k;\n",
" __pyx_t_17 = __pyx_v_j;\n",
" __pyx_t_12 = ((((*((__pyx_t_5numpy_intp_t *) ( /* dim=0 */ (__pyx_v_peaks.data + __pyx_t_16 * __pyx_v_peaks.strides[0]) ))) - (*((__pyx_t_5numpy_intp_t *) ( /* dim=0 */ (__pyx_v_peaks.data + __pyx_t_17 * __pyx_v_peaks.strides[0]) )))) &lt; __pyx_v_distance_) != 0);\n",
" __pyx_t_11 = __pyx_t_12;\n",
" __pyx_L15_bool_binop_done:;\n",
" if (!__pyx_t_11) break;\n",
"</pre><pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">73</span>: <span class=\"n\">keep</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mf\">0</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_t_18 = __pyx_v_k;\n",
" *((__pyx_t_5numpy_int8_t *) ( /* dim=0 */ ((char *) (((__pyx_t_5numpy_int8_t *) __pyx_v_keep.data) + __pyx_t_18)) )) = 0;\n",
"</pre><pre class=\"cython line score-0\" onclick='toggleDiv(this)'>+<span class=\"\">74</span>: <span class=\"n\">k</span> <span class=\"o\">+=</span> <span class=\"mf\">1</span></pre>\n",
"<pre class='cython code score-0 '> __pyx_v_k = (__pyx_v_k + 1);\n",
" }\n",
" __pyx_L6_continue:;\n",
" }\n",
" }\n",
"</pre><pre class=\"cython line score-0\">&#xA0;<span class=\"\">75</span>: </pre>\n",
"<pre class=\"cython line score-24\" onclick='toggleDiv(this)'>+<span class=\"\">76</span>: <span class=\"k\">return</span> <span class=\"n\">keep</span><span class=\"o\">.</span><span class=\"n\">base</span><span class=\"o\">.</span><span class=\"n\">view</span><span class=\"p\">(</span><span class=\"n\">dtype</span><span class=\"o\">=</span><span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">bool</span><span class=\"p\">)</span> <span class=\"c\"># Return as boolean array</span></pre>\n",
"<pre class='cython code score-24 '> <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_r);\n",
" __pyx_t_5 = __pyx_memoryview_fromslice(__pyx_v_keep, 1, (PyObject *(*)(char *)) __pyx_memview_get_nn___pyx_t_5numpy_int8_t, (int (*)(char *, PyObject *)) __pyx_memview_set_nn___pyx_t_5numpy_int8_t, 0);;<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" __pyx_t_3 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_5, __pyx_n_s_base);<span class='error_goto'> if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_3);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n",
" __pyx_t_5 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_3, __pyx_n_s_view);<span class='error_goto'> if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_5);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_3); __pyx_t_3 = 0;\n",
" __pyx_t_3 = <span class='pyx_c_api'>__Pyx_PyDict_NewPresized</span>(1);<span class='error_goto'> if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_3);\n",
" __pyx_t_4 = <span class='pyx_c_api'>__Pyx_GetModuleGlobalName</span>(__pyx_n_s_np);<span class='error_goto'> if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_4);\n",
" __pyx_t_1 = <span class='pyx_c_api'>__Pyx_PyObject_GetAttrStr</span>(__pyx_t_4, __pyx_n_s_bool);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_4); __pyx_t_4 = 0;\n",
" if (<span class='py_c_api'>PyDict_SetItem</span>(__pyx_t_3, __pyx_n_s_dtype, __pyx_t_1) &lt; 0) <span class='error_goto'>__PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n",
" __pyx_t_1 = <span class='pyx_c_api'>__Pyx_PyObject_Call</span>(__pyx_t_5, __pyx_empty_tuple, __pyx_t_3);<span class='error_goto'> if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 76, __pyx_L1_error)</span>\n",
" <span class='refnanny'>__Pyx_GOTREF</span>(__pyx_t_1);\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n",
" <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_3); __pyx_t_3 = 0;\n",
" __pyx_r = __pyx_t_1;\n",
" __pyx_t_1 = 0;\n",
" goto __pyx_L0;\n",
"</pre></div></body></html>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%cython -a\n",
"\n",
"import numpy as np\n",
"import cython\n",
"\n",
"cimport numpy as np\n",
"from libc.math cimport ceil\n",
"\n",
"\n",
"@cython.wraparound(False)\n",
"@cython.boundscheck(False)\n",
"def select_by_peak_distance_cy(np.intp_t[:] peaks not None,\n",
" np.float64_t[:] priority not None,\n",
" np.float64_t distance):\n",
" \"\"\"\n",
" Evaluate which peaks fulfill the distance condition.\n",
"\n",
" Parameters\n",
" ----------\n",
" peaks : ndarray\n",
" Indices of peaks in `vector`.\n",
" priority : ndarray\n",
" An array matching `peaks` used to determine priority of each peak. A peak\n",
" with a higher priority value is kept over one with a lower one.\n",
" dmin : number\n",
" Minimal distance that peaks must be spaced.\n",
"\n",
" Returns\n",
" -------\n",
" keep : ndarray[bool]\n",
" A boolean mask evaluating to true where `peaks` fulfill the distance\n",
" condition.\n",
"\n",
" Notes\n",
" -----\n",
"\n",
" .. versionadded:: 1.1.0\n",
" \"\"\"\n",
" cdef:\n",
" np.intp_t[::1] priority_to_position\n",
" np.int8_t[::1] keep\n",
" np.intp_t i, j, k, peaks_size, distance_\n",
"\n",
" peaks_size = peaks.shape[0]\n",
" # Round up because actual peak distance can only be natural number\n",
" distance_ = <np.intp_t>ceil(distance)\n",
" keep = np.ones(peaks_size, dtype=np.int8) # Prepare array of flags\n",
"\n",
" # Create map from `i` (index for `peaks` sorted by `priority`) to `j` (index\n",
" # for `peaks` sorted by position). This allows to iterate `peaks` and `keep`\n",
" # with `j` by order of `priority` while still maintaining the ability to\n",
" # step to neighbouring peaks with (`j` + 1) or (`j` - 1).\n",
" priority_to_position = np.argsort(priority)\n",
"\n",
" with nogil:\n",
" # Highest priority first -> iterate in reverse order (decreasing)\n",
" for i in range(peaks_size - 1, -1, -1):\n",
" # \"Translate\" `i` to `j` which points to current peak whose\n",
" # neighbours are to be evaluated\n",
" j = priority_to_position[i]\n",
" if keep[j] == 0:\n",
" # Skip evaluation for peak already marked as \"don't keep\"\n",
" continue\n",
"\n",
" k = j - 1\n",
" # Flag \"earlier\" peaks for removal until minimal distance is exceeded\n",
" while 0 <= k and peaks[j] - peaks[k] < distance_:\n",
" keep[k] = 0\n",
" k -= 1\n",
"\n",
" k = j + 1\n",
" # Flag \"later\" peaks for removal until minimal distance is exceeded\n",
" while k < peaks_size and peaks[k] - peaks[j] < distance_:\n",
" keep[k] = 0\n",
" k += 1\n",
"\n",
" return keep.base.view(dtype=np.bool) # Return as boolean array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Perfomance evaluation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10923\n"
]
}
],
"source": [
"peaks = np.arange(1, 2 ** 15, 3)\n",
"print(peaks.size)\n",
"priority = peaks.copy().astype(np.float64)\n",
"\n",
"np.random.seed(20180313)\n",
"np.random.shuffle(priority)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"188 ms ± 7.36 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"951 µs ± 3.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit select_by_peak_distance_py(peaks, priority, 4.5)\n",
"%timeit select_by_peak_distance_cy(peaks, priority, 4.5)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25 ms ± 411 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"909 µs ± 2.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit select_by_peak_distance_py(peaks, priority, 32)\n",
"%timeit select_by_peak_distance_cy(peaks, priority, 32)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.42 ms ± 55.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"900 µs ± 2.67 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit select_by_peak_distance_py(peaks, priority, 1024)\n",
"%timeit select_by_peak_distance_cy(peaks, priority, 1024)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.69 ms ± 43.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"893 µs ± 1.86 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit select_by_peak_distance_py(peaks, priority, 2 ** 15)\n",
"%timeit select_by_peak_distance_cy(peaks, priority, 2 ** 15)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.69 ms ± 35.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"894 µs ± 2.98 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit select_by_peak_distance_py(peaks, priority, 2 ** 17)\n",
"%timeit select_by_peak_distance_cy(peaks, priority, 2 ** 17)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment