示例#1
0
文件: preproc.py 项目: ebadi/preTpost
# Harmony Algorithm 2 for handling numeric attributes
t = [
    float(data['secret']['value0']),
    float(data['secret']['value1']),
    float(data['secret']['value2'])
]
d = 3
epsilon = 2.0
# **IMPORTANT** chosen by curator in both preprocessing and postprocessing
Aj = 1
# **IMPORTANT**: Instead of using the same seed in
# preprocessing and postprocesing algorithm to have identical Aj
# the curator can simply choose the Aj

# IMPORTANT: This is unique for each agent and not accessable to the curator.
# agentAnalysisSeed = 123456789
# To reproduce results set see value accordingly
# random.seed(agentAnalysisSeed)
# Aj = random.randint(0,d)
assert Aj < d

# To reproduce results set see value accordingly
prng = np.random.RandomState(None)
# To reproduce results set see value accordingly
random.seed(None)

u = np.random.choice([1, -1], 1, p=[(t[Aj] + 1.0) / 2.0, (1.0 - t[Aj]) / 2.0])

libdp.toXml(outFile, u)
示例#2
0
        return item


try:
    InLines = libdp.fromXml(preProcResult, numOutput)
except:
    libdp.log("Private Policy and Clamping",
              "Bad outputs, will be replaced by dummy values")

# Plece holder for the private policy

for i in range(0, numOutput):
    if os.path.isfile(dataDir + str(i) + '.analysesMatrix'):
        mat, fn = libdp.loadTransitionMatrix(dataDir + str(i) +
                                             '.analysesMatrix')
        # print (mat , fn , InLines)
        InLines[i] = clamp(InLines[i], fn)  # Maybe randomly pick one
        OutLines[i] = libdp.randmizer(InLines[i], mat)
    elif os.path.isfile(dataDir + str(i) + '.analysesLaplace'):
        fd = open(dataDir + str(i) + '.analysesLaplace', "r")
        epsilon = float(fd.readline())
        try:
            # Clamp into the range [+1,-1]
            InLines[i] = max(min(float(InLines[i]), 1.0), -1.0)
        except:  # if index was not defined
            InLines[i] = 0.0
        OutLines[i] = str(libdp.laplace(epsilon, InLines[i]))
    else:
        print("wtf")
libdp.toXml(clmapedRandomisedResult, OutLines)
示例#3
0
import random
import math

sys.path.append('/libdp')
import libdp

inFile = sys.argv[1]
outFile = sys.argv[2]

data = libdp.fromXml(inFile)

d = 3
epsilon = 2.0
# **IMPORTANT** chosen by curator in both preprocessing and postprocessing
Aj = 1
# **IMPORTANT**: Instead of using the same seed in
# preprocessing and postprocesing algorithm to have identical Aj
# the curator can simply choose the Aj

# IMPORTANT: This is unique for each agent and not accessable to the curator.
# agentAnalysisSeed = 123456789
# To reproduce results set see value accordingly
# random.seed(agentAnalysisSeed)
# Aj = random.randint(0,d)
assert Aj < d

out = [0.0] * d
out[Aj] = float(
    data[0]) * float(d) * (math.exp(epsilon) + 1.0) / (math.exp(epsilon) - 1.0)
libdp.toXml(outFile, out)
示例#4
0
import sys
import configparser
sys.path.append('/libdp')
import libdp

inFile = sys.argv[1]
outFile = sys.argv[2]

print(inFile)
data = configparser.ConfigParser()
data.read(inFile + 'data.ini')
#with open(inFile + 'data.ini', 'r') as fin:
#    print (fin.read())

l = [data['secret']['cat0'], data['secret']['cat1']]
libdp.toXml(outFile, l)
示例#5
0
文件: preproc.py 项目: ebadi/preTpost
    #  log('hash_input %r', value)
    #  log('Cohort %d', cohort)
    #  log('MD5 %s', md5.hexdigest())

    # python2 :
    # return [ord(digest[i]) % num_bloombits for i in xrange(num_hashes)]
    # python3 :
    return [digest[i] % num_bloombits for i in range(num_hashes)]


