Skip to content

utils

Utility functions for the figure_resampler submodule.

is_figure(figure)

Check if the figure is a plotly go.Figure or a FigureResampler.

Note

This method does not use isinstance(figure, go.Figure) as this will not work when go.Figure is decorated (after executing the register_plotly_resampler function).

Parameters:

Name Type Description Default
figure Any

The figure to check.

required

Returns:

Type Description
bool

True if the figure is a plotly go.Figure or a FigureResampler.

Source code in plotly_resampler/figure_resampler/utils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def is_figure(figure: Any) -> bool:
    """Check if the figure is a plotly go.Figure or a FigureResampler.

    !!! note

        This method does not use isinstance(figure, go.Figure) as this will not work
        when go.Figure is decorated (after executing the
        ``register_plotly_resampler`` function).

    Parameters
    ----------
    figure : Any
        The figure to check.

    Returns
    -------
    bool
        True if the figure is a plotly go.Figure or a FigureResampler.
    """
    return isinstance(figure, BaseFigure) and (not isinstance(figure, BaseFigureWidget))

is_figurewidget(figure)

Check if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.

Note

This method does not use isinstance(figure, go.FigureWidget) as this will not work when go.FigureWidget is decorated (after executing the register_plotly_resampler function).

Parameters:

Name Type Description Default
figure Any

The figure to check.

required

Returns:

Type Description
bool

True if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.

Source code in plotly_resampler/figure_resampler/utils.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def is_figurewidget(figure: Any):
    """Check if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.

    !!! note

        This method does not use isinstance(figure, go.FigureWidget) as this will not
        work when go.FigureWidget is decorated (after executing the
        ``register_plotly_resampler`` function).

    Parameters
    ----------
    figure : Any
        The figure to check.

    Returns
    -------
    bool
        True if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.
    """
    return isinstance(figure, BaseFigureWidget)

is_fr(figure)

Check if the figure is a FigureResampler.

Note

This method will not return True if the figure is a plotly go.Figure.

Parameters:

Name Type Description Default
figure Any

The figure to check.

required

Returns:

Type Description
bool

True if the figure is a FigureResampler.

Source code in plotly_resampler/figure_resampler/utils.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def is_fr(figure: Any) -> bool:
    """Check if the figure is a FigureResampler.

    !!! note

        This method will not return True if the figure is a plotly go.Figure.

    Parameters
    ----------
    figure : Any
        The figure to check.

    Returns
    -------
    bool
        True if the figure is a FigureResampler.
    """
    from plotly_resampler import FigureResampler

    return isinstance(figure, FigureResampler)

is_fwr(figure)

Check if the figure is a FigureWidgetResampler.

Note

This method will not return True if the figure is a plotly go.FigureWidget.

Parameters:

Name Type Description Default
figure Any

The figure to check.

required

Returns:

Type Description
bool

True if the figure is a FigureWidgetResampler.

Source code in plotly_resampler/figure_resampler/utils.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def is_fwr(figure: Any) -> bool:
    """Check if the figure is a FigureWidgetResampler.

    !!! note

        This method will not return True if the figure is a plotly go.FigureWidget.

    Parameters
    ----------
    figure : Any
        The figure to check.

    Returns
    -------
    bool
        True if the figure is a FigureWidgetResampler.
    """
    from plotly_resampler import FigureWidgetResampler

    return isinstance(figure, FigureWidgetResampler)

round_number_str(number)

Round a number to the nearest unit and convert to a string.

Parameters:

Name Type Description Default
number float

The number to round.

required

Returns:

Type Description
str

The rounded number as a string. If the number is == 0, None is returned.

Source code in plotly_resampler/figure_resampler/utils.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def round_number_str(number: float) -> str:
    """Round a number to the nearest unit and convert to a string.

    Parameters
    ----------
    number : float
        The number to round.

    Returns
    -------
    str
        The rounded number as a string.
        If the number is == 0, None is returned.

    """
    sign = "-" if number < 0 else ""
    number = abs(number)
    if number > 0.95:
        for unit, scaling in [
            ("T", int(1e12)),  # Trillion
            ("B", int(1e9)),  # Billion
            ("M", int(1e6)),  # Million
            ("k", int(1e3)),  # Thousand
        ]:
            if number / scaling > 0.95:
                return f"{round(number / scaling)}{unit}"
        return sign + str(round(number))
    if number > 0:  # avoid log10(0)
        # we have a number between 0-0.95 -> round till nearest non-zero digit
        return sign + str(round(number, 1 + abs(int(math.log10(number)))))

round_td_str(td)

Round a timedelta to the nearest unit and convert to a string.

Parameters:

Name Type Description Default
td Timedelta

The timedelta to round.

required

Returns:

Type Description
str

The rounded timedelta as a string. If the timedelta is == 0, None is returned.

See Also

timedelta_to_str

Source code in plotly_resampler/figure_resampler/utils.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def round_td_str(td: pd.Timedelta) -> str:
    """Round a timedelta to the nearest unit and convert to a string.

    Parameters
    ----------
    td : pd.Timedelta
        The timedelta to round.

    Returns
    -------
    str
        The rounded timedelta as a string.
        If the timedelta is == 0, None is returned.

    !!! info "See Also"
        [`timedelta_to_str`][figure_resampler.utils.timedelta_to_str]

    """
    for t_s in ("D", "h", "min", "s", "ms", "us", "ns"):
        if td > 0.95 * pd.Timedelta(f"1{t_s}"):
            return timedelta_to_str(td.round(t_s))

timedelta_to_str(td)

Construct a tight string representation for the given timedelta arg.

Parameters:

Name Type Description Default
td Timedelta

The timedelta for which the string representation is constructed

required

Returns:

Name Type Description
str str

The tight string bounds of format ‘$d-$h$m$s.$ms’. If the timedelta is negative, the string starts with ‘NEG’.

Source code in plotly_resampler/figure_resampler/utils.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def timedelta_to_str(td: pd.Timedelta) -> str:
    """Construct a tight string representation for the given timedelta arg.

    Parameters
    ----------
    td: pd.Timedelta
        The timedelta for which the string representation is constructed

    Returns
    -------
    str:
        The tight string bounds of format '$d-$h$m$s.$ms'.
        If the timedelta is negative, the string starts with 'NEG'.

    """
    out_str = ""

    # Edge case if we deal with negative
    if td < pd.Timedelta(seconds=0):
        td *= -1
        out_str += "NEG"

    # Note: this must happen after the *= -1
    c = td.components
    if c.days > 0:
        out_str += f"{c.days}D"
    if c.hours > 0 or c.minutes > 0 or c.seconds > 0 or c.milliseconds > 0:
        out_str += "_" if out_str else ""  # add seperator if non-empty

    if c.hours > 0:
        out_str += f"{c.hours}h"
    if c.minutes > 0:
        out_str += f"{c.minutes}m"
    if c.seconds > 0:
        if c.milliseconds:
            out_str += (
                f"{c.seconds}.{str(c.milliseconds / 1000).split('.')[-1].rstrip('0')}s"
            )
        else:
            out_str += f"{c.seconds}s"
    elif c.milliseconds > 0:
        out_str += f"{c.milliseconds}ms"
    if c.microseconds > 0:
        out_str += f"{c.microseconds}us"
    if c.nanoseconds > 0:
        out_str += f"{c.nanoseconds}ns"
    return out_str