def test_get_subdomain_grid(self): mapping1 = np.array([ [1,1,2,2], [1,1,2,2], [1,1,2,2]]) d1 = support.get_subdomain_grid(mapping1, mapping1.shape) self.assertDictEqual(d1, {1: (3, 2), 2: (3, 2)}) mapping2 = np.array([ [1,1,2,2], [1,1,2,2], [3,3,0,0]]) d2 = support.get_subdomain_grid(mapping2, mapping2.shape) self.assertDictEqual(d2, {1: (2, 2), 2: (2, 2), 3: (1,2), 0: (1,2)}) mapping3 = np.array([ [1,1,2,1]]) d3 = support.get_subdomain_grid(mapping3, mapping3.shape) self.assertEqual(d3, None) mapping4 = np.array([ [1,1,2,3], [1,1,1,4]]) d4 = support.get_subdomain_grid(mapping4, mapping4.shape) self.assertEqual(d4, None)
def Run(self, W, x, eps, seed): assert len(x.shape) == 2, "Adaptive Grid only works for 2D domain" shape_2d = x.shape x = x.flatten() prng = np.random.RandomState(seed) Ms = [] ys = [] M = selection.UniformGrid(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c).select() y = measurement.Laplace(M, self.alpha*eps).measure(x, prng) x_hat = inference.LeastSquares().infer(M, y) Ms.append(M) ys.append(y) # Prepare parition object for later SplitByParition. # This Partition selection operator is missing from Figure 2, plan 12 in the paper. uniform_mapping = mapper.UGridPartition(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c).mapping() x_sub_list = meta.SplitByPartition(uniform_mapping).transform(x) sub_domains = support.get_subdomain_grid(uniform_mapping, shape_2d) ll, hi =[], [] for i in sorted(set(uniform_mapping)): x_i = x_sub_list[i] P_i = support.projection_matrix(uniform_mapping, i) x_hat_i = P_i * x_hat sub_domain_shape = sub_domains[i] M_i = selection.AdaptiveGrid(sub_domain_shape, x_hat_i, (1-self.alpha)*eps, c2=self.c2).select() y_i = measurement.Laplace(M_i, (1-self.alpha)*eps).measure(x_i, prng) offset = np.unravel_index(P_i.matrix.nonzero()[1][0], shape_2d) ll.extend(M_i._lower + np.array(offset)) hi.extend(M_i._higher + np.array(offset)) ys.append(y_i) Ms.append(workload.RangeQueries(shape_2d, np.array(ll), np.array(hi))) x_hat = inference.LeastSquares().infer(Ms, ys) return x_hat
def Run(self, W, x, eps, seed): assert len(x.shape) == 2, "Adaptive Grid only works for 2D domain" shape_2d = x.shape x = x.flatten() prng = np.random.RandomState(seed) Ms = [] ys = [] M = selection.UniformGrid(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c).select() if not isinstance(M, np.ndarray): M = M.toarray() y = measurement.Laplace(M, self.alpha*eps).measure(x, prng) x_hat = inference.LeastSquares().infer(M, y) Ms.append(M) ys.append(y) # Prepare parition object for later SplitByParition. # This Partition selection operator is missing from Figure 2, plan 12 in the paper. uniform_mapping = mapper.UGridPartition(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c).mapping() x_sub_list = meta.SplitByPartition(uniform_mapping).transform(x) sub_domains = support.get_subdomain_grid(uniform_mapping, shape_2d) for i in sorted(set(uniform_mapping)): x_i = x_sub_list[i] P_i = support.projection_matrix(uniform_mapping, i) x_hat_i = P_i * x_hat sub_domain_shape = sub_domains[i] M_i = selection.AdaptiveGrid(sub_domain_shape, x_hat_i, (1-self.alpha)*eps, c2=self.c2).select() if not isinstance(M, np.ndarray): M_i = M_i.toarray() y_i = measurement.Laplace(M_i, (1-self.alpha)*eps).measure(x_i, prng) M_i_o = M_i * P_i Ms.append(M_i_o) ys.append(y_i) x_hat = inference.LeastSquares().infer(Ms, ys, [1.0]*len(ys)) return x_hat
def Run(self, W, x, eps): assert len(self.domain_shape ) == 2, "Adaptive Grid only works for 2D domain_shape" shape_2d = self.domain_shape Ms = [] ys = [] M = ugrid_select(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c) y = x.laplace(M, self.alpha * eps) x_hat = least_squares(M, y) Ms.append(M) ys.append(y) # Prepare parition object for later SplitByParition. # This Partition selection operator is missing from Figure 2, plan 12 in the paper. uniform_mapping = ugrid_mapper(shape_2d, self.data_scale, eps, ag_flag=True, c=self.c) x_sub_list = x.split_by_partition(uniform_mapping) sub_domains = support.get_subdomain_grid(uniform_mapping, shape_2d) for i in sorted(set(uniform_mapping)): x_i = x_sub_list[i] P_i = support.projection_matrix(uniform_mapping, i) x_hat_i = P_i * x_hat sub_domain_shape = sub_domains[i] M_i = agrid_select(sub_domain_shape, x_hat_i, (1 - self.alpha) * eps, c2=self.c2) y_i = x_i.laplace(M_i, (1 - self.alpha) * eps) M_i_o = M_i * P_i Ms.append(M_i_o) ys.append(y_i) x_hat2 = least_squares(Ms, ys, [1.0] * len(ys)) return x_hat2