-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.py
More file actions
130 lines (107 loc) · 4.72 KB
/
Copy pathplots.py
File metadata and controls
130 lines (107 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import sys
import warnings
from pandas.api.types import CategoricalDtype
def filter_na_quantile(data):
print('Previous Shape', data.shape)
# Drop nan from CEVAE
data = data.dropna(axis=0)
print('New Shape after remove NAN', data.shape)
data.reset_index(inplace=True, drop=True)
# Drop the top 10 worse results from all methods
filter_quantile = data.groupby(['Method']).quantile(0.85)['MAE']
filter_quantile = pd.DataFrame(filter_quantile)
filter_quantile.reset_index(inplace=True)
print(filter_quantile)
drop_rows = []
for i in range(data.shape[0]):
th = filter_quantile[filter_quantile['Method'] == data['Method'][i]]
th = th['MAE'].values
if data['MAE'][i] >= th:
drop_rows.append(i)
print(drop_rows)
data.drop(index=drop_rows, inplace=True)
print('New shape after removing worse 10%', data.shape)
return data
def read_all_configs(listfile, path, dataname='gwas'):
table = pd.DataFrame()
for i, config in enumerate(listfile['config'].values):
# print(path ,'output_' , dataname ,'_' , config , '.csv')
_table = pd.read_csv(path + 'output_' + dataname + '_' + config + '.csv').iloc[:, 1:]
_table['config'] = listfile['config'][i]
_table['sample_size'] = listfile['sample_size'][i]
_table['treatments'] = listfile['treatments'][i]
_table['confounders'] = listfile['confounders'][i]
table = pd.concat([table, _table], axis=0)
# table.reset_index(drop=True,inplace=True)
table['type'] = 'baseline'
table['type'][table['Method'] == 'M3E2'] = 'proposed'
table['Method'] = table['Method'].str.replace("Dragonnet", 'Drag.')
table = table[table['Method'] != 'TrueTreat']
table.reset_index(drop=True, inplace=True)
# table['sample_size'] = table['sample_size'].astype(str)
# table['treatments'] = table['treatments'].astype(str)
# table['confounders'] = table['confounders'].astype(str)
return table
def read_one_config(path, dataname='copula', config='config2a'):
data = pd.read_csv(path + 'output_' + dataname + '_' + config + '.csv').iloc[:, 1:]
data['Method'] = data['Method'].str.replace("Dragonnet", 'Drag.')
data['type'] = 'Baselines'
data['type'][data['Method'] == 'M3E2'] = 'Proposed'
TrueEffect = data[data['Method'] == 'TrueTreat']
TrueEffect.drop_duplicates(keep='first', inplace=True)
data = data[data['Method'] != 'TrueTreat']
print(data.groupby(['Method']).mean())
return data, TrueEffect
def plot_treat(data, TrueEffect, ax, treat, colors, order, x_label_letter, x_label_number, seed=6, isGwas=True,
size=9, ymin=-0.5, ymax=1.5):
data_ = data.set_index('Method').loc[order]
if isGwas:
data_ = data_[data_['seed_data'] == seed]
true = TrueEffect[TrueEffect['seed_data'] == seed][treat].values[0]
print(true)
else:
TrueEffect.reset_index(drop=True, inplace=True)
true = TrueEffect[treat][0] #* (-1)
data_.reset_index(drop=False, inplace=True)
data_[treat] = data_[treat].round(2)
sns.swarmplot(x="Method", y=treat,
hue='type',
data=data_,
palette=sns.color_palette(colors),
ax=ax,
size=size)
ax.set_xticklabels(ax.get_xticklabels())
ax.legend([], [], frameon=False)
ax.axhline(y=true, color='r', linestyle='-')
ax.set(xlabel=x_label_letter + ' τ' + x_label_number, ylabel='')
ax.axis(ymin=ymin, ymax=ymax)
return ax
def plot_barplot(ax, data, order, colors, title, ylabel='MAE'):
sns.barplot(ax=ax, x='Method', y='MAE', palette=sns.color_palette(colors),
hue='type', dodge=False, order=order, data=data)
ax.set(xlabel='', ylabel=ylabel)
ax.set_title(title, fontsize=14)
ax.legend([], [], frameon=False)
return ax
def plot_lines(data, listconfigs, ax, colors, x, ymin=0.10, ymax=0.26,
xlabel='x. bla', ylabel='MAE', addLegend=False, categories = []):
if len(categories)==0:
cat_type = CategoricalDtype(categories=pd.unique(data[x]).astype(str), ordered=True)
else:
cat_type = CategoricalDtype(categories=categories, ordered=True)
data[x] = data[x].astype(str)
data[x] = data[x].astype(cat_type)
data = data[data['config'].isin(listconfigs)]
sns.lineplot(data=data, x=x, y="MAE", ax=ax, hue='Method',
style='Method', markers=True, palette=sns.color_palette(colors),
legend='brief', markersize=12)
ax.axis(ymin=ymin, ymax=ymax)
ax.set(xlabel=xlabel)
if addLegend:
ax.legend(ncol=2)
else:
ax.legend([], [], frameon=False)