Visualization
Visualising lineages¶
Usually, tracked lineages contain hundreds of thousands of nodes, thus calculating each position for the nodes of the tree graph is time-consuming. To solve this problem, we decided to use a model that reduces the number of nodes to a minimum. In this model, we only use the start and end of each chain, while the length of their link would correspond to the time distance between the two nodes, as shown in the next image.

This way, the whole lineage can be plotted efficiently, even if the second graph is more representative of the truth.
API Reference¶
lineagetree.plot
¶
Functions:
| Name | Description |
|---|---|
draw_tree_graph |
Function to plot the tree graph. |
plot_all_lineages |
Plots all lineages. |
plot_dtw_heatmap |
Plot DTW cost matrix between two chains in heatmap format |
plot_dtw_trajectory |
Plots DTW trajectories aligment between two chains in 2D or 3D |
plot_subtree |
Plots the subtree spawn by a node. |
__plot_edges
¶
__plot_edges(
hier: dict,
lnks_tms: dict,
selected_edges: Iterable,
color: str | dict | list,
lw: float,
ax: Axes,
default_color: str = "black",
**kwargs
) -> None
Private method that plots the edges of the tree.
Source code in src/lineagetree/plot.py
__plot_nodes
¶
__plot_nodes(
hier: dict,
selected_nodes: set,
color: str | dict | list,
size: int | float,
ax: Axes,
leaves: set,
default_color: str = "black",
**kwargs
) -> None
Private method that plots the nodes of the tree.
Source code in src/lineagetree/plot.py
_create_dict_of_plots
¶
_create_dict_of_plots(
lT: LineageTree,
node: int | Iterable[int] | None = None,
start_time: int | None = None,
end_time: int | None = None,
) -> dict[int, dict]
Generates a dictionary of graphs where the keys are the index of the graph and
the values are the graphs themselves which are produced by create_links_and_chains
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
int or Iterable of int
|
The id of the node/nodes to produce the simple graphs, if not provided or None will calculate the dicts for every root that starts before 'start_time' |
None
|
|
int
|
Important only if there are no nodes it will produce the graph of every root that starts before or at start time. If not provided or None the 'start_time' defaults to the start of the dataset. |
None
|
|
int
|
The last timepoint to be considered, if not provided or None the last timepoint of the dataset (t_e) is considered. |
None
|
Returns:
| Type | Description |
|---|---|
dict mapping int to dict
|
The keys are just index values 0-n and the values are the graphs produced. |
Source code in src/lineagetree/plot.py
draw_tree_graph
¶
draw_tree_graph(
lT: LineageTree,
hier: dict[int, tuple[int, int]],
lnks_tms: dict[str, dict[int, list | int]],
selected_nodes: list | set | None = None,
selected_edges: list | set | None = None,
color_of_nodes: str | dict = "magenta",
color_of_edges: str | dict = "magenta",
size: int | float = 10,
lw: float = 0.3,
ax: Axes | None = None,
default_color: str = "black",
**kwargs
) -> tuple[plt.Figure, plt.Axes]
Function to plot the tree graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
dict mapping int to tuple of int
|
Dictionary that contains the positions of all nodes. |
required |
|
dict mapping string to dictionaries mapping int to list or int
|
|
required |
|
list or set
|
Which nodes are to be selected (Painted with a different color, according to 'color_'of_nodes') |
None
|
|
list or set
|
Which edges are to be selected (Painted with a different color, according to 'color_'of_edges') |
None
|
|
str
|
Color of selected nodes |
"magenta"
|
|
str
|
Color of selected edges |
"magenta"
|
|
int
|
Size of the nodes, defaults to 10 |
10
|
|
float
|
The width of the edges of the tree graph, defaults to 0.3 |
0.3
|
|
Axes
|
Plot the graph on existing ax. If not provided or None a new ax is going to be created. |
None
|
|
str
|
Default color of nodes |
"black"
|
Returns:
| Type | Description |
|---|---|
Figure
|
The matplotlib figure |
Axes
|
The matplotlib ax |
Source code in src/lineagetree/plot.py
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 | |
plot_all_lineages
¶
plot_all_lineages(
lT: LineageTree,
nodes: list | None = None,
last_time_point_to_consider: int | None = None,
nrows: int = 1,
figsize: tuple[int, int] = (10, 15),
dpi: int = 100,
fontsize: int = 15,
axes: Axes | None = None,
vert_gap: int = 1,
**kwargs
) -> tuple[plt.Figure, plt.Axes, dict[plt.Axes, int]]
Plots all lineages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
list
|
The nodes spawning the graphs to be plotted. |
None
|
|
int
|
Which timepoints and upwards are the graphs to be plotted.
For example if start_time is 10, then all trees that begin
on tp 10 or before are calculated. Defaults to None, where
it will plot all the roots that exist on |
None
|
|
int
|
How many rows of plots should be printed. |
1
|
|
tuple
|
The size of the figure. |
(10, 15)
|
|
int
|
The dpi of the figure. |
100
|
|
int
|
The fontsize of the labels. |
15
|
|
Axes
|
The axes to plot the graphs on. If None or not provided new axes are going to be created. |
None
|
|
int
|
space between the nodes, defaults to 1 |
1
|
|
kwargs accepted by matplotlib.pyplot.plot, matplotlib.pyplot.scatter |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure |
Axes
|
The axes |
dict of plt.Axes to int
|
A dictionary that maps the axes to the root of the tree. |
Source code in src/lineagetree/plot.py
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 | |
plot_dtw_heatmap
¶
plot_dtw_heatmap(
lT: LineageTree,
nodes1: int,
nodes2: int,
threshold: int = 1000,
regist: bool = True,
start_d: int = 0,
back_d: int = 0,
fast: bool = False,
w: int = 0,
centered_band: bool = True,
) -> tuple[float, plt.Figure]
Plot DTW cost matrix between two chains in heatmap format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
int
|
node to compare distance |
required |
|
int
|
node to compare distance |
required |
|
int
|
set a maximum number of points a chain can have |
1000
|
|
bool
|
Rotate and translate trajectories |
True
|
|
int
|
start delay |
0
|
|
int
|
end delay |
0
|
|
bool
|
if |
False
|
|
int
|
window size |
0
|
|
bool
|
when running the fast algorithm, |
True
|
Returns:
| Type | Description |
|---|---|
float
|
DTW distance |
Figure
|
Heatmap of cost matrix with opitimal path |
Source code in src/lineagetree/plot.py
plot_dtw_trajectory
¶
plot_dtw_trajectory(
lT: LineageTree,
nodes1: int,
nodes2: int,
threshold: int = 1000,
regist: bool = True,
start_d: int = 0,
back_d: int = 0,
fast: bool = False,
w: int = 0,
centered_band: bool = True,
projection: Literal[
"3d", "xy", "xz", "yz", "pca", None
] = None,
alig: bool = False,
) -> tuple[float, plt.Figure]
Plots DTW trajectories aligment between two chains in 2D or 3D
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
int
|
node to compare distance |
required |
|
int
|
node to compare distance |
required |
|
int
|
set a maximum number of points a chain can have |
1000
|
|
bool
|
Rotate and translate trajectories |
True
|
|
int
|
start delay |
0
|
|
int
|
end delay |
0
|
|
int
|
window size |
0
|
|
bool
|
True if the user wants to run the fast algorithm with window restrains |
False
|
|
bool
|
if running the fast algorithm, True if the windown is centered |
True
|
|
('3d', 'xy', 'xz', 'yz', 'pca')
|
specify which 2D to plot -> "3d" : for the 3d visualization "xy" or None (default) : 2D projection of axis x and y "xz" : 2D projection of axis x and z "yz" : 2D projection of axis y and z "pca" : PCA projection |
"3d"
|
|
bool
|
True to show alignment on plot |
False
|
Returns:
| Type | Description |
|---|---|
float
|
DTW distance |
figure
|
Trajectories Plot |
Source code in src/lineagetree/plot.py
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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
plot_subtree
¶
plot_subtree(
lT: LineageTree,
node: int,
end_time: int | None = None,
figsize: tuple[int, int] = (4, 7),
dpi: int = 150,
vert_gap: int = 2,
selected_nodes: list | None = None,
selected_edges: list | None = None,
color_of_nodes: str | dict = "magenta",
color_of_edges: str | dict = "magenta",
size: int | float = 10,
lw: float = 0.1,
default_color: str = "black",
ax: Axes | None = None,
) -> tuple[plt.Figure, plt.Axes]
Plots the subtree spawn by a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
LineageTree
|
The LineageTree instance. |
required |
|
int
|
The id of the node that is going to be plotted. |
required |
|
int
|
The last timepoint to be considered, if None or not provided the last timepoint of the dataset (t_e) is considered. |
None
|
|
tuple of 2 ints
|
The size of the figure, deafults to (4,7) |
(4,7)
|
|
int
|
The verical gap of a node when it divides, defaults to 2. |
2
|
|
int
|
The dpi of the figure, defaults to 150 |
150
|
|
list
|
The nodes that are selected by the user to be colored in a different color, defaults to None |
None
|
|
list
|
The edges that are selected by the user to be colored in a different color, defaults to None |
None
|
|
str
|
The color of the nodes to be colored, except the default colored ones, defaults to "magenta" |
"magenta"
|
|
str
|
The color of the edges to be colored, except the default colored ones, defaults to "magenta" |
"magenta"
|
|
int
|
The size of the nodes, defaults to 10 |
10
|
|
float
|
The widthe of the edges of the tree graph, defaults to 0.1 |
0.1
|
|
str
|
The default color of nodes and edges, defaults to "black" |
"black"
|
|
Axes
|
The ax where the plot is going to be applied, if not provided or None new axes will be created. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
The matplotlib figure |
Axes
|
The matplotlib axes |
Raises:
| Type | Description |
|---|---|
Warning
|
If more than one nodes are received |
Source code in src/lineagetree/plot.py
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 | |