Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lineagetree/_mixins/plot_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
plot_dtw_heatmap,
plot_dtw_trajectory,
plot_subtree,
plot_chain_histogram,
)

from ._methodize import AutoMethodizeMeta
Expand All @@ -19,3 +20,4 @@ class PlotMixin(metaclass=AutoMethodizeMeta):
plot_dtw_heatmap = plot_dtw_heatmap
plot_dtw_trajectory = plot_dtw_trajectory
plot_subtree = plot_subtree
plot_chain_histogram = plot_chain_histogram
47 changes: 47 additions & 0 deletions src/lineagetree/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,50 @@ def plot_dtw_trajectory(
)

return distance, fig


def plot_chain_histogram(
lT: LineageTree,
*,
with_leaves: bool = False,
with_roots: bool = False,
bins: int = None,
color: str = "blue",
ax: plt.Axes | None = None,
**kwargs,
):
"""Plot the histograms of the chains length of the dataset. By default the chains tha tcontain root or leaf ndoes are not included.

Parameters
----------
lT : LineageTree
The LineageTree instance.
with_leaves : bool, optional
Include the chains that contain leaves in the histogram, by default False
with_roots : bool, optional
Include the chains that contain roots in the histogram, by default False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not optional, they have default values, there is a difference

bins : int, optional
Bins of tghe histogram, if none they are set automatically, by default None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo tghe

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also "if None ..."

color : str, optional
Color of the histogram, by default "blue"
ax : plt.Axes | None, optional
The plt.Axes instance, if None it creates a new Axes instance, by default None

Returns
-------
plt.Figure
The matplotlib figure
plt.Axes
The matplotlib axes
"""

list_of_chains = [c for c in lT.all_chains]
if not with_roots:
list_of_chains = [c for c in list_of_chains if c[0] not in lT.roots]
if not with_leaves:
list_of_chains = [c for c in list_of_chains if c[-1] not in lT.leaves]
chain_lens = [len(c) for c in list_of_chains]
if not ax:
_, ax = plt.subplots(nrows=1, ncols=1, **kwargs)
ax.hist(chain_lens, bins, color=color)
return ax.get_figure(), ax
21 changes: 21 additions & 0 deletions tests/test_lineageTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,24 @@ def test_change_labels():
24: "left",
25: "left",
}


def test_plot_chain_hist():
_, ax1 = lt.plot_chain_histogram()
_, ax2 = lt.plot_chain_histogram(with_leaves=True)
_, ax3 = lt.plot_chain_histogram(with_roots=True)
_, ax4 = lt.plot_chain_histogram(with_roots=True, with_leaves=True)
assert sum(p.get_height() for p in ax4.patches) == len(lt.all_chains)
assert sum(p.get_height() for p in ax3.patches) == (
len(lt.all_chains) - len(lt.leaves)
)
assert sum(p.get_height() for p in ax2.patches) == (
len(lt.all_chains) - len(lt.roots)
)
print(
sum(p.get_height() for p in ax1.patches),
len(lt.all_chains) - len(lt.leaves) - len(lt.roots),
)
assert sum(p.get_height() for p in ax1.patches) == (
len(lt.all_chains) - len(lt.leaves.union(lt.roots))
)
Loading