-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathUsers.py
64 lines (55 loc) · 1.92 KB
/
Users.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
import numpy as np
from util_functions import featureUniform, gaussianFeature, fileOverWriteWarning
import json
from random import choice, randint
class User():
def __init__(self, id, theta = None, CoTheta = None):
self.id = id
self.theta = theta
self.CoTheta = CoTheta
self.uncertainty = 0.0
def updateUncertainty(uncertainty):
self.uncertainty = uncertainty
class UserManager():
def __init__(self, dimension, userNum, UserGroups, thetaFunc, argv = None):
self.dimension = dimension
self.thetaFunc = thetaFunc
self.userNum = userNum
self.UserGroups = UserGroups
self.argv = argv
self.signature = "A-"+"+PA"+"+TF-"+self.thetaFunc.__name__
def saveUsers(self, users, filename, force = False):
fileOverWriteWarning(filename, force)
with open(filename, 'w') as f:
for i in range(len(users)):
print users[i].theta
f.write(json.dumps((users[i].id, users[i].theta.tolist())) + '\n')
def loadUsers(self, filename):
users = []
with open(filename, 'r') as f:
for line in f:
id, theta = json.loads(line)
users.append(User(id, np.array(theta)))
return users
def generateMasks(self):
mask = {}
for i in range(self.UserGroups):
mask[i] = np.random.randint(2, size = self.dimension)
return mask
def simulateThetafromUsers(self):
usersids = {}
users = []
mask = self.generateMasks()
if self.UserGroups >1:
for i in range(self.UserGroups):
usersids[i] = range(self.userNum*i/self.UserGroups, (self.userNum*(i+1))/self.UserGroups)
for key in usersids[i]:
thetaVector = np.multiply(self.thetaFunc(self.dimension, argv = self.argv), mask[i])
l2_norm = np.linalg.norm(thetaVector, ord =2)
users.append(User(key, thetaVector/l2_norm))
else:
for i in range(self.userNum):
thetaVector = self.thetaFunc(self.dimension, argv = self.argv)
theta_l2_norm = np.linalg.norm(thetaVector, ord =2)
users.append(User(i, thetaVector/theta_l2_norm))
return users