/
datablocker.py
164 lines (141 loc) · 6.15 KB
/
datablocker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
"""
Author: Prashanth Mohan <prashmohan@gmail.com>
http://www.cs.berkeley.edu/~prmohan
Copyright (c) 2011, University of California at Berkeley
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of University of California, Berkeley nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
OF CALIFORNIA AT BERKELEY BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
"""
import sys
import os
import logging
import math
import random
import time
from common import *
logger = logging.getLogger(__name__)
class DataBlockerFactory(object):
@staticmethod
def get_blocker(blocker_name):
for klass in GuptDataBlocker.__subclasses__():
if klass.__name__ == blocker_name:
return klass
return None
@staticmethod
def get_blocker_names():
return [klass.__name__ for klass in GuptDataBlocker.__subclasses__()]
class GuptDataBlocker(object):
"""
Base class that defines how to sample data records into multiple
blocks
"""
def __init__(self, args):
self.args = args
def get_blocks(self, records):
"""
Convert the given set of records into multiple smaller blocks
of data records
"""
raise Exception("This function should be over ridden")
def get_sensitivity_factor(self):
"""
Return the change in sensitivity caused because of the blocking.
"""
return 1.0
class NaiveDataBlocker(GuptDataBlocker):
def get_blocks(self, records):
"""
Convert the given set of records into multiple smaller blocks
of data records
"""
logger.debug("Using " + self.__class__.__name__ + " for data blocking")
num_records = len(records)
num_blocks = int(num_records ** 0.4)
block_size = int(math.ceil(num_records / num_blocks))
logger.info("Num Records: %d, Block size: %d, Num blocks: %d" %
(num_records, block_size, num_blocks))
return [records[indices : indices + block_size] for indices in range(0, num_records, block_size)]
def _sample(dataset, sample_num):
if len(dataset) < sample_num:
return dataset
else:
return random.sample(dataset, sample_num)
class ResamplingBlocker(object):
@staticmethod
def get_blocks_gamma(records, num_blocks, block_size, gamma):
"""
Subsample from the set of records into num_blocks distinct
blocks. Distribute each record among gamma of the subsampled
blocks (each of which is of size block_size)
"""
logger.info("Num Records: %d, Block size: %d, Num blocks: %d, gamma: %d" %
(len(records), block_size, num_blocks, gamma))
# Size of each block does not change
blocks = []
for x in range(num_blocks):
blocks.append([])
nonfull_blocks = range(num_blocks)
for record in records:
for block_no in _sample(nonfull_blocks, gamma):
blocks[block_no].append(record)
if len(blocks[block_no]) >= block_size:
# Remove block from contenders list if block is
# already full
nonfull_blocks.remove(block_no)
return blocks
class ResamplingDataBlockerConstantSize(GuptDataBlocker):
@profile_func
def get_blocks(self, records):
logger.debug("Using " + self.__class__.__name__ + " for data blocking")
num_records = len(records)
num_blocks = self.args[0] * int(math.ceil(num_records ** 0.4))
# Each record is put into num_blocks blocks. But each element
# is repeated in gamma blocks. Thus the total number of blocks
# becomes gamma * original number of block
block_size = int(math.ceil(num_records ** 0.6))
return ResamplingBlocker.get_blocks_gamma(records, num_blocks, block_size, self.args[0])
def get_sensitivity_factor(self):
"""
Return the change in sensitivity caused because of the blocking.
"""
return self.args[0]
class ResamplingDataBlockerConstantBlocks(GuptDataBlocker):
@profile_func
def get_blocks(self, records):
"""
Convert the given set of records into multiple smaller blocks
of data records
"""
logger.debug("Using " + self.__class__.__name__ + " for data blocking")
num_records = len(records)
num_blocks = int(math.ceil(num_records ** 0.4))
# Each record is put into gamma blocks. Thus the total size of
# each blocks becomes gamma * original size of block
block_size = self.args[0] * int(math.ceil(num_records ** 0.6))
return ResamplingBlocker.get_blocks_gamma(records, num_blocks, block_size, self.args[0])
def get_sensitivity_factor(self):
"""
Return the change in sensitivity caused because of the blocking.
"""
return self.args[0]