Skip to content

Commit

Permalink
tv_denoise work
Browse files Browse the repository at this point in the history
  • Loading branch information
alisiafadini committed Aug 11, 2024
1 parent 5eed4a5 commit 51e978b
Showing 1 changed file with 8 additions and 140 deletions.
148 changes: 8 additions & 140 deletions meteor/TV_filtering/tv_denoise_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,22 @@ def main():
0
] # TODO this seems problematic when full paths are not given

# Read in mtz file
og_mtz = meteor_io.subset_to_FandPhi(
*args.mtz, {args.mtz[1]: "F", args.mtz[2]: "Phi"}
).dropna()
og_mtz = og_mtz.compute_dHKL()

# Apply resolution cut if specified
if args.highres is not None:
high_res = args.highres
else:
high_res = np.min(og_mtz.compute_dHKL()["dHKL"])

# Read in mtz file
high_res = np.min(["dHKL"])

og_mtz = og_mtz.compute_dHKL()
# og_mtz = og_mtz[og_mtz["dHKL"] < 10]
og_mtz = og_mtz.loc[og_mtz.compute_dHKL()["dHKL"] > high_res]
og_mtz = og_mtz.loc[og_mtz["dHKL"] > high_res]

# Find and save denoised maps that (1) minimizes the map error or (2) maximizes the map negentropy
# Find and save denoised maps that maximizes the map negentropy
(
TVmap_best_err,
TVmap_best_entr,
Expand All @@ -88,16 +87,14 @@ def main():
ph_change,
) = tv.find_TVmap(og_mtz, "F", "Phi", name, path, map_res, cell, space_group)

print("high res", high_res)

