-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[FIX] Edit Domain (and perhaps other widgets) could cause missing data later in the workflow #4922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import unittest | ||
|
|
||
| from Orange.data import DiscreteVariable, ContinuousVariable | ||
| from Orange.preprocess.impute import ReplaceUnknownsRandom, ReplaceUnknowns | ||
| from Orange.statistics.distribution import Discrete | ||
|
|
||
|
|
||
| class TestReplaceUnknowns(unittest.TestCase): | ||
| def test_equality(self): | ||
| v1 = ContinuousVariable("x") | ||
| v2 = ContinuousVariable("x") | ||
| v3 = ContinuousVariable("y") | ||
|
|
||
| t1 = ReplaceUnknowns(v1, 0) | ||
| t1a = ReplaceUnknowns(v2, 0) | ||
| t2 = ReplaceUnknowns(v3, 0) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| t1 = ReplaceUnknowns(v1, 0) | ||
| t1a = ReplaceUnknowns(v1, 1) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
|
|
||
| class TestReplaceUnknownsRandom(unittest.TestCase): | ||
| def test_equality(self): | ||
| v1 = DiscreteVariable("x", tuple("abc")) | ||
| v2 = DiscreteVariable("x", tuple("abc")) | ||
| v3 = DiscreteVariable("y", tuple("abc")) | ||
|
|
||
| d1 = Discrete([1, 2, 3], v1) | ||
| d2 = Discrete([1, 2, 3], v2) | ||
| d3 = Discrete([1, 2, 3], v3) | ||
|
|
||
| t1 = ReplaceUnknownsRandom(v1, d1) | ||
| t1a = ReplaceUnknownsRandom(v2, d2) | ||
| t2 = ReplaceUnknownsRandom(v3, d3) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| d1[1] += 1 | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from Orange.data import DiscreteVariable | ||
| from Orange.preprocess.transformation import \ | ||
| Transformation, _Indicator, Normalizer, Lookup | ||
|
|
||
|
|
||
| class TestTransformEquality(unittest.TestCase): | ||
| def setUp(self): | ||
| self.disc1 = DiscreteVariable("d1", values=tuple("abc")) | ||
| self.disc1a = DiscreteVariable("d1", values=tuple("abc")) | ||
| self.disc2 = DiscreteVariable("d2", values=tuple("abc")) | ||
| assert self.disc1 == self.disc1a | ||
|
|
||
| def test_transformation(self): | ||
| t1 = Transformation(self.disc1) | ||
| t1a = Transformation(self.disc1a) | ||
| t2 = Transformation(self.disc2) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| def test_indicator(self): | ||
| t1 = _Indicator(self.disc1, 0) | ||
| t1a = _Indicator(self.disc1a, 0) | ||
| t2 = _Indicator(self.disc2, 0) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| t1 = _Indicator(self.disc1, 0) | ||
| t1a = _Indicator(self.disc1a, 1) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
| def test_normalizer(self): | ||
| t1 = Normalizer(self.disc1, 0, 1) | ||
| t1a = Normalizer(self.disc1a, 0, 1) | ||
| t2 = Normalizer(self.disc2, 0, 1) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| t1 = Normalizer(self.disc1, 0, 1) | ||
| t1a = Normalizer(self.disc1a, 1, 1) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
| t1 = Normalizer(self.disc1, 0, 1) | ||
| t1a = Normalizer(self.disc1a, 0, 2) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
| def test_lookup(self): | ||
| t1 = Lookup(self.disc1, np.array([0, 2, 1]), 1) | ||
| t1a = Lookup(self.disc1a, np.array([0, 2, 1]), 1) | ||
| t2 = Lookup(self.disc2, np.array([0, 2, 1]), 1) | ||
| self.assertEqual(t1, t1) | ||
| self.assertEqual(t1, t1a) | ||
| self.assertNotEqual(t1, t2) | ||
|
|
||
| self.assertEqual(hash(t1), hash(t1a)) | ||
| self.assertNotEqual(hash(t1), hash(t2)) | ||
|
|
||
| t1 = Lookup(self.disc1, np.array([0, 2, 1]), 1) | ||
| t1a = Lookup(self.disc1a, np.array([1, 2, 0]), 1) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
| t1 = Lookup(self.disc1, np.array([0, 2, 1]), 1) | ||
| t1a = Lookup(self.disc1a, np.array([0, 2, 1]), 2) | ||
| self.assertNotEqual(t1, t1a) | ||
| self.assertNotEqual(hash(t1), hash(t1a)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,25 +48,21 @@ def transform(self, c): | |
| raise NotImplementedError( | ||
| "ColumnTransformations must implement method 'transform'.") | ||
|
|
||
|
|
||
| class Identity(Transformation): | ||
| """Return an untransformed value of `c`. | ||
| """ | ||
| def transform(self, c): | ||
| return c | ||
|
|
||
| def __eq__(self, other): | ||
| return type(other) is type(self) and self.variable == other.variable | ||
|
|
||
| def __hash__(self): | ||
| return hash((type(self), self.variable)) | ||
|
|
||
|
|
||
| class Indicator(Transformation): | ||
| """ | ||
| Return an indicator value that equals 1 if the variable has the specified | ||
| value and 0 otherwise. | ||
| class Identity(Transformation): | ||
| """Return an untransformed value of `c`. | ||
| """ | ||
| def transform(self, c): | ||
| return c | ||
|
|
||
|
|
||
| class _Indicator(Transformation): | ||
| def __init__(self, variable, value): | ||
| """ | ||
| :param variable: The variable whose transformed value is returned. | ||
|
|
@@ -78,26 +74,27 @@ def __init__(self, variable, value): | |
| super().__init__(variable) | ||
| self.value = value | ||
|
|
||
| def __eq__(self, other): | ||
| return super().__eq__(other) and self.value == other.value | ||
|
|
||
| def __hash__(self): | ||
| return hash((type(self), self.variable, self.value)) | ||
|
|
||
|
|
||
| class Indicator(_Indicator): | ||
| """ | ||
| Return an indicator value that equals 1 if the variable has the specified | ||
| value and 0 otherwise. | ||
| """ | ||
| def transform(self, c): | ||
| return c == self.value | ||
|
|
||
|
|
||
| class Indicator1(Transformation): | ||
| class Indicator1(_Indicator): | ||
| """ | ||
| Return an indicator value that equals 1 if the variable has the specified | ||
| value and -1 otherwise. | ||
| """ | ||
| def __init__(self, variable, value): | ||
| """ | ||
| :param variable: The variable whose transformed value is returned. | ||
| :type variable: int or str or :obj:`~Orange.data.Variable` | ||
|
|
||
| :param value: The value to which the indicator refers | ||
| :type value: int or float | ||
| """ | ||
| super().__init__(variable) | ||
| self.value = value | ||
|
|
||
| def transform(self, c): | ||
| return (c == self.value) * 2 - 1 | ||
|
|
||
|
|
@@ -129,6 +126,13 @@ def transform(self, c): | |
| else: | ||
| return (c - self.offset) * self.factor | ||
|
|
||
| def __eq__(self, other): | ||
| return super().__eq__(other) \ | ||
| and self.offset == other.offset and self.factor == other.factor | ||
|
|
||
| def __hash__(self): | ||
| return hash((type(self), self.variable, self.offset, self.factor)) | ||
|
|
||
|
|
||
| class Lookup(Transformation): | ||
| """ | ||
|
|
@@ -139,7 +143,7 @@ def __init__(self, variable, lookup_table, unknown=np.nan): | |
| :param variable: The variable whose transformed value is returned. | ||
| :type variable: int or str or :obj:`~Orange.data.DiscreteVariable` | ||
| :param lookup_table: transformations for each value of `self.variable` | ||
| :type lookup_table: np.array or list or tuple | ||
| :type lookup_table: np.array | ||
| :param unknown: The value to be used as unknown value. | ||
| :type unknown: float or int | ||
| """ | ||
|
|
@@ -156,3 +160,13 @@ def transform(self, column): | |
| column[mask] = 0 | ||
| values = self.lookup_table[column] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noticed this in passing - the docs say |
||
| return np.where(mask, self.unknown, values) | ||
|
|
||
| def __eq__(self, other): | ||
| return super().__eq__(other) \ | ||
| and np.allclose(self.lookup_table, other.lookup_table, | ||
| equal_nan=True) \ | ||
| and np.allclose(self.unknown, other.unknown, equal_nan=True) | ||
|
|
||
| def __hash__(self): | ||
| return hash((type(self), self.variable, | ||
| tuple(self.lookup_table), self.unknown)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about the default
__eq__forTransformationalong these lines:Seems that that is usually what we want and it would make a lot of trivial overloads of eq unnecessary?
__hash__is a bit more tricky since class attributes can often be unhashable objects. We could leave it as it is or try hashing a sorted tuple ofvars(self). The latter should work for simple cases likeIndicatorand at least fails more noticably if it should have been overloaded in a subclass but was not!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant here. It looks like a good idea, but making the parent class to smart might shoot a derived class in the foot. Somebody could add an attribute without realizing it's used in comparisons. Maybe it's better to be explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You convinced me at first, but then again, you can make an equivalent claim for the other side:
Somebody could add an attribute without realizing it's NOT used in comparisons.
Maybe I preferred comparing everything (vs the minimum) because it behaves closer to how it was before - if some class doesn't overload eq it might erroneously return False (like up to now), but a True should be correct.
In the end it does not really matter, if something is not working like it should it's a bug to be fixed in either case.
I don't mind merging it with either default.