phylogenie 2.1.8__py3-none-any.whl → 2.1.9__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.

Potentially problematic release.


This version of phylogenie might be problematic. Click here for more details.

phylogenie/plot.py CHANGED
@@ -1,4 +1,7 @@
1
+ from enum import Enum
2
+
1
3
  import matplotlib.colors as mcolors
4
+ import matplotlib.patches as mpatches
2
5
  import matplotlib.pyplot as plt
3
6
 
4
7
  from phylogenie import Tree
@@ -6,34 +9,69 @@ from phylogenie.tree import Tree
6
9
  from phylogenie.utils import get_times
7
10
 
8
11
 
12
+ class Coloring(str, Enum):
13
+ DISCRETE = "discrete"
14
+ CONTINUOUS = "continuous"
15
+
16
+
9
17
  def plot_tree(
10
18
  tree: Tree,
11
19
  ax: plt.Axes | None = None, # pyright: ignore
12
20
  color_by: str | None = None,
13
21
  default_color: str = "black",
14
- cmap: str = "tab20",
22
+ coloring: str | Coloring | None = None,
23
+ cmap: str | None = None,
24
+ show_legend: bool = True,
15
25
  ) -> plt.Axes: # pyright: ignore
16
26
  if ax is None:
17
27
  ax = plt.gca()
18
28
 
19
29
  xs = get_times(tree)
20
- ys = {node.name: i for i, node in enumerate(tree.inorder_traversal())}
30
+ ys = {node: i for i, node in enumerate(tree.inorder_traversal())}
31
+
21
32
  if color_by is not None:
22
33
  features = set(node.get(color_by) for node in tree)
23
- feature_colors = {
24
- f: mcolors.to_hex(plt.get_cmap(cmap, len(features))(i))
25
- for i, f in enumerate(features)
26
- }
27
- colors = {node.name: feature_colors[node.get(color_by)] for node in tree}
34
+ if coloring is None and any(isinstance(f, float) for f in features):
35
+ coloring = Coloring.CONTINUOUS
36
+ elif coloring is None:
37
+ coloring = Coloring.DISCRETE
38
+ if coloring == Coloring.DISCRETE:
39
+ if any(isinstance(f, float) for f in features):
40
+ raise ValueError(
41
+ "Discrete coloring selected but feature values are not all categorical."
42
+ )
43
+ cmap = "tab20" if cmap is None else cmap
44
+ colormap = plt.get_cmap(cmap, len(features))
45
+ feature_colors = {
46
+ f: mcolors.to_hex(colormap(i)) for i, f in enumerate(features)
47
+ }
48
+ colors = {node: feature_colors[node.get(color_by)] for node in tree}
49
+ legend_handles = [
50
+ mpatches.Patch(color=feature_colors[f], label=str(f)) for f in features
51
+ ]
52
+ if show_legend:
53
+ ax.legend(handles=legend_handles, title=color_by) # pyright: ignore
54
+ elif coloring in {Coloring.CONTINUOUS}:
55
+ cmap = "viridis" if cmap is None else cmap
56
+ values = list(map(float, features))
57
+ norm = mcolors.Normalize(vmin=min(values), vmax=max(values))
58
+ colormap = plt.get_cmap(cmap)
59
+ colors = {node: colormap(norm(float(node.get(color_by)))) for node in tree}
60
+ else:
61
+ raise ValueError(
62
+ f"Unknown coloring method: {coloring}. Choices are {list(Coloring)}."
63
+ )
28
64
  else:
29
- colors = {node.name: default_color for node in tree}
65
+ colors = {node: default_color for node in tree}
30
66
 
31
67
  for node in tree:
68
+ x1, y1 = xs[node.name], ys[node]
32
69
  if node.parent is None:
70
+ ax.hlines(y=y1, xmin=0, xmax=x1, color=colors[node]) # pyright: ignore
33
71
  continue
34
- x0, y0 = xs[node.parent.name], ys[node.parent.name]
35
- x1, y1 = xs[node.name], ys[node.name]
36
- ax.plot([x0, x0], [y0, y1], color=colors[node.name]) # pyright: ignore
37
- ax.plot([x0, x1], [y1, y1], color=colors[node.name]) # pyright: ignore
72
+ x0, y0 = xs[node.parent.name], ys[node.parent]
73
+ ax.vlines(x=x0, ymin=y0, ymax=y1, color=colors[node]) # pyright: ignore
74
+ ax.hlines(y=y1, xmin=x0, xmax=x1, color=colors[node]) # pyright: ignore
75
+
38
76
  ax.set_yticks([]) # pyright: ignore
39
77
  return ax
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: phylogenie
3
- Version: 2.1.8
3
+ Version: 2.1.9
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=yLJ331UNta0J3d8qdLBtLdIXsC3Qd0anA1qf_5AUwK8,1310
13
+ phylogenie/plot.py,sha256=9lXZokOi7sJ1HLDPQArzjODnQLL_nv16Fr7AtP-5eFE,2869
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=Ct0lfn6maKtjuFxivWx1MbFHvH3Y-fiJ0XXMdkN
28
28
  phylogenie/typeguards.py,sha256=JtqmbEWJZBRHbWgCvcl6nrWm3VcBfzRbklbTBYHItn0,1325
29
29
  phylogenie/typings.py,sha256=GknvAFXyiaWeeYJ8Lk5d6E2VHT-xW6ONEojYbtJYiB8,476
30
30
  phylogenie/utils.py,sha256=JDe6_8-9AFYf7SuTrqUXTZ7n3CA-goQGg9vTi5UcHB8,878
31
- phylogenie-2.1.8.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
32
- phylogenie-2.1.8.dist-info/METADATA,sha256=-F1xhii9rWBPtXGyjIr0UTVWbsrD7wnswCbw--LoS8I,5476
33
- phylogenie-2.1.8.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
34
- phylogenie-2.1.8.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
35
- phylogenie-2.1.8.dist-info/RECORD,,
31
+ phylogenie-2.1.9.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
32
+ phylogenie-2.1.9.dist-info/METADATA,sha256=V-aDacuPL0dfohsDmU69cvzHakcf6RfdDvrgK3zSCgc,5476
33
+ phylogenie-2.1.9.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
34
+ phylogenie-2.1.9.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
35
+ phylogenie-2.1.9.dist-info/RECORD,,