-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_correlation.py
More file actions
170 lines (137 loc) · 3.88 KB
/
plot_correlation.py
File metadata and controls
170 lines (137 loc) · 3.88 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
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
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import reciprocalspaceship as rs
import torch
from integrator.utils import BaseParser
def get_preds(pt: str | Path):
return torch.load(pt, weights_only=False)
def concatenate_preds(preds: dict):
return np.concatenate(preds)
def plot_isigi_raw(
df,
im_name,
savefig=False,
title="I/Sig(I)",
dpi=300,
):
fig, ax = plt.subplots()
x = 1 / (df["dHKL"] ** 2)
y = df["I"] / df["SIGI"]
ax.scatter(x, y, color="black", alpha=0.4, s=3.0)
ax.set_xlabel("(1/d^2)")
ax.set_ylabel("Mean(I)/Sig(I)")
ax.grid(alpha=0.4)
ax.set_title(title)
if savefig:
fig.savefig(im_name, dpi=dpi)
plt.close(fig)
def plot_mean_isigi_raw(
df,
im_name,
savefig=False,
bins=None,
title="Mean(Mean(I)/Sig(I))",
dpi=300,
):
fig, ax = plt.subplots()
x = df.index
y = df["isigi"]
ax.plot(x, y, color="black")
ax.set_xlabel("resolution bin")
ax.set_ylabel("Mean(Mean(I)/Sig(I))")
if bins is not None:
ax.set_xticks(x, labels=bins, rotation=55)
ax.grid(alpha=0.4)
ax.set_title(title)
fig.tight_layout()
if savefig:
fig.savefig(im_name, dpi=dpi)
plt.close(fig)
def plot_intensity(
intensity_x,
intensity_y,
im_name: str,
title: str,
savefig: bool,
dpi=300,
axis_scale: str = "symlog",
):
# equality line
max = np.max([intensity_x, intensity_y])
fig, ax = plt.subplots()
# plot intensities
ax.plot(
[0, max],
[0, max],
color="red",
alpha=0.7,
label="x=y",
)
ax.scatter(
intensity_x,
intensity_y,
color="black",
alpha=0.3,
s=5.0,
)
ax.set_yscale(axis_scale)
ax.set_xscale(axis_scale)
ax.set_ylabel("laue-dials")
ax.set_xlabel("model")
ax.grid(alpha=0.3)
ax.set_title(title)
ax.legend()
if savefig:
fig.savefig(im_name, dpi=dpi)
plt.close(fig)
def main(args):
# form path to lightning log
root = Path(args.root)
id = Path(args.wandb_id)
path = list((root).glob(f"*{id}"))[0]
# iterate of all preds and plots raw intensity correlation against laue-dials
for p in list(path.glob("**/preds.pt")):
# get some metadata
epoch = p.parent.name
wandb_id = p.parents[2].name.split("-")[-1]
# load torch predictions
preds = get_preds(p.as_posix())
# get network and laue-dials intensity
intensity_x = concatenate_preds(preds["intensity_mean"])
intensity_y = concatenate_preds(preds["dials_I_prf_value"])
# plots the intensity correlations
plot_intensity(
intensity_x=intensity_x,
intensity_y=intensity_y,
im_name=f"{p.parent.as_posix()}/I_ld_vs_nn.png",
title=f"LD vs NN {epoch}\nwandb id: {wandb_id}",
savefig=True,
)
# plot I/sigI as function of resolution
for p in list(path.glob("**/preds.mtz")):
# get some metadata
epoch = p.parent.name
wandb_id = p.parents[2].name.split("-")[-1]
# load mtz file
df = rs.read_mtz(p.as_posix()).compute_dHKL()
df, bins = df.assign_resolution_bins()
df["isigi"] = df["I"] / df["SIGI"]
mean_df = df.groupby("bin").mean()
plot_isigi_raw(
df,
im_name=f"{p.parent.as_posix()}/ISigI_nn_{epoch}.png",
savefig=True,
title=f"Mean(I)/Sig(I) {epoch}\nwandb_id: {wandb_id}",
)
plot_mean_isigi_raw(
mean_df,
im_name=f"{p.parent.as_posix()}/mean_isigi_nn_{epoch}.png",
savefig=True,
bins=bins,
title=f"Mean(Mean(I)/Sig(I){epoch}\nwandb_id:{wandb_id}",
)
if __name__ == "__main__":
argparser = BaseParser()
args = argparser.parse_args()
main(args)