chrisjsewell / ipypublish
1
#!/usr/bin/env python
2 3
"""
3
Some setup for improved latex/pdf output
4

5
at top of workbook, use:
6

7
.. code:: python
8

9
    from ipynb_latex_setup import *
10

11
"""
12 3
from __future__ import division as _division
13

14
# Py2/Py3 compatibility
15
# =====================
16 3
from __future__ import print_function as _print_function
17

18 3
import warnings
19

20 3
warnings.warn(
21
    "this approach is now deprecated, "
22
    "instead please use\n`from ipypublish.scripts import nb_setup`\n"
23
    "then use the individual functions it provides to setup matplotlib, etc",
24
    DeprecationWarning,
25
    stacklevel=2,
26
)
27

28

29
# PYTHON
30
# =======
31

32
# IPYTHON
33
# =======
34 3
try:
35 3
    from IPython import get_ipython
36 3
    from IPython.display import Image, Latex
37 3
    from IPython.display import set_matplotlib_formats
38

39 3
    _ipy_present = True
40 0
except ImportError:
41 0
    _ipy_present = False
42 3
if _ipy_present:
43 3
    ipython = get_ipython()
44 3
    if ipython is not None:
45 0
        try:
46 0
            ipython.magic("config InlineBackend.figure_format = 'svg'")
47 0
            ipython.magic("matplotlib inline")
48 0
            set_matplotlib_formats("pdf", "svg")
49 0
        except Exception:
50 0
            pass
51

52
# NUMPY
53
# =====
54 3
try:
55 3
    import numpy as np
56 3
except ImportError:
57 3
    pass
58

59
# MATPLOTLIB
60
# ===========
61 3
try:
62 3
    import matplotlib as mpl
63

64 0
    _mpl_present = True
65 3
except ImportError:
66 3
    _mpl_present = False
67

68 3
if _mpl_present:
69 0
    mpl.rcParams["savefig.dpi"] = 75
70 0
    mpl.rcParams["figure.figsize"] = (7, 4)
71 0
    mpl.rcParams["figure.autolayout"] = False
72 0
    mpl.rcParams["axes.labelsize"] = 18
73 0
    mpl.rcParams["axes.titlesize"] = 20
74 0
    mpl.rcParams["font.size"] = 16
75 0
    mpl.rcParams["lines.linewidth"] = 2.0
76 0
    mpl.rcParams["lines.markersize"] = 8
77 0
    mpl.rcParams["legend.fontsize"] = 14
78 0
    mpl.rcParams["text.usetex"] = True
79 0
    mpl.rcParams["font.family"] = "serif"
80 0
    mpl.rcParams["font.serif"] = "cm"
81 0
    mpl.rcParams["text.latex.preamble"] = r"\usepackage{subdepth}, \usepackage{type1cm}"
82

83
# PANDAS
84
# ======
85 3
try:
86 3
    import pandas as pd
87

88 0
    _pandas_present = True
89 3
except ImportError:
90 3
    _pandas_present = False
91

92 3
if _pandas_present and ipython:
93 0
    pd.set_option("display.latex.repr", True)
94 0
    pd.set_option("display.latex.longtable", False)
95 0
    pd.set_option("display.latex.escape", True)
96

97
# SYMPY
98
# =====
99 3
try:
100 3
    import sympy as sym
101

102 0
    _sympy_present = True
103 3
except ImportError:
104 3
    _sympy_present = False
105 3
if _sympy_present:
106 0
    sym.init_printing(use_latex=True)
107

108
# IMAGE ARRANGEMENT with PIL
109
# ==========================
110 3
try:
111 3
    from PIL import Image as PImage
112

113 3
    _pil_present = True
114 0
except ImportError:
115 0
    _pil_present = False
116 3
if _pil_present:
117

118 3
    def create_test_image(size=(50, 50)):
119 3
        from io import BytesIO
120

121 3
        file = BytesIO()
122 3
        image = PImage.new("RGBA", size=size, color=(155, 0, 0))
123 3
        image.save(file, "png")
124 3
        file.name = "test.png"
125 3
        file.seek(0)
126 3
        return file
127

128 3
    def images_read(paths):
129
        """read a list of image paths to a list of PIL.IMAGE instances """
130 3
        return [PImage.open(i).convert("RGBA") for i in paths]
131

132 3
    def images_hconcat(images, width=700, height=700, gap=0, aspaths=True):
133
        """concatenate multiple images horizontally
134

135
        Parameters
136
        ----------
137
        images : list
138
            if aspaths=True, list of path strings, else list of PIL.Image instances
139
        width : int or list[int]
140
            maximum width of final image, or of individual images
141
        height : int or list[int]
142
            maximum height of final image, or of individual images
143
        gap : int
144
            size of space between images
145

146
        Returns
147
        -------
148
        image : PIL.Image
149

150
        Examples
151
        --------
152
        >>> img_path = create_test_image(size=(50,50))
153
        >>> img = images_hconcat([img_path,img_path])
154
        >>> img.size
155
        (100, 50)
156

157
        >>> img_path = create_test_image(size=(50,50))
158
        >>> img = images_hconcat([img_path,img_path],width=40,height=40)
159
        >>> img.size
160
        (40, 20)
161

162
        >>> img_path = create_test_image(size=(50,50))
163
        >>> img = images_hconcat([img_path,img_path],width=[40,30])
164
        >>> img.size
165
        (70, 40)
166

167
        >>> img_path = create_test_image(size=(50,50))
168
        >>> img = images_hconcat([img_path,img_path],gap=10)
169
        >>> img.size
170
        (110, 50)
171

172
        """
