Example #1
0
    def test_all_models_work_after_unpickling(self):
        Variable._clear_all_caches()
        datasets = [Table('iris'), Table('titanic')]
        for learner in list(self.all_learners()):
            try:
                learner = learner()
            except Exception:
                print('%s cannot be used with default parameters' % learner.__name__)
                traceback.print_exc()
                continue
            # Skip slow tests
            if isinstance(learner, _RuleLearner):
                continue

            for ds in datasets:
                model = learner(ds)
                s = pickle.dumps(model, 0)
                model2 = pickle.loads(s)

                np.testing.assert_almost_equal(
                    Table(model.domain, ds).X, Table(model2.domain, ds).X)
                np.testing.assert_almost_equal(
                    model(ds), model2(ds),
                    err_msg='%s does not return same values when unpickled %s'
                    % (learner.__class__.__name__, ds.name))
 def setUp(self):
     """ Override __eq__ for Orange objects that do not implement it"""
     self.add_comparator(Domain,
                         compare_members=("attributes", "class_vars",
                                          "class_var", "variables", "metas",
                                          "anonymous"))
     Variable._clear_all_caches()
Example #3
0
 def setUp(self):
     """ Override __eq__ for Orange objects that do not implement it"""
     self.add_comparator(Domain,
                         compare_members=("attributes", "class_vars",
                                          "class_var", "variables",
                                          "metas", "anonymous"))
     Variable._clear_all_caches()
Example #4
0
    def test_all_models_work_after_unpickling(self):
        Variable._clear_all_caches()
        datasets = [Table('iris'), Table('titanic')]
        for learner in list(self.all_learners()):
            try:
                learner = learner()
            except Exception:
                print('%s cannot be used with default parameters' %
                      learner.__name__)
                traceback.print_exc()
                continue
            # Skip slow tests
            if isinstance(learner, _RuleLearner):
                continue

            for ds in datasets:
                model = learner(ds)
                s = pickle.dumps(model, 0)
                model2 = pickle.loads(s)

                np.testing.assert_almost_equal(
                    Table(model.domain, ds).X,
                    Table(model2.domain, ds).X)
                np.testing.assert_almost_equal(
                    model(ds),
                    model2(ds),
                    err_msg='%s does not return same values when unpickled %s'
                    % (learner.__class__.__name__, ds.name))
Example #5
0
    def test_to_val(self):
        string_var = StringVariable("x")
        self.assertEqual(string_var.to_val("foo"), "foo")
        self.assertEqual(string_var.to_val(42), "42")

        cont_var = ContinuousVariable("x")
        self.assertTrue(math.isnan(cont_var.to_val("?")))
        self.assertTrue(math.isnan(Unknown))

        var = Variable("x")
        self.assertEqual(var.to_val("x"), "x")
Example #6
0
    def test_to_val(self):
        string_var = StringVariable("x")
        self.assertEqual(string_var.to_val("foo"), "foo")
        self.assertEqual(string_var.to_val(42), "42")

        cont_var = ContinuousVariable("x")
        self.assertTrue(math.isnan(cont_var.to_val("?")))
        self.assertTrue(math.isnan(Unknown))

        var = Variable("x")
        self.assertEqual(var.to_val("x"), "x")
    def setUpClass(cls):
        super().setUpClass()
        Variable._clear_all_caches()
        cls._init_data()
        cls.signal_name = "Reference Data"
        cls.signal_data = cls.data
        cls.same_input_output_domain = False

        genes_path = serverfiles.localpath_download("marker_genes", "panglao_gene_markers.tab")
        filter_ = FilterString("Organism", FilterString.Equal, "Human")
        cls.genes = Values([filter_])(Table(genes_path))
        cls.genes.attributes[TAX_ID] = "9606"
