def RapporClientSim(params, irr_rand, csv_in): """Read true values from csv_in and output encoded values on csv_out.""" header = ['client', 'cohort', 'bloom', 'prr', 'irr'] out_rows = {} # TODO: It would be more instructive/efficient to construct an encoder # instance up front per client, rather than one per row below. for i, (index, client_str, cohort_str, true_value) in csv_in.iterrows(): cohort = int(cohort_str) secret = client_str e = rappor.Encoder(params, cohort, secret, irr_rand) # Real users should call e.encode(). For testing purposes, we also want # the PRR. bloom, prr, irr = e._internal_encode(true_value) bloom_str = rappor.bit_string(bloom, params.num_bloombits) prr_str = rappor.bit_string(prr, params.num_bloombits) irr_str = rappor.bit_string(irr, params.num_bloombits) out_rows[i] = [client_str, cohort_str, bloom_str, prr_str, irr_str] output = pd.DataFrame.from_dict(out_rows, orient='index') output.columns = header output.to_csv('/Users/Michael/PycharmProjects/untitled1/data/output.csv')
def GenAssocTestdata(params1, params2, irr_rand, assoc_testdata_count, csv_in, csv_out): """Read true values from csv_in and output encoded values on csv_out. Replicate assoc_testdata_count times. First value is a string, second is a bool. TODO: Generalize this. """ rows = [] for i, (true_value1, true_value2) in enumerate(csv_in): if i == 0: v1_name = true_value1 v2_name = true_value2 continue # skip header row rows.append((true_value1, true_value2)) # Use the same column names header = ('client', 'cohort', v1_name, v2_name) csv_out.writerow(header) n = assoc_testdata_count report_index = 0 for i in xrange(n): for v1, v2 in rows: client_str = 'c%d' % report_index # randint(a, b) gives i such that a <= i <= b cohort = random.randint(0, params1.num_cohorts - 1) string_encoder = rappor.Encoder(params1, cohort, client_str, irr_rand) bool_encoder = rappor.Encoder(params2, cohort, client_str, irr_rand) # Real users should call e.encode(). For testing purposes, we also want # the PRR. irr1 = string_encoder.encode(v1) # TODO: Convert to bool and encode with basic RAPPOR v2_int = int(v2) #print v2_int irr2 = bool_encoder.encode_bits(v2_int) irr1_str = rappor.bit_string(irr1, params1.num_bloombits) irr2_str = rappor.bit_string(irr2, params2.num_bloombits) csv_out.writerow((client_str, cohort, irr1_str, irr2_str)) report_index += 1
def RapporClientSim(params, irr_rand, csv_in, csv_out): """Read true values from csv_in and output encoded values on csv_out.""" header = ('client', 'cohort', 'bloom', 'prr', 'irr') csv_out.writerow(header) # TODO: It would be more instructive/efficient to construct an encoder # instance up front per client, rather than one per row below. start_time = time.time() for i, (client_str, cohort_str, true_value) in enumerate(csv_in): if i == 0: if client_str != 'client': raise RuntimeError('Expected client header, got %s' % client_str) if cohort_str != 'cohort': raise RuntimeError('Expected cohort header, got %s' % cohort_str) if true_value != 'value': raise RuntimeError('Expected value header, got %s' % value) continue # skip header row #if i == 30: # EARLY STOP # break if i % 10000 == 0: elapsed = time.time() - start_time log('Processed %d inputs in %.2f seconds', i, elapsed) cohort = int(cohort_str) secret = client_str e = rappor.Encoder(params, cohort, secret, irr_rand) # Real users should call e.encode(). For testing purposes, we also want # the PRR. bloom, prr, irr = e._internal_encode(true_value) bloom_str = rappor.bit_string(bloom, params.num_bloombits) prr_str = rappor.bit_string(prr, params.num_bloombits) irr_str = rappor.bit_string(irr, params.num_bloombits) out_row = (client_str, cohort_str, bloom_str, prr_str, irr_str) csv_out.writerow(out_row)
def main(argv): (opts, argv) = CreateOptionsParser().parse_args(argv) # Copy flags into params params = rappor.Params() params.num_bloombits = opts.num_bits params.num_hashes = opts.num_hashes params.num_cohorts = opts.num_cohorts params.prob_p = opts.prob_p params.prob_q = opts.prob_q params.prob_f = opts.prob_f if opts.random_mode == 'simple': irr_rand = rappor.SimpleIrrRand(params) elif opts.random_mode == 'fast': if fastrand: log('Using fastrand extension') # NOTE: This doesn't take 'rand'. It's seeded in C with srand(). irr_rand = fastrand.FastIrrRand(params) else: log('Warning: fastrand module not importable; see README for build ' 'instructions. Falling back to simple randomness.') irr_rand = rappor.SimpleIrrRand(params) else: raise AssertionError # Other possible implementations: # - random.SystemRandom (probably uses /dev/urandom on Linux) # - HMAC-SHA256 with another secret? This could match C++ byte for byte. # - or srand(0) might do it. csv_in = csv.reader(sys.stdin) csv_out = csv.writer(sys.stdout) header = ('client', 'cohort', 'bloom', 'prr', 'irr') csv_out.writerow(header) # TODO: It would be more instructive/efficient to construct an encoder # instance up front per client, rather than one per row below. start_time = time.time() for i, (client_str, cohort_str, true_value) in enumerate(csv_in): if i == 0: if client_str != 'client': raise RuntimeError('Expected client header, got %s' % client_str) if cohort_str != 'cohort': raise RuntimeError('Expected cohort header, got %s' % cohort_str) if true_value != 'value': raise RuntimeError('Expected value header, got %s' % value) continue # skip header row #if i == 30: # EARLY STOP # break if i % 10000 == 0: elapsed = time.time() - start_time log('Processed %d inputs in %.2f seconds', i, elapsed) cohort = int(cohort_str) secret = client_str e = rappor.Encoder(params, cohort, secret, irr_rand) # Real users should call e.encode(). For testing purposes, we also want # the PRR. bloom, prr, irr = e._internal_encode(true_value) bloom_str = rappor.bit_string(bloom, params.num_bloombits) prr_str = rappor.bit_string(prr, params.num_bloombits) irr_str = rappor.bit_string(irr, params.num_bloombits) out_row = (client_str, cohort_str, bloom_str, prr_str, irr_str) csv_out.writerow(out_row)