From dd4c0004962ebda84bbb95fa1cb50813c0b88674 Mon Sep 17 00:00:00 2001 From: Alexander Sundin Date: Fri, 13 Sep 2024 14:07:12 +0200 Subject: [PATCH] adding new release and formatting from make black --- HISTORY.rst | 5 +++++ README.md | 2 +- setup.cfg | 2 +- .../analysis/frequentist/chartify_grapher.py | 14 ++++++++------ .../confidence_computers/confidence_computer.py | 10 +++------- .../confidence_computers/z_test_computer.py | 8 +++++--- .../analysis/frequentist/multiple_comparison.py | 17 ++++++----------- .../analysis/frequentist/nims_and_mdes.py | 8 +++++--- tests/frequentist/test_ztest.py | 8 ++++---- 9 files changed, 38 insertions(+), 36 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c55a904..b56ea39 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,11 @@ History ======= +3.0.4 (2023-10-12) +------------------ +* Fixing so that other multiple correction methods than Bonferroni are applied correctly. + + 3.0.3 (2023-10-12) ------------------ * Relaxing version requirements for scipy and pandas to allow versions 2.x diff --git a/README.md b/README.md index 3a54a49..76cd0fe 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Spotify Confidence ======== ![Status](https://img.shields.io/badge/Status-Beta-blue.svg) -![Latest release](https://img.shields.io/badge/release-3.0.3-green.svg "Latest release: 3.0.3") +![Latest release](https://img.shields.io/badge/release-3.0.3-green.svg "Latest release: 3.0.4") ![Python](https://img.shields.io/badge/Python-3.7-blue.svg "Python") ![Python](https://img.shields.io/badge/Python-3.8-blue.svg "Python") ![Python](https://img.shields.io/badge/Python-3.9-blue.svg "Python") diff --git a/setup.cfg b/setup.cfg index ee31e27..918c3cb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = spotify-confidence -version = 3.0.3 +version = 3.0.4 author = Per Sillren author_email = pers@spotify.com description = Package for calculating and visualising confidence intervals, e.g. for A/B test analysis. diff --git a/spotify_confidence/analysis/frequentist/chartify_grapher.py b/spotify_confidence/analysis/frequentist/chartify_grapher.py index 3a297c1..874a5da 100644 --- a/spotify_confidence/analysis/frequentist/chartify_grapher.py +++ b/spotify_confidence/analysis/frequentist/chartify_grapher.py @@ -278,12 +278,14 @@ def _categorical_difference_chart( df[~df[NIM].isna()] .assign( color_column=lambda df: df.apply( - lambda row: "red" - if row[LOWER] < row[NULL_HYPOTHESIS] - and row[PREFERENCE] == "increase" - or row[NULL_HYPOTHESIS] < row[UPPER] - and row[PREFERENCE] == "decrease" - else "green", + lambda row: ( + "red" + if row[LOWER] < row[NULL_HYPOTHESIS] + and row[PREFERENCE] == "increase" + or row[NULL_HYPOTHESIS] < row[UPPER] + and row[PREFERENCE] == "decrease" + else "green" + ), axis=1, ) ) diff --git a/spotify_confidence/analysis/frequentist/confidence_computers/confidence_computer.py b/spotify_confidence/analysis/frequentist/confidence_computers/confidence_computer.py index ac0bfaf..4c47c22 100644 --- a/spotify_confidence/analysis/frequentist/confidence_computers/confidence_computer.py +++ b/spotify_confidence/analysis/frequentist/confidence_computers/confidence_computer.py @@ -588,17 +588,13 @@ def _add_variance_reduction_rate(df: DataFrame, **kwargs: Dict) -> DataFrame: def _add_p_value(df: DataFrame, **kwargs: Dict) -> DataFrame: - return ( - df.pipe(set_alpha_and_adjust_preference, **kwargs) - .assign(**{P_VALUE: lambda df: df.pipe(_p_value, **kwargs)}) + return df.pipe(set_alpha_and_adjust_preference, **kwargs).assign( + **{P_VALUE: lambda df: df.pipe(_p_value, **kwargs)} ) def _add_ci_and_adjust_if_absolute(df: DataFrame, **kwargs: Dict) -> DataFrame: - return ( - df.pipe(add_ci, **kwargs) - .pipe(_adjust_if_absolute, absolute=kwargs[ABSOLUTE]) - ) + return df.pipe(add_ci, **kwargs).pipe(_adjust_if_absolute, absolute=kwargs[ABSOLUTE]) def _adjust_if_absolute(df: DataFrame, absolute: bool) -> DataFrame: diff --git a/spotify_confidence/analysis/frequentist/confidence_computers/z_test_computer.py b/spotify_confidence/analysis/frequentist/confidence_computers/z_test_computer.py index cbb0319..7f0395d 100644 --- a/spotify_confidence/analysis/frequentist/confidence_computers/z_test_computer.py +++ b/spotify_confidence/analysis/frequentist/confidence_computers/z_test_computer.py @@ -141,9 +141,11 @@ def adjusted_alphas_for_group(grp: DataFrame) -> Series: .assign( **{ ADJUSTED_ALPHA: lambda df: df.apply( - lambda row: 2 * (1 - st.norm.cdf(row["zb"])) - if (grp[PREFERENCE_TEST] == TWO_SIDED).all() - else 1 - st.norm.cdf(row["zb"]), + lambda row: ( + 2 * (1 - st.norm.cdf(row["zb"])) + if (grp[PREFERENCE_TEST] == TWO_SIDED).all() + else 1 - st.norm.cdf(row["zb"]) + ), axis=1, ) } diff --git a/spotify_confidence/analysis/frequentist/multiple_comparison.py b/spotify_confidence/analysis/frequentist/multiple_comparison.py index a8130ec..65cdc46 100644 --- a/spotify_confidence/analysis/frequentist/multiple_comparison.py +++ b/spotify_confidence/analysis/frequentist/multiple_comparison.py @@ -4,9 +4,7 @@ from pandas import DataFrame from statsmodels.stats.multitest import multipletests -from spotify_confidence.analysis.confidence_utils import ( - groupbyApplyParallel -) +from spotify_confidence.analysis.confidence_utils import groupbyApplyParallel from spotify_confidence.analysis.constants import ( BONFERRONI, BONFERRONI_ONLY_COUNT_TWOSIDED, @@ -154,13 +152,10 @@ def add_adjusted_p_and_is_significant(df: DataFrame, **kwargs: Dict) -> DataFram for column in df.index.names if kwargs[ORDINAL_GROUP_COLUMN] is not None and column is not None - and (column != kwargs[ORDINAL_GROUP_COLUMN] - or kwargs[FINAL_EXPECTED_SAMPLE_SIZE] is None) + and (column != kwargs[ORDINAL_GROUP_COLUMN] or kwargs[FINAL_EXPECTED_SAMPLE_SIZE] is None) ] df = groupbyApplyParallel( - df.groupby( - groups_except_ordinal + [kwargs[METHOD], "level_1", "level_2"], as_index=False, sort=False - ), + df.groupby(groups_except_ordinal + [kwargs[METHOD], "level_1", "level_2"], as_index=False, sort=False), lambda df: compute_sequential_adjusted_alpha(df, **kwargs), ) elif kwargs[CORRECTION_METHOD] in [ @@ -273,9 +268,9 @@ def set_alpha_and_adjust_preference(df: DataFrame, **kwargs: Dict) -> DataFrame: return df.assign( **{ ALPHA: df.apply( - lambda row: 2 * alpha_0 - if kwargs[CORRECTION_METHOD] == SPOT_1 and row[PREFERENCE] != TWO_SIDED - else alpha_0, + lambda row: ( + 2 * alpha_0 if kwargs[CORRECTION_METHOD] == SPOT_1 and row[PREFERENCE] != TWO_SIDED else alpha_0 + ), axis=1, ) } diff --git a/spotify_confidence/analysis/frequentist/nims_and_mdes.py b/spotify_confidence/analysis/frequentist/nims_and_mdes.py index 5ffdbb5..7bfc20f 100644 --- a/spotify_confidence/analysis/frequentist/nims_and_mdes.py +++ b/spotify_confidence/analysis/frequentist/nims_and_mdes.py @@ -29,9 +29,11 @@ def add_nim_input_columns_from_tuple_or_dict(df, nims: NIM_TYPE, mde_column: str elif nims is None or not nims: return df.assign(**{NIM_COLUMN_DEFAULT: None}).assign( **{ - PREFERRED_DIRECTION_COLUMN_DEFAULT: None - if PREFERRED_DIRECTION_COLUMN_DEFAULT not in df or mde_column is None - else df[PREFERRED_DIRECTION_COLUMN_DEFAULT] + PREFERRED_DIRECTION_COLUMN_DEFAULT: ( + None + if PREFERRED_DIRECTION_COLUMN_DEFAULT not in df or mde_column is None + else df[PREFERRED_DIRECTION_COLUMN_DEFAULT] + ) } ) else: diff --git a/tests/frequentist/test_ztest.py b/tests/frequentist/test_ztest.py index a67d72d..b03b4f1 100644 --- a/tests/frequentist/test_ztest.py +++ b/tests/frequentist/test_ztest.py @@ -1541,7 +1541,7 @@ def test_multiple_difference(self, correction_method): BONFERRONI, BONFERRONI_ONLY_COUNT_TWOSIDED, BONFERRONI_DO_NOT_COUNT_NON_INFERIORITY, - SPOT_1 + SPOT_1, ]: difference_df = self.test.multiple_difference( level=("control", 1), groupby="country", level_as_reference=True @@ -1584,7 +1584,7 @@ def test_multiple_difference(self, correction_method): _, adjusted_p, _, _ = multipletests( pvals=difference_df["p-value"], alpha=1 - self.test._confidence_computer._interval_size, - method=corr_method + method=corr_method, ) assert np.allclose( @@ -1600,7 +1600,7 @@ def test_multiple_difference_groupby(self, correction_method): BONFERRONI, BONFERRONI_ONLY_COUNT_TWOSIDED, BONFERRONI_DO_NOT_COUNT_NON_INFERIORITY, - SPOT_1 + SPOT_1, ]: difference_df = self.test.multiple_difference( level="control", groupby=["days_since_reg", "country"], level_as_reference=True @@ -1640,7 +1640,7 @@ def test_multiple_difference_groupby(self, correction_method): _, adjusted_p, _, _ = multipletests( pvals=difference_df["p-value"], alpha=1 - self.test._confidence_computer._interval_size, - method=corr_method + method=corr_method, ) assert np.allclose(