예제 #1
0
 def _gather_conditions(self):
     conditions = []
     attr = self.attribute
     group_attr = self.group_var
     for data_range in self.selection:
         if attr.is_discrete:
             # If some value was removed from the data (in case settings are
             # loaded from a scheme), do not include the corresponding
             # filter; this is appropriate since data with such value does
             # not exist anyway
             if not data_range.value:
                 condition = IsDefined([attr], negate=True)
             elif data_range.value not in attr.values:
                 continue
             else:
                 condition = FilterDiscrete(attr, [data_range.value])
         else:
             condition = FilterContinuous(attr, FilterContinuous.Between,
                                          data_range.low, data_range.high)
         if data_range.group_value:
             if not data_range.group_value:
                 grp_filter = IsDefined([group_attr], negate=True)
             elif data_range.group_value not in group_attr.values:
                 continue
             else:
                 grp_filter = FilterDiscrete(group_attr,
                                             [data_range.group_value])
             condition = Values([condition, grp_filter], conjunction=True)
         conditions.append(condition)
     return conditions
    def test_is_defined_filter_instance(self):
        instance_with_missing = self.table[0]
        instance_without_missing = self.table[3]

        filter_ = IsDefined()
        self.assertFalse(filter_(instance_with_missing))
        self.assertTrue(filter_(instance_without_missing))

        filter_ = IsDefined(negate=True)
        self.assertTrue(filter_(instance_with_missing))
        self.assertFalse(filter_(instance_without_missing))
    def test_is_defined_filter_table(self):
        filter_ = IsDefined()
        without_missing = filter_(self.table)
        self.assertEqual(len(without_missing),
                         len(self.table) - self.n_missing)
        self.assertFalse(without_missing.has_missing())

        filter_ = IsDefined(negate=True)
        just_missing = filter_(self.table)
        self.assertEqual(len(just_missing), self.n_missing)
        self.assertTrue(just_missing.has_missing())
    def generate_plot_curves(self) -> List[EstimatedFunctionCurve]:
        self._data = None
        self.Warning.missing_values.clear()

        if self.time_var is None or self.event_var is None:
            return []

        filter_ = IsDefined(columns=[self.time_var, self.event_var])

        # if undefined values are detected in time_var or event_var omit rows with undefined values
        self._data = filter_(self.data)
        if len(self.data) == len(self._data):
            self._data = None
        else:
            self.Warning.missing_values()

        data = self.data if self._data is None else self._data
        time, _ = data.get_column_view(self.time_var)
        events, _ = data.get_column_view(self.event_var)

        if self.group_var:
            groups, _ = data.get_column_view(self.group_var)
            group_indexes = [index for index, _ in enumerate(self.group_var.values)]
            colors = [self._get_discrete_var_color(index) for index in group_indexes]
            masks = groups == np.reshape(group_indexes, (-1, 1))

            return [
                EstimatedFunctionCurve(time[mask], events[mask], color=color, label=label)
                for mask, color, label in zip(masks, colors, self.group_var.values)
                if mask.any()
            ]

        else:
            return [EstimatedFunctionCurve(time, events)]
    def set_data(self, data: Table):
        self.closeContext()

        if data and data.has_missing():
            filter_ = IsDefined(columns=data.domain.class_vars)
            self.data = filter_(data)
        else:
            self.data = data

        # exclude class vars
        domain = data.domain if data else None
        if domain is not None:
            domain = Domain(domain.attributes, metas=domain.metas)

        self.controls.group_var.model().set_domain(domain)
        if domain is not None:
            group_var_model = self.controls.group_var.model()
            if 'Cohorts' in domain and domain['Cohorts'] in group_var_model:
                self.group_var = domain['Cohorts']
            else:
                self.group_var = None

        self.graph.selection = {}
        self.openContext(domain)

        self.graph.curves = {
            curve_id: curve
            for curve_id, curve in enumerate(self.generate_plot_curves())
        }
        self.graph.update_plot(**self._get_plot_options())

        self.commit.now()
예제 #6
0
    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)