Skip to content

Commit f3d1eac

Browse files
Better doc string support.
1 parent 37e335a commit f3d1eac

File tree

5 files changed

+55
-36
lines changed

5 files changed

+55
-36
lines changed

matplotview/__init__.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from typing import Optional, Iterable, Type, Union
22
from matplotlib.artist import Artist
33
from matplotlib.axes import Axes
4-
from matplotview._view_axes import view_wrapper, ViewSpecification
4+
from matplotview._view_axes import view_wrapper, ViewSpecification, DEFAULT_RENDER_DEPTH
5+
from matplotview._docs import dynamic_doc_string, get_interpolation_list_str
56

67

78
__all__ = ["view", "inset_zoom_axes", "ViewSpecification"]
89

910

11+
@dynamic_doc_string(render_depth=DEFAULT_RENDER_DEPTH, interp_list=get_interpolation_list_str())
1012
def view(
1113
axes: Axes,
1214
axes_to_view: Axes,
@@ -28,29 +30,31 @@ def view(
2830
The axes to display the contents of in the first axes, the 'viewed'
2931
axes.
3032
31-
image_interpolation: string, default of "nearest"
33+
image_interpolation: string, default of '{image_interpolation}'
3234
The image interpolation method to use when displaying scaled images
33-
from the axes being viewed. Defaults to "nearest". Supported options
34-
are 'antialiased', 'nearest', 'bilinear', 'bicubic', 'spline16',
35-
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
36-
'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos',
37-
or 'none'
35+
from the axes being viewed. Defaults to '{image_interpolation}'. Supported options
36+
are {interp_list}.
3837
3938
render_depth: optional int, positive, defaults to None
4039
The number of recursive draws allowed for this view, this can happen
4140
if the view is a child of the axes (such as an inset axes) or if
4241
two views point at each other. If None, uses the default render depth
43-
of 5, unless the axes passed is already a view axes, in which case the
42+
of {render_depth}, unless the axes passed is already a view axes, in which case the
4443
render depth the view already has will be used.
4544
4645
filter_set: Iterable[Union[Type[Artist], Artist]] or None
4746
An optional filter set, which can be used to select what artists
4847
are drawn by the view. Any artists or artist types in the set are not
4948
drawn.
5049
51-
scale_lines: bool, defaults to True
50+
scale_lines: bool, defaults to {scale_lines}
5251
Specifies if lines should be drawn thicker based on scaling in the
5352
view.
53+
54+
Returns
55+
-------
56+
axes
57+
The modified `~.axes.Axes` instance which is now a view.
5458
"""
5559
view_obj = view_wrapper(type(axes)).from_axes(axes, render_depth)
5660
view_obj.view_specifications[axes_to_view] = ViewSpecification(
@@ -61,6 +65,7 @@ def view(
6165
return view_obj
6266

6367

68+
@dynamic_doc_string(render_depth=DEFAULT_RENDER_DEPTH, interp_list=get_interpolation_list_str())
6469
def inset_zoom_axes(
6570
axes: Axes,
6671
bounds: Iterable,
@@ -69,7 +74,7 @@ def inset_zoom_axes(
6974
render_depth: Optional[int] = None,
7075
filter_set: Optional[Iterable[Union[Type[Artist], Artist]]] = None,
7176
scale_lines: bool = True,
72-
transform=None,
77+
transform = None,
7378
zorder: int = 5,
7479
**kwargs
7580
) -> Axes:
@@ -90,31 +95,28 @@ def inset_zoom_axes(
9095
Axes-relative coordinates.
9196
9297
zorder: number
93-
Defaults to 5 (same as `.Axes.legend`). Adjust higher or lower
98+
Defaults to {zorder} (same as `.Axes.legend`). Adjust higher or lower
9499
to change whether it is above or below data plotted on the
95100
parent Axes.
96101
97102
image_interpolation: string
98-
Supported options are 'antialiased', 'nearest', 'bilinear',
99-
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite',
100-
'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
101-
'sinc', 'lanczos', or 'none'. The default value is 'nearest'. This
103+
Supported options are {interp_list}. The default value is '{image_interpolation}'. This
102104
determines the interpolation used when attempting to render a
103105
zoomed version of an image.
104106
105107
render_depth: optional int, positive, defaults to None
106108
The number of recursive draws allowed for this view, this can happen
107109
if the view is a child of the axes (such as an inset axes) or if
108110
two views point at each other. If None, uses the default render depth
109-
of 5, unless the axes passed is already a view axes, in which case the
111+
of {render_depth}, unless the axes passed is already a view axes, in which case the
110112
render depth the view already has will be used.
111113
112114
filter_set: Iterable[Union[Type[Artist], Artist]] or None
113115
An optional filter set, which can be used to select what artists
114116
are drawn by the view. Any artists or artist types in the set are not
115117
drawn.
116118
117-
scale_lines: bool, defaults to True
119+
scale_lines: bool, defaults to {scale_lines}
118120
Specifies if lines should be drawn thicker based on scaling in the
119121
view.
120122

matplotview/_docs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import inspect
2+
from inspect import signature
3+
4+
5+
def dynamic_doc_string(**kwargs):
6+
def convert(func):
7+
default_vals = {
8+
k: v.default for k, v in signature(func).parameters.items() if(v.default is not inspect.Parameter.empty)
9+
}
10+
default_vals.update(kwargs)
11+
func.__doc__ = func.__doc__.format(**default_vals)
12+
13+
return func
14+
15+
return convert
16+
17+
18+
def get_interpolation_list_str():
19+
from matplotlib.image import _interpd_
20+
return ", ".join([f"'{k}'" if(i != len(_interpd_) - 1) else f"or '{k}'" for i, k in enumerate(_interpd_)])

matplotview/_transform_renderer.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import matplotlib._image as _image
77
import numpy as np
88
from matplotlib.image import _interpd_
9+
from matplotview._docs import dynamic_doc_string, get_interpolation_list_str
910

1011

1112
class _TransformRenderer(RendererBase):
@@ -15,6 +16,7 @@ class _TransformRenderer(RendererBase):
1516
original renderer.
1617
"""
1718

19+
@dynamic_doc_string(interp_list=get_interpolation_list_str())
1820
def __init__(
1921
self,
2022
base_renderer,
@@ -42,22 +44,19 @@ def __init__(
4244
4345
transform: `~matplotlib.transforms.Transform`
4446
The main transform to be used for plotting all objects once
45-
converted into the mock_transform coordinate space. Typically this
47+
converted into the mock_transform coordinate space. Typically, this
4648
is the child axes data coordinate space (transData).
4749
4850
bounding_axes: `~matplotlib.axes.Axes`
4951
The axes to plot everything within. Everything outside of this
5052
axes will be clipped.
5153
5254
image_interpolation: string
53-
Supported options are 'antialiased', 'nearest', 'bilinear',
54-
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite',
55-
'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
56-
'sinc', 'lanczos', or 'none'. The default value is 'nearest'. This
55+
Supported options are {interp_list}. The default value is '{image_interpolation}'. This
5756
determines the interpolation used when attempting to render a
5857
zoomed version of an image.
5958
60-
scale_linewidths: bool, default is True
59+
scale_linewidths: bool, default is {scale_linewidths}
6160
Specifies if line widths should be scaled, in addition to the
6261
paths themselves.
6362

matplotview/_view_axes.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from matplotlib.artist import Artist
99
from matplotlib.backend_bases import RendererBase
1010
from dataclasses import dataclass
11+
from matplotview._docs import dynamic_doc_string, get_interpolation_list_str
1112

1213
DEFAULT_RENDER_DEPTH = 5
1314

@@ -96,6 +97,7 @@ def _view_from_pickle(builder, args):
9697
return res
9798

9899

100+
@dynamic_doc_string(render_depth=DEFAULT_RENDER_DEPTH, interp_list=get_interpolation_list_str())
99101
@dataclass
100102
class ViewSpecification:
101103
"""
@@ -105,19 +107,16 @@ class ViewSpecification:
105107
Parameters:
106108
-----------
107109
image_interpolation: string
108-
Supported options are 'antialiased', 'nearest', 'bilinear',
109-
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite',
110-
'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
111-
'sinc', 'lanczos', or 'none'. The default value is 'nearest'. This
110+
Supported options are {interp_list}. The default value is '{image_interpolation}'. This
112111
determines the interpolation used when attempting to render a
113112
zoomed version of an image.
114113
115-
filter_set: Iterable[Union[Type[Artist], Artist]] or None
114+
filter_set: Iterable[Union[Type[Artist], Artist]] or {filter_set}
116115
An optional filter set, which can be used to select what artists
117116
are drawn by the view. Any artists or artist types in the set are not
118117
drawn.
119118
120-
scale_lines: bool, defaults to True
119+
scale_lines: bool, defaults to {scale_lines}
121120
Specifies if lines should be drawn thicker based on scaling in the
122121
view.
123122
"""
@@ -170,6 +169,8 @@ class View(axes_class, __ViewType):
170169
An axes which automatically displays elements of another axes. Does not
171170
require Artists to be plotted twice.
172171
"""
172+
173+
@dynamic_doc_string()
173174
def __init__(
174175
self,
175176
*args,
@@ -181,17 +182,14 @@ def __init__(
181182
182183
Parameters
183184
----------
184-
axes_to_view: `~.axes.Axes`
185-
The axes to create a view of.
186-
187185
*args
188186
Additional arguments to be passed to the Axes class this
189187
ViewAxes wraps.
190188
191-
render_depth: int, positive, defaults to 5
189+
render_depth: int, positive, defaults to {render_depth}
192190
The number of recursive draws allowed for this view, this can
193191
happen if the view is a child of the axes (such as an inset
194-
axes) or if two views point at each other. Defaults to 5.
192+
axes) or if two views point at each other. Defaults to {render_depth}.
195193
196194
**kwargs
197195
Other optional keyword arguments supported by the Axes
@@ -342,6 +340,7 @@ def view_specifications(self) -> Dict[Axes, ViewSpecification]:
342340
view_specs = view_specifications
343341

344342
@classmethod
343+
@dynamic_doc_string(render_depth=DEFAULT_RENDER_DEPTH)
345344
def from_axes(
346345
cls,
347346
axes: Axes,
@@ -362,7 +361,7 @@ def from_axes(
362361
The number of recursive draws allowed for this view, this can
363362
happen if the view is a child of the axes (such as an inset
364363
axes) or if two views point at each other. If none, use the
365-
default value (5) if the render depth is not already set.
364+
default value ({render_depth}) if the render depth is not already set.
366365
367366
Returns
368367
-------

matplotview/tests/test_view_obj.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from matplotview._view_axes import DEFAULT_RENDER_DEPTH, view_wrapper
66
import numpy as np
77

8-
98
def test_obj_comparison():
109
from matplotlib.axes import Subplot, Axes
1110

0 commit comments

Comments
 (0)