Example #8
0
    def test_exclusive(self):
        var = Variable("x")
        colors = [Mock(), Mock()]
        palette = Mock()
        var.colors = colors
        var.palette = palette
        self.assertIsNone(var.colors)
        self.assertTrue("palette" in var.attributes)
        self.assertFalse("colors" in var.attributes)

        var.colors = colors
        self.assertIsNone(var.palette)
        self.assertTrue("colors" in var.attributes)
        self.assertFalse("palette" in var.attributes)
    def test_datasets_info_features(self):
        for dataset, info in datasets.items():

            Variable._clear_all_caches()

            if info["location"].startswith("http"):
                continue  # Tested elsewhere

            table = Table(dataset)
            domain = table.domain

            # Test features
            self.assertEqual(table.X.shape[0], info["rows"], dataset)
            self.assertEqual(table.has_missing(), info["missing"], dataset)
            self.assertEqual(len(domain.metas), info["features"]["meta"],
                             dataset)
            self.assertEqual(
                sum(i.is_discrete for i in domain.attributes),
                info["features"]["discrete"],
                dataset,
            )
            self.assertEqual(
                sum(i.is_continuous for i in domain.attributes),
                info["features"]["continuous"],
                dataset,
            )

            # Test class vars
            if len(domain.class_vars) > 1:
                self.assertEqual(
                    [
                        "discrete" if i.is_discrete else "continuous"
                        for i in domain.class_vars
                    ],
                    info["target"]["type"],
                    dataset,
                )
            elif len(domain.class_vars) == 1:
                cls = domain.class_var
                self.assertEqual(
                    "discrete" if cls.is_discrete else "continuous",
                    info["target"]["type"],
                    dataset,
                )
                if cls.is_discrete:
                    self.assertEqual(len(cls.values), info["target"]["values"],
                                     dataset)
            else:
                self.assertEqual(False, info["target"]["type"], dataset)
Example #10
0
def create_domain(*ss):
    Variable._clear_all_caches()
    vars=dict(
        age=ContinuousVariable(name="AGE"),
        gender=DiscreteVariable(name="Gender", values=["M", "F"]),
        incomeA=ContinuousVariable(name="incomeA"),
        income=ContinuousVariable(name="income"),
        education=DiscreteVariable(name="education", values=["GS", "HS", "C"]),
        ssn=StringVariable(name="SSN"),
        race=DiscreteVariable(name="race",
                              values=["White", "Hypsanic", "African", "Other"]))

    def map_vars(s):
        return [vars[x] for x in s]
    return Domain(*[map_vars(s) for s in ss])
Example #11
0
def create_domain(*ss):
    Variable._clear_all_caches()
    vars=dict(
        age=ContinuousVariable(name="AGE"),
        gender=DiscreteVariable(name="Gender", values=["M", "F"]),
        incomeA=ContinuousVariable(name="incomeA"),
        income=ContinuousVariable(name="income"),
        education=DiscreteVariable(name="education", values=["GS", "HS", "C"]),
        ssn=StringVariable(name="SSN"),
        race=DiscreteVariable(name="race",
                              values=["White", "Hypsanic", "African", "Other"]))

    def map_vars(s):
        return [vars[x] for x in s]
    return Domain(*[map_vars(s) for s in ss])
Example #12
0
    def setUpClass(cls):
        """Prepare environment for test execution

        Construct a dummy signal manager and monkey patch
        OWReport.get_instance to return a manually created instance.
        """
        super().setUpClass()

        cls.widgets = []

        cls.signal_manager = DummySignalManager()

        report = OWReport()
        cls.widgets.append(report)
        OWReport.get_instance = lambda: report
        Variable._clear_all_caches()
Example #13
0
    def setUpClass(cls):
        """Prepare environment for test execution

        Construct a dummy signal manager and monkey patch
        OWReport.get_instance to return a manually created instance.
        """
        super().setUpClass()

        cls.widgets = []

        cls.signal_manager = DummySignalManager()

        report = OWReport()
        cls.widgets.append(report)
        OWReport.get_instance = lambda: report
        Variable._clear_all_caches()
Example #14
0
    def test_bad_data(self):
        """
        Firstly it creates predictions with TreeLearner. Then sends predictions and
        different data with different domain to Predictions widget. Those different
        data and domain are similar to original data and domain but they have three
        different target values instead of two.
        GH-2129
        """
        Variable._clear_all_caches()

        filestr1 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tyes
        """
        file1 = io.StringIO(filestr1)
        table = TabReader(file1).read()
        learner = TreeLearner()
        tree = learner(table)

        filestr2 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tunknown
        """
        file2 = io.StringIO(filestr2)
        bad_table = TabReader(file2).read()

        self.send_signal(self.widget.Inputs.predictors, tree, 1)

        with excepthook_catch():
            self.send_signal(self.widget.Inputs.data, bad_table)

        Variable._clear_all_caches(
        )  # so that test excepting standard titanic work
