Skip to content

Commit

Permalink
linting and simplifications
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Charlier <477844+trevismd@users.noreply.github.com>
  • Loading branch information
trevismd committed Oct 16, 2022
1 parent 6589bf4 commit 1524df8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 62 deletions.
2 changes: 1 addition & 1 deletion statannotations/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.4"
__version__ = "0.4.5"
22 changes: 9 additions & 13 deletions statannotations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,22 @@ def _check_pairs_in_data_no_hue(pairs: Union[list, tuple],
f" (specified in `pairs`) in data")


def _check_pairs_in_data_with_hue(pairs: Union[list, tuple],
data: Union[None, List[list],
pd.DataFrame] = None,
group_coord: Union[None, str, list] = None,
hue: Union[None, str, list] = None,
) -> set:

# Arrays provided, not Dataframe
def _check_pairs_in_data_with_hue(
pairs: Union[list, tuple],
data: Union[None, List[list], pd.DataFrame] = None,
group_coord: Union[None, str, list] = None,
hue: Union[None, str, list] = None) -> set:

if data is None:
hue_values = set(hue)
return hue_values
else:
hue_values = set(data[hue].unique())

x_values = get_x_values(data, group_coord)
seen_group_values = set()

hue_values = set(data[hue].unique())

for group_value, hue_value in itertools.chain(itertools.chain(*pairs)):
if (group_value not in seen_group_values
and group_value not in x_values):
if group_value not in seen_group_values and group_value not in x_values:
raise ValueError(
f"Missing group value `{group_value}` in {group_coord}"
f" (specified in `pairs`)")
Expand Down
31 changes: 24 additions & 7 deletions tests/test_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def setUp(self):
8: {'x': "b", 'y': 18, 'color': 'red'}
}).T

self.pairs_for_df = [(("a", "blue"), ("b", "blue")),
(("a", "blue"), ("a", "red"))]
self.pairs = [(("a", "blue"), ("b", "blue")),
(("a", "blue"), ("a", "red"))]
self.df.y = self.df.y.astype(float)
self.params_df = {
"data": self.df,
Expand All @@ -40,14 +40,24 @@ def setUp(self):
"hue": "color",
"order": ["a", "b"],
"hue_order": ['red', 'blue']}
self.params_arrays = {
"data": None,
"x": self.df['x'],
"y": self.df['y'],
"hue": self.df['color'],
"order": ["a", "b"],
"hue_order": ['red', 'blue']}

def test_init_simple(self):
self.annot = Annotator(self.ax, [(0, 1)], data=self.data)

def test_init_df(self):
self.ax = sns.boxplot(**self.params_df)
self.annot = Annotator(self.ax, pairs=self.pairs_for_df,
**self.params_df)
self.annot = Annotator(self.ax, pairs=self.pairs, **self.params_df)

def test_init_arrays(self):
self.ax = sns.boxplot(**self.params_arrays)
self.annot = Annotator(self.ax, pairs=self.pairs, **self.params_arrays)

def test_init_barplot(self):
ax = sns.barplot(data=self.data)
Expand Down Expand Up @@ -87,6 +97,13 @@ def test_unmatched_hue_in_box_pairs(self):
order=["a", "b"], hue='color',
hue_order=['red', 'blue'])

def test_unmatched_hue_in_box_pairs_arrays(self):
with self.assertRaisesRegex(ValueError, "(specified in `pairs`)"):
self.annot = Annotator(self.ax, [(("a", "yellow"), ("b", "blue"))],
data=None, x=self.df['x'], y=self.df['y'],
order=["a", "b"], hue='color',
hue_order=['red', 'blue'])

def test_unmatched_x_in_box_pairs_with_hue(self):
with self.assertRaisesRegex(ValueError, "(specified in `pairs`)"):
self.annot = Annotator(self.ax, [(("c", "blue"), ("b", "blue"))],
Expand Down Expand Up @@ -159,7 +176,7 @@ def test_get_annotation_text_in_input_order(self):
self.assertEqual(expected, self.annot.get_annotations_text())

def test_init_df_inverted(self):
box_pairs = self.pairs_for_df[::-1]
box_pairs = self.pairs[::-1]
self.ax = sns.boxplot(**self.params_df)
self.annot = Annotator(self.ax, pairs=box_pairs, **self.params_df)

Expand All @@ -179,7 +196,7 @@ def test_apply_no_apply_warns(self):
self.annot.apply_and_annotate()

self.ax = sns.boxplot(**self.params_df)
self.annot.new_plot(self.ax, self.pairs_for_df, **self.params_df)
self.annot.new_plot(self.ax, self.pairs, **self.params_df)
self.annot.configure(test="Levene", text_format="simple")
with self.assertWarns(UserWarning):
self.annot.annotate()
Expand All @@ -190,7 +207,7 @@ def test_apply_apply_no_warns(self):
self.annot.apply_and_annotate()

self.ax = sns.boxplot(**self.params_df)
self.annot.new_plot(self.ax, self.pairs_for_df, **self.params_df)
self.annot.new_plot(self.ax, self.pairs, **self.params_df)
self.annot.configure(test="Mann-Whitney-gt", text_format="simple")
self.annot.apply_and_annotate()

Expand Down
18 changes: 18 additions & 0 deletions tests/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def setUp(self) -> None:
"y": "y",
"hue": 'color',
}

self.plotting_arrays = {
"data": None,
"x": self.df["x"],
"y": self.df["y"],
"hue": self.df["color"],
}

self.pairs = [(("a", "blue"), ("a", "red")),
(("b", "blue"), ("b", "red")),
(("a", "blue"), ("b", "blue"))]
Expand Down Expand Up @@ -61,3 +69,13 @@ def test_seaborn_plots_horizontal(self):
ax = getattr(sns, plotter)(**plotting)
_SeabornPlotter(ax, self.pairs, plot=plotter, verbose=False,
**plotting)

def test_seaborn_plots_with_arrays(self):
for plotter in IMPLEMENTED_PLOTTERS["seaborn"]:
if plotter in ("stripplot", "swarmplot"):
plotting = {**self.plotting_arrays, "dodge": True}
else:
plotting = self.plotting_arrays
ax = getattr(sns, plotter)(**plotting)
_SeabornPlotter(ax, self.pairs, plot=plotter, verbose=False,
**plotting)
41 changes: 0 additions & 41 deletions tests/test_plotter_no_df.py

This file was deleted.

0 comments on commit 1524df8

Please sign in to comment.