-
Notifications
You must be signed in to change notification settings - Fork 80
plot trend #565
Merged
Merged
plot trend #565
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9cf9f53
plot trend
Ama16 87a1185
Fix imports
Ama16 e47ef8d
changelog
Ama16 708791c
fix comments
Ama16 f102cff
fix imports
Ama16 4869e12
minor fix
Ama16 37d50e0
Merge branch 'master' into issue-515
Ama16 3375261
fix representation of k
Ama16 66bd488
Merge branch 'issue-515' of https://github.com/tinkoff-ai/etna into i…
Ama16 4e48559
lint
Ama16 a351975
Merge branch 'master' into issue-515
iKintosh 3245fd0
Merge branch 'master' into issue-515
alex-hse-repository File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,10 @@ | |||||
|
||||||
if TYPE_CHECKING: | ||||||
from etna.datasets import TSDataset | ||||||
from etna.transforms.decomposition.change_points_trend import ChangePointsTrendTransform | ||||||
from etna.transforms.decomposition.detrend import LinearTrendTransform | ||||||
from etna.transforms.decomposition.detrend import TheilSenTrendTransform | ||||||
from etna.transforms.decomposition.stl import STLTransform | ||||||
|
||||||
|
||||||
def prepare_axes(segments: List[str], columns_num: int, figsize: Tuple[int, int]) -> Sequence[matplotlib.axes.Axes]: | ||||||
|
@@ -729,3 +733,69 @@ def plot_residuals( | |||||
ax[i].set_title(segment) | ||||||
ax[i].tick_params("x", rotation=45) | ||||||
ax[i].set_xlabel(feature) | ||||||
|
||||||
|
||||||
TrendTransformType = Union[ | ||||||
"ChangePointsTrendTransform", "LinearTrendTransform", "TheilSenTrendTransform", "STLTransform" | ||||||
] | ||||||
|
||||||
|
||||||
def __get_labels_names(trend_transform, segments): | ||||||
"""If only unique transform classes are used then show their short names (without parameters). Otherwise show their full repr as label.""" | ||||||
from etna.transforms.decomposition.detrend import LinearTrendTransform | ||||||
from etna.transforms.decomposition.detrend import TheilSenTrendTransform | ||||||
|
||||||
labels = [transform.__repr__() for transform in trend_transform] | ||||||
labels_short = [i[: i.find("(")] for i in labels] | ||||||
if len(np.unique(labels_short)) == len(labels_short): | ||||||
labels = labels_short | ||||||
linear_coeffs = dict(zip(segments, ["" for i in range(len(segments))])) | ||||||
if len(trend_transform) == 1 and isinstance(trend_transform[0], (LinearTrendTransform, TheilSenTrendTransform)): | ||||||
for seg in segments: | ||||||
linear_coeffs[seg] = ", k=" + str(trend_transform[0].segment_transforms[seg]._linear_model.coef_[0]) | ||||||
return labels, linear_coeffs | ||||||
|
||||||
|
||||||
def plot_trend( | ||||||
ts: "TSDataset", | ||||||
trend_transform: Union["TrendTransformType", List["TrendTransformType"]], | ||||||
segments: Optional[List[str]] = None, | ||||||
columns_num: int = 2, | ||||||
figsize: Tuple[int, int] = (10, 5), | ||||||
): | ||||||
"""Plot series and trend from trend transform for this series. | ||||||
|
||||||
If only unique transform classes are used then show their short names (without parameters). Otherwise show their full repr as label | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
ts: | ||||||
dataframe of timeseries that was used for trend plot | ||||||
trend_transform: | ||||||
trend transform or list of trend transforms to apply | ||||||
segments: | ||||||
segments to use | ||||||
columns_num: | ||||||
number of columns in subplots | ||||||
figsize: | ||||||
size of the figure per subplot with one segment in inches | ||||||
""" | ||||||
if not segments: | ||||||
segments = list(set(ts.columns.get_level_values("segment"))) | ||||||
|
||||||
ax = prepare_axes(segments=segments, columns_num=columns_num, figsize=figsize) | ||||||
df = ts.df | ||||||
|
||||||
if not isinstance(trend_transform, list): | ||||||
trend_transform = [trend_transform] | ||||||
|
||||||
df_detrend = [transform.fit_transform(df.copy()) for transform in trend_transform] | ||||||
labels, linear_coeffs = __get_labels_names(trend_transform, segments) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same as above https://stackoverflow.com/a/1301369/7415703 |
||||||
|
||||||
for i, segment in enumerate(segments): | ||||||
ax[i].plot(df[segment]["target"], label="Initial series") | ||||||
for label, df_now in zip(labels, df_detrend): | ||||||
ax[i].plot(df[segment, "target"] - df_now[segment, "target"], label=label + linear_coeffs[segment]) | ||||||
ax[i].set_title(segment) | ||||||
ax[i].tick_params("x", rotation=45) | ||||||
ax[i].legend() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://stackoverflow.com/a/1301369/7415703