Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added plot_lagplot #548

Merged
merged 22 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0f34ab4
added lag_plot
Gerhardsa0 Feb 19, 2024
418e62d
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
b7d31d8
changed name and added numerical error if target contains non numeric…
Gerhardsa0 Feb 20, 2024
cd63409
Merge remote-tracking branch 'origin/519-feat-add-lag-plot-for-time-s…
Gerhardsa0 Feb 20, 2024
e1d52dd
changed name and added numerical error if target contains non numeric…
Gerhardsa0 Feb 20, 2024
7df7130
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
d0ce066
fixed test
Gerhardsa0 Feb 20, 2024
73565b4
fixed test
Gerhardsa0 Feb 20, 2024
516941d
Merge remote-tracking branch 'origin/519-feat-add-lag-plot-for-time-s…
Gerhardsa0 Feb 20, 2024
d1f06fb
comment fixed
Gerhardsa0 Feb 20, 2024
02e6b33
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
f820086
comment fixed
Gerhardsa0 Feb 20, 2024
be85f41
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
ac2ef44
added review changes
Gerhardsa0 Feb 20, 2024
dda83cc
Merge remote-tracking branch 'origin/519-feat-add-lag-plot-for-time-s…
Gerhardsa0 Feb 20, 2024
e82f112
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
cededcc
added review changes
Gerhardsa0 Feb 20, 2024
123accd
removed typehints
Gerhardsa0 Feb 20, 2024
081eea4
Merge remote-tracking branch 'origin/519-feat-add-lag-plot-for-time-s…
Gerhardsa0 Feb 20, 2024
e4138c7
removed typehints and reverted colum changes
Gerhardsa0 Feb 20, 2024
1d91367
style: apply automated linter fixes
megalinter-bot Feb 20, 2024
4560e4e
reverted colum changes
Gerhardsa0 Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/safeds/data/tabular/containers/_time_series.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

import io
import sys
from typing import TYPE_CHECKING

import matplotlib.pyplot as plt
import pandas as pd

from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Column, Row, Table, TaggedTable
from safeds.exceptions import (
ColumnIsTargetError,
ColumnIsTimeError,
IllegalSchemaModificationError,
NonNumericColumnError,
UnknownColumnNameError,
)

Expand Down Expand Up @@ -857,3 +863,34 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Time
),
time_name=self.time.name,
)

def plot_lagplot(self, lag: int) -> Image:
"""
Plot a lagplot for target column.
Gerhardsa0 marked this conversation as resolved.
Show resolved Hide resolved

lars-reimann marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
plot: Image
Gerhardsa0 marked this conversation as resolved.
Show resolved Hide resolved
The plot as an image.

Raises
------
NonNumericColumnError
If the time series targets contains non-numerical values.

Examples
--------
>>> from safeds.data.tabular.containers import TimeSeries
>>> table = TimeSeries({"time":[1, 2], "target": [3, 4], "feature":[2,2]}, target_name= "target", time_name="time", feature_names=["feature"], )
>>> image = table.plot_lagplot(lag = 1)

"""
if not self.target.type.is_numeric():
raise NonNumericColumnError("This time series target contains non-numerical columns.")
ax = pd.plotting.lag_plot(self.target._data, lag=lag)
fig = ax.figure
buffer = io.BytesIO()
fig.savefig(buffer, format="png")
plt.close() # Prevents the figure from being displayed directly
buffer.seek(0)
return Image.from_bytes(buffer.read())
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
from safeds.data.tabular.containers import TimeSeries
from safeds.exceptions import NonNumericColumnError
from syrupy import SnapshotAssertion


def test_should_return_table(snapshot_png: SnapshotAssertion) -> None:
table = TimeSeries(
{
"time": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"feature_1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"target": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
target_name="target",
time_name="time",
feature_names=None,
)
lag_plot = table.plot_lagplot(lag=1)
assert lag_plot == snapshot_png


def test_should_raise_if_column_contains_non_numerical_values() -> None:
table = TimeSeries(
{
"time": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"feature_1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"target": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
},
target_name="target",
time_name="time",
feature_names=None,
)
with pytest.raises(
NonNumericColumnError,
match=(
r"Tried to do a numerical operation on one or multiple non-numerical columns: \nThis time series target"
r" contains"
r" non-numerical columns."
),
):
table.plot_lagplot(2)
Loading