Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dimension parameter for scale bar to all plot functions #11

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

steps:
- uses: actions/[email protected]
- uses: actions/setup-python@v2
- uses: actions/setup-python@v2.1.1
with:
python-version: ${{ matrix.python-version }}
# architecture: x64
Expand Down
22 changes: 21 additions & 1 deletion src/seaborn_image/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

from ._colormap import _CMAP_QUAL

# dimensions for scalebar
_DIMENSIONS = {
"si": "si-length",
"si-reciprocal": "si-length-reciprocal",
"imperial": "imperial-length",
"angle": "angle",
"pixel": "pixel-length",
}


def _check_dict(dictionary):
if not isinstance(dictionary, dict):
Expand All @@ -23,6 +32,7 @@ def __init__(
fontdict=None,
dx=None,
units=None,
dimension=None,
cbar=None,
cbar_fontdict=None,
cbar_label=None,
Expand All @@ -39,6 +49,7 @@ def __init__(
self.fontdict = fontdict
self.dx = dx
self.units = units
self.dimension = dimension
self.cbar = cbar
self.cbar_fontdict = cbar_fontdict
self.cbar_label = cbar_label
Expand Down Expand Up @@ -75,7 +86,16 @@ def _setup_scalebar(self, ax):
"'units' must be specified when 'dx' (scalebar) is used"
)

scalebar = ScaleBar(dx=dx, units=units)
if self.dimension is None:
_dimension = _DIMENSIONS.get("si")
elif self.dimension in _DIMENSIONS.keys():
_dimension = _DIMENSIONS.get(self.dimension)
else:
raise ValueError(
f"Unsupported dimension. Supported dimensions are : {_DIMENSIONS.keys()}"
)

scalebar = ScaleBar(dx=dx, units=units, dimension=_dimension)

ax.add_artist(scalebar)

Expand Down
13 changes: 13 additions & 0 deletions src/seaborn_image/_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def filterplot(
vmax=None,
dx=None,
units=None,
dimension=None,
cbar=True,
cbar_label=None,
cbar_fontdict=None,
Expand Down Expand Up @@ -47,6 +48,14 @@ def filterplot(
dx (float, optional): Size per pixel of the image data. If scalebar
is required, `dx` and `units` must be sepcified. Defaults to None.
units (str, optional): Units of `dx`. Defaults to None.
dimension (str, optional): dimension of `dx` and `units`.
Options include :
- "si" : scale bar showing km, m, cm, etc.
- "imperial" : scale bar showing in, ft, yd, mi, etc.
- "si-reciprocal" : scale bar showing 1/m, 1/cm, etc.
- "angle" : scale bar showing °, ʹ (minute of arc) or ʹʹ (second of arc).
- "pixel" : scale bar showing px, kpx, Mpx, etc.
Defaults to None.
cbar (bool, optional): Specify if a colorbar is required or not.
Defaults to True.
cbar_label (str, optional): Colorbar label. Defaults to None.
Expand Down Expand Up @@ -160,6 +169,7 @@ def filterplot(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand All @@ -176,6 +186,7 @@ def filterplot(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand Down Expand Up @@ -214,6 +225,7 @@ def filterplot(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand All @@ -230,6 +242,7 @@ def filterplot(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand Down
20 changes: 20 additions & 0 deletions src/seaborn_image/_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def imgplot(
vmax=None,
dx=None,
units=None,
dimension=None,
cbar=True,
cbar_label=None,
cbar_fontdict=None,
Expand All @@ -41,6 +42,14 @@ def imgplot(
dx (float, optional): Size per pixel of the image data. If scalebar
is required, `dx` and `units` must be sepcified. Defaults to None.
units (str, optional): Units of `dx`. Defaults to None.
dimension (str, optional): dimension of `dx` and `units`.
Options include (similar to `matplotlib_scalebar`):
- "si" : scale bar showing km, m, cm, etc.
- "imperial" : scale bar showing in, ft, yd, mi, etc.
- "si-reciprocal" : scale bar showing 1/m, 1/cm, etc.
- "angle" : scale bar showing °, ʹ (minute of arc) or ʹʹ (second of arc).
- "pixel" : scale bar showing px, kpx, Mpx, etc.
Defaults to None.
cbar (bool, optional): Specify if a colorbar is required or not.
Defaults to True.
cbar_label (str, optional): Colorbar label. Defaults to None.
Expand Down Expand Up @@ -110,6 +119,7 @@ def imgplot(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand All @@ -132,6 +142,7 @@ def imghist(
vmax=None,
dx=None,
units=None,
dimension=None,
cbar=True,
cbar_label=None,
cbar_fontdict=None,
Expand All @@ -155,6 +166,14 @@ def imghist(
dx (float, optional): Size per pixel of the image data. If scalebar
is required, `dx` and `units` must be sepcified. Defaults to None.
units (str, optional): Units of `dx`. Defaults to None.
dimension (str, optional): dimension of `dx` and `units`.
Options include :
- "si" : scale bar showing km, m, cm, etc.
- "imperial" : scale bar showing in, ft, yd, mi, etc.
- "si-reciprocal" : scale bar showing 1/m, 1/cm, etc.
- "angle" : scale bar showing °, ʹ (minute of arc) or ʹʹ (second of arc).
- "pixel" : scale bar showing px, kpx, Mpx, etc.
Defaults to None.
cbar (bool, optional): Specify if a colorbar is required or not.
Defaults to True.
cbar_label (str, optional): Colorbar label. Defaults to None.
Expand Down Expand Up @@ -204,6 +223,7 @@ def imghist(
vmax=vmax,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_label=cbar_label,
cbar_fontdict=cbar_fontdict,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_setup_scalebar():
img_setup._setup_scalebar(ax)


def test_setup_scalebar_dimension():
with pytest.raises(ValueError):
img_setup = isns._core._SetupImage(
data, dx=1, units="nm", dimension="imperial-reciprocal"
)
f, ax = img_setup._setup_figure()
img_setup._setup_scalebar(ax)


def test_plot_check_cbar_dict():
with pytest.raises(TypeError):
img_setup = isns._core._SetupImage(
Expand All @@ -48,6 +57,7 @@ def test_plot_check_cbar_dict():
@pytest.mark.parametrize(
"units", ["m", "um"]
) # units can't be None when dx is not None
@pytest.mark.parametrize("dimension", [None, "si"])
@pytest.mark.parametrize("cbar", [None, True, False])
@pytest.mark.parametrize("cbar_fontdict", [None, {"fontsize": 20}])
@pytest.mark.parametrize("cbar_label", [None, "Cbar Label"])
Expand All @@ -61,6 +71,7 @@ def test_plot_w_all_inputs(
fontdict,
dx,
units,
dimension,
cbar,
cbar_fontdict,
cbar_label,
Expand All @@ -76,6 +87,7 @@ def test_plot_w_all_inputs(
fontdict=fontdict,
dx=dx,
units=units,
dimension=dimension,
cbar=cbar,
cbar_fontdict=cbar_fontdict,
cbar_label=cbar_label,
Expand All @@ -90,3 +102,5 @@ def test_plot_w_all_inputs(
assert isinstance(cax, Axes)
else:
assert cax is None

plt.close("all")
5 changes: 4 additions & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.axes import Axes
from matplotlib.figure import Figure
Expand All @@ -16,7 +17,7 @@ def test_filter_not_implemented():


@pytest.mark.parametrize(
"filter,fft", [(["gaussian"], True), ("gaussian", "True"), (["gaussian"], "True")]
"filter,fft", [(["gaussian"], True), ("gaussian", "True"), (["gaussian"], None)]
)
def test_filter_types(filter, fft):
with pytest.raises(TypeError):
Expand All @@ -34,3 +35,5 @@ def test_filters(filter, fft):
assert isinstance(ax, np.ndarray)
assert isinstance(ax.ravel().all(), Axes)
assert filt_data.all() == data.all()

plt.close("all")
7 changes: 5 additions & 2 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_imgplot_return():
def test_imgplot_w_all_valid_inputs(
cmap, cbar, cbar_label, cbar_fontdict, showticks, title, title_fontdict
):
isns.imgplot(
f, ax, cax = isns.imgplot(
data,
ax=None,
cmap=cmap,
Expand All @@ -79,6 +79,7 @@ def test_imgplot_w_all_valid_inputs(
title=title,
title_fontdict=title_fontdict,
)
plt.close("all")


@pytest.mark.parametrize("bins", [None, 200.0, -400.13])
Expand Down Expand Up @@ -113,7 +114,7 @@ def test_imghist_return():
def test_imghist_w_all_valid_inputs(
cmap, bins, cbar, cbar_label, cbar_fontdict, showticks, title, title_fontdict
):
isns.imghist(
f, axes, cax = isns.imghist(
data,
cmap=cmap,
bins=bins,
Expand All @@ -124,3 +125,5 @@ def test_imghist_w_all_valid_inputs(
title=title,
title_fontdict=title_fontdict,
)

plt.close("all")