<p dir="auto">Hi</p>
<p dir="auto">a bunch of examples seem to be missing <code class="notranslate">import numpy as np</code>. At least for me they throw <code class="notranslate">NameError: name 'np' is not defined</code>.</p>
<p dir="auto">Following examples seem to be missing <code class="notranslate">import numpy as np</code> (non exhaustive):</p>
<ul dir="auto">
<li>scipy.signal.spectrogram</li>
<li>scipy.signal.periodogram</li>
<li>scipy.signal.welch</li>
</ul>
<p dir="auto"><code class="notranslate">scipy.__version__</code> gives <code class="notranslate">1.4.1</code>. However, the examples seem to be the same in the current master.</p>
<p dir="auto">Maybe I am missing something? Any help is appreciated!<br>
Thanks :)</p>
<p dir="auto">It is not really a bug but an issue of typical int32 overflow.<br>
Below is an image, which, if converted to a numpy matrix is int32 and the scipy center of mass function delivers coordinates outside the grid, which is wrong. This happens, because in scipy:<br>
<code class="notranslate">normalizer = sum(input, labels, index)</code><br>
overflows. Especially as 16bit images contain large numbers.</p>
<p dir="auto">There are many methods to avoid, like making the matrix float, etc, but can it maybe be handled inside the scipy functions?</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/44144239/185934034-d7382b2f-6f36-4416-b144-5ac5c16722d8.png"><img src="https://user-images.githubusercontent.com/44144239/185934034-d7382b2f-6f36-4416-b144-5ac5c16722d8.png" alt="nf_2" style="max-width: 100%;"></a></p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from PIL import Image
from scipy import ndimage
import numpy as np

im = Image.open('nf_2.png')

npa = np.array(im) 

print(npa.shape,npa.dtype) # is int32
print('min/max of array',npa.min(),npa.max())
print(ndimage.center_of_mass(npa))

com_x = 0.0
com_y = 0.0
sum = 0.0 # float
for x in range(npa.shape[1]):
    for y in range(npa.shape[0]):
        sum += npa[y][x] # overflow if sum is int32
        com_x += float(npa[y][x])*float(x)
        com_y += float(npa[y][x])*float(y)
com_x /= sum
com_y /= sum
print('should be:',com_y,com_x)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-v">PIL</span> <span class="pl-k">import</span> <span class="pl-v">Image</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span> <span class="pl-k">import</span> <span class="pl-s1">ndimage</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-v">Image</span>.<span class="pl-en">open</span>(<span class="pl-s">'nf_2.png'</span>)

<span class="pl-s1">npa</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">im</span>) 

<span class="pl-en">print</span>(<span class="pl-s1">npa</span>.<span class="pl-s1">shape</span>,<span class="pl-s1">npa</span>.<span class="pl-s1">dtype</span>) <span class="pl-c"># is int32</span>
<span class="pl-en">print</span>(<span class="pl-s">'min/max of array'</span>,<span class="pl-s1">npa</span>.<span class="pl-en">min</span>(),<span class="pl-s1">npa</span>.<span class="pl-en">max</span>())
<span class="pl-en">print</span>(<span class="pl-s1">ndimage</span>.<span class="pl-en">center_of_mass</span>(<span class="pl-s1">npa</span>))

<span class="pl-s1">com_x</span> <span class="pl-c1">=</span> <span class="pl-c1">0.0</span>
<span class="pl-s1">com_y</span> <span class="pl-c1">=</span> <span class="pl-c1">0.0</span>
<span class="pl-s1">sum</span> <span class="pl-c1">=</span> <span class="pl-c1">0.0</span> <span class="pl-c"># float</span>
<span class="pl-k">for</span> <span class="pl-s1">x</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">npa</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>]):
    <span class="pl-k">for</span> <span class="pl-s1">y</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">npa</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">0</span>]):
        <span class="pl-s1">sum</span> <span class="pl-c1">+=</span> <span class="pl-s1">npa</span>[<span class="pl-s1">y</span>][<span class="pl-s1">x</span>] <span class="pl-c"># overflow if sum is int32</span>
        <span class="pl-s1">com_x</span> <span class="pl-c1">+=</span> <span class="pl-en">float</span>(<span class="pl-s1">npa</span>[<span class="pl-s1">y</span>][<span class="pl-s1">x</span>])<span class="pl-c1">*</span><span class="pl-en">float</span>(<span class="pl-s1">x</span>)
        <span class="pl-s1">com_y</span> <span class="pl-c1">+=</span> <span class="pl-en">float</span>(<span class="pl-s1">npa</span>[<span class="pl-s1">y</span>][<span class="pl-s1">x</span>])<span class="pl-c1">*</span><span class="pl-en">float</span>(<span class="pl-s1">y</span>)
<span class="pl-s1">com_x</span> <span class="pl-c1">/=</span> <span class="pl-s1">sum</span>
<span class="pl-s1">com_y</span> <span class="pl-c1">/=</span> <span class="pl-s1">sum</span>
<span class="pl-en">print</span>(<span class="pl-s">'should be:'</span>,<span class="pl-s1">com_y</span>,<span class="pl-s1">com_x</span>)</pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="(1216, 1936) int32
min/max of array 96 65520
(2645.1217782736508, 4121.233298317583)
should be: 622.3249332060187 969.6136707837329"><pre class="notranslate">(1216, 1936) int32
min/max of array 96 65520
(2645.1217782736508, 4121.233298317583)
should be: 622.3249332060187 969.6136707837329</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">1.7.1 1.20.3 sys.version_info(major=3, minor=7, micro=7, releaselevel='final', serial=0)</p>