-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlotData.py
277 lines (235 loc) · 8.75 KB
/
PlotData.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/python3
import sys
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from pathlib import Path
from modules.loadData import read_files
from modules.thesisUtils import arg_parse_plts
def get_parameters():
"""
Function to get command line parameters and set the parameters used in this script accordingly
:return: Dictionary with parameters
"""
cmd_par = arg_parse_plts(sys.argv[1:])
parameter_dict = {
"path": "experiments/new/error",
"x": None,
"y": None,
"group": None,
"network": None,
"stimulus": None,
"experiment": None,
"sampling": None,
"parameter": None,
"measure": None,
"name": "Violin plot",
"save_plot": True,
}
if cmd_par.show:
parameter_dict["save_plot"] = False
if cmd_par.path is not None:
parameter_dict["path"] = cmd_par.path
if cmd_par.x is not None:
parameter_dict["x"] = cmd_par.x
if cmd_par.y is not None:
parameter_dict["y"] = cmd_par.y
if cmd_par.group is not None:
parameter_dict["group"] = cmd_par.group
if cmd_par.network is not None:
parameter_dict["network"] = cmd_par.network
if cmd_par.input is not None:
parameter_dict["stimulus"] = cmd_par.input
if cmd_par.experiment is not None:
parameter_dict["experiment"] = cmd_par.experiment
if cmd_par.sampling is not None:
parameter_dict["sampling"] = cmd_par.sampling
if cmd_par.parameter is not None:
parameter_dict["parameter"] = cmd_par.parameter
if cmd_par.measure is not None:
parameter_dict["measure"] = cmd_par.measure
if cmd_par.title is not None:
parameter_dict["name"] = cmd_par.title
return parameter_dict
def filter_dataframe(df, params, ignore_sampling=False):
"""
Filter dataframe according to parameters
:param df: Dataframe
:param params: Parameters
:param ignore_sampling: When set to true, ignore the sampling rate if set as parameter. This is important when
information loss is plotted
:return: New filtered dataframe
"""
new_df = df.copy()
save_string = params["x"] + "_" + params["y"]
if params["network"] is not None:
save_string += "_network_%s" % params["network"]
new_df = new_df[new_df["network"] == params["network"].replace("\\n", "\n")]
if params["stimulus"] is not None:
save_string += "_input_%s" % params["stimulus"]
new_df = new_df[new_df["stimulus"] == int(params["stimulus"])]
if params["experiment"] is not None:
save_string += "_experiment_%s" % params["experiment"]
new_df = new_df[new_df["experiment"] == params["experiment"]]
if params["sampling"] is not None and not ignore_sampling:
save_string += "_sampling_%s" % params["sampling"]
new_df = new_df[new_df["sampling"] == params["sampling"]]
if params["parameter"] is not None:
save_string += "_parameter_%s" % params["parameter"]
new_df = new_df[new_df["parameter"] == params["parameter"]]
if params["measure"] is not None and params["measure"] != "li":
save_string += "_measure_%s" % params["measure"]
new_df = new_df[new_df["measure"] == params["measure"]]
new_df.dropna()
return new_df, save_string
def create_li_df(df):
"""
Create new dataframe that is used for determining the information loss
:param df: The dataframe with the loaded data
:return: New datafram that contains the lost information for different sampling rates
"""
df_data_full = df[np.logical_and(df["sampling"] == "1.0", df["measure"] == "distance")].sort_values(
by=list(df.columns)
).drop(columns="sampling", inplace=False)
data_full = df_data_full["value"]
data_80 = df[np.logical_and(df["sampling"] == "0.8", df["measure"] == "distance")].sort_values(
by=list(df.columns)
)["value"]
data_60 = df[np.logical_and(df["sampling"] == "0.6", df["measure"] == "distance")].sort_values(
by=list(df.columns)
)["value"]
data_40 = df[np.logical_and(df["sampling"] == "0.4", df["measure"] == "distance")].sort_values(
by=list(df.columns)
)["value"]
loss_80 = np.asarray(data_80) - np.asarray(data_full)
loss_60 = np.asarray(data_60) - np.asarray(data_full)
loss_40 = np.asarray(data_40) - np.asarray(data_full)
df_li = df_data_full.copy()
df_li["li_type"] = np.full(loss_80.size, "Information Loss Full:80%")
df_li["li"] = loss_80
df_data_full_60 = df_data_full.copy()
df_data_full_60["li_type"] = np.full(loss_60.size, "Information Loss Full:60%")
df_data_full_60["li"] = loss_60
df_li = df_li.append(df_data_full_60, ignore_index=True)
df_data_full_40 = df_data_full.copy()
df_data_full_40["li_type"] = np.full(loss_40.size, "Information Loss Full:40%")
df_data_full_40["li"] = loss_40
df_li = df_li.append(df_data_full_40, ignore_index=True)
return df_li
def violin_plot(df, params):
"""
Plot violin plot
:param df: Dataframe
:param params: Parameters
:return: None
"""
plt.rcParams.update({"font.size": 20})
new_df, save_string = filter_dataframe(df, params)
figure = plt.gcf()
figure.set_size_inches((15, 10))
ax = figure.add_subplot(1, 2 if len(new_df[new_df.value > 1]) > 0 else 1, 1)
sb.boxplot(
x=params["x"],
y=params["y"],
hue=params["group"],
data=new_df[new_df.value <= 1].sort_values(params["group"]),
dodge=True,
order=sorted(new_df[params["x"]].drop_duplicates().tolist()),
ax=ax,
boxprops={"alpha":0.2}
)
sb.swarmplot(
x=params["x"],
y=params["y"],
hue=params["group"],
data=new_df[new_df.value <= 1].sort_values(params["group"]),
order=sorted(new_df[params["x"]].drop_duplicates().tolist()),
ax=ax,
dodge=True,
)
ax.set_ylim(0., 1.)
handles, lables = ax.get_legend_handles_labels()
num_labels = len(new_df[params["group"]].drop_duplicates())
ax.legend(handles[:num_labels], lables[:num_labels])
if len(new_df[new_df.value > 1]) > 0:
ax.set_title("Error distribution for Error values <= 1")
ax_2 = figure.add_subplot(1, 2, 2)
sb.boxplot(
x=params["x"],
y=params["y"],
hue=params["group"],
data=new_df[new_df.value > 1].sort_values(params["group"]),
order=sorted(new_df[params["x"]].drop_duplicates().tolist()),
dodge=True,
boxprops={"alpha": 0.2},
ax=ax_2
)
sb.swarmplot(
x=params["x"],
y=params["y"],
hue=params["group"],
data=new_df[new_df.value > 1].sort_values(params["group"]),
order=sorted(new_df[params["x"]].drop_duplicates().tolist()),
ax=ax,
dodge=True,
)
ax_2.set_title("Error distribution for Error values > 1")
figure.suptitle(params["name"], fontsize=20)
ax.set_ylabel("Reconstruction Error E")
if params["save_plot"]:
curr_dir = os.getcwd()
Path(curr_dir + "/figures/data_analysis").mkdir(parents=True, exist_ok=True)
save_name = curr_dir + "/figures/data_analysis/%s_violin_plot.png" % save_string
plt.savefig(save_name)
plt.close()
else:
plt.show()
def information_loss_plot(df, params):
"""
Plot information loss as boxen plot
:param df: Dataframe
:param params: Parameters
:return: None
"""
plt.rcParams.update({"font.size": 20})
new_df, save_string = filter_dataframe(df, params, ignore_sampling=True)
df_li = create_li_df(new_df)
df_li.sort_values("li_type", inplace=True)
plt.figure(figsize=(15, 8))
sb.boxenplot(
x="li_type",
y="li",
hue=params["x"],
hue_order=sorted(new_df[params["x"]].drop_duplicates().tolist()),
data=df_li,
dodge=True,
)
plt.title(params["name"], fontsize=16)
plt.xlabel("Type of Lost Information")
plt.ylabel("Lost Information")
plt.ylim(-0.5, 1.)
if params["save_plot"]:
curr_dir = os.getcwd()
Path(curr_dir + "/figures/data_analysis").mkdir(parents=True, exist_ok=True)
save_name = curr_dir + "/figures/data_analysis/%s_lost_information.png" % save_string
plt.savefig(save_name)
plt.close()
else:
plt.show()
plt.show()
def main():
"""
Main function
:return: None
"""
params = get_parameters()
df = read_files(params["path"])
if params["measure"] == "distance":
violin_plot(df, params)
elif params["measure"] == "li":
information_loss_plot(df, params)
else:
raise ValueError("The passed measure cannot be plotted")
if __name__ == '__main__':
main()