Skip to content

Commit

Permalink
Merge pull request #6221 from VesnaT/fix_stats_empty_data
Browse files Browse the repository at this point in the history
[FIX] stats: Handle empty array
  • Loading branch information
janezd authored Nov 25, 2022
2 parents 66fb829 + 7765af8 commit 1c3c00f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Orange/statistics/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down
4 changes: 4 additions & 0 deletions Orange/tests/test_basic_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 12 additions & 0 deletions Orange/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions Orange/widgets/data/tests/test_owtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1c3c00f

Please sign in to comment.