Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom annotations per facet #134

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## v0.6

### v0.6.1
- Add option to give facet-specific annotation parameters, such as to
set custom annotations

### v0.6.0
#### Features
- Add option to skip annotation of non-significant results
Expand Down
21 changes: 15 additions & 6 deletions statannotations/Annotator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from typing import List, Optional, Union
from typing import List, Optional, Union, Iterator, Dict, Any

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
Expand Down Expand Up @@ -357,7 +357,7 @@ def set_pvalues(self, pvalues, num_comparisons='auto'):

return self

def set_custom_annotations(self, text_annot_custom):
def set_custom_annotations(self, text_annot_custom: List):
"""
:param text_annot_custom: List of strings to annotate for each
`pair`
Expand Down Expand Up @@ -816,12 +816,16 @@ def _maybe_warn_about_configuration(self):

def plot_and_annotate_facets(
self, plot: str, plot_params: dict, configuration: dict,
annotation_func: str, *args, annotation_params: dict = None,
annotation_func: str, *args,
annotation_params: dict = None,
annotation_params_each: Optional[
Iterator[Dict[str, Any]]] = None,
ax_op_before: List[Union[str, Optional[list],
Optional[dict]]] = None,
ax_op_after: List[Union[str, Optional[list],
Optional[dict]]] = None,
annotate_params: dict = None, **kwargs):
annotate_params: dict = None,
**kwargs):
"""
Plots using seaborn and annotates in a single call, to be used within
a `FacetGrid`.
Expand All @@ -836,6 +840,8 @@ def plot_and_annotate_facets(
* 'set_pvalues'
* 'apply_test'
:param annotation_params: parameters for the annotation function
:param annotation_params_each: parameters for the annotation function to
iterate on to find values for each facet.
:param ax_op_before: list of [func_name, args, kwargs] to apply on `ax`
before annotating
:param ax_op_after: list of [func_name, args, kwargs] to apply on `ax`
Expand All @@ -846,14 +852,17 @@ def plot_and_annotate_facets(
"""
annotate_params = empty_dict_if_none(annotate_params)
annotation_params = empty_dict_if_none(annotation_params)

ax = getattr(sns, plot)(*args, **plot_params, **kwargs)

_apply_ax_operations(ax, ax_op_before)

self.new_plot(ax, plot=plot, **plot_params, data=kwargs['data'])
self.configure(**configuration)
getattr(self, annotation_func)(**annotation_params)
if annotation_params_each:
annotation_args, annotation_kwargs = next(annotation_params_each)
getattr(self, annotation_func)(*annotation_args, **annotation_params, **annotation_kwargs)
else:
getattr(self, annotation_func)(**annotation_params)
self.annotate(**annotate_params)

_apply_ax_operations(ax, ax_op_after)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_integrate_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,24 @@ def test_plot_and_annotate_facets(self):
ax_op_after=[['set_xlabel', ['Group'], None]],
annotation_params={'num_comparisons': 'auto'}
)

def test_plot_and_annotate_facets_custom_annotation_params_each(self):
custom_annotations = iter([
[[["this"]], {}],
[[["that"]], {}]
])

annotator = Annotator(None, self.simple_pairs)
g = sns.FacetGrid(self.params_df.pop("data"),
col=self.params_df.pop("hue"),
height=10, sharey=False)
self.params_df.pop("hue_order")
g.map_dataframe(annotator.plot_and_annotate_facets,
plot="boxplot",
plot_params=self.params_df,
configuration={"comparisons_correction": None},
annotation_func="set_custom_annotations",
annotation_params_each=custom_annotations,
ax_op_after=[['set_xlabel', ['Group'], None]],
)
self.assertEqual(annotator.get_annotations_text(), ["that"]) # last facet
422 changes: 412 additions & 10 deletions usage/example.ipynb

Large diffs are not rendered by default.