Repository: biolab/orange3

Issue:
DistMatrix: np.testing.assert_array_equal crashes on two different matrices
<!--
This is an issue template. Please fill in the relevant details in the
sections below.

Wrap code and verbatim terminal window output into triple backticks, see:
https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

If you're raising an issue about an add-on (e.g. installed via
Options > Add-ons), raise an issue on the relevant add-on's issue
tracker instead. See: https://github.com/biolab?q=orange3
-->

##### Orange version
<!-- From menu _Help→About→Version_ or code `Orange.version.full_version` -->
3.21.dev

##### Expected behavior
```
data = np.random.randint(3, 5, (3, 5))
data2 = np.random.randint(3, 5, (3, 5))
np.testing.assert_array_equal(data, data2)
```

Works.

```
dm1 = Orange.misc.DistMatrix(data)
dm2 = Orange.misc.DistMatrix(data2)
np.testing.assert_array_equal(dm1, dm2)
```

Works.

##### Actual behavior
```
dm1 = Orange.misc.DistMatrix(data)
dm2 = Orange.misc.DistMatrix(data2)
np.testing.assert_array_equal(dm1, dm2)
```
Fails with:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/miniconda3/envs/o3/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 896, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "/miniconda3/envs/o3/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 805, in assert_array_compare
    + array2string(max_abs_error))
  File "/miniconda3/envs/o3/lib/python3.7/site-packages/numpy/core/arrayprint.py", line 687, in array2string
    if a.size == 0:
AttributeError: 'int' object has no attribute 'size'
```

##### Steps to reproduce the behavior
See above.

##### Additional info (worksheets, data, screenshots, ...)
```
err1 = abs(data - data2)
type(err1.max())

err2 = abs(dm1 - dm2)
type(err2.max())
```




Discussion:
Works for me. Can you print out `dm1` and `dm2`?
```
dm1
DistMatrix([[3, 3, 3, 3, 4],
            [4, 4, 4, 4, 4],
            [3, 3, 3, 4, 4]])
dm2
DistMatrix([[3, 3, 3, 4, 4],
            [4, 4, 3, 4, 3],
            [4, 4, 4, 4, 3]])
```
Tested on latest master.
Also using latest numpy (1.16.2).
Strange that it works for @janezd , I get the same problem as @ajdapretnar .
```
>>> type(dm1.max())
<class 'int'>
```
as opposed to
```
>>> type(data.max())
<class 'numpy.int64'>
```

EDIT: Also, just to be clear, the elements themselves are numpy ints:
```
>>> type(dm1[0, 0])
<class 'numpy.int64'>
```

Resolving patch:
diff --git a/Orange/misc/distmatrix.py b/Orange/misc/distmatrix.py
--- a/Orange/misc/distmatrix.py
+++ b/Orange/misc/distmatrix.py
@@ -51,7 +51,7 @@ def __array_finalize__(self, obj):
 
     def __array_wrap__(self, out_arr, context=None):
         if out_arr.ndim == 0:  # a single scalar
-            return out_arr.item()
+            return out_arr[()]
         return np.ndarray.__array_wrap__(self, out_arr, context)
 
     """


Test patch:
diff --git a/Orange/tests/test_distances.py b/Orange/tests/test_distances.py
--- a/Orange/tests/test_distances.py
+++ b/Orange/tests/test_distances.py
@@ -186,6 +186,17 @@ def test_save(self):
                              ["danny", "eve", "frank"])
             self.assertEqual(m.axis, 0)
 
+    def test_numpy_type(self):
+        """GH-3658"""
+        data1 = np.array([1, 2], dtype=np.int64)
+        data2 = np.array([2, 3], dtype=np.int64)
+        dm1, dm2 = DistMatrix(data1), DistMatrix(data2)
+
+        self.assertIsInstance(dm1.max(), np.int64)
+        self.assertNotIsInstance(dm1.max(), int)
+        with self.assertRaises(AssertionError):
+            np.testing.assert_array_equal(dm1, dm2)
+
 
 # noinspection PyTypeChecker
 class TestEuclidean(TestCase):