phylogenie 2.1.17__py3-none-any.whl → 2.1.18__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.
phylogenie/plot.py CHANGED
@@ -1,9 +1,11 @@
1
1
  from enum import Enum
2
+ from typing import Any
2
3
 
3
4
  import matplotlib.colors as mcolors
4
5
  import matplotlib.patches as mpatches
5
6
  import matplotlib.pyplot as plt
6
7
  from matplotlib.axes import Axes
8
+ from mpl_toolkits.axes_grid1.inset_locator import inset_axes # pyright: ignore
7
9
 
8
10
  from phylogenie.tree import Tree
9
11
  from phylogenie.utils import get_node_depth_levels, get_node_depths
@@ -22,7 +24,10 @@ def plot_tree(
22
24
  coloring: str | Coloring | None = None,
23
25
  cmap: str | None = None,
24
26
  show_legend: bool = True,
25
- ) -> Axes:
27
+ show_hist: bool = True,
28
+ hist_kwargs: dict[str, Any] | None = None,
29
+ hist_axes_kwargs: dict[str, Any] | None = None,
30
+ ) -> Axes | tuple[Axes, Axes]:
26
31
  if ax is None:
27
32
  ax = plt.gca()
28
33
 
@@ -34,7 +39,8 @@ def plot_tree(
34
39
  ys = {node: i for i, node in enumerate(tree.inorder_traversal())}
35
40
 
36
41
  if color_by is not None:
37
- features = set(node.get(color_by) for node in tree if color_by in node.features)
42
+ features = [node.get(color_by) for node in tree if color_by in node.features]
43
+
38
44
  if coloring is None and any(isinstance(f, float) for f in features):
39
45
  coloring = Coloring.CONTINUOUS
40
46
  elif coloring is None:
@@ -47,7 +53,7 @@ def plot_tree(
47
53
  )
48
54
  colormap = plt.get_cmap("tab20" if cmap is None else cmap)
49
55
  feature_colors = {
50
- f: mcolors.to_hex(colormap(i)) for i, f in enumerate(features)
56
+ f: mcolors.to_hex(colormap(i)) for i, f in enumerate(set(features))
51
57
  }
52
58
  colors = {
53
59
  node: (
@@ -61,7 +67,7 @@ def plot_tree(
61
67
  if show_legend:
62
68
  legend_handles = [
63
69
  mpatches.Patch(color=feature_colors[f], label=str(f))
64
- for f in features
70
+ for f in feature_colors
65
71
  ]
66
72
  if any(color_by not in node.features for node in tree):
67
73
  legend_handles.append(
@@ -81,8 +87,34 @@ def plot_tree(
81
87
  for node in tree
82
88
  }
83
89
 
84
- sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
85
- ax.get_figure().colorbar(sm, ax=ax) # pyright: ignore
90
+ if show_hist:
91
+ default_hist_axes_kwargs = {
92
+ "width": "25%",
93
+ "height": "25%",
94
+ "loc": "lower left",
95
+ "borderpad": 3,
96
+ }
97
+ if hist_axes_kwargs is not None:
98
+ default_hist_axes_kwargs.update(hist_axes_kwargs)
99
+ hist_ax = inset_axes(ax, **default_hist_axes_kwargs) # pyright: ignore
100
+
101
+ hist_kwargs = {} if hist_kwargs is None else hist_kwargs
102
+ _, bins, patches = hist_ax.hist( # pyright: ignore
103
+ features, **hist_kwargs
104
+ )
105
+
106
+ for patch, b0, b1 in zip( # pyright: ignore
107
+ patches, bins[:-1], bins[1:] # pyright: ignore
108
+ ):
109
+ midpoint = (b0 + b1) / 2 # pyright: ignore
110
+ patch.set_facecolor(colormap(norm(midpoint))) # pyright: ignore
111
+
112
+ hist_ax.set_xlabel(color_by.capitalize(), fontsize=8) # pyright: ignore
113
+ hist_ax.set_ylabel("Count", fontsize=8) # pyright: ignore
114
+ hist_ax.tick_params(axis="both", labelsize=6) # pyright: ignore
115
+ else:
116
+ sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
117
+ ax.get_figure().colorbar(sm, ax=ax) # pyright: ignore
86
118
 
87
119
  else:
88
120
  raise ValueError(
@@ -100,5 +132,14 @@ def plot_tree(
100
132
  ax.vlines(x=x0, ymin=y0, ymax=y1, color=colors[node]) # pyright: ignore
101
133
  ax.hlines(y=y1, xmin=x0, xmax=x1, color=colors[node]) # pyright: ignore
102
134
 
135
+ for node in tree:
136
+ x1, y1 = xs[node], ys[node]
137
+ if node.parent is None:
138
+ ax.hlines(y=y1, xmin=0, xmax=x1, color=colors[node]) # pyright: ignore
139
+ continue
140
+ x0, y0 = xs[node.parent], ys[node.parent]
141
+ ax.vlines(x=x0, ymin=y0, ymax=y1, color=colors[node]) # pyright: ignore
142
+ ax.hlines(y=y1, xmin=x0, xmax=x1, color=colors[node]) # pyright: ignore
143
+
103
144
  ax.set_yticks([]) # pyright: ignore
104
145
  return ax
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: phylogenie
3
- Version: 2.1.17
3
+ Version: 2.1.18
4
4
  Summary: Generate phylogenetic datasets with minimal setup effort
5
5
  Author: Gabriele Marino
6
6
  Author-email: gabmarino.8601@gmail.com
@@ -10,7 +10,7 @@ phylogenie/io.py,sha256=nwy8DOknt0HqF9qMeFZHrCmSXpM5AGrU5oajwTtD6vY,3973
10
10
  phylogenie/main.py,sha256=vtvSpQxBNlYABoFQ25czl-l3fIr4QRo3svWVd-jcArw,1170
11
11
  phylogenie/models.py,sha256=pCg9ob0RpLUHwM49x4knKxL4FNPr3-EU_6zMXsvxtAg,370
12
12
  phylogenie/msa.py,sha256=JDGyZUsAq6-m-SQjoCDjAkAZIxfgyl_PDIhdYn5HOow,2064
13
- phylogenie/plot.py,sha256=I1QH7bZ09t7KtI4LleQlph9YZZeyCwu_Jr-YH2JKrW0,3618
13
+ phylogenie/plot.py,sha256=MgRIquHtI2QpHR7NH7dsmfaeDN1ICU6CpZjShLqHQC0,5505
14
14
  phylogenie/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
15
  phylogenie/skyline/__init__.py,sha256=7pF4CUb4ZCLzNYJNhOjpuTOLTRhlK7L6ugfccNqjIGo,620
16
16
  phylogenie/skyline/matrix.py,sha256=Gl8OgKjtieG0NwPYiPimKI36gefV8fm_OeorjdXxPTs,9146
@@ -28,8 +28,8 @@ phylogenie/treesimulator/model.py,sha256=Nyg6R8XmMwZMSw1-dII81sU9uU7tDe-NMs8v1qK
28
28
  phylogenie/typeguards.py,sha256=JtqmbEWJZBRHbWgCvcl6nrWm3VcBfzRbklbTBYHItn0,1325
29
29
  phylogenie/typings.py,sha256=GknvAFXyiaWeeYJ8Lk5d6E2VHT-xW6ONEojYbtJYiB8,476
30
30
  phylogenie/utils.py,sha256=faDicpAtjxM-QDwPKOv8AOxPe7skPUOGKmVWBZRzUbU,1990
31
- phylogenie-2.1.17.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
32
- phylogenie-2.1.17.dist-info/METADATA,sha256=TUa2AtURiUDVcWfYzt_6BQYaH7N4Izgco0O5vKVOuH0,5477
33
- phylogenie-2.1.17.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
34
- phylogenie-2.1.17.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
35
- phylogenie-2.1.17.dist-info/RECORD,,
31
+ phylogenie-2.1.18.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
32
+ phylogenie-2.1.18.dist-info/METADATA,sha256=N3F8qjtVEtqb0kxfywFd7GpTiGyCSNWwCwlUmyuNUfQ,5477
33
+ phylogenie-2.1.18.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
34
+ phylogenie-2.1.18.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
35
+ phylogenie-2.1.18.dist-info/RECORD,,