inFile = sys.argv[1]
outFile = sys.argv[2]
num_bloombits = int(sys.argv[3])
num_hashes = int(sys.argv[4])

data = configparser.ConfigParser()
data.read(inFile)

# python2 : secret = data['secret']['strx']
secret = str.encode(data['secret']['strx'])

cohort = 1

setBits = get_bloom_bits(secret, cohort, num_hashes, num_bloombits)
bloom = [0] * num_bloombits
for i in setBits:
    bloom[i] = 1

libdp.log('RAPPOR', secret=secret, bloombits=setBits, bloom=bloom)
libdp.toXml(outFile, bloom)
示例#6
0
# privacy parameter
epsilon = float(sys.argv[7])
# Shared seeds between preprocessing and postprocessing to generate s
SSharedRandomSeeds = int(sys.argv[8])
# This seed is used to generate identical Johnson-Lindenstrauss
# random projection matrix sharepd between the agents and the curator
PhiSharedRandomSeeds = int(sys.argv[9])

vi = int(data['secret'][x])

assert vi < d + 1 and vi > 0
assert beta > 0
# To avoid negative log
assert beta < 2
assert epsilon > 0
assert d > 1

gamma = math.sqrt(math.log(2.0 * d / beta) / epsilon**2.0 * n)
mx = math.log(float(d) + 1) * math.log(2.0 / beta) / (gamma**2)
# ceil, floor or round ?
# TODO: Better way to convert m to an integer!
m = int(math.ceil(mx))
np.random.seed(PhiSharedRandomSeeds)
phi = np.random.choice([-1.0, 1.0], size=(d, m))

# To reproduce results set see value accordingly
random.seed(SSharedRandomSeeds)
j = random.randint(0, m)
l = phi[vi][j]
libdp.toXml(outFile, [l])
示例#7
0
import configparser
import random
import numpy as np
sys.path.append('/libdp')
import libdp

inFile = sys.argv[1]
outFile = sys.argv[2]

data = configparser.ConfigParser()
data.read(inFile)

# Duchi Algorithm 1 for handling numeric attributes
t = [
    float(data['secret']['value0']),
    float(data['secret']['value1']),
    float(data['secret']['value2'])
]
d = 3

# To reproduce results set see value accordingly
prng = np.random.RandomState(None)
# To reproduce results set see value accordingly
random.seed(None)
for Aj in range(0, len(t)):
    v = np.random.choice([1, -1],
                         d,
                         p=[(t[Aj] + 1.0) / 2.0, (1.0 - t[Aj]) / 2.0])

libdp.toXml(outFile, v)
示例#8
0
# privacy parameter
epsilon = float(sys.argv[7])
# Shared seeds between preprocessing and postprocessing to generate s
SSharedRandomSeeds = int(sys.argv[8])
# This seed is used to generate identical
# Johnson-Lindenstrauss random projection matrix
# sharepd between the agents and the curator
PhiSharedRandomSeeds = int(sys.argv[9])

assert beta > 0
# To avoid negative log
assert beta < 2
assert epsilon > 0
assert d > 1

gamma = math.sqrt(math.log(2.0 * d / beta) / epsilon**2.0 * n)
mx = math.log(float(d) + 1) * math.log(2.0 / beta) / (gamma**2)
# ceil, floor or round ?
m = int(math.ceil(mx))
# TODO: Better way to convert m to an integer!

# To reproduce results set see value accordingly
random.seed(SSharedRandomSeeds)
j = random.randint(0, m)

zj = float(data[0]) * math.sqrt(m) * ((math.exp(epsilon) + 1) /
                                      (math.exp(epsilon) - 1))
o = '(' + str(j) + ',' + str(zj) + ')'

libdp.toXml(outFile, [o])