本文整理汇总了Python中experiments.experiment_counters.ExperimentCounter.goal_distribution方法的典型用法代码示例。如果您正苦于以下问题:Python ExperimentCounter.goal_distribution方法的具体用法?Python ExperimentCounter.goal_distribution怎么用?Python ExperimentCounter.goal_distribution使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类experiments.experiment_counters.ExperimentCounter
的用法示例。
在下文中一共展示了ExperimentCounter.goal_distribution方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WebUserTests
# 需要导入模块: from experiments.experiment_counters import ExperimentCounter [as 别名]
# 或者: from experiments.experiment_counters.ExperimentCounter import goal_distribution [as 别名]
class WebUserTests(object):
def setUp(self):
self.experiment = Experiment(name=EXPERIMENT_NAME, state=ENABLED_STATE)
self.experiment.save()
self.request = request_factory.get('/')
self.request.session = DatabaseSession()
self.experiment_counter = ExperimentCounter()
def tearDown(self):
self.experiment_counter.delete(self.experiment)
def test_enrollment_initially_control(self):
experiment_user = participant(self.request)
self.assertEqual(experiment_user.get_alternative(EXPERIMENT_NAME), 'control', "Default Enrollment wasn't control")
def test_user_enrolls(self):
experiment_user = participant(self.request)
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
self.assertEqual(experiment_user.get_alternative(EXPERIMENT_NAME), TEST_ALTERNATIVE, "Wrong Alternative Set")
def test_record_goal_increments_counts(self):
experiment_user = participant(self.request)
experiment_user.confirm_human()
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
self.assertEqual(self.experiment_counter.goal_count(self.experiment, TEST_ALTERNATIVE, TEST_GOAL), 0)
experiment_user.goal(TEST_GOAL)
self.assertEqual(self.experiment_counter.goal_count(self.experiment, TEST_ALTERNATIVE, TEST_GOAL), 1, "Did not increment Goal count")
def test_can_record_goal_multiple_times(self):
experiment_user = participant(self.request)
experiment_user.confirm_human()
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
experiment_user.goal(TEST_GOAL)
experiment_user.goal(TEST_GOAL)
experiment_user.goal(TEST_GOAL)
self.assertEqual(self.experiment_counter.goal_count(self.experiment, TEST_ALTERNATIVE, TEST_GOAL), 1, "Did not increment goal count correctly")
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, TEST_GOAL), {3: 1}, "Incorrect goal count distribution")
def test_counts_increment_immediately_once_confirmed_human(self):
experiment_user = participant(self.request)
experiment_user.confirm_human()
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
self.assertEqual(self.experiment_counter.participant_count(self.experiment, TEST_ALTERNATIVE), 1, "Did not count participant after confirm human")
def test_visit_increases_goal(self):
thetime = timezone.now()
with patch('experiments.utils.now', return_value=thetime):
experiment_user = participant(self.request)
experiment_user.confirm_human()
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
experiment_user.visit()
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_NOT_PRESENT_COUNT_GOAL), {1: 1}, "Not Present Visit was not correctly counted")
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_PRESENT_COUNT_GOAL), {}, "Present Visit was not correctly counted")
with patch('experiments.utils.now', return_value=thetime + timedelta(hours=7)):
experiment_user.visit()
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_NOT_PRESENT_COUNT_GOAL), {2: 1}, "No Present Visit was not correctly counted")
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_PRESENT_COUNT_GOAL), {1: 1}, "Present Visit was not correctly counted")
def test_visit_twice_increases_once(self):
experiment_user = participant(self.request)
experiment_user.confirm_human()
experiment_user.set_alternative(EXPERIMENT_NAME, TEST_ALTERNATIVE)
experiment_user.visit()
experiment_user.visit()
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_NOT_PRESENT_COUNT_GOAL), {1: 1}, "Visit was not correctly counted")
self.assertEqual(self.experiment_counter.goal_distribution(self.experiment, TEST_ALTERNATIVE, VISIT_PRESENT_COUNT_GOAL), {}, "Present Visit was not correctly counted")
def test_user_force_enrolls(self):
experiment_user = participant(self.request)
experiment_user.enroll(EXPERIMENT_NAME, ['control', 'alternative1', 'alternative2'], force_alternative='alternative2')
self.assertEqual(experiment_user.get_alternative(EXPERIMENT_NAME), 'alternative2')
def test_user_does_not_force_enroll_to_new_alternative(self):
alternatives = ['control', 'alternative1', 'alternative2']
experiment_user = participant(self.request)
experiment_user.enroll(EXPERIMENT_NAME, alternatives)
alternative = experiment_user.get_alternative(EXPERIMENT_NAME)
self.assertIsNotNone(alternative)
other_alternative = random.choice(list(set(alternatives) - set(alternative)))
experiment_user.enroll(EXPERIMENT_NAME, alternatives, force_alternative=other_alternative)
self.assertEqual(alternative, experiment_user.get_alternative(EXPERIMENT_NAME))
def test_second_force_enroll_does_not_change_alternative(self):
alternatives = ['control', 'alternative1', 'alternative2']
experiment_user = participant(self.request)
experiment_user.enroll(EXPERIMENT_NAME, alternatives, force_alternative='alternative1')
alternative = experiment_user.get_alternative(EXPERIMENT_NAME)
self.assertIsNotNone(alternative)
other_alternative = random.choice(list(set(alternatives) - set(alternative)))
experiment_user.enroll(EXPERIMENT_NAME, alternatives, force_alternative=other_alternative)
self.assertEqual(alternative, experiment_user.get_alternative(EXPERIMENT_NAME))
示例2: get_result_context
# 需要导入模块: from experiments.experiment_counters import ExperimentCounter [as 别名]
# 或者: from experiments.experiment_counters.ExperimentCounter import goal_distribution [as 别名]
def get_result_context(request, experiment):
experiment_counter = ExperimentCounter()
try:
chi2_goals = experiment.relevant_chi2_goals.replace(" ", "").split(",")
except AttributeError:
chi2_goals = [u'']
try:
mwu_goals = experiment.relevant_mwu_goals.replace(" ", "").split(",")
except AttributeError:
mwu_goals = [u'']
relevant_goals = set(chi2_goals + mwu_goals)
alternatives = {}
for alternative_name in experiment.alternatives.keys():
alternatives[alternative_name] = experiment_counter.participant_count(experiment, alternative_name)
alternatives = sorted(alternatives.items())
control_participants = experiment_counter.participant_count(experiment, conf.CONTROL_GROUP)
results = {}
for goal in conf.ALL_GOALS:
show_mwu = goal in mwu_goals
alternatives_conversions = {}
control_conversions = experiment_counter.goal_count(experiment, conf.CONTROL_GROUP, goal)
control_conversion_rate = rate(control_conversions, control_participants)
if show_mwu:
mwu_histogram = {}
control_conversion_distribution = fixup_distribution(experiment_counter.goal_distribution(experiment, conf.CONTROL_GROUP, goal), control_participants)
control_average_goal_actions = average_actions(control_conversion_distribution)
mwu_histogram['control'] = control_conversion_distribution
else:
control_average_goal_actions = None
for alternative_name in experiment.alternatives.keys():
if not alternative_name == conf.CONTROL_GROUP:
alternative_conversions = experiment_counter.goal_count(experiment, alternative_name, goal)
alternative_participants = experiment_counter.participant_count(experiment, alternative_name)
alternative_conversion_rate = rate(alternative_conversions, alternative_participants)
alternative_confidence = chi_squared_confidence(alternative_participants, alternative_conversions, control_participants, control_conversions)
if show_mwu:
alternative_conversion_distribution = fixup_distribution(experiment_counter.goal_distribution(experiment, alternative_name, goal), alternative_participants)
alternative_average_goal_actions = average_actions(alternative_conversion_distribution)
alternative_distribution_confidence = mann_whitney_confidence(alternative_conversion_distribution, control_conversion_distribution)
mwu_histogram[alternative_name] = alternative_conversion_distribution
else:
alternative_average_goal_actions = None
alternative_distribution_confidence = None
alternative = {
'conversions': alternative_conversions,
'conversion_rate': alternative_conversion_rate,
'improvement': improvement(alternative_conversion_rate, control_conversion_rate),
'confidence': alternative_confidence,
'average_goal_actions': alternative_average_goal_actions,
'mann_whitney_confidence': alternative_distribution_confidence,
}
alternatives_conversions[alternative_name] = alternative
control = {
'conversions': control_conversions,
'conversion_rate': control_conversion_rate,
'average_goal_actions': control_average_goal_actions,
}
results[goal] = {
"control": control,
"alternatives": sorted(alternatives_conversions.items()),
"relevant": goal in relevant_goals or relevant_goals == {u''},
"mwu": goal in mwu_goals,
"mwu_histogram": conversion_distributions_to_graph_table(mwu_histogram) if show_mwu else None
}
return {
'experiment': experiment.to_dict(),
'alternatives': alternatives,
'control_participants': control_participants,
'results': results,
'column_count': len(alternatives_conversions) * 3 + 2, # Horrible coupling with template design
'user_alternative': participant(request).get_alternative(experiment.name),
}