Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare (assert equality of) two complex data structures containing numpy arrays in unittest
    text
    copied!<p>I use Python's <code>unittest</code> module and want to check if two complex data structures are equal. The objects can be lists of dicts with all sorts of values: numbers, strings, Python containers (lists/tuples/dicts) and <code>numpy</code> arrays. The latter are the reason for asking the question, because I cannot just do</p> <pre><code>self.assertEqual(big_struct1, big_struct2) </code></pre> <p>because it produces a</p> <pre class="lang-none prettyprint-override"><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() </code></pre> <p>I imagine that I need to write my own equality test for this. It should work for arbitrary structures. My current idea is a recursive function that:</p> <ul> <li>tries direct comparison of the current "node" of <code>arg1</code> to the corresponding node of <code>arg2</code>;</li> <li>if no exception is raised, moves on ("terminal" nodes/leaves are processed here, too);</li> <li>if <code>ValueError</code> is caught, goes deeper until it finds a <code>numpy.array</code>;</li> <li>compares the arrays (e.g. <a href="https://stackoverflow.com/a/10580782/1258041">like this</a>).</li> </ul> <p>What seems a little problematic is keeping track of "corresponding" nodes of two structures, but perhaps <code>zip</code> is all I need here.</p> <p><strong>The question is: are there good (simpler) alternatives to this approach?</strong> Maybe <code>numpy</code> presents some tools for this? If no alternatives are suggested, I will implement this idea (unless I have a better one) and post as an answer.</p> <p>P.S. I have a vague feeling that I might have seen a question addressing this problem, but I can't find it now.</p> <p>P.P.S. An alternative approach would be a function that traverses the structure and converts all <code>numpy.array</code>s to lists, but is this any easier to implement? Seems the same to me.</p> <hr> <p><strong>Edit:</strong> Subclassing <code>numpy.ndarray</code> sounds very promising, but obviously I don't have both sides of the comparison hard-coded into a test. One of them, though, is indeed hardcoded, so I can:</p> <ul> <li>populate it with custom subclasses of <code>numpy.array</code>;</li> <li>change <code>isinstance(other, SaneEqualityArray)</code> to <code>isinstance(other, np.ndarray)</code> in <a href="https://stackoverflow.com/a/14249234/1258041"><strong>jterrace</strong>'s answer</a>;</li> <li>always use it as LHS in comparisons.</li> </ul> <p>My questions in this regard are:</p> <ol> <li>Will it work (I mean, it sounds all right to me, but maybe some tricky edge cases will not be handled correctly)? Will my custom object always end up as LHS in the recursive equality checks, as I expect?</li> <li>Again, are there better ways (given that I get at least one of the structures with real <code>numpy</code> arrays). </li> </ol> <hr> <p><strong>Edit 2</strong>: I tried it out, the (seemingly) working implementation is shown in <a href="https://stackoverflow.com/a/14276901/1258041">this answer</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload