# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# This software and its source code are provided solely for the purpose of the peer-review process of Submission #71 titled "Privacy and Integrity Preserving Computations with CRISP" submitted to the 30th Usenix Security Symposium (2021). All other uses are forbidden.
#
# This notebook contains necessary tools to explore the results of the conducted experiments.

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output

# +
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
import seaborn as sns

from matplotlib import rc
rc('text', usetex=True)
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
pd.set_option('display.max_columns', 999)


from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
# -

# # Import data as dataframes
#

# ## Running
#
#
# REMEBER TO ALSO CHANGE PARAMS.H WHEN RUNNING HEAAN 

# Load the feature csv for interpolated Garmin type 1 10-3-30 dataset 
df1=pd.read_csv('Garmin/BabelGarminAnalysis1-10-3-30.csv', encoding='utf-8', engine='python')
df1.describe()

# Load first results of the run on CRISP
dfrun=pd.read_csv('Garmin/Manjaro_resDistUtils.csv', encoding='utf-8', engine='python')
dfrun.drop_duplicates()
dfrun['error']=dfrun['error']/100
dfrun.describe()

# Merge with feature csv for Garm XYTIn1 10-3-30
sdf1 = df1.merge(dfrun, on='Name', how='inner')
sdf1.describe()

data1 = sdf1[(sdf1['VQ25']>0.3)&((sdf1['dmax'])<30**2)&(sdf1['Vmax']<10)&((sdf1['tQ75'])>0)&(sdf1['RealRes']>15)]
(data1['error'].abs()).describe()

from sklearn.metrics import mean_squared_error
np.sqrt(mean_squared_error(data1['RealRes']/data1['RealRes'], data1['result']/data1['RealRes']))

plt.rcParams["figure.figsize"] = (7,7)
plt.rcParams.update({'font.size': 30})
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, figsize=(7, 7),gridspec_kw={"height_ratios": (.1, .85), "hspace": (0)})
sns.boxplot(data1['error'], showmeans=True, ax=ax_box,color="aliceblue",fliersize="3") #showfliers=False,
plt.setp(ax_box.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax_box.lines, color='k')
sns.distplot(data1['error'], kde=False, ax=ax_hist)
ax_box.axis('off')
ax_hist.set(xlabel='$\epsilon$', ylabel='count')
ax_hist.ticklabel_format(axis='x', style='sci', scilimits=(0,0), useOffset=False, useLocale=None, useMathText=None)
plt.tight_layout()
#f.savefig('images/RunHistGo.png')

sdf3 = (1-data1['error'].abs()/100)
sdf3.mean()

# ## Genomics

dfgen=pd.read_csv('Genomic/Manjaro_resDSUtils.csv', encoding='utf-8', engine='python')
dfgen.drop_duplicates()
dfgen['error']=dfgen['error']/100
dfgen.describe()

from sklearn.metrics import mean_squared_error
np.sqrt(mean_squared_error(dfgen['RealRes']/dfgen['RealRes'], dfgen['result']/dfgen['RealRes']))

(dfgen['error'].abs()).describe()

plt.rcParams["figure.figsize"] = (7,7)
plt.rcParams.update({'font.size': 30})
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, figsize=(7, 7),gridspec_kw={"height_ratios": (.1, .85), "hspace": (0)})
sns.boxplot(dfgen['error'], showmeans=True, ax=ax_box,color="aliceblue",fliersize="3") #showfliers=False, 
plt.setp(ax_box.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax_box.lines, color='k')
sns.distplot(dfgen['error'], kde=False,ax=ax_hist)
ax_box.axis('off')
ax_hist.set(xlabel='$\epsilon$', ylabel='count')
ax_hist.ticklabel_format(axis='x', style='sci', scilimits=(0,0), useOffset=False, useLocale=None, useMathText=None)
plt.tight_layout()


sdfgen = (1-dfgen['error'].abs()/100)
sdfgen.mean()

# ## Smart Meters

