tree-annotated-plot 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tree_annotated_plot/_plot.py +119 -67
- {tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/METADATA +1 -1
- {tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/RECORD +6 -6
- {tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/WHEEL +0 -0
- {tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/entry_points.txt +0 -0
- {tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/licenses/LICENSE +0 -0
tree_annotated_plot/_plot.py
CHANGED
|
@@ -7,6 +7,7 @@ import json
|
|
|
7
7
|
import math
|
|
8
8
|
import re
|
|
9
9
|
import warnings
|
|
10
|
+
from collections.abc import Iterator
|
|
10
11
|
from html.parser import HTMLParser
|
|
11
12
|
from pathlib import Path
|
|
12
13
|
from typing import Any, Literal
|
|
@@ -223,11 +224,15 @@ def _build(
|
|
|
223
224
|
legend_show=config.tree_color_legend_show,
|
|
224
225
|
)
|
|
225
226
|
|
|
226
|
-
new_chart =
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
227
|
+
new_chart = copy.deepcopy(chart)
|
|
228
|
+
suppress_axis_chrome = config.connect_leader_to_label
|
|
229
|
+
n_hits = 0
|
|
230
|
+
for ch in _iter_strain_axis_channels(new_chart, config.chart_strain_field):
|
|
231
|
+
ch.sort = list(tip_names)
|
|
232
|
+
if suppress_axis_chrome:
|
|
233
|
+
ch.axis = alt.Axis(labels=False, ticks=False, domain=False, title=None)
|
|
234
|
+
n_hits += 1
|
|
235
|
+
_check_walker_hits("strain-axis update", n_hits, len(axis_hits), axis)
|
|
231
236
|
hoisted_config, hoisted_other = _pop_toplevel_only_attrs(new_chart)
|
|
232
237
|
|
|
233
238
|
combined = _concat_for_location(
|
|
@@ -916,11 +921,45 @@ def _enumerate_tree_tip_attrs(
|
|
|
916
921
|
return out
|
|
917
922
|
|
|
918
923
|
|
|
919
|
-
def
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
+
def _check_walker_hits(operation: str, actual: int, expected: int, axis: str) -> None:
|
|
925
|
+
"""Cross-check the live-object walk's hit count against the spec walk's.
|
|
926
|
+
|
|
927
|
+
`_find_strain_encoding` walks the chart's serialized dict form to count
|
|
928
|
+
how many `x`/`y` strain encodings the chart has and to validate
|
|
929
|
+
consistency (axis agreement, type=nominal/ordinal, etc.). The live
|
|
930
|
+
iteration in `_build` (driven by `_iter_strain_axis_channels`) must
|
|
931
|
+
visit exactly the same number of encodings — fewer would silently skip
|
|
932
|
+
applying the tree's tip order (or the axis suppression) to part of the
|
|
933
|
+
chart, more would mean we mutated structures the dict walker doesn't
|
|
934
|
+
know about.
|
|
935
|
+
|
|
936
|
+
The check is symmetric (`!=`) rather than one-sided (`<`) because
|
|
937
|
+
either direction signals that spec-level introspection and
|
|
938
|
+
live-object traversal have diverged, and continuing would render an
|
|
939
|
+
unverified chart.
|
|
940
|
+
"""
|
|
941
|
+
if actual != expected:
|
|
942
|
+
raise RuntimeError(
|
|
943
|
+
f"internal consistency check failed for {operation!r}: "
|
|
944
|
+
f"_find_strain_encoding located {expected} strain {axis!r}-axis "
|
|
945
|
+
f"encoding(s) in the chart spec, but the live-object walk "
|
|
946
|
+
f"updated {actual}. Spec-level and live-object traversal have "
|
|
947
|
+
"diverged, which would render the chart with a wrong tip order "
|
|
948
|
+
"or leave axis chrome behind. Please file a bug at "
|
|
949
|
+
"https://github.com/jbloomlab/tree-annotated-plot/issues with a "
|
|
950
|
+
"minimal reproducer."
|
|
951
|
+
)
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
def _iter_strain_axis_channels(node: Any, chart_strain_field: str) -> Iterator[Any]:
|
|
955
|
+
"""Yield every live x/y channel object whose field matches
|
|
956
|
+
`chart_strain_field`.
|
|
957
|
+
|
|
958
|
+
Pure read — no mutation, no count. The caller iterates and applies
|
|
959
|
+
whatever mutation it needs (currently `sort` and, when
|
|
960
|
+
`connect_leader_to_label=True`, an axis-suppression `alt.Axis(...)`),
|
|
961
|
+
counting hits as it goes so the cross-check in `_check_walker_hits`
|
|
962
|
+
can compare against the spec walker.
|
|
924
963
|
|
|
925
964
|
Walks the live altair object tree (Chart / LayerChart / FacetChart /
|
|
926
965
|
HConcatChart / VConcatChart / ConcatChart) rather than its dict form,
|
|
@@ -929,62 +968,25 @@ def _apply_tree_order_to_chart_object(
|
|
|
929
968
|
dict approach has to fight. Modifying the object in place is robust as
|
|
930
969
|
long as altair's container attribute names hold (.hconcat / .vconcat /
|
|
931
970
|
.concat / .layer / .spec / .encoding), which is stable in altair 5+.
|
|
971
|
+
`FacetChart.spec` is recursed into unconditionally so we descend to the
|
|
972
|
+
inner LayerChart / Chart that actually carries the encoding (gating on
|
|
973
|
+
`spec.encoding is not None` would skip LayerCharts, whose encodings
|
|
974
|
+
live on their layers rather than at the top level).
|
|
932
975
|
"""
|
|
933
|
-
new_chart = copy.deepcopy(chart)
|
|
934
|
-
_walk_and_apply_sort(new_chart, chart_strain_field, sort_order)
|
|
935
|
-
return new_chart
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
def _walk_and_apply_sort(
|
|
939
|
-
node: Any, chart_strain_field: str, sort_order: list[str]
|
|
940
|
-
) -> None:
|
|
941
|
-
"""Recursively set sort on every encoding whose field == chart_strain_field
|
|
942
|
-
on a live altair chart object."""
|
|
943
976
|
enc = _live_attr(node, "encoding")
|
|
944
977
|
if enc is not None:
|
|
945
978
|
for channel in ("x", "y"):
|
|
946
979
|
ch = _live_attr(enc, channel)
|
|
947
980
|
if ch is not None and _channel_field(ch) == chart_strain_field:
|
|
948
|
-
ch
|
|
981
|
+
yield ch
|
|
949
982
|
for attr in ("hconcat", "vconcat", "concat", "layer"):
|
|
950
983
|
sub = _live_attr(node, attr)
|
|
951
984
|
if isinstance(sub, list):
|
|
952
985
|
for s in sub:
|
|
953
|
-
|
|
954
|
-
# FacetChart.spec is the chart being faceted; recurse unconditionally so
|
|
955
|
-
# we descend into the inner LayerChart / Chart that actually carries the
|
|
956
|
-
# encoding. Gating on `spec.encoding is not None` was wrong because a
|
|
957
|
-
# LayerChart has no top-level encoding — its encodings live on its layers.
|
|
986
|
+
yield from _iter_strain_axis_channels(s, chart_strain_field)
|
|
958
987
|
spec = _live_attr(node, "spec")
|
|
959
988
|
if spec is not None:
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
def _suppress_chart_strain_axis(
|
|
964
|
-
chart: alt.TopLevelMixin, chart_strain_field: str
|
|
965
|
-
) -> None:
|
|
966
|
-
"""Hide labels, ticks, axis line, and title on every chart strain-axis encoding.
|
|
967
|
-
|
|
968
|
-
Walks the live altair object the same way `_walk_and_apply_sort` does
|
|
969
|
-
and replaces the matching encoding's ``axis`` with one that suppresses
|
|
970
|
-
every visible bit of axis chrome. This **overrides** any user-supplied
|
|
971
|
-
``axis=alt.Axis(...)`` on those encodings; that's documented on the
|
|
972
|
-
``connect_leader_to_label`` description in `_config.py`.
|
|
973
|
-
"""
|
|
974
|
-
enc = _live_attr(chart, "encoding")
|
|
975
|
-
if enc is not None:
|
|
976
|
-
for channel in ("x", "y"):
|
|
977
|
-
ch = _live_attr(enc, channel)
|
|
978
|
-
if ch is not None and _channel_field(ch) == chart_strain_field:
|
|
979
|
-
ch.axis = alt.Axis(labels=False, ticks=False, domain=False, title=None)
|
|
980
|
-
for attr in ("hconcat", "vconcat", "concat", "layer"):
|
|
981
|
-
sub = _live_attr(chart, attr)
|
|
982
|
-
if isinstance(sub, list):
|
|
983
|
-
for s in sub:
|
|
984
|
-
_suppress_chart_strain_axis(s, chart_strain_field)
|
|
985
|
-
spec = _live_attr(chart, "spec")
|
|
986
|
-
if spec is not None:
|
|
987
|
-
_suppress_chart_strain_axis(spec, chart_strain_field)
|
|
989
|
+
yield from _iter_strain_axis_channels(spec, chart_strain_field)
|
|
988
990
|
|
|
989
991
|
|
|
990
992
|
def _live_attr(obj: Any, name: str) -> Any:
|
|
@@ -1044,21 +1046,71 @@ def _apply_combined_config(combined: alt.HConcatChart, hoisted_config: Any) -> N
|
|
|
1044
1046
|
def _channel_field(ch: Any) -> str | None:
|
|
1045
1047
|
"""Read the underlying `field` string from an altair channel object.
|
|
1046
1048
|
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
+
Returns the field name when the channel references a data field (either
|
|
1050
|
+
via the `field=` keyword or via positional shorthand like
|
|
1051
|
+
`alt.Y("strain")` / `alt.Y("strain:N")` / `alt.Y("mean(titer):Q")`).
|
|
1052
|
+
Returns `None` when the channel has no field at all (a `value=` /
|
|
1053
|
+
`datum=` constant encoding). Raises `ValueError` when the channel's
|
|
1054
|
+
`_kwds` shape is unrecognized — silent fallthrough hid a real bug
|
|
1055
|
+
where untyped shorthand axes were never reordered to match the tree.
|
|
1056
|
+
|
|
1057
|
+
Reads `ch._kwds` directly rather than going through `ch.to_dict()`:
|
|
1058
|
+
altair's `to_dict()` on a bare channel raises when the shorthand has
|
|
1059
|
+
no explicit type (e.g. `alt.Y("strain")`), because the `nominal` /
|
|
1060
|
+
`quantitative` inference needs the chart's data context. That
|
|
1061
|
+
exception is what the previous catch-all hid.
|
|
1049
1062
|
"""
|
|
1050
|
-
|
|
1051
|
-
if not
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1063
|
+
kwds = getattr(ch, "_kwds", None)
|
|
1064
|
+
if not isinstance(kwds, dict):
|
|
1065
|
+
raise ValueError(
|
|
1066
|
+
f"channel object {type(ch).__name__} has no `_kwds` mapping; "
|
|
1067
|
+
"this isn't a recognized altair channel encoding."
|
|
1068
|
+
)
|
|
1069
|
+
field = kwds.get("field")
|
|
1070
|
+
if field is not None and field is not alt.Undefined:
|
|
1071
|
+
# `from_dict`-roundtripped channels store the field as a
|
|
1072
|
+
# `FieldName(SchemaBase)` wrapper rather than a plain str; its
|
|
1073
|
+
# `to_dict()` returns the raw string, while `str(...)` returns the
|
|
1074
|
+
# repr `FieldName('x')`. Cover both.
|
|
1075
|
+
if isinstance(field, str):
|
|
1076
|
+
if field:
|
|
1077
|
+
return field
|
|
1078
|
+
elif hasattr(field, "to_dict"):
|
|
1079
|
+
unwrapped = field.to_dict()
|
|
1080
|
+
if isinstance(unwrapped, str) and unwrapped:
|
|
1081
|
+
return unwrapped
|
|
1082
|
+
raise ValueError(
|
|
1083
|
+
f"channel field wrapper {type(field).__name__} unwrapped "
|
|
1084
|
+
f"to {unwrapped!r}; expected a non-empty string."
|
|
1085
|
+
)
|
|
1086
|
+
else:
|
|
1087
|
+
raise ValueError(
|
|
1088
|
+
f"channel field has unexpected value {field!r} "
|
|
1089
|
+
f"(type {type(field).__name__}); expected a string."
|
|
1090
|
+
)
|
|
1091
|
+
shorthand = kwds.get("shorthand")
|
|
1092
|
+
if shorthand is None or shorthand is alt.Undefined:
|
|
1056
1093
|
return None
|
|
1057
|
-
if isinstance(
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1094
|
+
if not isinstance(shorthand, str) or not shorthand:
|
|
1095
|
+
raise ValueError(
|
|
1096
|
+
f"channel shorthand has unexpected value {shorthand!r}; "
|
|
1097
|
+
"expected a string like 'strain', 'strain:N', or "
|
|
1098
|
+
"'mean(strain):Q'."
|
|
1099
|
+
)
|
|
1100
|
+
# Shorthand grammar: '[<aggregate>(]<field>[)][:<type>]'.
|
|
1101
|
+
bare = shorthand.split(":", 1)[0]
|
|
1102
|
+
if "(" in bare:
|
|
1103
|
+
if not bare.endswith(")"):
|
|
1104
|
+
raise ValueError(
|
|
1105
|
+
f"channel shorthand {shorthand!r} has an unbalanced "
|
|
1106
|
+
"aggregate wrapper; expected 'aggregate(field)[:type]'."
|
|
1107
|
+
)
|
|
1108
|
+
bare = bare[bare.index("(") + 1 : -1]
|
|
1109
|
+
if not bare:
|
|
1110
|
+
raise ValueError(
|
|
1111
|
+
f"channel shorthand {shorthand!r} parsed to an empty field name."
|
|
1112
|
+
)
|
|
1113
|
+
return bare
|
|
1062
1114
|
|
|
1063
1115
|
|
|
1064
1116
|
def _chart_strain_dim(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tree-annotated-plot
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Annotate the axis of an Altair / Vega-Lite plot with a phylogenetic tree.
|
|
5
5
|
Project-URL: Documentation, https://jbloomlab.github.io/tree-annotated-plot/
|
|
6
6
|
Project-URL: Homepage, https://github.com/jbloomlab/tree-annotated-plot
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
tree_annotated_plot/__init__.py,sha256=R39gjVnl3K2VjLba6zwtwPGADFmtNOE2UYNk1y9_z6k,351
|
|
2
2
|
tree_annotated_plot/_color.py,sha256=nOZp-KIPy6s-6gBkzW36oHrwDk_SmvVNsMiIJ-dY1Io,26519
|
|
3
3
|
tree_annotated_plot/_config.py,sha256=XwokRKY-ytNXelxwuapRKedTBaEedDi_erUp3su7zUs,12498
|
|
4
|
-
tree_annotated_plot/_plot.py,sha256=
|
|
4
|
+
tree_annotated_plot/_plot.py,sha256=l0GP66kLsVl1UjBmHIpnFcRGbFfk6qztxgi8ebYyxnM,68334
|
|
5
5
|
tree_annotated_plot/_tree.py,sha256=Bj-ABt8aooSwV7wAVTbwFGMmCnTPGOKweUWAdfb7aEI,13563
|
|
6
6
|
tree_annotated_plot/cli.py,sha256=yXI5fcLoIoLf6EjgPLmIQIAzDfkVoS5mSZ26_HSHVmk,9786
|
|
7
|
-
tree_annotated_plot-0.2.
|
|
8
|
-
tree_annotated_plot-0.2.
|
|
9
|
-
tree_annotated_plot-0.2.
|
|
10
|
-
tree_annotated_plot-0.2.
|
|
11
|
-
tree_annotated_plot-0.2.
|
|
7
|
+
tree_annotated_plot-0.2.1.dist-info/METADATA,sha256=EWRbJmbr3W_Rrm3-ggkTB_mhA06czQDaBO91RM_NdBQ,11353
|
|
8
|
+
tree_annotated_plot-0.2.1.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
9
|
+
tree_annotated_plot-0.2.1.dist-info/entry_points.txt,sha256=K7edTocRbwmtM2OeZmCy3DLjmY3MR-q2EJmQQq4rj0E,69
|
|
10
|
+
tree_annotated_plot-0.2.1.dist-info/licenses/LICENSE,sha256=UIp30ue1HZit7tMmSK0SS3swJBqOcLLnLJn5LY5YdNE,1036
|
|
11
|
+
tree_annotated_plot-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
{tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{tree_annotated_plot-0.2.0.dist-info → tree_annotated_plot-0.2.1.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|