Skip to content

Commit

Permalink
Merge pull request #136 from ianhi/segment-cmaps
Browse files Browse the repository at this point in the history
more kwarg options for image segmenter
  • Loading branch information
ianhi authored Nov 17, 2020
2 parents 050c989 + 752f737 commit 81f477b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 37 deletions.
49 changes: 48 additions & 1 deletion examples/image-segmentation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,53 @@
"display(preloaded)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Styling\n",
"\n",
"\n",
"### imshow parameters\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import HBox\n",
"\n",
"grayscale_image = image.mean(axis=-1)\n",
"gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\")\n",
"display(gray)\n",
"plasma = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"plasma\")\n",
"display(plasma)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## LassoSelector line\n",
"\n",
"You can change the appearance of the LassoSelector line using the `lineprops` kwarg. So to make the line very thick and red:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lineprops = {\"color\":\"red\", \"linewidth\":10}\n",
"gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\",lineprops = lineprops)\n",
"display(gray)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -195,7 +242,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
"version": "3.9.0"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion mpl_interactions/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (0, 10, 1)
version_info = (0, 11, 0)
__version__ = ".".join(map(str, version_info))
19 changes: 13 additions & 6 deletions mpl_interactions/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,14 @@ def __init__(
mask=None,
mask_colors=None,
mask_alpha=0.75,
lineprops=None,
figsize=(10, 10),
cmap="viridis",
**kwargs,
):
"""
Create an image segmenter. Any ``kwargs`` will be passed through to the ``imshow``
call that displays *img*.
parameters
----------
img : array_like
Expand All @@ -409,12 +413,14 @@ def __init__(
mask_alpha : float, default .75
The alpha values to use for selected regions. This will always override the alpha values
in mask_colors if any were passed
lineprops : dict, default: None
lineprops passed to LassoSelector. If None the default values are:
{"color": "black", "linewidth": 1, "alpha": 0.8}
figsize : (float, float), optional
passed to plt.figure
cmap : 'string'
the colormap to use if img has shape (X,Y)
**kwargs:
All other kwargs will passed to the imshow command for the image
"""

# ensure mask colors is iterable and the same length as the number of classes
# choose colors from default color cycle?

Expand Down Expand Up @@ -450,10 +456,11 @@ def __init__(
with ioff:
self.fig = figure(figsize=figsize)
self.ax = self.fig.gca()
self.displayed = self.ax.imshow(self._img)
self.displayed = self.ax.imshow(self._img, **kwargs)
self._mask = self.ax.imshow(self._overlay)

lineprops = {"color": "black", "linewidth": 1, "alpha": 0.8}
if lineprops is None:
lineprops = {"color": "black", "linewidth": 1, "alpha": 0.8}
useblit = False if "ipympl" in get_backend().lower() else True
self.lasso = LassoSelector(self.ax, self._onselect, lineprops=lineprops, useblit=useblit)
self.lasso.set_visible(True)
Expand Down
58 changes: 58 additions & 0 deletions mpl_interactions/mpl_kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# this is a list of options to Line2D partially taken from
# https://github.com/matplotlib/matplotlib/blob/f9d29189507cfe4121a231f6ab63539d216c37bd/lib/matplotlib/lines.py#L271
# many of these can also be made into functions
plot_kwargs_list = [
"alpha",
"linewidth",
"linestyle",
"color",
"marker",
"markersize",
"markeredgewidth",
"markeredgecolor",
"markerfacecolor",
"markerfacecoloralt",
"fillstyle",
"antialiased",
"dash_capstyle",
"solid_capstyle",
"dash_joinstyle",
"solid_joinstyle",
"pickradius",
"drawstyle",
"markevery",
"label",
]

imshow_kwargs_list = [
"cmap",
"norm",
"aspect",
"interpolation",
"alpha",
"vmin",
"vmax",
"origin",
"extent",
"filternorm",
"filterrad",
"resample",
"url",
]


def kwarg_popper(kwargs, mpl_kwargs):
"""
This will not modify kwargs for you.
Usage
-----
kwargs, plot_kwargs = kwarg_popper(kwargs, plot_kwargs_list)
"""
kwargs = dict(kwargs)
passthrough = {}
for k in mpl_kwargs:
if k in kwargs:
passthrough[k] = kwargs.pop(k)
return kwargs, passthrough
31 changes: 2 additions & 29 deletions mpl_interactions/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
notebook_backend,
update_datalim_from_bbox,
)
from .mpl_kwargs import plot_kwargs_list, imshow_kwargs_list, kwarg_popper

# functions that are methods
__all__ = [
Expand Down Expand Up @@ -128,35 +129,7 @@ def f(x, tau):
interactive_plot(x, f, tau=(0, np.pi, 1000))
"""
# this is a list of options to Line2D partially taken from
# https://github.com/matplotlib/matplotlib/blob/f9d29189507cfe4121a231f6ab63539d216c37bd/lib/matplotlib/lines.py#L271
# many of these can also be made into functions
plot_kwargs_list = [
"alpha",
"linewidth",
"linestyle",
"color",
"marker",
"markersize",
"markeredgewidth",
"markeredgecolor",
"markerfacecolor",
"markerfacecoloralt",
"fillstyle",
"antialiased",
"dash_capstyle",
"solid_capstyle",
"dash_joinstyle",
"solid_joinstyle",
"pickradius",
"drawstyle",
"markevery",
"label",
]
plot_kwargs = {}
for k in plot_kwargs_list:
if k in kwargs:
plot_kwargs[k] = kwargs.pop(k)
kwargs, plot_kwargs = kwarg_popper(kwargs, plot_kwargs_list)
x_and_y = False
x = None
fmt = None
Expand Down

0 comments on commit 81f477b

Please sign in to comment.