Skip to content

Commit f021c05

Browse files
committed
Transpose: Remove redundant instance
1 parent 198580f commit f021c05

File tree

4 files changed

+553
-92
lines changed

4 files changed

+553
-92
lines changed

Orange/data/table.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ def _compute_contingency(self, col_vars=None, row_var=None):
17051705
@classmethod
17061706
def transpose(cls, table, feature_names_column="",
17071707
meta_attr_name="Feature name", feature_name="Feature",
1708-
progress_callback=None):
1708+
remove_redundant_inst=False, progress_callback=None):
17091709
"""
17101710
Transpose the table.
17111711
@@ -1715,21 +1715,37 @@ def transpose(cls, table, feature_names_column="",
17151715
:param meta_attr_name: str - name of new meta attribute into which
17161716
feature names are mapped
17171717
:param feature_name: str - default feature name prefix
1718+
:param remove_redundant_inst: bool - remove instance that
1719+
represents feature_names_column
17181720
:param progress_callback: callable - to report the progress
17191721
:return: Table - transposed table
17201722
"""
17211723
if progress_callback is None:
17221724
progress_callback = dummy_callback
17231725
progress_callback(0, "Transposing...")
17241726

1727+
if isinstance(feature_names_column, Variable):
1728+
feature_names_column = feature_names_column.name
1729+
17251730
self = cls()
17261731
n_cols, self.n_rows = table.X.shape
17271732
old_domain = table.attributes.get("old_domain")
1733+
table_domain_attributes = list(table.domain.attributes)
1734+
attr_index = None
1735+
if remove_redundant_inst:
1736+
attr_names = [a.name for a in table_domain_attributes]
1737+
if feature_names_column and feature_names_column in attr_names:
1738+
attr_index = attr_names.index(feature_names_column)
1739+
self.n_rows = self.n_rows - 1
1740+
table_domain_attributes.remove(
1741+
table_domain_attributes[attr_index])
17281742

17291743
# attributes
17301744
# - classes and metas to attributes of attributes
17311745
# - arbitrary meta column to feature names
17321746
self.X = table.X.T
1747+
if attr_index is not None:
1748+
self.X = np.delete(self.X, attr_index, 0)
17331749
if feature_names_column:
17341750
names = [str(row[feature_names_column]) for row in table]
17351751
progress_callback(0.1)
@@ -1772,7 +1788,7 @@ def set_attributes_of_attributes(_vars, _table):
17721788

17731789
def get_table_from_attributes_of_attributes(_vars, _dtype=float):
17741790
T = np.empty((self.n_rows, len(_vars)), dtype=_dtype)
1775-
for i, _attr in enumerate(table.domain.attributes):
1791+
for i, _attr in enumerate(table_domain_attributes):
17761792
for j, _var in enumerate(_vars):
17771793
val = str(_attr.attributes.get(_var.name, ""))
17781794
if not _var.is_string:
@@ -1792,13 +1808,13 @@ def get_table_from_attributes_of_attributes(_vars, _dtype=float):
17921808
# - feature names and attributes of attributes to metas
17931809
self.metas, metas = np.empty((self.n_rows, 0), dtype=object), []
17941810
if meta_attr_name not in [m.name for m in table.domain.metas] and \
1795-
table.domain.attributes:
1796-
self.metas = np.array([[a.name] for a in table.domain.attributes],
1811+
table_domain_attributes:
1812+
self.metas = np.array([[a.name] for a in table_domain_attributes],
17971813
dtype=object)
17981814
metas.append(StringVariable(meta_attr_name))
17991815

18001816
names = chain.from_iterable(list(attr.attributes)
1801-
for attr in table.domain.attributes)
1817+
for attr in table_domain_attributes)
18021818
names = sorted(set(names) - {var.name for var in class_vars})
18031819
progress_callback(0.6)
18041820

0 commit comments

Comments
 (0)