Skip to content

Commit 81f477b

Browse files
authored
Merge pull request #136 from ianhi/segment-cmaps
more kwarg options for image segmenter
2 parents 050c989 + 752f737 commit 81f477b

File tree

5 files changed

+122
-37
lines changed

5 files changed

+122
-37
lines changed

examples/image-segmentation.ipynb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,53 @@
171171
"display(preloaded)"
172172
]
173173
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"## Styling\n",
179+
"\n",
180+
"\n",
181+
"### imshow parameters\n",
182+
"\n",
183+
"You can modify the display of the image using any kwargs accepted the [imshow](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html) command. For example if we convert our example image to gray-scale then we can choose the colormap with the `cmap` argument."
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
192+
"from ipywidgets import HBox\n",
193+
"\n",
194+
"grayscale_image = image.mean(axis=-1)\n",
195+
"gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\")\n",
196+
"display(gray)\n",
197+
"plasma = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"plasma\")\n",
198+
"display(plasma)"
199+
]
200+
},
201+
{
202+
"cell_type": "markdown",
203+
"metadata": {},
204+
"source": [
205+
"## LassoSelector line\n",
206+
"\n",
207+
"You can change the appearance of the LassoSelector line using the `lineprops` kwarg. So to make the line very thick and red:"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": [
216+
"lineprops = {\"color\":\"red\", \"linewidth\":10}\n",
217+
"gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\",lineprops = lineprops)\n",
218+
"display(gray)"
219+
]
220+
},
174221
{
175222
"cell_type": "code",
176223
"execution_count": null,
@@ -195,7 +242,7 @@
195242
"name": "python",
196243
"nbconvert_exporter": "python",
197244
"pygments_lexer": "ipython3",
198-
"version": "3.7.8"
245+
"version": "3.9.0"
199246
}
200247
},
201248
"nbformat": 4,

mpl_interactions/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version_info = (0, 10, 1)
1+
version_info = (0, 11, 0)
22
__version__ = ".".join(map(str, version_info))

mpl_interactions/generic.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,14 @@ def __init__(
393393
mask=None,
394394
mask_colors=None,
395395
mask_alpha=0.75,
396+
lineprops=None,
396397
figsize=(10, 10),
397-
cmap="viridis",
398+
**kwargs,
398399
):
399400
"""
401+
Create an image segmenter. Any ``kwargs`` will be passed through to the ``imshow``
402+
call that displays *img*.
403+
400404
parameters
401405
----------
402406
img : array_like
@@ -409,12 +413,14 @@ def __init__(
409413
mask_alpha : float, default .75
410414
The alpha values to use for selected regions. This will always override the alpha values
411415
in mask_colors if any were passed
416+
lineprops : dict, default: None
417+
lineprops passed to LassoSelector. If None the default values are:
418+
{"color": "black", "linewidth": 1, "alpha": 0.8}
412419
figsize : (float, float), optional
413420
passed to plt.figure
414-
cmap : 'string'
415-
the colormap to use if img has shape (X,Y)
421+
**kwargs:
422+
All other kwargs will passed to the imshow command for the image
416423
"""
417-
418424
# ensure mask colors is iterable and the same length as the number of classes
419425
# choose colors from default color cycle?
420426

@@ -450,10 +456,11 @@ def __init__(
450456
with ioff:
451457
self.fig = figure(figsize=figsize)
452458
self.ax = self.fig.gca()
453-
self.displayed = self.ax.imshow(self._img)
459+
self.displayed = self.ax.imshow(self._img, **kwargs)
454460
self._mask = self.ax.imshow(self._overlay)
455461

456-
lineprops = {"color": "black", "linewidth": 1, "alpha": 0.8}
462+
if lineprops is None:
463+
lineprops = {"color": "black", "linewidth": 1, "alpha": 0.8}
457464
useblit = False if "ipympl" in get_backend().lower() else True
458465
self.lasso = LassoSelector(self.ax, self._onselect, lineprops=lineprops, useblit=useblit)
459466
self.lasso.set_visible(True)

mpl_interactions/mpl_kwargs.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# this is a list of options to Line2D partially taken from
2+
# https://github.com/matplotlib/matplotlib/blob/f9d29189507cfe4121a231f6ab63539d216c37bd/lib/matplotlib/lines.py#L271
3+
# many of these can also be made into functions
4+
plot_kwargs_list = [
5+
"alpha",
6+
"linewidth",
7+
"linestyle",
8+
"color",
9+
"marker",
10+
"markersize",
11+
"markeredgewidth",
12+
"markeredgecolor",
13+
"markerfacecolor",
14+
"markerfacecoloralt",
15+
"fillstyle",
16+
"antialiased",
17+
"dash_capstyle",
18+
"solid_capstyle",
19+
"dash_joinstyle",
20+
"solid_joinstyle",
21+
"pickradius",
22+
"drawstyle",
23+
"markevery",
24+
"label",
25+
]
26+
27+
imshow_kwargs_list = [
28+
"cmap",
29+
"norm",
30+
"aspect",
31+
"interpolation",
32+
"alpha",
33+
"vmin",
34+
"vmax",
35+
"origin",
36+
"extent",
37+
"filternorm",
38+
"filterrad",
39+
"resample",
40+
"url",
41+
]
42+
43+
44+
def kwarg_popper(kwargs, mpl_kwargs):
45+
"""
46+
This will not modify kwargs for you.
47+
48+
Usage
49+
-----
50+
51+
kwargs, plot_kwargs = kwarg_popper(kwargs, plot_kwargs_list)
52+
"""
53+
kwargs = dict(kwargs)
54+
passthrough = {}
55+
for k in mpl_kwargs:
56+
if k in kwargs:
57+
passthrough[k] = kwargs.pop(k)
58+
return kwargs, passthrough

mpl_interactions/pyplot.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
notebook_backend,
2525
update_datalim_from_bbox,
2626
)
27+
from .mpl_kwargs import plot_kwargs_list, imshow_kwargs_list, kwarg_popper
2728

2829
# functions that are methods
2930
__all__ = [
@@ -128,35 +129,7 @@ def f(x, tau):
128129
interactive_plot(x, f, tau=(0, np.pi, 1000))
129130
130131
"""
131-
# this is a list of options to Line2D partially taken from
132-
# https://github.com/matplotlib/matplotlib/blob/f9d29189507cfe4121a231f6ab63539d216c37bd/lib/matplotlib/lines.py#L271
133-
# many of these can also be made into functions
134-
plot_kwargs_list = [
135-
"alpha",
136-
"linewidth",
137-
"linestyle",
138-
"color",
139-
"marker",
140-
"markersize",
141-
"markeredgewidth",
142-
"markeredgecolor",
143-
"markerfacecolor",
144-
"markerfacecoloralt",
145-
"fillstyle",
146-
"antialiased",
147-
"dash_capstyle",
148-
"solid_capstyle",
149-
"dash_joinstyle",
150-
"solid_joinstyle",
151-
"pickradius",
152-
"drawstyle",
153-
"markevery",
154-
"label",
155-
]
156-
plot_kwargs = {}
157-
for k in plot_kwargs_list:
158-
if k in kwargs:
159-
plot_kwargs[k] = kwargs.pop(k)
132+
kwargs, plot_kwargs = kwarg_popper(kwargs, plot_kwargs_list)
160133
x_and_y = False
161134
x = None
162135
fmt = None

0 commit comments

Comments
 (0)