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

import math
import random
import hashlib
import numpy as np

def OUEUnbiasedProcess(noise_aggregate_all_data,aggregate_level_user_number,oue_params,layer_number,all_user_number):
    """oue法无偏处理聚合数据,得到各个结点无偏频率"""
    # noise_aggregate_all_data 聚合的噪声数据,形式为[[0],[0,0,0,0],[],[]]
    # aggregate_level_user_number 每一层的报告数
    # oue_params OUE法扰动概率
    # layer_number 四分树的高度
    # all_user_number 总用户数
    
    prob_p1=oue_params[0]
    prob_q2=oue_params[3]
    
    noise_unbiase_aggregate_all_data=[[] for _ in xrange(layer_number)]
    num1=prob_p1-prob_q2
    
    for i in xrange(layer_number):
        num2=float(aggregate_level_user_number[i])*prob_q2
        for j in xrange(len(noise_aggregate_all_data[i])):
            num3=float(noise_aggregate_all_data[i][j]-num2)/num1 # 无偏处理后的频数
            num4=float(num3)/all_user_number # 无偏处理后的频率
            if num4 < 0:
                temp_num=0
            elif num4 > 1:
                temp_num=1
            else:
                temp_num=num4
            noise_unbiase_aggregate_all_data[i].append(temp_num)
            
    return noise_unbiase_aggregate_all_data

def UnbiasedProcess(noise_aggregate_all_data,aggregate_level_user_number,params,layer_number,all_user_number):
    """RAPPOR、KRR法无偏处理聚合数据,得到各个结点无偏频率"""
    # noise_aggregate_all_data 聚合的噪声数据,形式为[[0],[0,0,0,0],[],[]]
    # aggregate_level_user_number 每一层的报告数
    # params 扰动概率
    # layer_number 四分树的高度
    # all_user_number 总用户数
    
    prob_p=params[0]
    prob_q=params[1]
    
    noise_unbiase_aggregate_all_data=[[] for _ in xrange(layer_number)]
    num1=prob_p-prob_q
    
    for i in xrange(layer_number):
        num2=float(aggregate_level_user_number[i])*prob_q
        for j in xrange(len(noise_aggregate_all_data[i])):
            num3=float(noise_aggregate_all_data[i][j]-num2)/num1 # 无偏处理后的频数
            num4=float(num3)/all_user_number # 无偏处理后的频率
            if num4 < 0:
                temp_num=0
            elif num4 > 1:
                temp_num=1
            else:
                temp_num=num4
            noise_unbiase_aggregate_all_data[i].append(temp_num)
            
    return noise_unbiase_aggregate_all_data


def NIndexOUEUnbiasedProcess(noise_aggregate_all_data,all_user_number,oue_params,grid_number):
    """非索引oue法无偏处理聚合数据,得到各个结点无偏频率"""
    # noise_aggregate_all_data 聚合的噪声数据,形式为[a,b,c,d]
    # all_user_number 总用户数
    # oue_params OUE法扰动概率
    # grid_number 网格个数
    
    prob_p1=oue_params[0]
    prob_q2=oue_params[3]
    
    noise_unbiase_aggregate_all_data=[]
    num1=prob_p1-prob_q2
    num2=all_user_number*prob_q2
    
    for i in xrange(grid_number):
        num3=float(noise_aggregate_all_data-num2)/num1 # 无偏处理后的频数
        num4=float(num3)/all_user_number # 无偏处理后的频率
        if num4 < 0:
            temp_num=0
        elif num4 > 1:
            temp_num=1
        else:
            temp_num=num4
        noise_unbiase_aggregate_all_data.append(temp_num)
    
    return noise_unbiase_aggregate_all_data   
    
def NIndexUnbiasedProcess(noise_aggregate_all_data,all_user_number,params,grid_number):
    """非索引RAPPOR、KRR法无偏处理聚合数据,得到各个结点无偏频率"""
    # noise_aggregate_all_data 聚合的噪声数据,形式为[a,b,c,d]
    # all_user_number 总用户数
    # oue_params OUE法扰动概率
    # grid_number 网格个数
    
    prob_p=params[0]
    prob_q=params[1]
    
    noise_unbiase_aggregate_all_data=[]
    num1=prob_p-prob_q
    num2=all_user_number*prob_q
    
    for i in xrange(grid_number):
        num3=float(noise_aggregate_all_data-num2)/num1 # 无偏处理后的频数
        num4=float(num3)/all_user_number # 无偏处理后的频率
        if num4 < 0:
            temp_num=0
        elif num4 > 1:
            temp_num=1
        else:
            temp_num=num4
        noise_unbiase_aggregate_all_data.append(temp_num)
    
    return noise_unbiase_aggregate_all_data      
    
    
    
    
        
