From 7765af8baf63d586fd7ac467fdcdfcd489be45af Mon Sep 17 00:00:00 2001 From: Vesna Tanko Date: Fri, 25 Nov 2022 11:31:49 +0100 Subject: [PATCH] stats: Handle empty array --- Orange/statistics/util.py | 2 ++ Orange/tests/test_basic_stats.py | 4 ++++ Orange/tests/test_statistics.py | 12 ++++++++++++ Orange/widgets/data/tests/test_owtable.py | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/Orange/statistics/util.py b/Orange/statistics/util.py index 979be60d7c0..c37715e4794 100644 --- a/Orange/statistics/util.py +++ b/Orange/statistics/util.py @@ -370,6 +370,8 @@ def stats(X, weights=None, compute_variance=False): X.shape[0] - non_zero, non_zero)) else: + if X.ndim == 1: + X = X[:, None] nans = (pandas.isnull(X).sum(axis=0) + (X == "").sum(axis=0)) \ if X.size else np.zeros(X.shape[1]) return np.column_stack(( diff --git a/Orange/tests/test_basic_stats.py b/Orange/tests/test_basic_stats.py index 3b5ec18792f..fb234a84e43 100644 --- a/Orange/tests/test_basic_stats.py +++ b/Orange/tests/test_basic_stats.py @@ -28,6 +28,10 @@ def test_domain_basic_stats(self): self.assertStatsEqual(domain_stats.stats, attr_stats + class_var_stats + meta_stats) + def test_empty_table(self): + domain_stats = DomainBasicStats(self.zoo[:0]) + self.assertEqual(len(domain_stats.stats), 17) + def test_speed(self): n, m = 10, 10000 data = Table.from_numpy(None, np.random.rand(n, m)) diff --git a/Orange/tests/test_statistics.py b/Orange/tests/test_statistics.py index 0e5ee9c1930..121c66cee92 100644 --- a/Orange/tests/test_statistics.py +++ b/Orange/tests/test_statistics.py @@ -170,6 +170,18 @@ def test_stats_non_numeric(self): [np.inf, -np.inf, 0, 0, 2, 1], [np.inf, -np.inf, 0, 0, 0, 3]]) + def test_stats_empty(self): + X = np.array([]) + np.testing.assert_equal(stats(X), [[np.inf, -np.inf, 0, 0, 0, 0]]) + + X = np.zeros((0,)) + np.testing.assert_equal(stats(X), [[np.inf, -np.inf, 0, 0, 0, 0]]) + + X = np.zeros((0, 4)) + np.testing.assert_equal(stats(X), [[np.inf, -np.inf, 0, 0, 0, 0]] * 4) + + + def test_stats_long_string_mem_use(self): X = np.full((1000, 1000), "a", dtype=object) t = time.time() diff --git a/Orange/widgets/data/tests/test_owtable.py b/Orange/widgets/data/tests/test_owtable.py index 18ec1e60efc..61dde5f3354 100644 --- a/Orange/widgets/data/tests/test_owtable.py +++ b/Orange/widgets/data/tests/test_owtable.py @@ -46,6 +46,12 @@ def test_input_data(self): self.send_signal(self.widget.Inputs.data, None, 1) self.assertEqual(self.widget.tabs.count(), 1) + def test_input_data_empty(self): + self.send_signal(self.widget.Inputs.data, self.data[:0]) + output = self.get_output(self.widget.Outputs.annotated_data) + self.assertIsInstance(output, Table) + self.assertEqual(len(output), 0) + def test_data_model(self): self.send_signal(self.widget.Inputs.data, self.data, 1) self.assertEqual(self.widget.tabs.widget(0).model().rowCount(),