Skip to content

Commit

Permalink
test: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosrcoelho committed Aug 12, 2022
1 parent cd62595 commit eeefa4c
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/wavy/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def dropna_match(x, y):
``Panel``: Panel with dropped frames and matched ids
"""

x_t = x.dropnna_frames()
x_t = x.dropna_frames()
y_t = y.match_frames(x_t)

y_t = y_t.dropna_frames()
Expand Down Expand Up @@ -167,7 +167,7 @@ def concat_panels(
panel = Panel(pd.concat(panels, axis=0))

if sort:
panel = panel.sort_ids()
panel = panel.sort_panel()

if reset_ids:
panel.reset_ids(inplace=True)
Expand Down Expand Up @@ -271,7 +271,7 @@ def ids(self, ids: list[int]) -> None:
ids (list): List of ids.
"""

ids = np.repeat(np.arange(len(self)), self.shape_panel[1])
ids = np.repeat(ids, self.shape_panel[1])
timestep = self.index.get_level_values(1)

index = pd.MultiIndex.from_arrays([ids, timestep], names=["id", timestep.name])
Expand Down
137 changes: 137 additions & 0 deletions test/test_wavy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
index = [1, 2, 3, 4, 5, 6]
data = [[np.nan, 2], [np.inf, 4], [5, 6], [7, 8], [9, 10], [11, 12]]

columns1 = ["C", "D"]
index1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data1 = [
[np.nan, 2],
[np.inf, 4],
[5, 6],
[7, 8],
[9, 10],
[11, 12],
[13, 14],
[15, 16],
[17, 18],
]


@pytest.fixture
def panel():
Expand Down Expand Up @@ -78,5 +92,128 @@ def test_reset_ids(panel):
assert x.ids.to_list() == [0, 1], "IDs x are not correct"


def test_dropna_match(panel):
x, y = panel
x = x.dropna_frames()
y = y.match_frames(x)
assert x.ids.to_list() == [1, 2], "IDs y are not correct"
assert y.ids.to_list() == [1, 2], "IDs y are not correct"


def test_row_panel(panel):
x, y = panel
assert x.row_panel().shape[0] == 3, "Row panel x is not correct"
assert y.row_panel().shape[0] == 3, "Row panel y is not correct"


def test_get_timesteps(panel):
x, _ = panel
assert x.get_timesteps().to_list() == [1, 2, 3], "Timesteps x are not correct"
assert x.get_timesteps(1).to_list() == [2, 3, 4], "Timesteps x are not correct"
assert x.get_timesteps(2).to_list() == [3, 4, 5], "Timesteps x are not correct"


def test_values_panel(panel):
x, y = panel
assert x.values_panel.shape == (3, 3, 2), "Values panel x is not correct"
assert y.values_panel.shape == (3, 1, 2), "Values panel y is not correct"


def test_flatten_panel(panel):
x, y = panel
assert x.flatten_panel().shape == (3, 6), "Flatten panel x is not correct"
assert y.flatten_panel().shape == (3, 2), "Flatten panel y is not correct"


def test_drop_ids(panel):
x, y = panel
assert x.drop_ids(ids=[1]).ids.to_list() == [0, 2], "IDs x are not correct"
assert wavy.Panel(x.drop_ids(ids=[0, 2])).ids.to_list() == [
1
], "IDs x are not correct"
assert y.drop_ids(ids=1).ids.to_list() == [0, 2], "IDs y are not correct"
assert wavy.Panel(y.drop_ids(ids=[0, 1])).ids.to_list() == [
2
], "IDs y are not correct"
x.drop_ids(ids=[1], inplace=True)
assert x.ids.to_list() == [0, 2], "IDs x are not correct"
wavy.Panel(x.drop_ids(ids=[1, 2], inplace=True))
assert x.ids.to_list() == [0], "IDs x are not correct"
y.drop_ids(ids=1, inplace=True)
assert y.ids.to_list() == [0, 2], "IDs y are not correct"
wavy.Panel(y.drop_ids(ids=[0, 1], inplace=True))
assert y.ids.to_list() == [2], "IDs y are not correct"


def test_match_frames(panel):
x, y = panel
assert x.match_frames(y).ids.to_list() == [0, 1, 2], "IDs x are not matching"
assert y.match_frames(x).ids.to_list() == [0, 1, 2], "IDs y are not matching"
x.match_frames(y, inplace=True)
assert x.ids.to_list() == [0, 1, 2], "IDs x are not matching"
y.match_frames(x, inplace=True)
assert y.ids.to_list() == [0, 1, 2], "IDs y are not matching"


def test_as_dataframe(panel):
x, y = panel
assert x.as_dataframe().shape == (5, 2), "Dataframe x is not correct"
assert y.as_dataframe().shape == (3, 2), "Dataframe y is not correct"


def test_head_panel(panel):
x, y = panel
assert x.head_panel(3).shape_panel == (3, 3, 2), "Head panel x is not correct"
assert y.head_panel(3).shape_panel == (3, 1, 2), "Head panel y is not correct"


def test_tail_panel(panel):
x, y = panel
assert x.tail_panel(2).shape_panel == (2, 3, 2), "Tail panel x is not correct"
assert y.tail_panel(2).shape_panel == (2, 1, 2), "Tail panel y is not correct"


def test_sort_panel(panel):
x, y = panel
x.sort_panel(ascending=False, inplace=True)
assert x.ids.to_list() == [2, 1, 0], "IDs x are not correct"
x.sort_panel(ascending=True, inplace=True)
assert x.ids.to_list() == [0, 1, 2], "IDs x are not correct"
a = x.sort_panel(ascending=False, inplace=False)
assert a.ids.to_list() == [2, 1, 0], "IDs x are not correct"


def test_concat_panel(panel):
x, y = panel
with pytest.raises(ValueError):
wavy.concat_panels([x, x])
a = x.copy()
a.ids = [3, 4, 5]
assert wavy.concat_panels([x, a]).ids.to_list() == [
0,
1,
2,
3,
4,
5,
], "IDs x are not correct"
assert wavy.concat_panels([a, x], reset_ids=True).ids.to_list() == [
0,
1,
2,
3,
4,
5,
], "IDs x are not correct"
assert wavy.concat_panels([a, x], sort=True).ids.to_list() == [
0,
1,
2,
3,
4,
5,
], "IDs x are not correct"


if __name__ == "__main__":
pytest.main()

0 comments on commit eeefa4c

Please sign in to comment.