Repository: biolab/orange3

Issue:
Create Class Misclassifying
<!-- 
Thanks for taking the time to report a bug!
If you're raising an issue about an add-on (i.e., installed via Options > Add-ons), raise an issue in the relevant add-on's issue tracker instead. See: https://github.com/biolab?q=orange3
To fix the bug, we need to be able to reproduce it. Please answer the following questions to the best of your ability.
-->


- [ ] **What's wrong?**
<!-- Be specific, clear, and concise. Include screenshots if relevant. -->
<!-- If you're getting an error message, copy it, and enclose it with three backticks (```). -->
![image](https://user-images.githubusercontent.com/47462814/121054912-ce91ad80-c78a-11eb-831c-bb915a8a8548.png)


When I attempt to create a class that includes information on the specific run this spectrum was taken from it seems to be pulling almost randomly. 



- [ ] **How can we reproduce the problem?** 
<!-- Upload a zip with the .ows file and data. -->
<!-- Describe the steps (open this widget, click there, then add this...) -->
As you can see I already have a variety of other classes being pulled up but for some reason, this one is being particularly nasty. Of particular weirdness is that while I was messing with this the bug fixed itself. So probably not a very high priority but still weird as heck.




- [ ] **What's your environment?**
<!-- To find your Orange version, see "Help → About → Version" or `Orange.version.full_version` in code -->
- Operating system: Windows 10
- Orange version: 3.28.0
- How you installed Orange: git pull



Discussion:
[Data.zip](https://github.com/biolab/orange3/files/6612420/Data.zip)

[bug.zip](https://github.com/biolab/orange3/files/6612427/bug.zip)

It popped up in another place, gonna try to cluelessly fix it. Will report back if I find a way to consistently fix
Thanks! Your screenshots clearly show that the classes got assigned wrongly. Can you reproduce the bug consistently?
@bluewin4, I tried your workflow and managed to reproduce the bug.

Resolving patch:
diff --git a/Orange/widgets/data/owcreateclass.py b/Orange/widgets/data/owcreateclass.py
--- a/Orange/widgets/data/owcreateclass.py
+++ b/Orange/widgets/data/owcreateclass.py
@@ -152,9 +152,14 @@ def unique_in_order_mapping(a):
     - unique elements of the input list (in the order of appearance)
     - indices of the input list onto the returned uniques
     """
-    u, idx, inv = np.unique(a, return_index=True, return_inverse=True)
-    unique_in_order = u[np.argsort(idx)]
-    mapping = np.argsort(idx)[inv]
+    first_position = {}
+    unique_in_order = []
+    mapping = []
+    for e in a:
+        if e not in first_position:
+            first_position[e] = len(unique_in_order)
+            unique_in_order.append(e)
+        mapping.append(first_position[e])
     return unique_in_order, mapping
 
 


Test patch:
diff --git a/Orange/widgets/data/tests/test_owcreateclass.py b/Orange/widgets/data/tests/test_owcreateclass.py
--- a/Orange/widgets/data/tests/test_owcreateclass.py
+++ b/Orange/widgets/data/tests/test_owcreateclass.py
@@ -89,6 +89,15 @@ def test_unique_in_order_mapping():
         u, m = unique_in_order_mapping([2, 1, 2, 3])
         np.testing.assert_equal(u, [2, 1, 3])
         np.testing.assert_equal(m, [0, 1, 0, 2])
+        u, m = unique_in_order_mapping([2, 3, 1])
+        np.testing.assert_equal(u, [2, 3, 1])
+        np.testing.assert_equal(m, [0, 1, 2])
+        u, m = unique_in_order_mapping([2, 3, 1, 1])
+        np.testing.assert_equal(u, [2, 3, 1])
+        np.testing.assert_equal(m, [0, 1, 2, 2])
+        u, m = unique_in_order_mapping([2, 3, 1, 2])
+        np.testing.assert_equal(u, [2, 3, 1])
+        np.testing.assert_equal(m, [0, 1, 2, 0])
 
     def test_value_from_string_substring(self):
         trans = ValueFromStringSubstring(StringVariable("x"), self.patterns)