Skip to content

Commit 17393d7

Browse files
authored
Merge pull request #134 from amosproj/docstrings_vis_ad
updated docstrings for ad visualization
2 parents 1f9020f + c2b756e commit 17393d7

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

src/sdk/python/rtdip_sdk/pipelines/visualization/matplotlib/anomaly_detection.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ def __init__(
7575
"""
7676
Initialize the AnomalyDetectionPlot component.
7777
78-
Args:
79-
ts_data: PySpark DataFrame with 'timestamp' and 'value' columns
80-
ad_data: PySpark DataFrame with 'timestamp' and 'value' columns
81-
sensor_id: Optional sensor identifier
82-
title: Optional custom title
83-
figsize: Figure size tuple
84-
linewidth: Line width for the time series
85-
anomaly_marker_size: Size of anomaly markers
86-
anomaly_color: Color for anomaly points
87-
ts_color: Color for time series line
88-
ax: Optional existing matplotlib axis to plot on
78+
Parameters:
79+
ts_data (SparkDataFrame): PySpark DataFrame with 'timestamp' and 'value' columns.
80+
ad_data (SparkDataFrame): PySpark DataFrame with 'timestamp' and 'value' columns.
81+
sensor_id (str, optional): Sensor identifier used in the plot title.
82+
title (str, optional): Custom plot title. If not provided, a default title is used.
83+
figsize (tuple, optional): Figure size as (width, height). Defaults to (18, 6).
84+
linewidth (float, optional): Line width for time series. Defaults to 1.6.
85+
anomaly_marker_size (int, optional): Marker size for anomalies. Defaults to 70.
86+
anomaly_color (str, optional): Color for anomaly markers. Defaults to "red".
87+
ts_color (str, optional): Color for time series line. Defaults to "steelblue".
88+
ax (matplotlib.axes.Axes, optional): Existing matplotlib axis to plot on.
8989
"""
9090
super().__init__()
9191

@@ -106,7 +106,13 @@ def __init__(
106106
self._validate_data()
107107

108108
def _validate_data(self) -> None:
109-
"""Validate that required columns exist in DataFrames."""
109+
"""
110+
Validate input data format and data types.
111+
112+
Ensures that both `ts_data` and `ad_data` contain the required columns
113+
{'timestamp', 'value'}. Automatically converts timestamp columns to
114+
datetime and value columns to numeric types when necessary.
115+
"""
110116
required_cols = {"timestamp", "value"}
111117

112118
if not required_cols.issubset(self.ts_data.columns):
@@ -146,11 +152,12 @@ def plot(self, ax: Optional[Axes] = None) -> Figure | SubFigure:
146152
"""
147153
Generate the anomaly detection visualization.
148154
149-
Args:
150-
ax: Optional matplotlib axis to plot on. If None, creates new figure.
155+
Parameters:
156+
ax (matplotlib.axes.Axes, optional): Existing matplotlib axis to plot on.
157+
If None, a new figure and axis are created.
151158
152159
Returns:
153-
matplotlib.figure.Figure: The generated figure
160+
Figure | SubFigure: The generated matplotlib figure containing the plot.
154161
"""
155162
# Use provided ax or instance ax
156163
use_ax = ax if ax is not None else self.ax
@@ -214,13 +221,13 @@ def save(
214221
"""
215222
Save the visualization to file.
216223
217-
Args:
218-
filepath (Union[str, Path]): Output file path
219-
dpi (int): Dots per inch. Defaults to 150
220-
**kwargs (Any): Additional arguments passed to savefig
224+
Parameters:
225+
filepath (Union[str, Path]): Output file path.
226+
dpi (int, optional): Dots per inch for the saved figure. Defaults to 150.
227+
**kwargs (Any): Additional keyword arguments passed to `matplotlib.pyplot.savefig`.
221228
222229
Returns:
223-
Path: The path to the saved file
230+
Path: Path to the saved figure file.
224231
"""
225232

226233
assert self._fig is not None, "Plot the figure before saving."

src/sdk/python/rtdip_sdk/pipelines/visualization/plotly/anomaly_detection.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class AnomalyDetectionPlotInteractive(PlotlyVisualizationInterface):
2727
Plot time series data with detected anomalies highlighted using Plotly.
2828
2929
This component is functionally equivalent to the Matplotlib-based
30-
AnomalyDetectionPlot. It visualizes the full time series as a line and
31-
overlays detected anomalies as markers. Hover tooltips on anomaly markers
32-
explicitly show timestamp and value.
30+
AnomalyDetectionPlot. It visualizes the full time series as an interactive
31+
line chart and overlays detected anomalies as markers. Hover tooltips on
32+
anomaly markers display timestamp and value information.
3333
"""
3434

3535
def __init__(
@@ -42,6 +42,20 @@ def __init__(
4242
anomaly_color: str = "red",
4343
anomaly_marker_size: int = 8,
4444
) -> None:
45+
"""
46+
Initialize the AnomalyDetectionPlotInteractive component.
47+
48+
Parameters:
49+
ts_data (SparkDataFrame): PySpark DataFrame with 'timestamp' and 'value' columns
50+
containing the full time series data.
51+
ad_data (SparkDataFrame, optional): PySpark DataFrame with 'timestamp' and 'value'
52+
columns containing detected anomalies.
53+
sensor_id (str, optional): Sensor identifier used in the plot title.
54+
title (str, optional): Custom plot title. If not provided, a default title is used.
55+
ts_color (str, optional): Color for the time series line. Defaults to "steelblue".
56+
anomaly_color (str, optional): Color for anomaly markers. Defaults to "red".
57+
anomaly_marker_size (int, optional): Marker size for anomaly points. Defaults to 8.
58+
"""
4559
super().__init__()
4660

4761
# Convert Spark DataFrames to Pandas
@@ -58,7 +72,13 @@ def __init__(
5872
self._validate_data()
5973

6074
def _validate_data(self) -> None:
61-
"""Validate required columns and enforce correct dtypes."""
75+
"""
76+
Validate input data format and data types.
77+
78+
Ensures that both `ts_data` and `ad_data` contain the required columns
79+
{'timestamp', 'value'}. Automatically converts timestamp columns to
80+
datetime and value columns to numeric types when necessary.
81+
"""
6282

6383
required_cols = {"timestamp", "value"}
6484

@@ -88,9 +108,8 @@ def plot(self) -> go.Figure:
88108
Generate the Plotly anomaly detection visualization.
89109
90110
Returns:
91-
plotly.graph_objects.Figure
111+
plotly.graph_objects.Figure: The generated interactive Plotly figure.
92112
"""
93-
94113
ts_sorted = self.ts_data.sort_values("timestamp")
95114

96115
fig = go.Figure()
@@ -155,14 +174,15 @@ def save(
155174
Save the Plotly visualization to file.
156175
157176
If the file suffix is `.html`, the figure is saved as an interactive HTML
158-
file. Otherwise, a static image is written (requires kaleido).
177+
file. Otherwise, a static image is written (requires the `kaleido` backend).
159178
160-
Args:
161-
filepath (Union[str, Path]): Output file path
162-
**kwargs (Any): Additional arguments passed to write_html or write_image
179+
Parameters:
180+
filepath (Union[str, Path]): Output file path.
181+
**kwargs (Any): Additional keyword arguments passed to `write_html`
182+
or `write_image`.
163183
164184
Returns:
165-
Path: The path to the saved file
185+
Path: Path to the saved visualization file.
166186
"""
167187
assert self._fig is not None, "Plot the figure before saving."
168188

0 commit comments

Comments
 (0)