-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·70 lines (62 loc) · 2 KB
/
Copy pathclient.py
File metadata and controls
executable file
·70 lines (62 loc) · 2 KB
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
import os
import random
import json
from datetime import datetime, timedelta
import pickle
def pick_random_address(ADDR_PATH):
selected_blk_file = random.choice(os.listdir(ADDR_PATH))
with open(ADDR_PATH+selected_blk_file,'r') as f:
address_pool = f.readlines()
return random.choice(address_pool)
def days_addr_used(client_addr, DAYS_PATH):
days = os.listdir(DAYS_PATH)
days.sort()
used_days = {}
for day in days:
print(day)
with open(DAYS_PATH+day, 'r') as f:
day_state = json.load(f)
if client_addr in day_state.keys():
used_days[day.split('.')[0]] = day_state[client_addr]
print(used_days)
return used_days
def chunk_predict(date_dict, start_date, due_date, beta):
start_date = datetime.strptime(start_date, "%Y%m%d")
due_date = datetime.strptime(due_date, "%Y%m%d")
score = 0
day_step = timedelta(days=1)
d = start_date
while d <= due_date:
if(d.strftime("%Y%m%d") in date_dict):
score = int((1-beta) * score + beta * date_dict[d.strftime("%Y%m%d")])
print(d.strftime("%Y%m%d"), date_dict[d.strftime("%Y%m%d")])
else:
score = int((1-beta) *score)
d += day_step
print(score)
return score
if __name__ == "__main__":
beta = 0.3
ADDR_PATH = 'Addresses/'
DAYS_PATH = 'Days/Jsons/'
CLIENTS_PATH = 'Clients/'
IS_PREPROC_ENABLE = True
#Find wich days address used
if(not IS_PREPROC_ENABLE):
#Pick a random address
client_addr = pick_random_address(ADDR_PATH)
print("client_addr: ", client_addr) # to remove newline
date_dict = days_addr_used(client_addr, DAYS_PATH)
with open(CLIENTS_PATH+client_addr+'.json', 'w') as f:
json.dump(date_dict, f)
else:
client_addr = 'Return Operation' + '\n'
with open(CLIENTS_PATH+client_addr+'.json', 'r') as f:
date_dict = json.load(f)
# print(date_dict)
#Predict chunk
predicted_score = chunk_predict(date_dict, '20190627', '20190807', beta)
print("predicted_score: ", predicted_score)
with open('Analysis/Values/0.3/20190807-state', 'rb') as f:
state , _ = pickle.load(f)
print("real_score: ", state[client_addr])