File: plots.py

package info (click to toggle)
taskflow 3.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 45,948 kB
  • sloc: cpp: 39,058; xml: 35,572; python: 12,935; javascript: 1,732; makefile: 59; sh: 16
file content (328 lines) | stat: -rw-r--r-- 15,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#
#   This file is part of m.css.
#
#   Copyright © 2017, 2018, 2019, 2020, 2021, 2022, 2023
#             Vladimír Vondruš <mosra@centrum.cz>
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#

import re

from docutils import nodes, utils
from docutils.parsers import rst
from docutils.parsers.rst import directives
from docutils.parsers.rst.roles import set_classes

import matplotlib as mpl
mpl.use('Agg') # otherwise it will attempt to use X11
import matplotlib.pyplot as plt
import numpy as np
import io

mpl.rcParams['font.size'] = '11'
mpl.rcParams['axes.titlesize'] = '13'

# Plot background. Replaced with .m-plot .m-background later, equivalent to
# --default-filled-background-color
mpl.rcParams['axes.facecolor'] = '#cafe01'

# All of these should match --color, replaced with .m-plot .m-text
mpl.rcParams['text.color'] = '#cafe02'
mpl.rcParams['axes.labelcolor'] = '#cafe02'
mpl.rcParams['xtick.color'] = '#cafe02'
mpl.rcParams['ytick.color'] = '#cafe02'

# no need to have a border around the plot
mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False

mpl.rcParams['svg.fonttype'] = 'none' # otherwise it renders text to paths
mpl.rcParams['figure.autolayout'] = True # so it relayouts everything to fit

# Gets increased for every graph on a page to (hopefully) ensure unique SVG
# IDs. The parameter is a number but "Support for setting an rcParam that
# expects a str value to a non-str value is deprecated since 3.5 and support
# will be removed two minor releases later", so we pass a string.
mpl.rcParams['svg.hashsalt'] = '0'

# Color codes for bars. Keep in sync with latex2svgextra.
style_mapping = {
    'default': '#cafe03',
    'primary': '#cafe04',
    'success': '#cafe05',
    'warning': '#cafe06',
    'danger': '#cafe07',
    'info': '#cafe08',
    'dim': '#cafe09'
}

# Patch to remove preamble and hardcoded sizes. Matplotlib 2.2 has a http URL
# while matplotlib 3 has a https URL, check for both. Matplotlib 3.3 has a new
# <metadata> field (which we're not interested in) and slightly different
# formatting of the global style after (which we unify to the compact version).
# Matplotlib 3.4 drops the "Created with" comment, as that's in the <metadata>
# already anyway. Matplotlib 3.5 changes order of the <svg> attributes (which
# we ignore anyway, so those aren't matched anymore), order of stroke CSS
# properties and whitespace/semicolons (which we preserve for consistency with
# the rest of the output).
_patch_src = re.compile(r"""<\?xml version="1\.0" encoding="utf-8" standalone="no"\?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1\.1//EN"
  "http://www\.w3\.org/Graphics/SVG/1\.1/DTD/svg11\.dtd">(
<!-- Created with matplotlib \(https?://matplotlib.org/\) -->)?
<svg [^>]+(?P<viewBox>viewBox="0 0 \d+ \d+(\.\d+)?")[^>]+>(
 <metadata>.+</metadata>)?
 <defs>
  <style type="text/css">
?\*{(?P<style>stroke-linecap:butt;stroke-linejoin:round;|stroke-linejoin: round; stroke-linecap: butt)}(
  )?</style>
 </defs>
""", re.DOTALL)
_patch_dst = r"""<svg \g<viewBox>>
 <defs>
  <style type="text/css">*{\g<style>}</style>
 </defs>
"""

# Remove needless newlines and trailing space in path data
_path_patch_src = re.compile('(?P<prev>[\\dz]) ?\n(?P<next>[LMz])', re.MULTILINE)
_path_patch_dst = '\\g<prev> \\g<next>'
_path_patch2_src = re.compile(' ?\n"')
_path_patch2_dst = '"'

