python - 'the truth value of an array with more than one element is ambiguous' - what truth value?
<p>first post! I've looked through a lot of other posts on this problem but can't find anything that applies to my code.</p>

<p>I'm trying to read an audio file and then find the max and min values of the array of samples, <code>x</code>.<br>
<code>wavread()</code> is a function defined in another module that I've imported.<br>
It returns <code>fs, x</code>.<br>
<code>x</code> is a one-dimensional array (<code>x.shape = (150529,)</code>.)</p>

<pre><code>def minMaxAudio(inputFile):
    (fs, x) = wavread(inputFile)
    max_val = numpy.amax(x)
    min_val = numpy.amin(x) 
    return (min_val, max_val)
</code></pre>

<p>when I type these lines individually into ipython, I get the result I want. but when I call this function from an imported .py file I get the error:</p>

<pre><code>ValueError: The truth value of an array with more than one element is ambiguous.   
Use a.any() or a.all()
</code></pre>

<p>It highlights the last line (return statement) as the location of the error.</p>

<p>Every other post on this that I've looked at includes some sort of evaluation or comparison operator in the code. Mine doesn't have one... does it?!</p>

<p>Thanks!</p>

<p>Have you noticed that if your WAV file has more than one channel, say it is stereo, min_val and max_val will be arrays themselves?</p>

<p>Such a code would trigger the error you encounter:</p>

<pre><code>min, max = minMaxAudio('acdc.wav')
# Assuming floats
if max &gt; 1:
    print('saturation')
</code></pre>

<p>Whereas the following will work:</p>

<pre><code>min, max = minMaxAudio('acdc.wav')
# Assuming floats
if np.any(max &gt; 1):
    print('saturation')
</code></pre>

python arrays numpy