-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlabel.py
More file actions
304 lines (266 loc) · 9.49 KB
/
label.py
File metadata and controls
304 lines (266 loc) · 9.49 KB
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
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
# pylint: disable=C0103
"""
Label Item builder
------------------
This module provides a set of factory functions to simplify the creation
of label items.
"""
# Note: when adding method to builder classes, please do not forget to update the
# documentation (see builder.rst file). Because of class inheritance, the methods
# are not automatically documented (otherwise, they would be sorted alphabetically,
# due to a limitation of sphinx auto-doc).
from __future__ import annotations
from collections.abc import Callable
from plotpy.config import CONF, _, make_title
from plotpy.items import (
CurveItem,
DataInfoLabel,
ImageItem,
LabelItem,
LegendBoxItem,
RangeComputation,
RangeComputation2d,
RangeInfo,
RectangleShape,
SelectedLegendBoxItem,
XRangeSelection,
)
from plotpy.styles import LabelParam, LabelParamWithContents, LegendParam
# default offset positions for anchors
ANCHOR_OFFSETS = {
"TL": (5, 5),
"TR": (-5, 5),
"BL": (5, -5),
"BR": (-5, -5),
"L": (5, 0),
"R": (-5, 0),
"T": (0, 5),
"B": (0, -5),
}
LABEL_COUNT = 0
class LabelBuilder:
"""Class regrouping a set of factory functions to simplify the creation
of label items."""
def label(
self,
text: str,
g: tuple[float, float] | str,
c: tuple[int, int],
anchor: str,
title: str = "",
) -> LabelItem:
"""Make a label `plot item`
Args:
text: label text
g: position in plot coordinates or relative position (string)
c: position in canvas coordinates
anchor: anchor position in relative position (string)
title: label title. Default is ''
Returns:
:py:class:`.LabelItem` object
Examples::
make.label("Relative position", (x[0], y[0]), (10, 10), "BR")
make.label("Absolute position", "R", (0,0), "R")
"""
basename = _("Label")
param = LabelParamWithContents(basename, icon="label.png")
param.read_config(CONF, "plot", "label")
if title:
param.label = title
else:
global LABEL_COUNT
LABEL_COUNT += 1
param.label = make_title(basename, LABEL_COUNT)
if isinstance(g, tuple):
param.abspos = False
param.xg, param.yg = g
else:
param.abspos = True
param.absg = g
if c is None:
c = ANCHOR_OFFSETS[anchor]
param.xc, param.yc = c
param.anchor = anchor
return LabelItem(text, param)
def legend(
self,
anchor: str = "TR",
c: tuple[int, int] | None = None,
restrict_items: list | None = None,
) -> LegendBoxItem | SelectedLegendBoxItem:
"""Make a legend `plot item`
Args:
anchor: legend position in relative position (string)
c: position in canvas coordinates
restrict_items: list of items to be shown in legend box.
Default is None. If None, all items are shown in legend box.
If [], no item is shown in legend box. If [item1, item2],
item1, item2 are shown in legend box.
Returns:
:py:class:`.LegendBoxItem` or
:py:class:`.SelectedLegendBoxItem` object
"""
param = LegendParam(_("Legend"), icon="legend.png")
param.read_config(CONF, "plot", "legend")
param.abspos = True
param.absg = anchor
param.anchor = anchor
if c is None:
c = ANCHOR_OFFSETS[anchor]
param.xc, param.yc = c
if restrict_items is None:
return LegendBoxItem(param)
else:
return SelectedLegendBoxItem(param, restrict_items)
def info_label(
self, anchor: str, comps: list, title: str | None = None
) -> DataInfoLabel:
"""Make an info label `plot item`
Args:
anchor: anchor position. See :py:class:`.LabelParam` for details
comps: list of :py:class:`.label.RangeComputation` objects
title: label name. Default is None
Returns:
:py:class:`.DataInfoLabel` object
"""
basename = _("Computation")
param = LabelParam(basename, icon="label.png")
param.read_config(CONF, "plot", "info_label")
if title is not None:
param.label = title
else:
global LABEL_COUNT
LABEL_COUNT += 1
param.label = make_title(basename, LABEL_COUNT)
param.abspos = True
param.absg = anchor
param.anchor = anchor
c = ANCHOR_OFFSETS[anchor]
param.xc, param.yc = c
return DataInfoLabel(param, comps)
def range_info_label(
self,
range: XRangeSelection,
anchor: str,
label: str,
function: Callable = None,
title: str | None = None,
) -> DataInfoLabel:
"""Make an info label `plot item` showing an XRangeSelection object infos
Args:
range: range selection object
anchor: anchor position. See :py:class:`.LabelParam` for details
label: label name. See :py:class:`.DataInfoLabel` for details
function: function to apply to the range selection object
Default is None (default function is `lambda x, dx: (x, dx)`)
title: label name. Default is None
Returns:
:py:class:`.DataInfoLabel` object
Example::
x = linspace(-10, 10, 10)
y = sin(sin(sin(x)))
range = make.range(-2, 2)
disp = make.range_info_label(range, 'BL', "x = %.1f ± %.1f cm",
lambda x, dx: (x, dx))
"""
info = RangeInfo(label, range, function)
return self.info_label(anchor, info, title=title)
def computation(
self,
range: XRangeSelection,
anchor: str,
label: str,
curve: CurveItem,
function: Callable,
title: str | None = None,
) -> DataInfoLabel:
"""Make a computation label `plot item`
Args:
range: range selection object
anchor: anchor position. See :py:class:`.LabelParam` for details
label: label name. See :py:class:`.DataInfoLabel` for details
curve: curve item
function: function to apply to the range selection object
Default is None (default function is `lambda x, dx: (x, dx)`)
Returns:
:py:class:`.DataInfoLabel` object
"""
if title is None:
title = curve.param.label
return self.computations(range, anchor, [(curve, label, function)], title=title)
def computations(
self, range: XRangeSelection, anchor: str, specs: list, title: str | None = None
) -> DataInfoLabel:
"""Make computation labels `plot item`
Args:
range: range selection object
anchor: anchor position. See :py:class:`.LabelParam` for details
specs: list of (curve, label, function) tuples
title: label name. Default is None
Returns:
Data info label object
"""
comps = []
same_curve = True
curve0 = None
for curve, label, function in specs:
comp = RangeComputation(label, curve, range, function)
comps.append(comp)
if curve0 is None:
curve0 = curve
same_curve = same_curve and curve is curve0
if title is None and same_curve:
title = curve.param.label
return self.info_label(anchor, comps, title=title)
def computation2d(
self,
rect: RectangleShape,
anchor: str,
label: str,
image: ImageItem,
function: Callable,
title: str | None = None,
) -> DataInfoLabel:
"""Make a 2D computation label `plot item`
Args:
rect: rectangle selection object
anchor: anchor position. See :py:class:`.LabelParam` for details
label: label name. See :py:class:`.DataInfoLabel` for details
image: image item
function: function to apply to the rectangle selection object
Default is None (default function is `lambda x, dx: (x, dx)`)
title: label name. Default is None
Returns:
Data info label object
"""
return self.computations2d(
rect, anchor, [(image, label, function)], title=title
)
def computations2d(
self, rect: RectangleShape, anchor: str, specs: list, title: str | None = None
) -> DataInfoLabel:
"""Make 2D computation labels `plot item`
Args:
rect: rectangle selection object
anchor: anchor position. See :py:class:`.LabelParam` for details
specs: list of (image, label, function) tuples
title: label name. Default is None
Returns:
Data info label object
"""
comps = []
same_image = True
image0 = None
for image, label, function in specs:
comp = RangeComputation2d(label, image, rect, function)
comps.append(comp)
if image0 is None:
image0 = image
same_image = same_image and image is image0
if title is None and same_image:
title = image.param.label
return self.info_label(anchor, comps, title=title)