-
Notifications
You must be signed in to change notification settings - Fork 3
/
parameter_plots.py
215 lines (177 loc) · 6.98 KB
/
parameter_plots.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
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.colors import colorConverter
from adjustText import adjust_text
def scatter_angle(
df,
x,
y,
xlabel,
ylabel,
lower_lim,
upper_lim,
highlight_color,
filename,
adjust=False,
):
fig, ax = plt.subplots(1, figsize=(6 * 1.0, 6))
text = []
labeled_df = pd.DataFrame()
for i, j in zip(df[x], df[y]):
# Find rows matching this value...
i = np.round(i, 2)
j = np.round(j, 2)
tmp = df[(np.round(df[x], 2) == i) & (np.round(df[y], 2) == j)]
atom1s = tmp.atom1
atom2s = tmp.atom2
atom3s = tmp.atom3
names = [f"{i}-{j}-{k}" for i, j, k in zip(atom1s, atom2s, atom3s)]
# I've added additional rounding because multiple angles are really quite close together.
labeled_df = labeled_df.append(
pd.DataFrame(
{"i": np.round(i, 1), "j": np.round(j, 1), "names": ",".join(names)},
index=[0],
),
ignore_index=True,
)
labeled_df.drop_duplicates(inplace=True)
labeled_df.reset_index(inplace=True)
for index, row in labeled_df.iterrows():
all_names = row.names.split(",")
elements = []
for name in all_names:
atoms = name.split("-")
elemental_angle = "--".join(atom[0] for atom in atoms)
elements.append(elemental_angle)
element_angles = [i for i in set(elements)]
element_names = ",".join(element_angles)
labeled_df.iloc[index, labeled_df.columns.get_loc("names")] = element_names
# return labeled_df, labeled_df.drop_duplicates(inplace=True)
# Merge angles which are the same, but labeled in reverse
for index, row in labeled_df.iterrows():
names = row.names
# seen = set ()
# Something like this should be more general: https://stackoverflow.com/questions/17212208/removing-reverse-elements-in-a-python-list
# reduced_names = [x for x in names if tuple(x[::-1]) not in seen and not seen.add(tuple(names))]
print(f"Pruning {names.split(',')[1:]} from {names.split(',')[0]}.")
# I am printing this out, because it is not the most general solution.
# This doesn't truly detect if things are palindromes.
labeled_df.iloc[index, labeled_df.columns.get_loc("names")] = names.split(",")[
0
]
# Deduplicate names, again.
labeled_df.drop_duplicates(subset="names", inplace=True)
labeled_df.reset_index(inplace=True)
for index, row in labeled_df.iterrows():
if abs(row.i - row.j) > 0.1 * row.i:
ax.scatter(
row.i, row.j, s=80, edgecolor="none", lw=0.2, color="r", zorder=2
)
text.append(plt.text(row.i, row.j, f"{row.names}", color="r", size=14))
else:
ax.scatter(row.i, row.j, s=80, edgecolor="none", lw=0.2, color="0.5")
ax.plot([-500, 500], [-500, 500], ls="-", c="0.3", zorder=-1, lw="0.5")
ax.set_ylim([lower_lim, upper_lim])
ax.set_xlim([lower_lim, upper_lim])
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
if adjust:
adjust_text(
text,
expand_points=(1.5, 1.5),
expand_text=(2, 2),
arrowprops=dict(arrowstyle="-", color="r"),
)
fig.savefig(f"figures/{filename}.pdf", bbox_inches="tight")
fig.savefig(f"figures/{filename}.png", dpi=300, bbox_inches="tight")
def scatter_dihedral(
df,
x,
y,
xlabel,
ylabel,
lower_lim,
upper_lim,
highlight_color,
filename,
adjust=False,
):
fig, ax = plt.subplots(1, figsize=(6 * 1.0, 6))
text = []
labeled_df = pd.DataFrame()
for i, j in zip(df[x], df[y]):
# Find rows matching this value...
i = np.round(i, 2)
j = np.round(j, 2)
tmp = df[(np.round(df[x], 2) == i) & (np.round(df[y], 2) == j)]
atom1s = tmp.atom1
atom2s = tmp.atom2
atom3s = tmp.atom3
atom4s = tmp.atom4
names = [
f"{i.strip()}-{j.strip()}-{k.strip()}-{l.strip()}"
for i, j, k, l in zip(atom1s, atom2s, atom3s, atom4s)
]
# I've added additional rounding because multiple angles are really quite close together.
labeled_df = labeled_df.append(
pd.DataFrame(
{"i": np.round(i, 2), "j": np.round(j, 2), "names": ",".join(names)},
index=[0],
),
ignore_index=True,
)
labeled_df.drop_duplicates(inplace=True)
labeled_df.reset_index(inplace=True)
for index, row in labeled_df.iterrows():
print(row.names.split(","))
all_names = row.names.split(",")
elements = []
for name in all_names:
atoms = name.split("-")
elemental_angle = "--".join(atom[0] for atom in atoms)
elements.append(elemental_angle)
element_angles = [i for i in set(elements)]
element_names = ",".join(element_angles)
labeled_df.iloc[index, labeled_df.columns.get_loc("names")] = element_names
# return labeled_df, labeled_df.drop_duplicates(inplace=True)
# Merge angles which are the same, but labeled in reverse
for index, row in labeled_df.iterrows():
names = row.names
# seen = set ()
# Something like this should be more general: https://stackoverflow.com/questions/17212208/removing-reverse-elements-in-a-python-list
# reduced_names = [x for x in names if tuple(x[::-1]) not in seen and not seen.add(tuple(names))]
print(f"Pruning {names.split(',')[1:]} from {names.split(',')[0]}.")
# I am printing this out, because it is not the most general solution.
# This doesn't truly detect if things are palindromes.
labeled_df.iloc[index, labeled_df.columns.get_loc("names")] = names.split(",")[
0
]
# Deduplicate names, again.
labeled_df.drop_duplicates(subset="names", inplace=True)
labeled_df.reset_index(inplace=True)
print(labeled_df)
for index, row in labeled_df.iterrows():
if abs(row.i - row.j) > 0.1 * row.i:
ax.scatter(
row.i, row.j, s=80, edgecolor="none", lw=0.2, color="r", zorder=2
)
text.append(plt.text(row.i, row.j, f"{row.names}", color="r", size=14))
else:
ax.scatter(row.i, row.j, s=80, edgecolor="none", lw=0.2, color="0.5")
ax.plot([-500, 500], [-500, 500], ls="-", c="0.3", zorder=-1, lw="0.5")
ax.set_ylim([lower_lim, upper_lim])
ax.set_xlim([lower_lim, upper_lim])
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
if adjust:
adjust_text(
text,
expand_points=(1.5, 1.5),
expand_text=(2, 2),
arrowprops=dict(arrowstyle="-", color="k"),
)
fig.savefig(f"figures/{filename}.pdf", bbox_inches="tight")
fig.savefig(f"figures/{filename}.png", dpi=300, bbox_inches="tight")