Skip to content

Commit

Permalink
Merge pull request #2417 from jerneju/index-feature
Browse files Browse the repository at this point in the history
[FIX] Feature Constructor: no fail when no values
  • Loading branch information
janezd authored Jun 21, 2017
2 parents a73dec0 + fe02689 commit 5afe1c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
14 changes: 8 additions & 6 deletions Orange/widgets/data/owfeatureconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def setEditorData(self, data, domain):
def editorData(self):
values = self.valuesedit.text()
values = re.split(r"(?<!\\),", values)
values = tuple(v.replace(r"\,", ",").strip() for v in values)
values = tuple(filter(None, [v.replace(r"\,", ",").strip() for v in values]))
return DiscreteDescriptor(
name=self.nameedit.text(),
values=values,
Expand Down Expand Up @@ -292,7 +292,7 @@ def data(self, index, role=Qt.DisplayRole):
return super().data(index, role)


class FeatureConstructorSettingsHandler(DomainContextHandler):
class FeatureConstructorHandler(DomainContextHandler):
"""Context handler that filters descriptors"""

def is_valid_item(self, setting, descriptor, attrs, metas):
Expand Down Expand Up @@ -326,7 +326,7 @@ class OWFeatureConstructor(OWWidget):

want_main_area = False

settingsHandler = FeatureConstructorSettingsHandler()
settingsHandler = FeatureConstructorHandler()
descriptors = ContextSetting([])
currentIndex = ContextSetting(-1)

Expand Down Expand Up @@ -850,7 +850,7 @@ def make_arg(name):
if sys.version_info >= (3, 0):
return ast.arg(arg=name, annotation=None)
else:
return ast.Name(id=arg, ctx=ast.Param(), lineno=1, col_offset=0)
return ast.Name(id=name, ctx=ast.Param(), lineno=1, col_offset=0)

lambda_ = ast.Lambda(
args=ast.arguments(
Expand Down Expand Up @@ -902,7 +902,7 @@ def make_arg(name):
"weibullvariate": random.weibullvariate,
"triangular": random.triangular,
"uniform": random.uniform}
)
)


class FeatureFunc:
Expand Down Expand Up @@ -930,8 +930,10 @@ def unique(seq):
return unique_el


def main(argv=sys.argv):
def main(argv=None):
from AnyQt.QtWidgets import QApplication
if argv is None:
argv = sys.argv
app = QApplication(list(argv))
argv = app.arguments()
if len(argv) > 1:
Expand Down
34 changes: 26 additions & 8 deletions Orange/widgets/data/tests/test_owfeatureconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from Orange.widgets.data.owfeatureconstructor import (DiscreteDescriptor,
ContinuousDescriptor,
StringDescriptor,
construct_variables, OWFeatureConstructor)
construct_variables, OWFeatureConstructor,
DiscreteFeatureEditor)

from Orange.widgets.data.owfeatureconstructor import (
freevars, make_lambda, validate_exp
)
from Orange.widgets.data.owfeatureconstructor import freevars, validate_exp

import dill as pickle # Import dill after Orange because patched

Expand Down Expand Up @@ -99,11 +98,11 @@ class PicklingTest(unittest.TestCase):
def test_lambdas_pickle(self):
NONLOCAL_CONST = 5

lambda_func = lambda x, LOCAL_CONST=7: \
x * LOCAL_CONST * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST
lambda_func = lambda x, local_const=7: \
x * local_const * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST

def nested_func(x, LOCAL_CONST=7):
return x * LOCAL_CONST * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST
def nested_func(x, local_const=7):
return x * local_const * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST

self.assertEqual(lambda_func(11),
pickle.loads(pickle.dumps(lambda_func))(11))
Expand Down Expand Up @@ -240,3 +239,22 @@ def test_error_invalid_expression(self):
)
self.widget.apply()
self.assertTrue(self.widget.Error.invalid_expressions.is_shown())

def test_discrete_no_values(self):
"""
Should not fail when there are no values set.
GH-2417
"""
data = Table("iris")
self.widget.setData(data)
discreteFeatureEditor = DiscreteFeatureEditor()

discreteFeatureEditor.valuesedit.setText("")
discreteFeatureEditor.nameedit.setText("D1")
discreteFeatureEditor.expressionedit.setText("iris")
self.widget.addFeature(
discreteFeatureEditor.editorData()
)
self.assertFalse(self.widget.Error.more_values_needed.is_shown())
self.widget.apply()
self.assertTrue(self.widget.Error.more_values_needed.is_shown())

0 comments on commit 5afe1c2

Please sign in to comment.