def test_same_value_filter_instance(self): inst = self.table[0] filter_ = SameValue(self.attr_disc, self.value_disc)(inst) self.assertEqual(filter_, inst[self.attr_disc] == self.value_disc) filter_n = SameValue(self.attr_disc, self.value_disc, negate=True)(inst) self.assertEqual(filter_n, inst[self.attr_disc] != self.value_disc)
def test_create_groups_table_include_unselected(self): group_indices = random.sample(range(0, len(self.zoo)), 20) selection = np.zeros(len(self.zoo), dtype=np.uint8) selection[group_indices[:10]] = 1 selection[group_indices[10:]] = 2 table = create_groups_table(self.zoo, selection) self.assertEqual( len(SameValue(table.domain["Selected"], "Unselected")(table)), len(self.zoo) - len(group_indices))
def test_same_value_filter_table(self): test_pairs = ( (self.attr_cont, 4, self.value_cont), (self.attr_disc, "mammal", self.value_disc), (self.attr_meta, "girl", self.value_meta), ) for var_index, value, num_value in test_pairs: filter_ = SameValue(var_index, value)(self.table) self.assertTrue( all(inst[var_index] == num_value for inst in filter_)) filter_inverse = SameValue(var_index, value, negate=True)(self.table) self.assertTrue( all(inst[var_index] != num_value for inst in filter_inverse)) self.assertEqual( len(filter_) + len(filter_inverse), len(self.table)) for t1, t2 in itertools.combinations(test_pairs, 2): pos1, val1, r1 = t1 pos2, val2, r2 = t2 filter_1 = SameValue(pos1, val1)(self.table) filter_2 = SameValue(pos2, val2)(self.table) filter_12 = SameValue(pos2, val2)(SameValue(pos1, val1)(self.table)) filter_21 = SameValue(pos1, val1)(SameValue(pos2, val2)(self.table)) self.assertEqual(len(filter_21), len(filter_12)) self.assertTrue(len(filter_1) >= len(filter_12)) self.assertTrue(len(filter_2) >= len(filter_12)) self.assertTrue( all(inst[pos1] == r1 and inst[pos2] == r2 and inst in filter_21 for inst in filter_12)) self.assertTrue( all(inst[pos1] == r1 and inst[pos2] == r2 and inst in filter_12 for inst in filter_21))
def test_reprs(self): flid = IsDefined(negate=True) flhc = HasClass() flr = Random() fld = FilterDiscrete(self.attr_disc, None) flsv = SameValue(self.attr_disc, self.value_disc, negate=True) flc = FilterContinuous(self.vs[0], FilterContinuous.Less, 5) flc2 = FilterContinuous(self.vs[1], FilterContinuous.Greater, 3) flv = Values([flc, flc2], conjunction=False, negate=True) flvf = ValueFilter(self.attr_disc) fls = FilterString("name", FilterString.Equal, "Aardvark", case_sensitive=False) flsl = FilterStringList("name", ["Aardvark"], case_sensitive=False) flrx = FilterRegex("name", "^c...$") filters = [flid, flhc, flr, fld, flsv, flc, flv, flvf, fls, flsl, flrx] for f in filters: repr_str = repr(f) new_f = eval(repr_str) self.assertEqual(repr(new_f), repr_str)
def commit(self): averages = None if self.data is not None: if self.group_var is None: averages = self.average_table(self.data) else: parts = [] for value in self.group_var.values: svfilter = SameValue(self.group_var, value) v_table = self.average_table(svfilter(self.data)) parts.append(v_table) # Using "None" as in OWSelectRows # Values is required because FilterDiscrete doesn't have # negate keyword or IsDefined method deffilter = Values( conditions=[FilterDiscrete(self.group_var, None)], negate=True) v_table = self.average_table(deffilter(self.data)) parts.append(v_table) averages = Orange.data.Table.concatenate(parts, axis=0) self.Outputs.averages.send(averages)