io.map2mtzfile(
meteor_io.map2mtzfile(
TVmap_best_err,
"{n}_TV_{l}_besterror.mtz".format(
n=name, l=np.round(lambda_best_err, decimals=3)
),
high_res,
)
io.map2mtzfile(
meteor_io.map2mtzfile(
TVmap_best_entr,
"{n}_TV_{l}_bestentropy.mtz".format(
n=name, l=np.round(lambda_best_entr, decimals=3)
Expand All @@ -112,136 +109,7 @@ def main():
)
)

# finmap = dsutils.map_from_Fs(TVmap_best_entr, "FWT", "PHWT", 8)
# TVmap_best_entr.write_ccp4_map("{p}{n}_TV_0.67map.ccp4".format(p=path, n=name))

# Optionally plot result for errors and negentropy
if args.plot is True:
fig, ax1 = plt.subplots(figsize=(10, 5))

color = "black"
ax1.set_xlabel(r"$\lambda$")
ax1.set_ylabel(
r"$\sum (\Delta \mathrm{Fobs}_\mathrm{free} - \Delta \mathrm{Fcalc}_\mathrm{free})^2$",
color=color,
)
ax1.plot(
np.linspace(1e-8, 0.1, len(errors)),
errors / np.max(errors),
color=color,
linewidth=5,
)
ax1.tick_params(axis="y", labelcolor=color)

ax2 = ax1.twinx()

color = "silver"
ax2.set_ylabel("Negentropy", color="grey")
ax2.plot(
np.linspace(1e-8, 0.1, len(entropies)), entropies, color=color, linewidth=5
)
ax2.tick_params(axis="y", labelcolor="grey")

fig.tight_layout()
fig.savefig("{p}{n}lambda-optimize.png".format(p=path, n=name))

print("Saving weight scan plot")

fig, ax = plt.subplots(figsize=(9, 5))
ax.set_xlabel(r"$\lambda$")
ax.set_ylabel(r" < | |F$_\mathrm{obs}$| - |F$_\mathrm{TV}$| | > ")
ax.plot(
np.linspace(1e-8, 0.1, len(entropies)),
np.mean(np.abs(amp_change), axis=1),
color="indigo",
linewidth=5,
)
fig.tight_layout()
fig.savefig("{p}{n}F-change.png".format(p=path, n=name))

fig, ax = plt.subplots(figsize=(9, 5))
ax.set_xlabel(r"$\lambda$")
ax.set_ylabel(r"< | $\phi_\mathrm{obs}$ - $\phi_\mathrm{TV}$ | > ($^\circ$)")
ax.plot(
np.linspace(1e-8, 0.1, len(entropies)),
np.mean(np.abs(ph_change), axis=1),
color="orangered",
linewidth=5,
)
ax.axhline(y=19.52, linewidth=2, linestyle="--", color="orangered")
fig.tight_layout()
fig.savefig("{p}{n}phi-change.png".format(p=path, n=name))

fig, ax = plt.subplots(figsize=(9, 5))
# ax.scatter(1/og_mtz.compute_dHKL()["dHKL"], np.abs(ph_change[np.argmin(errors)]), label="Best Error Map, mean = {}".format(np.round(np.mean(ph_change[np.argmin(errors)])), decimals=2), color='black', alpha=0.5)
# ax.scatter(1/og_mtz.compute_dHKL()["dHKL"], np.abs(ph_change[np.argmax(entropies)]), label="Best Entropy Map, mean = {}".format(np.round(np.mean(ph_change[np.argmax(entropies)])), decimals=2), color='grey', alpha=0.5)

res_mean, data_mean = dsutils.resolution_shells(
np.abs(ph_change[np.argmin(errors)]), 1 / og_mtz.compute_dHKL()["dHKL"], 20
)
# ax.plot(
# res_mean,
# data_mean,
# linewidth=3,
# linestyle="--",
# color="black",
# label="Best Error",
# )
res_mean, data_mean = dsutils.resolution_shells(
np.abs(ph_change[np.argmax(entropies)]),
1 / og_mtz.compute_dHKL()["dHKL"],
20,
)
ax.plot(
res_mean,
data_mean,
linewidth=3,
linestyle="--",
color="darkgray",
label="Best Negentropy",
)
ax.set_ylabel(r"| $\phi_\mathrm{obs}$ - $\phi_\mathrm{TV}$ | ($^\circ$)")
ax.set_xlabel(r"1/dHKL (${\AA}^{-1}$)")
fig.tight_layout()
fig.legend(handlelength=0.6, loc="center right")
fig.savefig("{p}{n}phi-change-dhkl.png".format(p=path, n=name))

fig, ax = plt.subplots(figsize=(9, 5))
# ax.scatter(1/og_mtz.compute_dHKL()["dHKL"], np.abs(amp_change[np.argmin(errors)]), label="Best Error Map, mean = {}".format(np.round(np.mean(amp_change[np.argmin(errors)])), decimals=2), color='black', alpha=0.5)
# ax.scatter(1/og_mtz.compute_dHKL()["dHKL"], np.abs(amp_change[np.argmax(entropies)]), label="Best Entropy Map, mean = {}".format(np.round(np.mean(amp_change[np.argmax(entropies)])), decimals=2), color='darkgray', alpha=0.5)

res_mean, data_mean = dsutils.resolution_shells(
np.abs(amp_change[np.argmin(errors)]), 1 / og_mtz.compute_dHKL()["dHKL"], 15
)
# ax.plot(
# res_mean,
# data_mean,
# linewidth=3,
# linestyle="--",
# color="black",
# label="Best Error",
# )
res_mean, data_mean = dsutils.resolution_shells(
np.abs(amp_change[np.argmax(entropies)]),
1 / og_mtz.compute_dHKL()["dHKL"],
15,
)
ax.plot(
res_mean,
data_mean,
linewidth=3,
linestyle="--",
color="darkgray",
label="Best Negentropy",
)
ax.set_ylabel(r"| |F$_\mathrm{obs}$| - |F$_\mathrm{TV}$| |")
ax.set_xlabel(r"1/dHKL (${\AA}^{-1}$)")
fig.tight_layout()
fig.legend(handlelength=0.6, loc="center right")
fig.savefig("{p}{n}amp-change-dhkl.png".format(p=path, n=name))

# plt.show()
print("DONE.")
print("DONE.")


if __name__ == "__main__":
Expand Down

0 comments on commit 51e978b

Please sign in to comment.