# Mapping from color codes to CSS classes. Matplotlib 3.5 added quite a lot of
# spacing to the style attributes compared to previous versions (and dropped
# trailing semicolons), so there's a lot of ` ?` and ` ;` to cover both
# variants.
_class_mapping = [
    # Graph background
    (re.compile('style="fill: ?#cafe01;?"'), 'class="m-background"'),

    # Tick <path> definition in <defs>
    (re.compile('style="stroke: ?#cafe02; ?stroke-width: ?0.8;?"'), 'class="m-line"'),
    # <use>, everything is defined in <defs>, no need to repeat. Matplotlib 3.5
    # has style at the end of the tag, so preserving whatever is in between.
    (re.compile('<use([^>]*) style="fill: ?#cafe02; ?stroke: ?#cafe02; ?stroke-width: ?0.8;?"'), '<use\\1'),

    # Text styles have `font-stretch:normal;` added in matplotlib 3.3;
    # 3.5 changes all font properties to just a single `font:` declaration.

    # Label text on left
    (re.compile('style="fill: ?#cafe02;(font-family:[^;]+;font-size:11px;(font-stretch:normal;)?font-style:normal;font-weight:normal;| font: 11px \'[^\']+\')"'), 'class="m-label"'),
    # Label text on bottom (has extra style params)
    (re.compile('style="fill: ?#cafe02;(font-family:[^;]+;font-size:11px;(font-stretch:normal;)?font-style:normal;font-weight:normal;| font: 11px \'[^\']+\'; )'), 'class="m-label" style="'),
    # Secondary label text
    (re.compile('style="fill: ?#cafe0b;(font-family:[^;]+;font-size:11px;(font-stretch:normal;)?font-style:normal;font-weight:normal;| font: 11px \'[^\']+\')"'), 'class="m-label m-dim"'),
    # Title text (has extra style params)
    (re.compile('style="fill: ?#cafe02;(font-family:[^;]+;font-size:13px;(font-stretch:normal;)?font-style:normal;font-weight:normal;| font: 13px \'[^\']+\'; )'), 'class="m-title" style="'),

    # Bar colors. Keep in sync with latex2svgextra.
    (re.compile('style="fill: ?#cafe03;?"'), 'class="m-bar m-default"'),
    (re.compile('style="fill: ?#cafe04;?"'), 'class="m-bar m-primary"'),
    (re.compile('style="fill: ?#cafe05;?"'), 'class="m-bar m-success"'),
    (re.compile('style="fill: ?#cafe06;?"'), 'class="m-bar m-warning"'),
    (re.compile('style="fill: ?#cafe07;?"'), 'class="m-bar m-danger"'),
    (re.compile('style="fill: ?#cafe08;?"'), 'class="m-bar m-info"'),
    (re.compile('style="fill: ?#cafe09;?"'), 'class="m-bar m-dim"'),

    # Error bar line
    (re.compile('style="fill: ?none; ?stroke: ?#cafe0a; ?stroke-width: ?1.5;?"'), 'class="m-error"'),
    # Error bar <path> definition in <defs>
    (re.compile('style="stroke: ?#cafe0a;?"'), 'class="m-error"'),
    # <use>, everything is defined in <defs>, no need to repeat
    (re.compile('<use([^>]*) style="fill: ?#cafe0a; ?stroke: ?#cafe0a;?"'), '<use\\1'),
]

# Titles for bars
_bar_titles_src = '<g id="plot{}-value{}-{}">'
_bar_titles_dst = '<g id="plot{}-value{}-{}"><title>{} {}</title>'
_bar_titles_dst_error = '<g id="plot{}-value{}-{}"><title>{} ± {} {}</title>'

