Skip to content

Commit

Permalink
Merge pull request #12 from transferwise/linting
Browse files Browse the repository at this point in the history
linting round 3
  • Loading branch information
julianteichgraber authored Jan 22, 2024
2 parents c8c6177 + e589667 commit dda2c98
Show file tree
Hide file tree
Showing 31 changed files with 355 additions and 441 deletions.
10 changes: 9 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ exclude =
docs
build
dist
venv
venv

per-file-ignores =
tw_experimentation/streamlit/pages_wrap/page4_Experiment_Evaluation.py:E501
tw_experimentation/streamlit/pages_wrap/page3_Monitoring.py:E501
tw_experimentation/streamlit/pages_wrap/page2_Experiment_Design.py:E501
tw_experimentation/streamlit/pages_wrap/page1_Data_Loading.py:E501
tw_experimentation/streamlit/pages_wrap/page5_Experiment_Evaluation_Bayesian.py:E501
tw_experimentation/streamlit/Main.py:E501
19 changes: 8 additions & 11 deletions notebooks/2_integrity_checks + evaluation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,18 @@
"metadata": {},
"outputs": [],
"source": [
"import sys \n",
"import sys\n",
"import os\n",
"root_path = os.path.realpath('..')\n",
"sys.path.insert(0, root_path)\n",
"\n",
"from tw_experimentation.utils import ExperimentDataset, variantname_color_map, variant_name_map\n",
"from tw_experimentation.utils import ExperimentDataset\n",
"from tw_experimentation.data_generation import RevenueConversion\n",
"from tw_experimentation.widgetizer import FrequentistEvaluation, SegmentationInterface, Segmentation\n",
"from tw_experimentation.statistical_tests import cuped\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import pickle\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import plotly.io as pio\n",
"pio.renderers.default = \"png\"\n",
Expand Down Expand Up @@ -93,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -104,14 +101,14 @@
"\n",
"rc = RevenueConversion()\n",
"\n",
"df_abn = rc.generate_data_abn_test( \n",
"df_abn = rc.generate_data_abn_test(\n",
" n=10000,\n",
" n_treatments = 2,\n",
" baseline_conversion = .4,\n",
" treatment_effect_conversion=.04,\n",
" treatment_effect_conversion=.02,\n",
" baseline_mean_revenue = 6,\n",
" sigma_revenue = 1,\n",
" treatment_effect_revenue = .1 \n",
" treatment_effect_revenue = .05\n",
")\n",
"\n",
"df_abn['currency'] = np.random.choice(['GBP', 'EUR', 'USD'], size=len(df_abn))\n",
Expand Down Expand Up @@ -235,7 +232,7 @@
"# ]\n",
"\n",
"# metrics = [ # Specify metrics types, or leave empty to infer automatically\n",
"# 'binary', \n",
"# 'binary',\n",
"# 'continuous'\n",
"# ]\n",
"\n",
Expand Down Expand Up @@ -1167,7 +1164,7 @@
"segmentation = Segmentation(ed=experiment_dataset)\n",
"wise_pizza_segmentation = segmentation.wise_pizza_frequentist(\n",
" target='revenue',\n",
" treatment=treatment, \n",
" treatment=treatment,\n",
" segments=segments,\n",
" max_depth=2, # customize to own preference\n",
" min_segments=3 # customize to own preference\n",
Expand Down
13 changes: 8 additions & 5 deletions tests/test_bayes_in_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,27 @@ def test_bayestest_end(experiment_data_two_treatment):
br.fig_posterior_difference_by_target(outcome_metric)

# greater than zero probability
description = f"The probability that the average treatment effect is greater than 0 for {outcome_metric} is\n"
description = f"The probability that the average treatment effect is greater than 0 for {outcome_metric} is\n" # noqa: E501
for variant in range(1, ed.n_variants):
description += f" {br.prob_greater_than_zero(outcome_metric)[variant]*100}% for variant {variant}"
description += f" {br.prob_greater_than_zero(outcome_metric)[variant]*100}% for variant {variant}" # noqa: E501
description += "." if variant == ed.n_variants - 1 else ",\n"
print(description)

# greater than threshold probability
threshold = 100
description = f"The probability that the average treatment effect is greater than {threshold} for {outcome_metric} is\n"
description = "The probability that the average treatment effect is greater than "
f"{threshold} for {outcome_metric} is\n"
for variant in range(1, ed.n_variants):
description += f" {br.prob_greater_than_z(threshold, outcome_metric)[variant]*100}% for variant {variant}"
description += f" {br.prob_greater_than_z(threshold, outcome_metric)[variant]*100}% for variant {variant}" # noqa: E501
description += "." if variant == ed.n_variants - 1 else ",\n"
print(description)

# rope probability
probs, rope_lower, rope_upper = br.rope(outcome_metric)

description = f"The probability that the average treatment effect is outside the region of practical equivalence ({rope_lower},{rope_upper}) for {outcome_metric} is\n"
description = "The probability that the average treatment effect is outside"
" the region of practical equivalence"
f" ({rope_lower},{rope_upper}) for {outcome_metric} is\n"
for variant in range(1, ed.n_variants):
description += f" {probs[variant]*100:.2f}% for variant {variant}"
description += "." if variant == ed.n_variants - 1 else ",\n"
Expand Down
3 changes: 0 additions & 3 deletions tests/test_bayestest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import pytest
import warnings

from tw_experimentation.utils import ExperimentDataset

from tw_experimentation.bayes.bayes_test import BayesTest
from tw_experimentation.data_generation import RevenueConversion

from numpyro.distributions import LogNormal, Gamma, Uniform


class TestBayesTesterEndToEnd(object):
def test_with_new_model(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def test_check_preprocessor(experiment_data):
"""checks if dataset can be preprocessed (dummy encoding of vars etc...)
Args:
df (pd.DataFrame): dataset for experiments, with cols for treatment, targets and covariates
df (pd.DataFrame): dataset for experiments, with cols for treatment,
targets and covariates
"""
ed = experiment_data
ed.preprocess_dataset()
Expand Down
10 changes: 6 additions & 4 deletions tests/test_experiment_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def experiment_data():


def test_check_preprocessor(experiment_data):
"""checks if dataset can be preprocessed (dummy encoding of vars etc...)
"""checks if dataset can be preprocessed
(dummy encoding of vars etc...)
Args:
df (pd.DataFrame): dataset for experiments, with cols for treatment, targets and covariates
df (pd.DataFrame): dataset for experiments, with cols
for treatment, targets and covariates
"""
ed = experiment_data
ed.preprocess_pre_experiment_dataset()
Expand Down Expand Up @@ -58,7 +60,7 @@ def test_auto_sample_size_filler(experiment_data):
relation="relative",
treatment_share=0.5,
)
sample_size = setup.sample_size_two_sample_proportion_z_test()
_ = setup.sample_size_two_sample_proportion_z_test()
elif exp_design_metric_type == "continuous":
setup = Setuper.from_uplift(
alpha=0.1,
Expand All @@ -69,7 +71,7 @@ def test_auto_sample_size_filler(experiment_data):
relation="relative",
treatment_share=0.4,
)
sample_size = setup.sample_size_t_test()
setup.sample_size_t_test()
else:
raise ValueError("metric type not supported")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_frequentist_hypothesis_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest

from pandas import testing as tm

from tw_experimentation.utils import ExperimentDataset
from tw_experimentation.data_generation import RevenueConversion

Expand All @@ -10,6 +8,8 @@
compute_frequentist_results,
)

from pandas import testing as tm


@pytest.fixture
def experiment_data_continuous(request):
Expand Down
28 changes: 1 addition & 27 deletions tests/test_frequentist_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from tw_experimentation.data_generation import RevenueConversion

from tw_experimentation.statistical_tests import FrequentistTest, BaseTest

from tw_experimentation.widgetizer import (
FrequentistEvaluation,
Segmentation,
SegmentationInterface,
)

from tw_experimentation.statistical_tests import cuped
Expand Down Expand Up @@ -142,30 +141,5 @@ def test_frequentist_in_notebook(experiment_data_two_treatment):
cuped(experiment_data_two_treatment, has_correction="Yes", alpha=0.05)


def test_frequentist_wise_pizza_segmentation_in_notebook(experiment_data_with_segments):
segments = ["segment_1", "segment_2", "segment_3", "currency"]

# change to treatment number of interest
treatment = 1

segmentation = Segmentation(ed=experiment_data_with_segments)
wise_pizza_segmentation = segmentation.wise_pizza_frequentist(
target="conversion",
treatment=treatment,
segments=segments,
max_depth=2, # customize to own preference
min_segments=10, # customize to own preference
)

slice_finder = segmentation.get_sf()
slice_finder.plot(width=1000, plot_is_static=True)


def test_frequentist_custom_segmentation_in_notebook(experiment_data_with_segments):
segments = ["segment_1", "segment_2", "segment_3", "currency"]

_ = SegmentationInterface(experiment_data_with_segments)


if __name__ == "__main__":
pytest.main([__file__])
9 changes: 4 additions & 5 deletions tests/test_integrity_checks_in_notebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

import pandas as pd
import numpy as np
from tw_experimentation.utils import ExperimentDataset
from tw_experimentation.data_generation import RevenueConversion
Expand Down Expand Up @@ -61,13 +60,13 @@ def test_monitoring_segments_in_notebook(experiment_data_with_segments):
# choose segments to monitor
segments = ["currency", "segment_1", "segment_2"]
segment_monitor = SegmentMonitoring(ed, segments)
segment_monitor_results = segment_monitor.create_tables_and_plots()
_ = segment_monitor.create_tables_and_plots()


def test_monitoring_in_notebook(experiment_data_with_segments):
ed = experiment_data_with_segments
monitor = Monitoring(ed)
monitor_results = monitor.create_tables_and_plots()
_ = monitor.create_tables_and_plots()


def test_normality_checks(experiment_data_with_segments):
Expand All @@ -84,8 +83,8 @@ def test_sequential_test_in_notebook(experiment_data_with_segments):
ed = experiment_data_with_segments
if ed.is_dynamic_observation:
sequential_test = SequentialTest(ed)
sequential_test_results = sequential_test.sequential_test_results()
fig = sequential_test.fig_sequential_test()
sequential_test.sequential_test_results()
_ = sequential_test.fig_sequential_test()


if __name__ == "__main__":
Expand Down
3 changes: 0 additions & 3 deletions tests/test_numpyro_sampler_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
from numpyro.infer import MCMC, NUTS


from tw_experimentation.statistical_tests import *


class TestNumpyroSampler(object):
@pytest.fixture
def data_input(self):
Expand Down
Loading

0 comments on commit dda2c98

Please sign in to comment.