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

import math
import random
import hashlib
import csv
import numpy as np

def ReadCsvFile(path,fileName):
    """将整个csv文件内容按行存放到一个list当中；
       csv文件内容的每一行为一个子list. 
       按行读取文件
    """
    newpath=path+fileName
    files=open(newpath,'r')
    value=csv.reader(files)
    data=[]
    for item in value:
        data.append(item)
    files.close()
    return data

def GetMatrix():
    """获得投影矩阵，保证列正交"""
    
    path='F:\\Python_pool\\Project\\RQLDP\\'
    filename='hadamard16.csv'
    matrix_list=ReadCsvFile(path,filename)
    
    length1=len(matrix_list)
    length2=len(matrix_list[0])
    
    for i in xrange(length1):
        for j in xrange(length2):
            matrix_list[i][j]=int(matrix_list[i][j])
    
    return matrix_list

def SHParams(epsilon):
    """计算SH法扰动概率"""
    # epsilon 隐私预算
    
    temp_data=math.pow(math.e,epsilon)
    prob_p = float(temp_data)/(temp_data+1)
    prob_q = 1.0/(temp_data+1)
    
    return [prob_p,prob_q]

def JudgeLocation(single_user_data,x_coordinate,y_coordinate):
    """判断用户数据的位置"""
    # single_user_data 单个用户数据，形式为['x','y']
    # x_coordinate x轴区间范围，形式为[[a,b],[c,d],[]]
    # y_coordinate y轴区间范围，形式为[[a,b],[c,d],[]]
    
    for i in xrange(len(x_coordinate)):
        temp1=x_coordinate[i][0]
        temp2=x_coordinate[i][1]
        if temp1<=float(single_user_data[0])<temp2:
            x_index=i
            break
    for j in xrange(len(y_coordinate)):
        temp3=y_coordinate[j][0]
        temp4=y_coordinate[j][1]
        if temp3<=float(single_user_data[1])<temp4:
            y_index=j
            break
    single_user_data_index=x_index*len(y_coordinate)+y_index # 计算用户数据在所有数据中的位置
    
    return single_user_data_index
    
def SingleDataDisturb(single_user_data_index,sh_params,matrix_list,grid_number):
    """扰动单个用户数据"""
    # single_user_data_index 单个用户数据索引
    # x_coordinate x轴区间范围，形式为[[a,b],[c,d],[]]
    # y_coordinate y轴区间范围，形式为[[a,b],[c,d],[]]
    # sh_params sh法扰动概率
    # matrix_list 投影矩阵
    # grid_number 网格个数
    
    prob_p=sh_params[0]
    random_row=random.randint(0,grid_number-1) # 随机选择投影矩阵的某一行
    
    encode_data=matrix_list[random_row][single_user_data_index] # 投影矩阵中的某个值
    
    temp_number=random.random()
    if temp_number < prob_p:
        noise_single_user_data=encode_data
    else:
        noise_single_user_data=encode_data*(-1)
    
    return [random_row,noise_single_user_data]
    
def PsdaAllDataDisturb(all_user_data,x_coordinate,y_coordinate,sh_params,matrix_list,grid_number):
    """扰动所有用户数据"""
    # all_user_data 所有用户数据列表
    # sh_params sh法扰动概率
    # matrix_list 投影矩阵
    # grid_number 网格个数
    
    noise_all_user_data=[]
    
    for i in all_user_data:
        single_user_data_index=JudgeLocation(i,x_coordinate,y_coordinate)
        noise_single_user_data=SingleDataDisturb(single_user_data_index,sh_params,matrix_list,grid_number)
        noise_all_user_data.append(noise_single_user_data)
    
    return noise_all_user_data    
    
    
    
    
    
    

   