Example #15
0
    def test_properties_as_predicates(self):
        a = ContinuousVariable()
        self.assertTrue(Variable.is_continuous(a))
        self.assertFalse(Variable.is_discrete(a))
        self.assertFalse(Variable.is_string(a))
        self.assertTrue(Variable.is_primitive(a))

        a = StringVariable()
        self.assertFalse(Variable.is_continuous(a))
        self.assertFalse(Variable.is_discrete(a))
        self.assertTrue(Variable.is_string(a))
        self.assertFalse(Variable.is_primitive(a))
Example #16
0
    def test_properties_as_predicates(self):
        a = ContinuousVariable()
        self.assertTrue(Variable.is_continuous(a))
        self.assertFalse(Variable.is_discrete(a))
        self.assertFalse(Variable.is_string(a))
        self.assertTrue(Variable.is_primitive(a))

        a = StringVariable()
        self.assertFalse(Variable.is_continuous(a))
        self.assertFalse(Variable.is_discrete(a))
        self.assertTrue(Variable.is_string(a))
        self.assertFalse(Variable.is_primitive(a))
Example #17
0
    def test_bad_data(self):
        """
        Firstly it creates predictions with TreeLearner. Then sends predictions and
        different data with different domain to Predictions widget. Those different
        data and domain are similar to original data and domain but they have three
        different target values instead of two.
        GH-2129
        """
        Variable._clear_all_caches()

        filestr1 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tyes
        """
        file1 = io.StringIO(filestr1)
        table = TabReader(file1).read()
        learner = TreeLearner()
        tree = learner(table)

        filestr2 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tunknown
        """
        file2 = io.StringIO(filestr2)
        bad_table = TabReader(file2).read()

        self.send_signal(self.widget.Inputs.predictors, tree, 1)

        with excepthook_catch():
            self.send_signal(self.widget.Inputs.data, bad_table)

        Variable._clear_all_caches()  # so that test excepting standard titanic work
Example #18
0
    def test_datasets_info_features(self):
        print(datasets.items())
        for dataset, info in datasets.items():

            Variable._clear_all_caches()

            if info['location'].startswith('http'):
                continue  # Tested elsewhere

            table = Table(dataset)
            domain = table.domain

            # Test features
            self.assertEqual(table.X.shape[0], info['rows'], dataset)
            self.assertEqual(table.has_missing(), info['missing'], dataset)
            self.assertEqual(len(domain.metas), info['features']['meta'],
                             dataset)
            self.assertEqual(sum(i.is_discrete for i in domain.attributes),
                             info['features']['discrete'], dataset)
            self.assertEqual(sum(i.is_continuous for i in domain.attributes),
                             info['features']['continuous'], dataset)

            # Test class vars
            if len(domain.class_vars) > 1:
                self.assertEqual([
                    'discrete' if i.is_discrete else 'continuous'
                    for i in domain.class_vars
                ], info['target']['type'], dataset)
            elif len(domain.class_vars) == 1:
                cls = domain.class_var
                self.assertEqual(
                    'discrete' if cls.is_discrete else 'continuous',
                    info['target']['type'], dataset)
                if cls.is_discrete:
                    self.assertEqual(len(cls.values), info['target']['values'],
                                     dataset)
            else:
                self.assertEqual(False, info['target']['type'], dataset)
Example #19
0
    def test_datasets_info_features(self):
        print(datasets.items())
        for dataset, info in datasets.items():

            Variable._clear_all_caches()

            if info['location'].startswith('http'): continue  # Tested elsewhere

            table = Table(dataset)
            domain = table.domain

            # Test features
            self.assertEqual(table.X.shape[0], info['rows'], dataset)
            self.assertEqual(table.has_missing(), info['missing'], dataset)
            self.assertEqual(len(domain.metas), info['features']['meta'], dataset)
            self.assertEqual(sum(i.is_discrete for i in domain.attributes),
                             info['features']['discrete'],
                             dataset)
            self.assertEqual(sum(i.is_continuous for i in domain.attributes),
                             info['features']['continuous'],
                             dataset)

            # Test class vars
            if len(domain.class_vars) > 1:
                self.assertEqual(['discrete' if i.is_discrete else 'continuous'
                                  for i in domain.class_vars],
                                 info['target']['type'],
                                 dataset)
            elif len(domain.class_vars) == 1:
                cls = domain.class_var
                self.assertEqual('discrete' if cls.is_discrete else 'continuous',
                                 info['target']['type'],
                                 dataset)
                if cls.is_discrete:
                    self.assertEqual(len(cls.values), info['target']['values'], dataset)
            else:
                self.assertEqual(False, info['target']['type'], dataset)
