Skip to content

Commit

Permalink
GroupBy: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Oct 9, 2024
1 parent ef24960 commit 36f21a8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
59 changes: 58 additions & 1 deletion Orange/data/tests/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from unittest.mock import Mock

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -132,13 +133,69 @@ def test_aggregation(self):
def test_preserve_table_class(self):
"""
Test whether result table has the same type than the imnput table,
e.g. if input table corpus the resutlitn table must be corpus too.
e.g. if input table corpus the resulting table must be corpus too.
"""
data = AlternativeTable.from_table(self.data.domain, self.data)
gb = data.groupby([data.domain["a"]])
output = gb.aggregate({data.domain["a"]: ["mean"]})
self.assertIsInstance(output, AlternativeTable)

def test_preserve_variables(self):
a, _, _, dvar = self.data.domain.attributes
gb = self.data.groupby([a])

a.attributes = {"foo": "bar"}
dvar.attributes = {"foo": "baz"}

a.copy = Mock(side_effect=a.copy)
a.make = Mock(side_effect=a.make)

def f(*_):
return 0

output = gb.aggregate(
{a: [("copy", f, True),
("make", f, False),
("auto", f, None),
("string", f, StringVariable),
("number", f, ContinuousVariable)],
dvar: [("copy", f, True),
("make", f, False),
("auto", f, None),
("string", f, StringVariable),
("discrete", f, DiscreteVariable)]}
)
self.assertIsInstance(output.domain["a - copy"], ContinuousVariable)
a.copy.assert_called_once()
self.assertEqual(output.domain["a - copy"].attributes, {"foo": "bar"})

self.assertIsInstance(output.domain["a - make"], ContinuousVariable)
a.make.assert_called_once()
self.assertNotEqual(output.domain["a - make"].attributes, {"foo": "bar"})

self.assertIsInstance(output.domain["a - auto"], ContinuousVariable)
self.assertNotEqual(output.domain["a - auto"].attributes, {"foo": "bar"})

self.assertIsInstance(output.domain["a - string"], StringVariable)

self.assertIsInstance(output.domain["a - number"], ContinuousVariable)
self.assertNotEqual(output.domain["a - number"].attributes, {"foo": "bar"})

self.assertIsInstance(output.domain["dvar - copy"], DiscreteVariable)
self.assertEqual(output.domain["dvar - copy"].attributes, {"foo": "baz"})

self.assertIsInstance(output.domain["dvar - make"], DiscreteVariable)
self.assertNotEqual(output.domain["dvar - make"].attributes, {"foo": "baz"})

# f returns 0, so the column looks numeric! Let's test that it is
# converted to numeric.
self.assertIsInstance(output.domain["dvar - auto"], ContinuousVariable)

self.assertIsInstance(output.domain["dvar - string"], StringVariable)

self.assertIsInstance(output.domain["dvar - discrete"], DiscreteVariable)
self.assertNotEqual(output.domain["dvar - discrete"].attributes, {"foo": "baz"})


if __name__ == "__main__":
unittest.main()
25 changes: 18 additions & 7 deletions Orange/data/tests/test_pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ def test_table_from_frame(self):
self.assertEqual(names, ['0', '1', '2'])
self.assertEqual(types, [DiscreteVariable, ContinuousVariable, TimeVariable])

# Specify (some) variables
dvar = DiscreteVariable('x', values=tuple("dacb"))
cvar = ContinuousVariable('y')
table = table_from_frame(df, variables=[dvar, cvar, None])
self.assertIs(table.domain[0], dvar)
self.assertIs(table.domain[1], cvar)
self.assertIsInstance(table.domain[2], TimeVariable)

table = table_from_frame(df,
variables=[None, None, None],
force_nominal=True)
self.assertIsInstance(table.domain[0], DiscreteVariable)
self.assertIsInstance(table.domain[1], ContinuousVariable)
self.assertIsInstance(table.domain[2], TimeVariable)

self.assertRaises(AssertionError,
table_from_frame, df, variables=[None, None])

# Include index
df.index = list('abaa')
table = table_from_frame(df)
Expand All @@ -72,13 +90,6 @@ def test_table_from_frame(self):
self.assertEqual(names, ['index', '1', '2'])
self.assertEqual(types, [DiscreteVariable, ContinuousVariable, TimeVariable])

dvar = DiscreteVariable('x', values=tuple("dacb"))
cvar = ContinuousVariable('y')
table = table_from_frame(df, variables=[dvar, cvar, None])
self.assertIs(table.domain[0], dvar)
self.assertIs(table.domain[1], cvar)
self.assertIsInstance(table.domain[3], TimeVariable)

def test_table_from_frame_keep_ids(self):
""" Test if indices are correctly transferred to Table"""
df = OrangeDataFrame(Table('iris')[:6])
Expand Down

0 comments on commit 36f21a8

Please sign in to comment.