dfsm=pd.read_csv('SmartMeters/Manjaro_resSMUtils.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
# rmv id MAC000197
# rmv id MAC000037
dfsm['error']=dfsm['error']/100
dfsm.describe()

from sklearn.metrics import mean_squared_error
np.sqrt(mean_squared_error(dfsm['RealRes']/dfsm['RealRes'], dfsm['result']/dfsm['RealRes']))

(dfsm['error'].abs()).describe()

sdfsm = (1-dfsm['error'].abs()/100)
sdfsm.mean()

plt.rcParams["figure.figsize"] = (7,7)
plt.rcParams.update({'font.size': 30})
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, figsize=(7, 7),gridspec_kw={"height_ratios": (.1, .85), "hspace": (0)})
sns.boxplot(dfsm['error'],showmeans=True, ax=ax_box,color="aliceblue",fliersize="3") #showfliers=False,
plt.setp(ax_box.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax_box.lines, color='k')
sns.distplot(dfsm['error'], kde=False, ax=ax_hist)
ax_box.axis('off')
ax_hist.set(xlabel='$\epsilon$', ylabel='count')
ax_hist.ticklabel_format(axis='x', style='sci', scilimits=(0,0), useOffset=False, useLocale=None, useMathText=None)
plt.xlim(-0.0011, 0.0011)
plt.xticks([-0.0008,-0.0004,0,0.0004,0.0008])
plt.tight_layout()

# # Timing analysis of the ZKCE

dfsm=pd.read_csv('ZKCEtimings/boolCirTime128.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
dfsm.describe()

dfsm=pd.read_csv('ZKCEtimings/boolCirTImeVer128.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
dfsm.describe()

# ## Running

dfsm=pd.read_csv('ZKCEtimings/timings184.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
dfsm.describe()

res_df = {'challenge','tgen','tsplit','tsign','tver'}
#for k in range(dfsm.count()//219):
print(dfsm['challenge'].median())
print(dfsm['tgen'].mean())
print(dfsm['tsplit'].mean()*219)
print(dfsm['tsign'].mean()*219)
print('tprove=',(dfsm['tsplit'].mean()*219+dfsm['tsign'].mean()*219 + 2048*181.83)/1000/60) #208.35 for ZKBool proof 
print('tver=',(dfsm['tver'].mean()*219 + 2048*73.1)/1000/60)

# ## Disease Susceptibility

dfsm=pd.read_csv('ZKCEtimings/timings56.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
dfsm.describe()

res_df = {'challenge','tgen','tsplit','tsign','tver'}
#for k in range(dfsm.count()//219):
print(dfsm['challenge'].median())
print(dfsm['tgen'].mean())
print(dfsm['tsplit'].mean()*219)
print(dfsm['tsign'].mean()*219)
print('tprove=',dfsm['tsplit'].mean()*219+dfsm['tsign'].mean()*219 + 2*181.83) # two hashes
print('tver=',dfsm['tver'].mean()*219 + 2*73.1)

# ## Smart Metering

dfsm=pd.read_csv('ZKCEtimings/timings45.csv', encoding='utf-8', engine='python')
dfsm.drop_duplicates()
dfsm.describe()

res_df = {'challenge','tgen','tsplit','tsign','tver'}
#for k in range(dfsm.count()//219):
print(dfsm['challenge'].median())
print(dfsm['tgen'].mean())
print(dfsm['tsplit'].mean()*219)
print('tprove=',(dfsm['tsplit'].mean()*219+dfsm['tsign'].mean()*219 + 1024*181.83)/1000/60) # 1024 slots
print('tver=',(dfsm['tver'].mean()*219 + 1024*73.1)/1000/60)

# # SM2: non linear price function

# +
# Check the HE computation of the cumulative price function. Some values are negative.
# Will check the effect of the params in ckks

dfsm2=pd.read_csv('SmartMeters/Manjaro_resSM2Utils400.csv', encoding='utf-8', engine='python')
dfsm2.drop_duplicates()
# rmv id MAC000197
# rmv id MAC000037
dfsm2.describe()
# -

datasm2 = dfsm2[(dfsm2['RealRes']>20)]
(datasm2['errorPol'].abs()).describe()

(1-dfsm2['errorPol'].abs()/100).median()


datasm2 = dfsm2[(dfsm2['RealRes']>20)&(dfsm2['RealRes']<500)]
(datasm2['errorPol'].abs()).describe()

StarcoderdataJupyterScriptsDedupFiltered