Skip to content

Commit

Permalink
Addition of draft keyword option and tests (#5)
Browse files Browse the repository at this point in the history
* Added draft keyword option and tests
  • Loading branch information
ebenp authored Nov 5, 2020
1 parent f3bcb18 commit 2e96fa2
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 3 deletions.
11 changes: 10 additions & 1 deletion figpager/figpager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import inspect
import os

# backend for display in GitHub Actions
if os.environ.get('DISPLAY','') == '':
import matplotlib as mpl
print('no display found. Using non-interactive Agg backend')
Expand Down Expand Up @@ -97,6 +98,7 @@ def __init__(
overwrite=False,
sharex=False,
sharey=False,
draft=True,
):

"""
Expand Down Expand Up @@ -125,7 +127,8 @@ def __init__(
direction: (string) (optional) subplot creation direction. Default is left-to-right.
overwrite: (boolean) (optional) Boolean on whether to overwrite existing output. Default is True
sharex: share x axes in subplots. Default is False
sharey:share y axes in subplots.Default is False
sharey:share y axes in subplots. Default is False
draft: Add draft stamp if available from ini. Default is True
"""

# obtain the caller path
Expand Down Expand Up @@ -225,6 +228,7 @@ def __init__(
self.width_ratios = width_ratios
self.sharex = sharex
self.sharey = sharey
self.draft = draft

# figure attributes
self.fig = None
Expand Down Expand Up @@ -1009,6 +1013,8 @@ def draw_page(self):
# add any layout set text here
for k in self.config["Text"].keys():
if self._parse_option("Text", k, "text") is not None:
if 'draft' in self._parse_option("Text", k, "text").lower():
if not self.draft: continue
self._text_from_label("Text", k, self._parse_option("Text", k, "text"))

# add any layout set images here
Expand All @@ -1022,6 +1028,9 @@ def draw_page(self):
# add any layout set watermarks here
for k in self.config["Watermark"].keys():
if self._parse_option("Watermark", k, "text") is not None:
# check for draft watermark status and whether user has overridden it
if 'draft' in self._parse_option("Watermark", k, "text").lower():
if not self.draft: continue
self._text_from_label(
"Watermark", k, self._parse_option("Watermark", k, "text")
)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def read(fname):

setup(
name="figpager",
version="0.25",
version="0.26",
author="Eben Pendleton",
author_email="4080051+ebenp@users.noreply.github.com",
url="https://github.com/ebenp/figpager",
Expand Down
Binary file added tests/figpager_no_draft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/figpager_no_draft_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/out.pdf
Binary file not shown.
Binary file modified tests/out_2.pdf
Binary file not shown.
Binary file modified tests/out_3.pdf
Binary file not shown.
Binary file modified tests/out_4.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_main():
"letter",
3,
2,
layout="Report",
layout="report",
outfile=outfile,
orientation="portrait",
height_ratios=[1, 1, 2],
Expand Down
115 changes: 115 additions & 0 deletions tests/test_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Test for multipage support

# Plots from: https://matplotlib.org/3.2.1/gallery/images_contours_and_fields/plot_streamplot.html#sphx-glr-gallery-images-contours-and-fields-plot-streamplot-py
import os

import numpy as np

from figpager import FigPager


def test_main():
# Initalize with a configuration that controls page margins
# and plot spacing
# Initalize with a page size and number of plots

# Initalize with an output file
outfile = "./tests/figpager_no_draft.png"

# plots an image from http://www.metmuseum.org/art/collection/search/334348 CC0 1.0 Public Domain
fp = FigPager(
"letter",
3,
2,
layout="report",
outfile=outfile,
orientation="portrait",
height_ratios=[1, 1, 2],
overwrite=True,
transparent=False,
draft=False
)

w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X ** 2 + Y
V = 1 + X - Y ** 2
speed = np.sqrt(U ** 2 + V ** 2)

ax0 = fp.add_subplot()
ax0.streamplot(X, Y, U, V, density=[0.5, 1])
ax0.set_title("Varying Density")

fp.text_from_label("Figure Title", "Figure 1")

# Varying color along a streamline
ax1 = fp.add_subplot()
strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap="autumn")
fp.fig.colorbar(strm.lines)
ax1.set_title("Varying Color")

# Varying line width along a streamline
ax2 = fp.add_subplot()
lw = 5 * speed / speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color="k", linewidth=lw)
ax2.set_title("Varying Line Width")

# Controlling the starting points of the streamlines
seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]])

ax3 = fp.add_subplot()
strm = ax3.streamplot(
X, Y, U, V, color=U, linewidth=2, cmap="autumn", start_points=seed_points.T
)
fp.fig.colorbar(strm.lines)
ax3.set_title("Controlling Starting Points")

# Displaying the starting points with blue symbols.
ax3.plot(seed_points[0], seed_points[1], "bo")
ax3.set(xlim=(-w, w), ylim=(-w, w))

# Create a mask
mask = np.zeros(U.shape, dtype=bool)
mask[40:60, 40:60] = True
U[:20, :20] = np.nan
U = np.ma.array(U, mask=mask)

ax4 = fp.add_subplot(gs=fp.gs[2:, :])
ax4.streamplot(X, Y, U, V, color="r")
ax4.set_title("Streamplot with Masking")

ax4.imshow(
~mask,
extent=(-w, w, -w, w),
alpha=0.5,
interpolation="nearest",
cmap="gray",
aspect="auto",
)

# this is the next page. Currently starts at [0, 0]

Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X ** 2 + Y
V = 1 + X - Y ** 2

ax3 = fp.add_subplot()
strm = ax3.streamplot(
X, Y, U, V, color=U, linewidth=2, cmap="autumn", start_points=seed_points.T
)
fp.fig.colorbar(strm.lines)
ax3.set_title("Controlling Starting Points")

# Displaying the starting points with blue symbols.
ax3.plot(seed_points[0], seed_points[1], "bo")
ax3.set(xlim=(-w, w), ylim=(-w, w))

# close the figure
fp.close()
print("outfile: " + fp.outfile)

print("--Done!--")


if __name__ == "__main__":
test_main()

0 comments on commit 2e96fa2

Please sign in to comment.