173 3
        images = images_read(images) if aspaths else images
174 3
        if not isinstance(width, list):
175 3
            widths = [width for _ in images]
176
        else:
177 3
            widths = width[:]
178 3
            width = sum(widths) + gap * (len(images) - 1)
179 3
        if not isinstance(height, list):
180 3
            heights = [height for _ in images]
181
        else:
182 0
            heights = height[:]
183 0
            height = sum(heights)
184 3
        for im, w, h in zip(images, widths, heights):
185 3
            im.thumbnail((w, h), PImage.ANTIALIAS)
186 3
        widths, heights = zip(*(i.size for i in images))
187 3
        total_width = sum(widths) + gap * (len(images) - 1)
188 3
        max_height = max(heights)
189 3
        new_im = PImage.new("RGBA", (total_width, max_height))
190 3
        x_offset = 0
191 3
        for im in images:
192 3
            new_im.paste(im, (x_offset, 0), mask=im)
193 3
            x_offset += im.size[0] + gap
194 3
        new_im.thumbnail((width, height), PImage.ANTIALIAS)
195 3
        return new_im
196

197 3
    def images_vconcat(images, width=700, height=700, gap=0, aspaths=True):
198
        """concatenate multiple images vertically
199

200
        Parameters
201
        ----------
202
        images : list
203
            if aspaths=True, list of path strings, else list of PIL.Image instances
204
        width : int or list[int]
205
            maximum width of final image, or of individual images
206
        height : int or list[int]
207
            maximum height of final image, or of individual images
208
        gap : int
209
            size of space between images
210

211
        Returns
212
        -------
213
        image : PIL.Image
214

215
        Examples
216
        --------
217
        >>> img_path = create_test_image(size=(50,50))
218
        >>> img = images_vconcat([img_path,img_path])
219
        >>> img.size
220
        (50, 100)
221

222
        >>> img_path = create_test_image(size=(50,50))
223
        >>> img = images_vconcat([img_path,img_path],width=40,height=40)
224
        >>> img.size
225
        (20, 40)
226

227
        >>> img_path = create_test_image(size=(50,50))
228
        >>> img = images_vconcat([img_path,img_path],width=[40,30])
229
        >>> img.size
230
        (40, 70)
231

232
        >>> img_path = create_test_image(size=(50,50))
233
        >>> img = images_vconcat([img_path,img_path],gap=10)
234
        >>> img.size
235
        (50, 110)
236

237

238
        """
239 3
        images = images_read(images) if aspaths else images
240 3
        if not isinstance(width, list):
241 3
            widths = [width for _ in images]
242
        else:
243 3
            widths = width[:]
244 3
            width = sum(widths)
245 3
        if not isinstance(height, list):
246 3
            heights = [height for _ in images]
247
        else:
248 0
            heights = height[:]
249 0
            height = sum(heights) + gap * (len(images) - 1)
250 3
        for im, w, h in zip(images, widths, heights):
251 3
            im.thumbnail((w, h), PImage.ANTIALIAS)
252 3
        widths, heights = zip(*(i.size for i in images))
253 3
        max_width = max(widths)
254 3
        total_height = sum(heights) + gap * (len(images) - 1)
255 3
        new_im = PImage.new("RGBA", (max_width, total_height))
256 3
        y_offset = 0
257 3
        for im in images:
258 3
            new_im.paste(im, (0, y_offset), mask=im)
259 3
            y_offset += im.size[1] + gap
260 3
        new_im.thumbnail((width, height), PImage.ANTIALIAS)
261 3
        return new_im
262

263 3
    def images_gridconcat(
264
        pathslist, width=700, height=700, aspaths=True, hgap=0, vgap=0
265
    ):
266
        """concatenate multiple images in a grid
267

268
        Parameters
269
        ----------
270
        pathslist : list[list]
271
            if aspaths=True, list of path strings, else list of PIL.Image instances
272
            each sub list constitutes a row
273
        width : int
274
            maximum width of final image
275
        height : int
276
            maximum height of final image
277
        hgap : int
278
            size of horizontal space between images
279
        vgap : int
280
            size of vertical space between images
281

282
        Returns
283
        -------
284
        image : PIL.Image
285

286
        """
287 0
        himages = [
288
            images_hconcat(paths, gap=hgap, aspaths=aspaths) for paths in pathslist
289
        ]
290 0
        new_im = images_vconcat(himages, gap=vgap, aspaths=False)
291 0
        new_im.thumbnail((width, height), PImage.ANTIALIAS)
292 0
        return new_im
293

294

295
# JSONEXTENDED
296
# ==========================
297 3
try:
298 3
    from jsonextended import ejson, edict
299

300 3
    _jsonextended_present = True
301 0
except ImportError:
302 0
    _jsonextended_present = False
303 3
if _jsonextended_present:
304 3
    from jsonextended import plugins as eplugins
305

306
    # eplugins.load_builtin_plugins()

Read our documentation on viewing source code .

Loading