forked from lilyguibessette/bwh-pharmacoepi-roybal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriverReward.py
89 lines (76 loc) · 3.91 KB
/
driverReward.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# <reward>
import sys
import time
from azure.cognitiveservices.personalizer import PersonalizerClient
from azure.cognitiveservices.personalizer.models import RankRequest
from msrest.authentication import CognitiveServicesCredentials
import pandas as pd
import numpy as np
import math
import time
from datetime import datetime, date, timedelta
import pytz
from collections import Counter
import string
import pickle
import json
import os
from datetime import date
import http.client, urllib.request, urllib.parse, urllib.error, base64
from exe_functions import build_path
def get_reward_update(pt_data, run_time):
fp = build_path("000_RewardData", str(run_time.date()) + "_reward_updates.csv")
today = run_time.date()
two_day_ago = (run_time - timedelta(days=2)).date()
yesterday = (run_time - timedelta(days=1)).date()
# Subset updated_pt_dict to what we need for reward calls and put in dataframe
# create an Empty DataFrame object
column_values = ['reward', 'frame_id', 'history_id', 'social_id', 'content_id', 'reflective_id', 'record_id', 'trial_day_counter',
'flag_send_reward_value_tX']
reward_updates = pd.DataFrame(columns=column_values)
for pt,data_row in pt_data.iterrows():
# Reward value, Rank_Id's
if(data_row["flag_send_reward_value_t0"] == True and data_row["censor_date"] >= today):
reward_row_t0 = [data_row["reward_value_t0"], data_row["rank_id_framing_t0"], data_row["rank_id_history_t0"],
data_row["rank_id_social_t0"], data_row["rank_id_content_t0"], data_row["rank_id_reflective_t0"],
data_row["record_id"], data_row["trial_day_counter"], "flag_send_reward_value_t0"]
reward_updates.loc[len(reward_updates)] = reward_row_t0
if(data_row["flag_send_reward_value_t1"] == True and data_row["censor_date"] >= yesterday):
reward_row_t1 = [data_row["reward_value_t1"], data_row["rank_id_framing_t1"], data_row["rank_id_history_t1"],
data_row["rank_id_social_t1"], data_row["rank_id_content_t1"], data_row["rank_id_reflective_t1"],
data_row["record_id"], data_row["trial_day_counter"], "flag_send_reward_value_t1"]
reward_updates.loc[len(reward_updates)] = reward_row_t1
# Write csv as a log for what we're sending to Personalizer
reward_updates.to_csv(fp, index=False)
reward_updates = reward_updates.to_numpy()
return reward_updates
def send_rewards(reward_updates, client):
# column_values = ['reward', '
# frame_id', 'history_id', 'social_id', 'content_id', 'reflective_id',
# 'study_id', 'trial_day_counter']
for i in range(0,reward_updates.shape[0]):
row = reward_updates[i, :]
reward_val = row[0]
for j in range(1,6):
if isinstance(row[j],str):
print("reward_val: ", reward_val)
print("event_id: ", row[j])
client.events.reward(event_id=row[j], value=reward_val)
##############---- If checking for connection with Personalizer ###############
# headers = {
# # Request headers
# 'Content-Type': 'application/json-patch+json',
# 'Ocp-Apim-Subscription-Key': '{subscription key}',
# }
# params = urllib.parse.urlencode({
# })
# try:
# conn = http.client.HTTPSConnection('westus2.api.cognitive.microsoft.com')
# conn.request("POST", "/personalizer/v1.0/events/{eventId}/reward?%s" % params, "{body}", headers)
# response = conn.getresponse()
# data = response.read()
# print(data)
# conn.close()
# except Exception as e:
# print("[Errno {0}] {1}".format(e.errno, e.strerror))
################################################################################