Example #20
0
    def test_exclusive(self):
        var = Variable("x")
        colors = [Mock(), Mock()]
        palette = Mock()
        var.colors = colors
        # set_color for variable does not set this attribute; derived methods do
        var.attributes["colors"] = colors
        var.palette = palette
        self.assertIsNone(var.colors)
        self.assertTrue("palette" in var.attributes)
        self.assertFalse("colors" in var.attributes)

        var.colors = colors
        # set_color for variable does not set this attribute; derived methods do
        var.attributes["colors"] = colors
        self.assertIsNone(var.palette)
        self.assertTrue("colors" in var.attributes)
        self.assertFalse("palette" in var.attributes)
Example #21
0
 def setUp(self):
     Variable._clear_all_caches()
     self.data = Table(test_filename("datasets/test4"))
Example #22
0
 def setUp(self):
     Variable._clear_all_caches()
     # Convergence warnings are irrelevant for these tests
     warnings.filterwarnings("ignore", ".*", ConvergenceWarning)
Example #23
0
 def setUp(self):
     Variable._clear_all_caches()
     dataset_dirs.append("Orange/tests")
Example #24
0
 def init(self):
     Variable._clear_all_caches()
     self.data = Table("iris")
Example #25
0
 def setUp(self):
     Variable._clear_all_caches()  # pylint: disable=protected-access
     random.seed(42)
     self.zoo = Table("zoo")
Example #26
0
 def setUp(self):
     Variable.clear_cache()
     self.data = Table("test4")
Example #27
0
 def setUp(self):
     Variable._clear_all_caches()
 def tearDown(self):
     Variable._clear_all_caches()
Example #29
0
 def setUp(self):
     Variable._clear_all_caches()
     self.data = Table("test4")
Example #30
0
 def setUp(self):
     Variable._clear_all_caches()
Example #31
0
 def setUp(self):
     Variable._clear_all_caches()
     # Convergence warnings are irrelevant for these tests
     warnings.filterwarnings("ignore", ".*", ConvergenceWarning)
Example #32
0
 def setUp(self):
     Variable._clear_all_caches()
     self.data = Table("test4")
Example #33
0
 def test_unpickling_recreates_known_domains(self):
     Variable._clear_all_caches()
     domain = Domain([])
     unpickled_domain = pickle.loads(pickle.dumps(domain))
     self.assertTrue(hasattr(unpickled_domain, '_known_domains'))
Example #34
0
 def test_palette(self):
     var = Variable("x")
     palette = Mock()
     var.palette = palette
     self.assertIs(var.palette, palette)
 def setUp(self):
     Variable._clear_all_caches()  # pylint: disable=protected-access
     random.seed(42)
     self.zoo = Table("zoo")
Example #36
0
 def setUp(self):
     Variable._clear_all_caches()
     data.table.dataset_dirs.append(test_dirname())
     print(data.table.dataset_dirs)
Example #37
0
 def setUpClass(cls):
     cls.var = Variable("x")
Example #38
0
 def init(self):
     Variable._clear_all_caches()
     self.data = Table("iris")
Example #39
0
 def test_colors(self):
     var = Variable("x")
     colors = [Mock(), Mock()]
     var.colors = colors
     self.assertIs(var.colors, colors)
Example #40
0
 def init(self):
     Variable._clear_all_caches()
     self.data = Table("iris")
     self.same_input_output_domain = True
Example #41
0
 def setUp(self):
     self.var = Variable("x")
Example #42
0
 def init(self):
     Variable._clear_all_caches()
     self.data = Table("iris")
     self.same_input_output_domain = True
Example #43
0
 def test_unpickling_recreates_known_domains(self):
     Variable._clear_all_caches()
     domain = Domain([])
     unpickled_domain = pickle.loads(pickle.dumps(domain))
     self.assertTrue(hasattr(unpickled_domain, '_known_domains'))
Example #44
0
 def setUp(self):
     Variable.clear_cache()
     self.data = Table("test4")
Example #45
0
 def setUpClass(cls):
     super().setUpClass()
     Variable._clear_all_caches()
Example #46
0
 def setUp(self):
     Variable._clear_all_caches()
     random.seed(42)
     self.zoo = Table("zoo")
Example #47
0
 def tearDown(self):
     Variable._clear_all_caches()
Example #48
0
 def setUp(self):
     Variable._clear_all_caches()
     data.table.dataset_dirs.append(test_dirname())