Skip to content

Commit 3f629f7

Browse files
authored
Merge pull request #6 from Jumitti/automatic_padding
Automatic padding
2 parents 3d685d5 + 8964a4c commit 3f629f7

12 files changed

Lines changed: 134 additions & 336 deletions

altair_upset/preprocessing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def preprocess_data(data, sets, abbre, sort_order):
3939
data = pd.melt(data, id_vars=["intersection_id", "count", "degree"])
4040
data = data.rename(columns={"variable": "set", "value": "is_intersect"})
4141

42+
# Create a column of concurrent groups (future update for better labelling)
43+
# sets_mapping = (data.loc[data["is_intersect"] > 0].groupby("intersection_id")["set"]
44+
# .apply(lambda x: " ".join(sorted(x))).to_dict())
45+
#
46+
# data["sets_graph"] = data.apply(
47+
# lambda row: row["set"] if row["is_intersect"] == 0 else sets_mapping.get(row["intersection_id"], ""),
48+
# axis=1).fillna("").astype(str)
49+
4250
if abbre is None:
4351
abbre = sets
4452

altair_upset/upset.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def UpSetAltair(
8989
line_connection_size: int = 1, # Reduced from 2
9090
horizontal_bar_size: int = 20,
9191
vertical_bar_label_size: int = 16,
92-
vertical_bar_padding: int = 20,
9392
theme: Optional[str] = None,
9493
) -> UpSetChart:
9594
"""Generate interactive UpSet plots using Altair. [Lex et al., 2014]_
@@ -138,8 +137,6 @@ def UpSetAltair(
138137
Height of horizontal bars in pixels.
139138
vertical_bar_label_size : int, default 16
140139
Font size of vertical bar labels.
141-
vertical_bar_padding : int, default 20
142-
Padding between vertical bars.
143140
theme : str, optional
144141
Altair theme to use. If None, uses the current default theme.
145142
@@ -214,10 +211,10 @@ def UpSetAltair(
214211
vertical_bar_chart_height = height * height_ratio
215212
matrix_height = (height - vertical_bar_chart_height) * 0.8 # Reduce height to tighten spacing
216213
matrix_width = width - horizontal_bar_chart_width
217-
vertical_bar_size = min(
218-
30,
219-
width / len(data["intersection_id"].unique().tolist()) - vertical_bar_padding,
220-
)
214+
215+
# Automatic padding
216+
num_intersections = max(1, len(data["intersection_id"].unique().tolist()))
217+
vertical_bar_size = min(30, (matrix_width / num_intersections) - 5) # 5 is good
221218

222219
# Setup styles
223220
main_color = "#3A3A3A"
@@ -232,9 +229,9 @@ def UpSetAltair(
232229
field="count" if sort_by == "frequency" else "degree", order=sort_order
233230
)
234231
tooltip = [
235-
alt.Tooltip("max(count):Q", title="Cardinality"),
232+
alt.Tooltip("count:Q", title="Cardinality"),
236233
alt.Tooltip("degree:Q", title="Degree"),
237-
alt.Tooltip("sets:N", title="Sets"),
234+
# alt.Tooltip("sets_graph:N", title="Groups"), # Bugged. sets_graph is already available in preprocessing.py
238235
]
239236

240237
# Create base chart
@@ -299,7 +296,7 @@ def UpSetAltair(
299296
horizontal_bar_axis,
300297
horizontal_bar.properties(width=horizontal_bar_chart_width),
301298
spacing=0, # Minimize spacing between components
302-
).resolve_scale(y="shared"),
299+
).resolve_scale(x="shared", y="shared"), # X shared also
303300
spacing=5,
304301
).add_params(legend_selection)
305302

tests/__snapshots__/test_covid_mutations/test_covid_mutations[vega_spec].json

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@
281281
},
282282
"tooltip": [
283283
{
284-
"aggregate": "max",
285284
"field": "count",
286285
"title": "Cardinality",
287286
"type": "quantitative"
@@ -290,11 +289,6 @@
290289
"field": "degree",
291290
"title": "Degree",
292291
"type": "quantitative"
293-
},
294-
{
295-
"field": "sets",
296-
"title": "Sets",
297-
"type": "nominal"
298292
}
299293
],
300294
"x": {
@@ -465,7 +459,6 @@
465459
},
466460
"tooltip": [
467461
{
468-
"aggregate": "max",
469462
"field": "count",
470463
"title": "Cardinality",
471464
"type": "quantitative"
@@ -474,11 +467,6 @@
474467
"field": "degree",
475468
"title": "Degree",
476469
"type": "quantitative"
477-
},
478-
{
479-
"field": "sets",
480-
"title": "Sets",
481-
"type": "nominal"
482470
}
483471
],
484472
"x": {
@@ -648,7 +636,6 @@
648636
},
649637
"tooltip": [
650638
{
651-
"aggregate": "max",
652639
"field": "count",
653640
"title": "Cardinality",
654641
"type": "quantitative"
@@ -657,11 +644,6 @@
657644
"field": "degree",
658645
"title": "Degree",
659646
"type": "quantitative"
660-
},
661-
{
662-
"field": "sets",
663-
"title": "Sets",
664-
"type": "nominal"
665647
}
666648
],
667649
"x": {
@@ -821,7 +803,6 @@
821803
},
822804
"tooltip": [
823805
{
824-
"aggregate": "max",
825806
"field": "count",
826807
"title": "Cardinality",
827808
"type": "quantitative"
@@ -830,11 +811,6 @@
830811
"field": "degree",
831812
"title": "Degree",
832813
"type": "quantitative"
833-
},
834-
{
835-
"field": "sets",
836-
"title": "Sets",
837-
"type": "nominal"
838814
}
839815
],
840816
"x": {
@@ -993,7 +969,6 @@
993969
},
994970
"tooltip": [
995971
{
996-
"aggregate": "max",
997972
"field": "count",
998973
"title": "Cardinality",
999974
"type": "quantitative"
@@ -1002,11 +977,6 @@
1002977
"field": "degree",
1003978
"title": "Degree",
1004979
"type": "quantitative"
1005-
},
1006-
{
1007-
"field": "sets",
1008-
"title": "Sets",
1009-
"type": "nominal"
1010980
}
1011981
],
1012982
"x": {
@@ -1485,6 +1455,7 @@
14851455
],
14861456
"resolve": {
14871457
"scale": {
1458+
"x": "shared",
14881459
"y": "shared"
14891460
}
14901461
},
@@ -1513,7 +1484,6 @@
15131484
},
15141485
"tooltip": [
15151486
{
1516-
"aggregate": "max",
15171487
"field": "count",
15181488
"title": "Cardinality",
15191489
"type": "quantitative"
@@ -1522,11 +1492,6 @@
15221492
"field": "degree",
15231493
"title": "Degree",
15241494
"type": "quantitative"
1525-
},
1526-
{
1527-
"field": "sets",
1528-
"title": "Sets",
1529-
"type": "nominal"
15301495
}
15311496
],
15321497
"x": {
@@ -1694,7 +1659,6 @@
16941659
},
16951660
"tooltip": [
16961661
{
1697-
"aggregate": "max",
16981662
"field": "count",
16991663
"title": "Cardinality",
17001664
"type": "quantitative"
@@ -1703,11 +1667,6 @@
17031667
"field": "degree",
17041668
"title": "Degree",
17051669
"type": "quantitative"
1706-
},
1707-
{
1708-
"field": "sets",
1709-
"title": "Sets",
1710-
"type": "nominal"
17111670
}
17121671
],
17131672
"x": {

tests/__snapshots__/test_covid_mutations/test_covid_mutations_subset[vega_spec].json

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@
262262
},
263263
"tooltip": [
264264
{
265-
"aggregate": "max",
266265
"field": "count",
267266
"title": "Cardinality",
268267
"type": "quantitative"
@@ -271,11 +270,6 @@
271270
"field": "degree",
272271
"title": "Degree",
273272
"type": "quantitative"
274-
},
275-
{
276-
"field": "sets",
277-
"title": "Sets",
278-
"type": "nominal"
279273
}
280274
],
281275
"x": {
@@ -436,7 +430,6 @@
436430
},
437431
"tooltip": [
438432
{
439-
"aggregate": "max",
440433
"field": "count",
441434
"title": "Cardinality",
442435
"type": "quantitative"
@@ -445,11 +438,6 @@
445438
"field": "degree",
446439
"title": "Degree",
447440
"type": "quantitative"
448-
},
449-
{
450-
"field": "sets",
451-
"title": "Sets",
452-
"type": "nominal"
453441
}
454442
],
455443
"x": {
@@ -609,7 +597,6 @@
609597
},
610598
"tooltip": [
611599
{
612-
"aggregate": "max",
613600
"field": "count",
614601
"title": "Cardinality",
615602
"type": "quantitative"
@@ -618,11 +605,6 @@
618605
"field": "degree",
619606
"title": "Degree",
620607
"type": "quantitative"
621-
},
622-
{
623-
"field": "sets",
624-
"title": "Sets",
625-
"type": "nominal"
626608
}
627609
],
628610
"x": {
@@ -772,7 +754,6 @@
772754
},
773755
"tooltip": [
774756
{
775-
"aggregate": "max",
776757
"field": "count",
777758
"title": "Cardinality",
778759
"type": "quantitative"
@@ -781,11 +762,6 @@
781762
"field": "degree",
782763
"title": "Degree",
783764
"type": "quantitative"
784-
},
785-
{
786-
"field": "sets",
787-
"title": "Sets",
788-
"type": "nominal"
789765
}
790766
],
791767
"x": {
@@ -934,7 +910,6 @@
934910
},
935911
"tooltip": [
936912
{
937-
"aggregate": "max",
938913
"field": "count",
939914
"title": "Cardinality",
940915
"type": "quantitative"
@@ -943,11 +918,6 @@
943918
"field": "degree",
944919
"title": "Degree",
945920
"type": "quantitative"
946-
},
947-
{
948-
"field": "sets",
949-
"title": "Sets",
950-
"type": "nominal"
951921
}
952922
],
953923
"x": {
@@ -1387,6 +1357,7 @@
13871357
],
13881358
"resolve": {
13891359
"scale": {
1360+
"x": "shared",
13901361
"y": "shared"
13911362
}
13921363
},
@@ -1415,7 +1386,6 @@
14151386
},
14161387
"tooltip": [
14171388
{
1418-
"aggregate": "max",
14191389
"field": "count",
14201390
"title": "Cardinality",
14211391
"type": "quantitative"
@@ -1424,11 +1394,6 @@
14241394
"field": "degree",
14251395
"title": "Degree",
14261396
"type": "quantitative"
1427-
},
1428-
{
1429-
"field": "sets",
1430-
"title": "Sets",
1431-
"type": "nominal"
14321397
}
14331398
],
14341399
"x": {
@@ -1586,7 +1551,6 @@
15861551
},
15871552
"tooltip": [
15881553
{
1589-
"aggregate": "max",
15901554
"field": "count",
15911555
"title": "Cardinality",
15921556
"type": "quantitative"
@@ -1595,11 +1559,6 @@
15951559
"field": "degree",
15961560
"title": "Degree",
15971561
"type": "quantitative"
1598-
},
1599-
{
1600-
"field": "sets",
1601-
"title": "Sets",
1602-
"type": "nominal"
16031562
}
16041563
],
16051564
"x": {
-5.94 KB
Loading

0 commit comments

Comments
 (0)