# -*- coding: utf-8 -*-

import math
import random
import hashlib
import numpy as np
from OUEdisturb import JudgeLocation
from NIndexOUE import NIndexJudgeLocation,SUMlist

def KRRParams(epsilon,layer_node_number):
    """获得KRR法的扰动参数"""
    # epsilon  隐私预算
    # layer_node_number  树中某层结点总个数
    
    ee=pow(math.e,epsilon)
    prob_p = float(ee)/(ee+layer_node_number-1)
    prob_q = 1.0/(ee+layer_node_number-1)
    
    return [prob_p,prob_q]

def NIndexDisturbSingleData(single_user_data_index,krr_params,grid_number):
    """扰动单个用户数据"""
    # single_user_data_index 单个用户数据索引
    # krr_params krr法扰动概率
    # grid_number 网格个数
    
    prob_p=krr_params[0]
    
    temp_number=random.random()
    if temp_number < prob_p:
        noise_single_user_data_index=single_user_data_index
    else:
        temp_list=list(range(grid_number))
        del (temp_list[single_user_data_index])
        noise_single_user_data_index=random.choice(temp_list)
    
    return noise_single_user_data_index

def NIndexKRRDAAllData(all_user_data,x_coordinate,y_coordinate,krr_params,grid_number):
    """扰动所有用户数据，并求加和值"""
    # all_user_data 所有用户数据列表
    # x_coordinate x轴区间范围，形式为[[a,b],[c,d],[]]
    # y_coordinate y轴区间范围，形式为[[a,b],[c,d],[]]
    # krr_params OUE法扰动概率
    # grid_number 网格个数
    
    noise_aggregate_all_data=[0 for _ in xrange(grid_number)] # 噪声数据加和值
    
    for i in all_user_data:
        single_user_data_index=NIndexJudgeLocation(i,x_coordinate,y_coordinate)
        noise_single_user_data_index=NIndexDisturbSingleData(single_user_data_index,krr_params,grid_number)
        noise_aggregate_all_data[noise_single_user_data_index] +=1
        
    return noise_aggregate_all_data





