Skip to content

Commit

Permalink
feat: added lag_plot (#548)
Browse files Browse the repository at this point in the history
Closes #519 

### Summary of Changes
I added the visualization of the lag plot to the timeseries class
---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
Gerhardsa0 and megalinter-bot authored Feb 20, 2024
1 parent 2f1d5c5 commit 0fb38d2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import sys
import io
import sys
from collections.abc import Sequence
from numbers import Number
from typing import TYPE_CHECKING, Any, TypeVar, overload
Expand Down
49 changes: 49 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 @@ -839,6 +845,13 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Time
The original time series is not modified.
Parameters
----------
name:
The name of the column to be transformed.
transformer:
The transformer to the given column
Returns
-------
result : TimeSeries
Expand All @@ -857,3 +870,39 @@ 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 the target column.
Parameters
----------
lag:
The amount of lag used to plot
Returns
-------
plot:
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)

0 comments on commit 0fb38d2

Please sign in to comment.