class Plot(rst.Directive):
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {'class': directives.class_option,
                   'name': directives.unchanged,
                   'type': directives.unchanged_required,
                   'labels': directives.unchanged_required,
                   'labels-extra': directives.unchanged,
                   'units': directives.unchanged_required,
                   'values': directives.unchanged_required,
                   'errors': directives.unchanged,
                   'colors': directives.unchanged,
                   'plot-width': directives.unchanged,
                   'bar-height': directives.unchanged,
                   # Legacy options with ugly underscores instead of dashes
                   'labels_extra': directives.unchanged,
                   'bar_height': directives.unchanged}
    has_content = False

    def run(self):
        set_classes(self.options)

        # Type
        assert self.options['type'] == 'barh'

        # Graph title and axis labels. Value labels are one per line.
        title = self.arguments[0]
        units = self.options['units']
        labels = self.options['labels'].split('\n')

        # Legacy options, convert underscores to dashes
        if 'labels_extra' in self.options:
            self.options['labels-extra'] = self.options['labels_extra']
            del self.options['labels_extra']
        if 'bar_height' in self.options:
            self.options['bar-height'] = self.options['bar_height']
            del self.options['bar_height']

        # Optional extra labels
        if 'labels-extra' in self.options:
            labels_extra = self.options['labels-extra'].split('\n')
            assert len(labels_extra) == len(labels)
        else:
            labels_extra = None

        # Values. Should be one for each label, if there are multiple lines
        # then the values get stacked.
        value_sets = []
        for row in self.options['values'].split('\n'):
            values = [float(v) for v in row.split()]
            assert len(values) == len(labels)
            value_sets += [values]

        # Optional errors
        if 'errors' in self.options:
            error_sets = []
            for row in self.options['errors'].split('\n'):
                errors = [float(e) for e in row.split()]
                assert len(errors) == len(values)
                error_sets += [errors]
            assert len(error_sets) == len(value_sets)
        else:
            error_sets = [None]*len(value_sets)

        # Colors. Should be either one for all or one for every value
        if 'colors' in self.options:
            color_sets = []
            for row in self.options['colors'].split('\n'):
                colors = [style_mapping[c] for c in row.split()]
                if len(colors) == 1: colors = colors[0]
                else: assert len(colors) == len(labels)
                color_sets += [colors]
            assert len(color_sets) == len(value_sets)
        else:
            color_sets = [style_mapping['default']]*len(value_sets)

        # Bar height
        bar_height = float(self.options.get('bar-height', '0.4'))

        # Increase hashsalt for every plot to ensure (hopefully) unique SVG
        # IDs. The parameter is a number but "Support for setting an rcParam
        # that expects a str value to a non-str value is deprecated since 3.5
        # and support will be removed two minor releases later", so we convert
        # back to a string.
        mpl.rcParams['svg.hashsalt'] = str(int(mpl.rcParams['svg.hashsalt']) + 1)

        # Setup the graph
        fig, ax = plt.subplots()
        # TODO: let matplotlib calculate the height somehow
        fig.set_size_inches(float(self.options.get('plot-width', 8)), 0.78 + len(labels)*bar_height)
        yticks = np.arange(len(labels))
        left = np.array([0.0]*len(labels))
        for i in range(len(value_sets)):
            plot = ax.barh(yticks, value_sets[i], xerr=error_sets[i],
                           align='center', color=color_sets[i], ecolor='#cafe0a', capsize=5*bar_height/0.4, left=left)
            left += np.array(value_sets[i])
            for j, v in enumerate(plot):
                v.set_gid('plot{}-value{}-{}'.format(mpl.rcParams['svg.hashsalt'], i, j))
        ax.set_yticks(yticks)
        ax.invert_yaxis() # top-to-bottom
        ax.set_xlabel(units)
        ax.set_title(title)

        # Value labels. If extra label is specified, create two multiline texts
        # with first having the second line empty and second having the first
        # line empty.
        if labels_extra:
            ax.set_yticklabels([y + ('' if labels_extra[i] == '..' else '\n') for i, y in enumerate(labels)])
            for i, label in enumerate(ax.get_yticklabels()):
                if labels_extra[i] == '..': continue
                ax.text(0, i + 0.05, '\n' + labels_extra[i],
                        va='center', ha='right',
                        transform=label.get_transform(), color='#cafe0b')
        else: ax.set_yticklabels(labels)

        # Export to SVG
        fig.patch.set_visible(False) # hide the white background
        imgdata = io.StringIO()
        fig.savefig(imgdata, format='svg')
        plt.close() # otherwise it consumes a lot of memory in autoreload mode

        # Patch the rendered output: remove preable and hardcoded size
        imgdata = _patch_src.sub(_patch_dst, imgdata.getvalue())
        # Remove needless newlines and trailing whitespace in path data
        imgdata = _path_patch2_src.sub(_path_patch2_dst, _path_patch_src.sub(_path_patch_dst, imgdata))
        # Replace color codes with CSS classes
        for src, dst in _class_mapping: imgdata = src.sub(dst, imgdata)
        # Add titles for bars
        for i in range(len(value_sets)):
            for j in range(len(labels)):
                id = i*len(labels) + j
                if error_sets[i]: imgdata = imgdata.replace(
                    _bar_titles_src.format(mpl.rcParams['svg.hashsalt'], i, j),
                    _bar_titles_dst_error.format(mpl.rcParams['svg.hashsalt'], i, j, value_sets[i][j], error_sets[i][j], units))
                else: imgdata = imgdata.replace(
                    _bar_titles_src.format(mpl.rcParams['svg.hashsalt'], i, j),
                    _bar_titles_dst.format(mpl.rcParams['svg.hashsalt'], i, j, value_sets[i][j], units))

        container = nodes.container(**self.options)
        container['classes'] += ['m-plot']
        node = nodes.raw('', imgdata, format='html')
        container.append(node)
        return [container]

def new_page(*args, **kwargs):
    # The parameter is a number but "Support for setting an rcParam that
    # expects a str value to a non-str value is deprecated since 3.5 and
    # support will be removed two minor releases later", so we pass a string.
    mpl.rcParams['svg.hashsalt'] = '0'

def register_mcss(mcss_settings, hooks_pre_page, **kwargs):
    font = mcss_settings.get('M_PLOTS_FONT', 'Source Sans Pro')
    mpl.rcParams['font.family'] = font

    hooks_pre_page += [new_page]

    rst.directives.register_directive('plot', Plot)

# Below is only Pelican-specific functionality. If Pelican is not found, these
# do nothing.

def _pelican_configure(pelicanobj):
    register_mcss(mcss_settings=pelicanobj.settings, hooks_pre_page=[])

def register(): # for Pelican
    from pelican import signals

    signals.initialized.connect(_pelican_configure)
    signals.content_object_init.connect(new_page)