Skip to content

Commit

Permalink
Merge pull request #292 from lilab-bcb/yiming
Browse files Browse the repository at this point in the history
Add show_only_expressed option to dotplot
  • Loading branch information
yihming committed Mar 15, 2024
2 parents 9dcd8a9 + 2acb6e0 commit 3328380
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pegasus/plotting/plot_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ def dotplot(
genes: Union[str, List[str]],
groupby: str,
reduce_function: Union[str, Callable[[np.ndarray], float]] = "mean",
show_only_expressed: bool = True,
fraction_min: float = 0,
fraction_max: float = None,
dot_min: int = 0,
Expand All @@ -1342,6 +1343,8 @@ def dotplot(
A categorical variable in data.obs that is used to categorize the cells, e.g. Clusters.
reduce_function: ``Union[str, Callable[[np.ndarray], float]]``, optional, default: ``"mean"``
Function to calculate statistic on expression data. Default is mean.
show_only_expressed: ``bool``, optional, default: `True`
If ``True``, the statistic is calculated over only cells expressing the selected genes; otherwise, it's calculated over all cells.
fraction_min: ``float``, optional, default: ``0``.
Minimum fraction of expressing cells to consider.
fraction_max: ``float``, optional, default: ``None``.
Expand Down Expand Up @@ -1401,10 +1404,14 @@ def dotplot(
logger.warning(f"The following categories contain no cells and are removed: {','.join(list(series.index[idx]))}.")

def non_zero(g):
return np.count_nonzero(g) / g.shape[0]
return np.count_nonzero(g.fillna(0.0)) / g.shape[0]

# Set observed=True to suppress warnings.
summarized_df = df.groupby(by=groupby, observed=True).aggregate([reduce_function, non_zero])
if show_only_expressed:
df.set_index(groupby, inplace=True)
is_expressed = df > 0.0
summarized_df = df.mask(~is_expressed).groupby(level=0, observed=True).aggregate([reduce_function, non_zero])
else:
summarized_df = df.groupby(by=groupby, observed=True).aggregate([reduce_function, non_zero])

row_indices = summarized_df.index.tolist()
if sort_function == "natsorted":
Expand Down

0 comments on commit 3328380

Please sign in to comment.