Skip to content

Commit

Permalink
Merge branch 'trafficlight-feedback-backend' of github.com:ssciwr/mon…
Browse files Browse the repository at this point in the history
…dey-frontend-prototype into trafficlight-feedback-backend
  • Loading branch information
MaHaWo committed Dec 5, 2024
2 parents 9106a4d + b174285 commit 82bdfc5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mondey_backend/src/mondey_backend/routers/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def leq(val: float, lim: float) -> bool:
else:
lim_lower = stat.avg_score - 2 * stat.stddev_score
lim_upper = stat.avg_score - stat.stddev_score
print('eval: ', lim_lower, lim_upper, stat.avg_score, score)
print("eval: ", lim_lower, lim_upper, stat.avg_score, score)
if leq(score, lim_lower):
return TrafficLight.red.value
elif score > lim_lower and leq(score, lim_upper):
Expand Down Expand Up @@ -133,7 +133,7 @@ def compute_milestonegroup_feedback_summary(
MilestoneGroupAgeScoreCollection.milestone_group_id == group
)
).first()
if stats is not None:
# if stats is not None:
# print('stats before: ', stats, stats.scores[age])
if stats is None or stats.created_at < today - timedelta(days=7):
new_stats = calculate_milestonegroup_statistics_by_age(session, group)
Expand Down
16 changes: 9 additions & 7 deletions mondey_backend/src/mondey_backend/routers/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def calculate_milestone_statistics_by_age(
if last_statistics is None:
# no statistics exists yet -> all answers from expired sessions are relevant
answers_query = (
select(MilestoneAnswer).join(
select(MilestoneAnswer)
.join(
MilestoneAnswerSession,
MilestoneAnswer.answer_session_id == MilestoneAnswerSession.id,
)
Expand Down Expand Up @@ -219,7 +220,8 @@ def calculate_milestonegroup_statistics_by_age(
# print(' no statistics')
# no statistics exists yet -> all answers from expired sessions are relevant
answer_query = (
select(MilestoneAnswer).join(
select(MilestoneAnswer)
.join(
MilestoneAnswerSession,
MilestoneAnswer.answer_session_id == MilestoneAnswerSession.id,
)
Expand All @@ -230,7 +232,7 @@ def calculate_milestonegroup_statistics_by_age(
)
)
else:
# print(' statistics exists')
# print(' statistics exists')
# we calculate the statistics with an online algorithm, so we only consider new data
# that has not been included in the last statistics but which stems from sessions that are expired
answer_query = (
Expand All @@ -249,10 +251,10 @@ def calculate_milestonegroup_statistics_by_age(
)
# all_answers = session.exec(select(MilestoneAnswer)).all()
# print('weird join: ', session.exec(select(MilestoneAnswer)
# .join(
# MilestoneAnswerSession,
# MilestoneAnswer.answer_session_id == MilestoneAnswerSession.id,
# )).all())
# .join(
# MilestoneAnswerSession,
# MilestoneAnswer.answer_session_id == MilestoneAnswerSession.id,
# )).all())
# print(' all answers: ', all_answers)
answers = session.exec(answer_query).all()
# print(' last statistics: ', last_statistics)
Expand Down
8 changes: 3 additions & 5 deletions mondey_backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,15 @@ def statistics_session(session):
)

# add another expired answersession for milestone 7 for child 3 that is a bit later
# than answersession 3 (the last one for the same child), but still expired
# than answersession 3 (the last one for the same child), but still expired
session.add(
MilestoneAnswerSession(
child_id=3,
user_id=1,
created_at=datetime.datetime(today.year - 1, 1, 10)
child_id=3, user_id=1, created_at=datetime.datetime(today.year - 1, 1, 10)
)
)
session.add(
MilestoneAnswer(
answer_session_id=5, milestone_id=7, milestone_group_id=2, answer=1
answer_session_id=5, milestone_id=7, milestone_group_id=2, answer=1
)
)

Expand Down
21 changes: 12 additions & 9 deletions mondey_backend/tests/utils/test_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def test_compute_feedback_simple():
)


def test_compute_summary_milestonegroup_feedback_for_answersession_with_recompute(statistics_session):
def test_compute_summary_milestonegroup_feedback_for_answersession_with_recompute(
statistics_session,
):
# there is an existing statistics for milestonegroup 1, which has milestones 1 and 2
# which gives mean = 1.92 and stddev = 0.21, and we have 2 additional answers for these m
# milestones with answers 3 and 2 for milestones 1 and 2 respectively. ==> statistics
# changes to mean = 2.446 +/- 0.89. The first call updates the statistics with the new
# milestones with answers 3 and 2 for milestones 1 and 2 respectively. ==> statistics
# changes to mean = 2.446 +/- 0.89. The first call updates the statistics with the new
# values, the second does not.
feedback = compute_milestonegroup_feedback_summary(
statistics_session, child_id=1, answersession_id=1
Expand All @@ -73,17 +75,18 @@ def test_compute_summary_milestonegroup_feedback_for_answersession_with_recomput
assert len(feedback) == 1
assert feedback[1] == TrafficLight.green.value


def test_compute_summary_milestonegroup_feedback_for_answersession_no_existing_stat(
statistics_session,
):
# there is only 2 answer sfor milestonegroup 2 which only has milestone 7.
# these 2 are from 2 answersessions which are 10 days apart so fall into the
# same age group => the feedback has only one entry for milestonegroup 2
# there is only 2 answer sfor milestonegroup 2 which only has milestone 7.
# these 2 are from 2 answersessions which are 10 days apart so fall into the
# same age group => the feedback has only one entry for milestonegroup 2
# and because the answers are 3 and 2 -> avg = 2.5 +/- 0.7071 -> green for answer = 3
feedback = compute_milestonegroup_feedback_summary(
statistics_session, child_id=3, answersession_id=3
)

assert len(feedback) == 1
assert feedback[2] == TrafficLight.green.value

Expand All @@ -96,7 +99,7 @@ def test_compute_summary_milestonegroup_feedback_for_answersession_no_existing_s
assert len(statistics) == 1
assert statistics[0].created_at >= datetime.now() - timedelta(
minutes=1
) # can be at max 1 min old
) # can be at max 1 min old

assert statistics[0].scores[42].count == 2
assert np.isclose(statistics[0].scores[42].avg_score, 2.5)
Expand Down Expand Up @@ -124,4 +127,4 @@ def test_compute_detailed_milestonegroup_feedback_for_answersession_no_stat(stat
)

assert len(feedback) == 1
assert feedback[2][7] == TrafficLight.green.green.value
assert feedback[2][7] == TrafficLight.green.green.value
13 changes: 8 additions & 5 deletions mondey_backend/tests/utils/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_online_statistics_computation():
data_second = data[100:200]

count = 0
avg = 0.
var = 0.
avg = 0.0
var = 0.0

for v in data_first:
count, avg, var = _add_sample(count, avg, var, v)
Expand Down Expand Up @@ -91,7 +91,11 @@ def test_get_score_statistics_by_age(session):
answers = session.exec(select(MilestoneAnswer)).all()
print(answers)
# which answers we choose here is arbitrary for testing, we just need to make sure it's fixed and not empty
child_ages = {1: 5, 2: 3, 3: 8,}
child_ages = {
1: 5,
2: 3,
3: 8,
}

count, avg, stddev = _get_statistics_by_age(answers, child_ages)

Expand All @@ -103,7 +107,7 @@ def test_get_score_statistics_by_age(session):
]
answers_8 = [
answer.answer + 1 for answer in answers if answer.answer_session_id == 3
]
]

assert count[5] == 2
assert count[3] == 2
Expand Down Expand Up @@ -139,7 +143,6 @@ def test_get_score_statistics_by_age(session):
),
)


# check that updating works correctly. This is not done for all sessions
second_answers = answers
for answer in second_answers:
Expand Down

0 comments on commit 82bdfc5

Please sign in to comment.