-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add moving average plot (#836)
Closes #XYZ ### Summary of Changes <!-- Please provide a summary of changes in this pull request, ensuring all changes are explained. --> added moving average plot --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
2b82db7
commit abcf68a
Showing
6 changed files
with
217 additions
and
34 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+23.2 KB
...apshots__/test_moving_average_plot/test_should_match_snapshot[date grouped].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+22.2 KB
...ing/__snapshots__/test_moving_average_plot/test_should_match_snapshot[date].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.3 KB
..._snapshots__/test_moving_average_plot/test_should_match_snapshot[numerical].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions
104
tests/safeds/data/tabular/plotting/test_moving_average_plot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import datetime | ||
|
||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.exceptions import ColumnNotFoundError, ColumnTypeError | ||
from syrupy import SnapshotAssertion | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("table", "x_name", "y_name", "window_size"), | ||
[ | ||
(Table({"A": [1, 2, 3], "B": [2, 4, 7]}), "A", "B", 2), | ||
# (Table({"A": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], "B": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]}), "A", "B", 2), | ||
( | ||
Table( | ||
{ | ||
"time": [ | ||
datetime.date(2022, 1, 10), | ||
datetime.date(2022, 1, 10), | ||
datetime.date(2022, 1, 11), | ||
datetime.date(2022, 1, 11), | ||
datetime.date(2022, 1, 12), | ||
datetime.date(2022, 1, 12), | ||
], | ||
"A": [10, 5, 20, 2, 1, 1], | ||
}, | ||
), | ||
"time", | ||
"A", | ||
2, | ||
), | ||
( | ||
Table( | ||
{ | ||
"time": [ | ||
datetime.date(2022, 1, 9), | ||
datetime.date(2022, 1, 10), | ||
datetime.date(2022, 1, 11), | ||
datetime.date(2022, 1, 12), | ||
], | ||
"A": [10, 5, 20, 2], | ||
}, | ||
), | ||
"time", | ||
"A", | ||
2, | ||
), | ||
], | ||
ids=["numerical", "date grouped", "date"], | ||
) | ||
def test_should_match_snapshot( | ||
table: Table, | ||
x_name: str, | ||
y_name: str, | ||
window_size: int, | ||
snapshot_png_image: SnapshotAssertion, | ||
) -> None: | ||
line_plot = table.plot.moving_average_plot(x_name, y_name, window_size) | ||
assert line_plot == snapshot_png_image | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("x", "y"), | ||
[ | ||
("C", "A"), | ||
("A", "C"), | ||
("C", "D"), | ||
], | ||
ids=["x column", "y column", "x and y column"], | ||
) | ||
def test_should_raise_if_column_does_not_exist_error_message(x: str, y: str) -> None: | ||
table = Table({"A": [1, 2, 3], "B": [2, 4, 7]}) | ||
with pytest.raises(ColumnNotFoundError): | ||
table.plot.moving_average_plot(x, y, window_size=2) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("table"), | ||
[ | ||
(Table({"A": [1, 2, 3], "B": ["2", 4, 7]})), | ||
(Table({"A": ["1", 2, 3], "B": [2, 4, 7]})), | ||
], | ||
ids=["x column", "y column"], | ||
) | ||
def test_should_raise_if_column_is_not_numerical(table: Table) -> None: | ||
with pytest.raises(ColumnTypeError): | ||
table.plot.moving_average_plot("A", "B", window_size=2) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("table", "column_name"), | ||
[ | ||
(Table({"A": [1, 2, 3], "B": [None, 4, 7]}), "B"), | ||
(Table({"A": [None, 2, 3], "B": [2, 4, 7]}), "A"), | ||
], | ||
ids=["x column", "y column"], | ||
) | ||
def test_should_raise_if_column_has_missing_value(table: Table, column_name: str) -> None: | ||
with pytest.raises( | ||
ValueError, | ||
match=f"there are missing values in column '{column_name}', use transformation to fill missing " | ||
f"values or drop the missing values", | ||
): | ||
table.plot.moving_average_plot("A", "B", window_size=2) |