]> git.armaanb.net Git - python_dp.git/blob - common.py
Initial commit
[python_dp.git] / common.py
1 # Implementations of common functions across these privacy notebooks
2
3 from random import getrandbits, randint
4
5 import numpy as np
6
7
8 def create_neighbour(x, verbose=False):
9     """ Creates a neighbouring dataset
10     Inputs:
11         x: original dataset
12         verbose: print detail
13     Output:
14         neighbouring dataset to x with 1 random value added or removed
15     """
16     
17     x2 = np.copy(x)
18     np.random.default_rng().shuffle(x2)
19
20     # Randomly chose whether to add or subtract a value
21     if getrandbits(1):
22         x2 = x2[1:]
23         if verbose: print("Subtracting value")
24     else:
25         x2 = np.append(x2, randint(min(x), max(x)))
26         if verbose: print("Adding value")
27
28     return x2
29
30 def print_hline(length):
31     """ Prints a horizontal line
32     Inputs:
33         length: length of the line in characters
34         
35     Output:
36         Unicode horizontal line printed to console
37     """
38     
39     print(u'\u2500' * length)
40     
41 def get_epsilons(max_epsilon, epsilon_step):
42     """ Create array of epsilon values to test using given parameters
43     Inputs:
44         max_epsilon: maximum epsilon value
45         epsilon_step: step size between epsilons
46         
47     Output:
48         Array of epsilon values to test
49     """
50     
51     epsilon_div = 1 / epsilon_step
52     return [i / epsilon_div for i in range(1, int(max_epsilon * epsilon_div))]