Skip to content

Commit 03bb4f6

Browse files
committed
switched to seaborn for histogram creation, removed return value (works now directly on axes object if passed to function) and ammended documentation
1 parent 55ee737 commit 03bb4f6

1 file changed

Lines changed: 62 additions & 19 deletions

File tree

coderdata/utils/stats.py

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from coderdata import DatasetLoader
77
import pandas as pd
88

9-
import matplotlib
109
import matplotlib.pyplot as plt
11-
from matplotlib.figure import Figure
10+
from matplotlib.axes import Axes
11+
import seaborn as sns
1212

1313
def summarize_response_metric(data: DatasetLoader) -> pd.DataFrame:
1414
"""
@@ -43,34 +43,79 @@ def summarize_response_metric(data: DatasetLoader) -> pd.DataFrame:
4343
def plot_response_metric(
4444
data: DatasetLoader,
4545
metric: str='auc',
46+
ax: Axes=None,
4647
**kwargs: dict
47-
) -> Figure:
48+
) -> None:
4849
"""
49-
Will cerate a `matplot.figure.Figure` object and return it, e.g. to
50-
be saved locally. If the only purpose is to display the figure in a
51-
Jupyter notebook for example the return value does not need to be
52-
caught.
50+
Creates a histogram detailing the distribution of dose response
51+
values for a given dose respones metric.
5352
53+
If used in conjunction with `matplotlib.pyplot.subplot` or
54+
`matplotlib.pyplot.subplots` and the axes object is passed to the
55+
function, the function populates the axes object with the generated
56+
plot.
57+
5458
Parameters
5559
----------
5660
data : coderdata.DataLoader
5761
A full CoderData object of a dataset
5862
metric : str, default='auc'
5963
A string that defines the response metric that should be plotted
64+
ax : matplotlib.axes.Axes, default=None
65+
An `Axes` object can be defined. This is uesful if a multipannel
66+
subplot has been defined prior via `matplotlib.pyplot.subplots`.
67+
Passing the location of the axes to the function will then
68+
populate the subplot at the given location with the generated
69+
plot.
6070
**kwargs : dict, optional
6171
Additional keyword arguments that can be passed to the function
72+
- bins : int - sets the number of bins; passed to
73+
`seaborn.histplot`
74+
- title : str - sets the title of the axes
75+
- kde : bool - adds a kernel density estimate plot into the
76+
histogram
6277
63-
Returns
78+
Example
6479
-------
65-
matplotlib.figure.Figure
66-
A `Figure` object that contains the generated figure. Can be
67-
passed to a variable and saved locally via `Figure.savefig()`
80+
In a Jupyter Notebook environment the following snippet can be used
81+
to display a histgram detailing the distribution of drug response
82+
AUC measures in the beataml dataset.
83+
84+
>>> import coderdata as cd
85+
>>> beataml = cd.DataLoader('beataml')
86+
>>> cd.plot_response_metric(data=beataml, metric='auc', bin=10)
87+
88+
For generating multipanel plots we can make use of matplotlib and
89+
the `ax` parameter of this function. Furthermore, other features /
90+
parameters of the cerated figure can be changed (e.g. the title of
91+
the figure via `suptitle()`). Finally it can be saved.
92+
93+
>>> import coderdata as cd
94+
>>> import matplotlib.pyplot as plt
95+
>>> beataml = cd.DataLoader('beataml')
96+
>>> fig, axs = plt.subplots(ncols=2, figsize=(10, 5))
97+
>>> plot_response_metric(
98+
... data=beataml,
99+
... metric='auc',
100+
... bins=10,
101+
... ax=axs[0]
102+
... )
103+
>>> plot_response_metric(
104+
... data=beataml,
105+
... metric='aac',
106+
... bins=10,
107+
... ax=axs[0]
108+
... )
109+
>>> fig.set_layout_engine('tight')
110+
>>> fig.suptitle('Distribution of drug response values')
111+
>>> fig.savefig('figure.png')
68112
"""
69113

70114
# assinging values to variables based on **kwargs and defining
71115
# default values if not present in **kwargs
72-
bins = kwargs.get('bins', 10)
73-
title = kwargs.get('title', f"Value distributino for metric '{metric}'")
116+
bins_ = kwargs.get('bins', 10)
117+
title_ = kwargs.get('title', None)
118+
kde_ = kwargs.get('kde', False)
74119

75120
# retrieving the data/values necessary to generate the figure
76121
metrics = (
@@ -80,9 +125,7 @@ def plot_response_metric(
80125
metric_ = metrics.get_group(metric) # retrieving the desired group
81126
x = metric_['dose_response_value'] # getting the values
82127

83-
fig, ax = plt.subplots() # generating the "Plot objects"
84-
85-
ax.hist(x, bins=bins, linewidth=0.5, edgecolor="white")
86-
ax.set_title(title)
87-
88-
return fig
128+
sns.set_theme(palette='colorblind')
129+
p = sns.histplot(data=x, kde=kde_, bins=bins_, ax=ax)
130+
p.set_xlabel(metric)
131+
p.set_title(title_)

0 commit comments

Comments
 (0)