Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejaKovacic committed Feb 10, 2020
1 parent 97a4856 commit 55ae682
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
32 changes: 20 additions & 12 deletions Orange/widgets/visualize/owvenndiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,21 @@ def extract_new_table(self, var_dict):
domain = defaultdict(lambda: [])
values = defaultdict(lambda: [])
atr_vals = {'metas': 'metas', 'attributes': 'X', 'class_vars': 'Y'}
renamed = []
for atr_type, vars_dict in var_dict.items():
for var_name, var_data in vars_dict.items():
if var_data[0]:
#columns are different, copy all, rename them
for var, table_key in var_data[1]:
idx = list(self.data.keys()).index(table_key) + 1
domain[atr_type].append(var.copy(name='{} ({})'.format(var_name, idx)))
domain[atr_type].append(var.copy(name='{} ({})'.format(var_name.name, idx)))
renamed.append(var_name.name)
values[atr_type].append(getattr(self.data[table_key].table[:, var_name], atr_vals[atr_type]).reshape(-1, 1))
else:
domain[atr_type].append(deepcopy(var_data[1][0][0]))
values[atr_type].append(getattr(self.data[var_data[1][0][1]].table[:, var_name], atr_vals[atr_type]).reshape(-1, 1))
if renamed:
self.Warning.renamed_vars(', '.join(renamed))
return self.merge_data(domain, values)

def curry_merge(self, table_key, atr_type, ids=None, selection=False):
Expand Down Expand Up @@ -490,15 +494,21 @@ def extract_rowwise(self, var_dict, ids=None, selection=False):
domain = defaultdict(lambda: [])
values = defaultdict(lambda: [])
atr_vals = {'metas': 'metas', 'attributes': 'X', 'class_vars': 'Y'}
renamed = []
for atr_type, vars_dict in var_dict.items():
for var_name, var_data in vars_dict.items():
different = var_data[0]
if different:
#columns are different, copy all, rename them
"""
Columns are different, copy and rename them.
Renaming is done here to mark appropriately the source table.
Additional strange clashes are checked later in merge_data
"""
for var, table_key in var_data[1]:
temp = self.data[table_key].table
idx = list(self.data.keys()).index(table_key) + 1
domain[atr_type].append(var.copy(name='{} ({})'.format(var_name, idx)))
renamed.append(var_name.name)
v = getattr(temp[list(ids[table_key].values()), var_name],
atr_vals[atr_type])
perm = permutations[table_key]
Expand All @@ -522,6 +532,8 @@ def extract_rowwise(self, var_dict, ids=None, selection=False):
value[perm] = v
values[atr_type].append(value)

if renamed:
self.Warning.renamed_vars(', '.join(renamed))
table = self.merge_data(domain, values)
mask = [idx in self.selected_items for idx in all_ids]
if selection:
Expand All @@ -533,9 +545,9 @@ def get_indices(self, table, selection):
if self.selected_feature:
if self.output_duplicates and selection:
items, inverse = np.unique(getattr(table[:, self.selected_feature], 'metas'),
return_inverse=True)
ids = [np.nonzero(inverse == idx)[0] for idx in range(len(items))]
else:
return_inverse=True)
ids = [np.nonzero(inverse == idx)[0] for idx in range(len(items))]
else:
items, ids = np.unique(getattr(table[:, self.selected_feature], 'metas'),
return_index=True)

Expand Down Expand Up @@ -588,19 +600,15 @@ def expand_tables(self, table, atrs, metas, cv):
array.fill(np.nan)
b = getattr(table, self.atr_vals[atr_type])
array[:, perm] = self.make_it_fit(array[:, perm].shape, b)
#array[:, perm] = np.atleast_2d(getattr(table, self.atr_vals[atr_type]))
exp.append(array)
#TODO: maybe this could be smarter
return exp[0], exp[1], exp[2]

def extract_rowwise_duplicates(self, var_dict, ids, relevant_keys):
#za vsak id v vsakemu stolpcu rabimo indekse
#extractamo celo podtabelo, vstavimo morebitne manjkajoče stolpce, na koncu vstack
all_ids = sorted(list(reduce(set.union, [set(val.keys()) for val in ids.values()], set())))
sort_key = lambda var: var.name
all_atrs = sorted([var for var in var_dict['attributes'].keys()], key=sort_key)
all_metas = sorted([var for var in var_dict['metas'].keys()], key=sort_key)
all_cv = sorted([var for var in var_dict['class_vars'].keys()], key=sort_key)
all_atrs = sorted((var for var in var_dict['attributes'].keys()), key=sort_key)
all_metas = sorted((var for var in var_dict['metas'].keys()), key=sort_key)
all_cv = sorted((var for var in var_dict['class_vars'].keys()), key=sort_key)

all_x, all_y, all_m = [], [], []
for idx in all_ids:
Expand Down
25 changes: 24 additions & 1 deletion Orange/widgets/visualize/tests/test_owvenndiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest
from unittest.mock import patch
from collections import defaultdict
from copy import deepcopy

import numpy as np
import scipy.sparse as sp
Expand Down Expand Up @@ -35,7 +36,29 @@ def _select_data(self):
self.widget.vennwidget.vennareas()[1].setSelected(True)
return list(range(len(self.signal_data)))

def test_multiple_input(self):
def test_rows_id(self):
data = Table('zoo')
data1 = deepcopy(data)
data1[:, 1] = 1
self.widget.rowwise = True
self.send_signal(self.signal_name, data1[:10], 1)
self.widget.selected_feature = 'name'
self.send_signal(self.signal_name, data[5:10], 2)

self.assertIsNone(self.get_output(self.widget.Outputs.selected_data))
self.assertTrue(self.widget.Warning.renamed_vars.is_shown())

self.widget.vennwidget.vennareas()[3].setSelected(True)
selected = self.get_output(self.widget.Outputs.selected_data)
sel_atrs = [atr.name for atr in selected.domain.attributes]
true_atrs = ['hair', 'feathers (1)', 'feathers (2)', 'eggs', 'milk', 'airborne', 'aquatic',
'predator', 'toothed', 'backbone', 'breathes', 'venomous', 'fins', 'legs',
'tail', 'domestic', 'catsize']
self.assertEqual(sel_atrs, true_atrs)
self.assertEqual(selected.domain.metas, data.domain.metas)
self.assertEqual(selected.domain.class_vars, data.domain.class_vars)

def test_multiple_input_rows_id(self):
"""Over rows"""
self.widget.rowwise = True
self.send_signal(self.signal_name, self.data[:100], 1)
Expand Down

0 comments on commit 55ae682

Please sign in to comment.