Skip to content

Commit

Permalink
owimpute: Make default_numeric locale independant
Browse files Browse the repository at this point in the history
The default_numeric was stored as localized string and was hence not
portable.

Replace use of QLineEdit with QDoubleSpin box for editing
  • Loading branch information
ales-erjavec committed Jan 26, 2021
1 parent 0d6da92 commit 2bfca2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 28 additions & 22 deletions Orange/widgets/data/owimpute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
QVBoxLayout, QStackedWidget, QComboBox, QWidget,
QButtonGroup, QStyledItemDelegate, QListView, QDoubleSpinBox, QLabel
)
from AnyQt.QtCore import Qt, QThread, QModelIndex, QDateTime, QLocale
from AnyQt.QtCore import Qt, QThread, QModelIndex, QDateTime
from AnyQt.QtCore import pyqtSlot as Slot
from AnyQt.QtGui import QDoubleValidator

from orangewidget.utils.listview import ListViewSearch

Expand Down Expand Up @@ -127,6 +126,10 @@ def var_key(var):
return qname, var.name


DBL_MIN = np.finfo(float).min
DBL_MAX = np.finfo(float).max


class OWImpute(OWWidget):
name = "Impute"
description = "Impute missing values in the data table."
Expand Down Expand Up @@ -156,7 +159,7 @@ class Warning(OWWidget.Warning):
_variable_imputation_state = settings.ContextSetting({}) # type: VariableState

autocommit = settings.Setting(True)
default_numeric = settings.Setting("")
default_numeric_value = settings.Setting(0.0)
default_time = settings.Setting(0)

want_main_area = False
Expand Down Expand Up @@ -206,17 +209,19 @@ def set_default_time(datetime):
button.setChecked(Method.Default == self.default_method_index)
hlayout.addWidget(button)

locale = QLocale()
locale.setNumberOptions(locale.NumberOption.RejectGroupSeparator)
validator = QDoubleValidator()
validator.setLocale(locale)
self.numeric_value_widget = le = gui.lineEdit(
None, self, "default_numeric",
validator=validator, alignment=Qt.AlignRight,
callback=self._invalidate,
enabled=self.default_method_index == Method.Default
self.numeric_value_widget = QDoubleSpinBox(
minimum=DBL_MIN, maximum=DBL_MAX, singleStep=.1, decimals=5,
value=self.default_numeric_value,
alignment=Qt.AlignRight,
enabled=self.default_method_index == Method.Default,
)
hlayout.addWidget(le)
self.numeric_value_widget.editingFinished.connect(
self.__on_default_numeric_value_edited
)
self.connect_control(
"default_numeric_value", self.numeric_value_widget.setValue
)
hlayout.addWidget(self.numeric_value_widget)

hlayout.addWidget(QLabel(", time:"))

Expand Down Expand Up @@ -278,7 +283,7 @@ def set_default_time(datetime):
)
self.value_double = QDoubleSpinBox(
editingFinished=self._on_value_selected,
minimum=-1000., maximum=1000., singleStep=.1, decimals=3,
minimum=DBL_MIN, maximum=DBL_MAX, singleStep=.1, decimals=5,
)
self.value_stack = value_stack = QStackedWidget()
value_stack.addWidget(self.value_combo)
Expand Down Expand Up @@ -323,15 +328,9 @@ def create_imputer(self, method, *args):
m.method = default
return m
elif method == Method.Default and not args: # global default values
if self.default_numeric == "":
default_num = np.nan
else:
default_num, ok = QLocale().toDouble(self.default_numeric)
if not ok:
default_num = np.nan
return impute.FixedValueByType(
default_continuous=default_num,
default_time=self.default_time or np.nan
default_continuous=self.default_numeric_value,
default_time=self.default_time
)
else:
return METHODS[method](*args)
Expand All @@ -357,6 +356,13 @@ def set_default_method(self, index):
"""
self.default_method_index = index

def __on_default_numeric_value_edited(self):
val = self.numeric_value_widget.value()
if val != self.default_numeric_value:
self.default_numeric_value = val
if self.default_method_index == Method.Default:
self._invalidate()

@Inputs.data
@check_sql_input
def set_data(self, data):
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/tests/test_owimpute.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_overall_default(self):
data = Table(domain, x, np.empty((2, 0)))

widget = self.widget
widget.default_numeric = QLocale().toString(3.14)
widget.default_numeric_value = 3.14
widget.default_time = 42
widget.default_method_index = Method.Default

Expand Down

0 comments on commit 2bfca2c

Please sign in to comment.