Skip to content

Commit

Permalink
owcsvimport: Mirror vartypes in import
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed Nov 7, 2020
1 parent b235b4a commit 13a54b3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
53 changes: 51 additions & 2 deletions Orange/widgets/data/owcsvimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
QSettings_readArray, QSettings_writeArray
)
from Orange.widgets.utils.state_summary import format_summary_details
from Orange.widgets.utils.textimport import columntype_for_vartype

if typing.TYPE_CHECKING:
# pylint: disable=invalid-name
Expand Down Expand Up @@ -221,6 +222,42 @@ def spec_from_encodable(spec, enumtype):
r.append((range(start, stop), enum_get(enumtype, name, None)))
return r

def update_type(self, var_i, type):
ctypes = [ctype
for r, ctype in self.columntypes
for _ in r]
ctypes[var_i] = type
self.__update_types(ctypes)

def __update_types(self, types):
"""
Parameters
----------
types (List[ColumnType]): list of types denoting not-skipped features
"""
new_ctypes: [(range, ColumnType)] = []
i = 0
skip_c = 0
for r, prevtype in self.columntypes:
if prevtype == ColumnType.Skip:
new_ctypes.append([r, prevtype])
rlen = r.stop - r.start
skip_c += rlen
i += rlen
continue
for _ in r:
newtype = types[i - skip_c]
if new_ctypes and newtype == new_ctypes[-1][1]:
oldr = new_ctypes[-1][0]
new_ctypes[-1] = \
(range(oldr.start, oldr.stop + 1),
new_ctypes[-1][1])
else:
new_ctypes.append((range(i, i + 1), newtype))
i += 1
self.columntypes = new_ctypes


class CSVImportDialog(QDialog):
"""
Expand Down Expand Up @@ -662,6 +699,7 @@ class Warning(widget.OWWidget.Warning):

def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self.data = None
self.settingsAboutToBePacked.connect(self._saveState)

self.__committimer = QTimer(self, singleShot=True)
Expand Down Expand Up @@ -1240,13 +1278,24 @@ def __handle_result(self, f):
domain = None

self.data = table
self.domain_editor.set_domain(domain)
self.domain_editor.set_domain(domain,
variable_order=list(df.columns))

self.send("Data Frame", df)
self.send('Data', table)
self._update_status_messages(table)

def __handle_domain_edit(self):
def __handle_domain_edit(self, index):
self.Warning.clear()
# set type in import dialogue
vartype = self.domain_editor.type_for_index(index)
if vartype:
t = columntype_for_vartype(vartype)
var_i = self.domain_editor.original_index(index)
opts = self.current_item().options()
opts.update_type(var_i, t)

# commit data to output
if self.data is None:
table = None
else:
Expand Down
33 changes: 25 additions & 8 deletions Orange/widgets/utils/domaineditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def setData(self, index, value, role=Qt.EditRole):
row, col = index.row(), index.column()
row_data = self.variables[row]
if role == Qt.EditRole:
end_index = index
if col == Column.name and not (value.isspace() or value == ""):
row_data[col] = value
elif col == Column.tpe:
Expand All @@ -110,12 +111,13 @@ def setData(self, index, value, role=Qt.EditRole):
if not vartype.is_primitive() and \
row_data[Column.place] < Place.meta:
row_data[Column.place] = Place.meta
end_index = index.sibling(row, Column.place)
elif col == Column.place:
row_data[col] = self.places.index(value)
else:
return False
# Settings may change background colors
self.dataChanged.emit(index.sibling(row, 0), index.sibling(row, 3))
self.dataChanged.emit(index, end_index)
return True
return False

Expand Down Expand Up @@ -205,6 +207,8 @@ class DomainEditor(QTableView):

def __init__(self, variables=None):
super().__init__()
self.variables = variables
self.ordered_variables = variables
self.setModel(VarTableModel(self, variables))

self.setSelectionMode(QTableView.NoSelection)
Expand Down Expand Up @@ -372,6 +376,16 @@ def numbers_are_round(var, col_data):
else:
return domain, [X, Y, m]

def original_index(self, index):
var = self.variables[index.row()]
return self.ordered_variables.index(var)

def type_for_index(self, index):
if index.column() != Column.tpe:
return None
var = self.variables[index.row()]
return var[Column.tpe]

@staticmethod
def _get_column(data, source_var, source_place):
""" Extract column from data and preserve sparsity. """
Expand All @@ -383,9 +397,16 @@ def _get_column(data, source_var, source_place):
col_data = data[:, source_var].X
return col_data

def set_domain(self, domain):
variables = self.parse_domain(domain)
self.model().set_variables(variables)
def set_domain(self, domain, variable_order=None):
self.variables = self.parse_domain(domain)
if variable_order:
self.ordered_variables = sorted(
self.variables,
key=lambda a: variable_order.index(a[0])
)
else:
self.ordered_variables = self.variables
self.model().set_variables(self.variables)

def reset_domain(self):
self.model().reset_variables()
Expand Down Expand Up @@ -459,7 +480,3 @@ def __init__(self, widget):
widget.contextAboutToBeOpened.connect(lambda args: self.set_domain(args[0]))
widget.contextOpened.connect(lambda: self.model().set_variables(self.variables))
widget.contextClosed.connect(lambda: self.model().set_variables([]))

def set_orig_domain(self, domain):
self.variables = self.parse_domain(domain)
self.model().set_orig_variables(self.variables)
16 changes: 16 additions & 0 deletions Orange/widgets/utils/textimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
QApplication, QAbstractItemView, QToolTip, QStyleOption
)

from Orange.data import (
DiscreteVariable, ContinuousVariable, StringVariable, \
TimeVariable
)
from Orange.widgets.utils import encodings
from Orange.widgets.utils.overlay import OverlayWidget

Expand Down Expand Up @@ -1449,6 +1453,18 @@ def icon_for_column_type(coltype):
return icon


def columntype_for_vartype(vartype):
# type: (Variable) -> ColumnType
if vartype == DiscreteVariable:
return ColumnType.Categorical
if vartype == ContinuousVariable:
return ColumnType.Numeric
if vartype == StringVariable:
return ColumnType.Text
if vartype == TimeVariable:
return ColumnType.Time


class SkipItemDelegate(PreviewItemDelegate):
def initStyleOption(self, option, index):
# type: (QStyleOptionViewItem, QModelIndex) -> None
Expand Down

0 comments on commit 13a54b3

Please sign in to comment.