-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_plot.py
More file actions
642 lines (553 loc) · 18.4 KB
/
bubble_plot.py
File metadata and controls
642 lines (553 loc) · 18.4 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
"""
Module to generate the Latex source files for bubble plots.
A plot has three facets, one as the y axis and two on the x axis.
"""
import colorsys
import csv
import os
from itertools import zip_longest
from string import Template
from typing import Dict, NamedTuple, Sequence, Set, Tuple
class Facets(NamedTuple):
"""
Describe the name of the facets of a bubble plot.
Attributes
----------
y: str
Name of the facet on the y axis.
x_left: str
Name of the facet on the left part of the x axis.
x_right: str
Name of the facet on the right part of the x axis.
"""
y: str
x_left: str
x_right: str
class Config(NamedTuple):
"""
Describe custom settings for the bubble plot.
Attributes
----------
x_left_offset: int
Define the offset at which the first x tick near the left of the y axis is placed.
x_right_offset: int
Define the offset at which the first x tick near the right of the y axis is placed.
class_year: str
Label representing the publication year.
field_names: Sequence[str]
Name of the columns for the output file of the bubble plot.
latex_template: str
Path to the Latex template.
output_dir: str
Path where to save the generated files (i.e., CSV & TeX).
color_map: Sequence[Tuple[float, float, float]]
Colour map for years.
"""
x_left_offset: int
x_right_offset: int
class_year: str
field_names: Sequence[str]
latex_template: str
output_dir: str
color_map: Sequence[Tuple[float, float, float]]
class Occurrence:
"""
Compute the occurrence of a bubble.
Attributes
----------
occurrence: int
Occurrence of a bubble.
year: str
Earliest year from entries related to the bubble.
"""
__slots__ = ("occurrence", "year")
def __init__(self, year: str):
"""
Parameters
----------
year: str
Year of the first entry related to the bubble.
"""
self.occurrence: int = 1
self.year: str = year
def update(self, new_year: str):
"""
Update the occurrences and save the earliest publish year.
Parameters
----------
new_year: str
New publish year to update `year` with if it's less than the current year.
"""
self.occurrence += 1
self.year = min(self.year, new_year)
def __str__(self):
return f"Occurrence: {self.occurrence}, Year: {self.year}"
def __repr__(self):
return f"{self.__class__.__name__}{(self.occurrence, self.year)}"
class Bubble(NamedTuple):
"""
Bubble label on the x axis and y axis.
Attributes
----------
label_x: str
Label displayed as a x tick label.
label_y: str
Label displayed as a y tick label.
Notes
-----
The x and y tick label are keys from the Latex
`pgfplots <https://ctan.org/pkg/pgfplots>`_ package.
"""
label_x: str
label_y: str
class SplitXAxis(NamedTuple):
"""
Facet on one side of the splitted x axis.
Attributes
----------
facet: str
Name of the facet.
bubbles: Dict[Bubble, Occurence]
Bubbles related to the facet
"""
facet: str
bubbles: Dict[Bubble, Occurrence]
def update(self, entry: Dict[str, str], year: str, y_axis: str) -> None:
"""
Update the bubble occurrences related to the side of the x axis.
Parameters
----------
entry: Dict[str, str]
Entries to take as input for the occurrence computation.
year: str
Publication year of the entry.
y_axis: str
Name of the facet of the y axis.
"""
try:
label_x = entry[self.facet]
except KeyError as error:
raise KeyError(
f"Unknown facet named: {error} on the x axis"
) from error
try:
label_y = entry[y_axis]
except KeyError as error:
raise KeyError(
f"Unknown facet named: {error} on the y axis"
) from error
bubble = Bubble(label_x=label_x, label_y=label_y)
if bubble in self.bubbles:
self.bubbles[bubble].update(year)
else:
self.bubbles[bubble] = Occurrence(year)
class XAxis(NamedTuple):
"""
Facets of the x axis.
Attributes
----------
left: SplitXAxis
Left facet of a bubble plot.
right: SplitXAxis
Right facet of a bubble plot.
"""
left: SplitXAxis
right: SplitXAxis
class BubblePlot:
"""
Describe a bubble plot with 3 facets.
Attributes
----------
x_axis: XAxis
Facets on the x axis. One to the left of the y axis and the other to the right.
y_axis: str
Facet on the y axis.
"""
__slots__ = ("x_axis", "y_axis")
def __init__(self, facets: Facets):
"""
Parameters
----------
facets: Facets
Describe the facets label of the bubble plot.
"""
y_axis, x_left_axis, x_right_axis = facets
self.x_axis: XAxis = XAxis(
left=SplitXAxis(facet=x_left_axis, bubbles={}),
right=SplitXAxis(facet=x_right_axis, bubbles={}),
)
self.y_axis: str = y_axis
def __str__(self):
"""
Returns
-------
str
The name of the left facet on the x axis,
the name of the facet on the y axis, and
the name of the right facet on the x axis.
"""
return (
f"{self.x_axis.left.facet}_{self.y_axis}_{self.x_axis.right.facet}"
)
def __repr__(self):
return f"{self.__class__.__name__}{(self.x_axis, self.y_axis)}"
def update(self, entry: Dict[str, str], year: str) -> None:
"""
Update the bubbles occurrences of the plot.
Parameters
----------
entry: Dict[str, str]
Label and value to use as input for a bubble.
year: str
Publication year of the entry.
conf: Config
Describe custom settings for the plots.
"""
self.x_axis.left.update(entry, year, self.y_axis)
self.x_axis.right.update(entry, year, self.y_axis)
class CSVWriter(NamedTuple):
"""
Save a bubble plot as a CSV file.
Attributes
----------
plot: BubblePlot
Plot to prepare and format.
years: Set[str]
Publication years.
conf: Config
Describe custom settings for the plots.
"""
plot: BubblePlot
years: Set[str]
conf: Config
def compute_year_score_mapping(self) -> Dict[str, int]:
"""
Compute the mapping between years and colour map scores. The scores are mapped in
chronological order of the years.
Returns
-------
Dict[str, str]
Mapping between a year and a score.
Notes
-----
The colour map score is in the interval [0-1000].
See paragraph ``Colormap Input Format Reference`` in Section 4.7.6 of the
Latex `pgfplots <https://ctan.org/pkg/pgfplots>`_ package.
"""
idx = 0
incr = 1000 // (len(self.years) - 1)
year_score = {}
for year in sorted(self.years):
year_score[year] = idx
idx += incr
return year_score
def compute_labels_indices_mapping(
self,
) -> Tuple[Dict[str, int], Dict[str, int], Dict[str, int]]:
"""
Compute the mapping between labels and their indices.
Returns
-------
Tuple[Dict[str, int], Dict[str, int], Dict[str, int]]
The mapping between:
- the left x tick labels and their indices.
- the right x tick labels and their indices.
- the y tick labels and their indices.
"""
labels_x_left = sorted(
set(b.label_x for b in self.plot.x_axis.left.bubbles)
)
labels_x_left_len = len(labels_x_left)
# From -N to -conf.x_left_offset
x_left_mapping = {
label: -(labels_x_left_len - i + self.conf.x_left_offset)
for i, label in enumerate(labels_x_left)
}
labels_x_right = sorted(
set(b.label_x for b in self.plot.x_axis.right.bubbles)
)
# From conf.x_left_offset to N
x_right_mapping = {
label: i + self.conf.x_right_offset
for i, label in enumerate(labels_x_right)
}
labels_y = sorted(
set(
b.label_y
for b in list(self.plot.x_axis.right.bubbles)
+ list(self.plot.x_axis.left.bubbles)
)
)
# From 0 to N-1
y_mapping = {label: i for i, label in enumerate(labels_y)}
return x_left_mapping, x_right_mapping, y_mapping
def prepared_bubbles_data(
self,
x_left_mapping: Dict[str, int],
x_right_mapping: Dict[str, int],
y_mapping: Dict[str, int],
year_mapping: Dict[str, int],
) -> Sequence[Tuple[int, int, int, int]]:
"""
Prepare bubbles data.
Parameters
----------
x_left_mapping: Dict[str, int]
The left x tick labels and their indices.
x_right_mapping: Dict[str, int]
The right x tick labels and their indices.
y_mapping: Dict[str, int]
The y tick labels and their indices.
year_mapping: Dict[str, int]
The publication years and their score.
Returns
-------
Sequence[Tuple[int, int, int, int]]
Lines of the CSV file representing each bubble.
The columns are, in order:
- The index of the y tick label related to the bubble.
- The index of the x tick label related to the bubble.
- The number of occurrence of the bubble.
- The earliest publication year related to the bubble.
"""
axis_mapping = (
(self.plot.x_axis.left, x_left_mapping),
(self.plot.x_axis.right, x_right_mapping),
)
return [
(
y_mapping[bubble.label_y],
x_mapping[bubble.label_x],
occurence.occurrence,
year_mapping[occurence.year],
)
for axis, x_mapping in axis_mapping
for bubble, occurence in axis.bubbles.items()
]
def write(
self,
data: Sequence[Tuple[int, int, int, int]],
labels_y: Sequence[str],
labels_x: Sequence[str],
) -> None:
"""
Save bubble plot as a CVS file.
Parameters
----------
data: Sequence[Tuple[int, int, int, int]]
Bubble plot data prepared.
labels_y: Sequence[str]
Labels of the facet on the y axis.
labels_x: Sequence[str]
Labels of the facets on the x axis.
"""
# TODO: Ugly
y_indices = []
x_indices = []
occurences = []
year_scores = []
for y_idx, x_idx, occ, y_s in data:
y_indices.append(y_idx)
x_indices.append(x_idx)
occurences.append(occ)
year_scores.append(y_s)
with open(
os.path.join(self.conf.output_dir, f"{self.plot}.csv"), "w"
) as output_file:
writer = csv.writer(output_file)
writer.writerow(self.conf.field_names)
writer.writerows(
zip_longest(
y_indices,
x_indices,
occurences,
year_scores,
labels_y,
labels_x,
)
)
def save_plot(self) -> None:
"""
Compute the mapping between labels and their indices; year and their scores.
Prepared bubble plot data for the CSV file. And write data and
labels into a file.
"""
(
x_left_mapping,
x_right_mapping,
y_mapping,
) = self.compute_labels_indices_mapping()
year_score = self.compute_year_score_mapping()
bubbles_data = self.prepared_bubbles_data(
x_left_mapping, x_right_mapping, y_mapping, year_score
)
sorted_bubbles_data = sorted(bubbles_data, key=lambda t: t[1])
self.write(
sorted_bubbles_data,
list(y_mapping.keys()),
list(x_left_mapping.keys()) + list(x_right_mapping.keys()),
)
def compute_color_map(years_len: int) -> Sequence[Tuple[float, float, float]]:
"""
Affect a distinct (as much as possible) colour to each year.
Parameters
----------
years_len: int
Number of publication years.
Returns
-------
Sequence[Tuple[float, float, float]]
Colours used for bubbles in the plot.
"""
hsv_tuples = [(i * 1.0 / years_len, 0.5, 1) for i in range(years_len)]
return tuple(colorsys.hsv_to_rgb(*hsv) for hsv in hsv_tuples)
class LatexBubblePlotWriter:
"""
Prepare the Latex template for a bubble plot.
Attributes
----------
plot: BubblePlot
Plot to prepare and format.
years: Sequence[str]
Publication years.
conf: Config
Describe custom settings for the plots.
x_indices: Sequence[int]
Indices of bubbles on the x axis.
"""
__slots__ = ("plot", "years", "year_color", "conf", "x_indices")
def __init__(self, plot: BubblePlot, years: Set[str], conf: Config):
"""
Parameters
----------
plot: BubblePlot
Plot to prepare and format.
years: Set[str]
Publication years.
conf: Config
Describe custom settings for the plots.
"""
self.plot: BubblePlot = plot
self.years: Sequence[str] = sorted(years)
self.conf: Config = conf
with open(
os.path.join(self.conf.output_dir, f"{plot}.csv"), "r"
) as plot_data:
reader = csv.DictReader(plot_data)
x_indice_field = conf.field_names[1]
self.x_indices: Sequence[int] = tuple(
int(row[x_indice_field]) for row in reader
)
def prepare_values(
self, color_map: Dict[str, Tuple[float, float, float]]
) -> Dict[str, str]:
"""
Prepare the values filled in the Latex template.
Parameters
----------
color_map: Dict[str, Tuple[float, float, float]]
Mapping between a year and the related colour used for a bubble on the plot.
Returns
-------
Dict[str, str]
Values to fill the Latex template with.
"""
delete_parenthesis = {ord(c): None for c in "[]"}
return {
"defineColorsYear": "\n".join(
(
f"\\definecolor{{{year}}}{{rgb}}"
f"{{{str(color_map[year]).translate(delete_parenthesis)}}}"
)
for year in self.years
),
"setColorsYear": "\n ".join(
f"color=({year})," for year in self.years
),
"xMin": str(min(map(int, self.x_indices))),
"xMax": str(max(map(int, self.x_indices))),
"yLabel": self.plot.y_axis,
"meta": self.conf.field_names[2],
"xField": self.conf.field_names[5],
"xIndexField": self.conf.field_names[1],
"yField": self.conf.field_names[4],
"yIndexField": self.conf.field_names[0],
"yearField": self.conf.field_names[3],
"xLeftLabel": self.plot.x_axis.left.facet,
"xRightLabel": self.plot.x_axis.right.facet,
"CSVDataFile": f"{self.plot}.csv",
"colorsYear": ", ".join(self.years),
}
def write(self, template_values: Dict[str, str]) -> None:
"""
Fill the Latex template with the bubble plot specific values.
Parameters
----------
template_values: Dict[str, str]
Values to fill the Latex template with.
"""
with open(self.conf.latex_template, "r") as latex_template:
template = Template(latex_template.read())
content = template.substitute(template_values)
with open(
os.path.join(self.conf.output_dir, f"{self.plot}.tex"), "w"
) as latex_output:
latex_output.write(content)
def save_plot(self) -> None:
"""Prepare the bubble plot specific values and fill the Latex template with them."""
years_len = len(self.years)
if years_len <= len(self.conf.color_map):
rgb_colours = self.conf.color_map[:years_len]
else:
rgb_colours = compute_color_map(years_len)
year_color = dict(zip(self.years, rgb_colours))
template_values = self.prepare_values(year_color)
self.write(template_values)
def compute_occurences_from(
entries: Sequence[Dict[str, str]], plot_plan: Facets, conf: Config
) -> Tuple[BubblePlot, Set[str]]:
"""
Compute the occurrences of each value for the given labels.
Parameters
----------
entries: Sequence[Dict[str, str]]
Labels and values to use as input for the bubble plot.
plot_plan: Facets
Describe the labels used for the facets of the plot.
conf: Config
Describe custom settings for the plots.
Returns
-------
Tuple[BubblePlot, Set[str]]
- The initialised bubble plot described by `plot_plan`.
- The years related to each entry.
"""
years = set()
plot = BubblePlot(plot_plan)
for entry in entries:
year = entry[conf.class_year]
plot.update(entry, year)
years.add(year)
return plot, years
def build_and_save_plots(
entries: Sequence[Dict[str, str]],
plot_plans: Sequence[Facets],
conf: Config,
) -> None:
"""
Build and save bubble plots as CSV files.
Parameters
----------
entries: Sequence[Dict[str, str]]
Labels and values to use as input for the bubble plot.
plot_plan: Facets
Describe the labels used for the facets of the plot.
conf: Config
Describe custom settings for the plots.
"""
for plot_plan in plot_plans:
plot, years = compute_occurences_from(entries, plot_plan, conf)
writer_csv = CSVWriter(plot, years, conf)
writer_csv.save_plot()
writer_latex = LatexBubblePlotWriter(plot, years, conf)
writer_latex.save_plot()