Skip to content

PDF comparison #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/diffpy/similarity/compare_pdfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from pathlib import Path

import numpy as np

from diffpy.pdfmorph.pdfmorphpy import pdfmorph
from diffpy.utils.parsers import loadData

grnames = [
"Si_O_only_10_common_grs",
"Ti_O_only_10_common_grs",
"Zr_O_only_10_common_grs",
]

gr_list = []

for grname in grnames:
grdir = Path(f"PDFs/{grname}")

gr_list_component = list(grdir.iterdir())
gr_list_component.sort()

gr_list.extend(gr_list_component)

grname = "All_O_only_10_common_grs"

datmatrw = []
datmatrw_morph = []

datmatpcc = []
datmatpcc_morph = []

labels = []

for grfile1 in gr_list:
with open(grfile1, "r") as grread:
for line in grread:
if "composition =" in line:
labels.append(line.split("=")[1].strip())

datrw_row = []
datpcc_row = []

datrw_row_morph = []
datpcc_row_morph = []

for grfile2 in gr_list:
rawdata = pdfmorph(grfile1, grfile2, noplot="")[0]
morphdata = pdfmorph(grfile1, grfile2, noplot="", stretch=0, scale=1)[
0
]

datrw_row.append(rawdata["Rw"])
datpcc_row.append(rawdata["Pearson"])

datrw_row_morph.append(morphdata["Rw"])
datpcc_row_morph.append(morphdata["Pearson"])

datmatrw.append(datrw_row)
datmatrw_morph.append(datrw_row_morph)

datmatpcc.append(datpcc_row)
datmatpcc_morph.append(datpcc_row_morph)

datmatrw = np.array(datmatrw)
datmatrw = np.sqrt(datmatrw * datmatrw.T)

datmatrw_morph = np.array(datmatrw_morph)
datmatrw_morph = np.sqrt(datmatrw_morph * datmatrw_morph.T)

datmatpcc = np.array(datmatpcc)
datmatpcc = (1 - datmatpcc) / 2
datmatpcc_morph = np.array(datmatpcc_morph)
datmatpcc_morph = (1 - datmatpcc_morph) / 2

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

for idx, df in enumerate(
[datmatrw, datmatrw_morph, datmatpcc, datmatpcc_morph]
):
dm = pd.DataFrame(df, index=labels, columns=labels)

mask = np.triu(
np.ones_like(np.array(dm), dtype=bool)
) # make a lower triangular mask
# Plot the heatmap with the mask
plt.figure(figsize=(15, 12))
sns.heatmap(dm, annot=False, vmin=0, vmax=1)
plt.grid(False)
plt.tight_layout()

iname = None
if idx == 0:
iname = "Rw"
elif idx == 1:
iname = "Rw-morphed"
elif idx == 2:
iname = "Pcc"
elif idx == 3:
iname = "Pcc-morphed"
dm.to_csv(Path("figs/CSVs") / f"{grname}-{iname}.csv")
plt.savefig(Path("figs") / f"{grname}-{iname}.png", dpi=300)
plt.savefig(Path("figs") / f"{grname}-{iname}.svg", dpi=300)

plt.show()
94 changes: 94 additions & 0 deletions src/diffpy/similarity/compare_pdfs_single.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from pathlib import Path

import numpy as np

from diffpy.pdfmorph.pdfmorphpy import pdfmorph
from diffpy.utils.parsers import loadData

grname = "Si_O_only_10_common_grs"
grdir = Path(f"PDFs/{grname}")

gr_list = list(grdir.iterdir())
gr_list.sort()

datmatrw = []
datmatrw_morph = []

datmatpcc = []
datmatpcc_morph = []

labels = []

for grfile1 in gr_list:
with open(grfile1, "r") as grread:
for line in grread:
if "composition =" in line:
labels.append(line.split("=")[1].strip())

datrw_row = []
datpcc_row = []

datrw_row_morph = []
datpcc_row_morph = []

for grfile2 in gr_list:
rawdata = pdfmorph(grfile1, grfile2, noplot="")[0]
morphdata = pdfmorph(grfile1, grfile2, noplot="", stretch=0, scale=1)[
0
]

datrw_row.append(rawdata["Rw"])
datpcc_row.append(rawdata["Pearson"])

datrw_row_morph.append(morphdata["Rw"])
datpcc_row_morph.append(morphdata["Pearson"])

datmatrw.append(datrw_row)
datmatrw_morph.append(datrw_row_morph)

datmatpcc.append(datpcc_row)
datmatpcc_morph.append(datpcc_row_morph)

datmatrw = np.array(datmatrw)
datmatrw = np.sqrt(datmatrw * datmatrw.T)

datmatrw_morph = np.array(datmatrw_morph)
datmatrw_morph = np.sqrt(datmatrw_morph * datmatrw_morph.T)

datmatpcc = np.array(datmatpcc)
datmatpcc = (1 - datmatpcc) / 2
datmatpcc_morph = np.array(datmatpcc_morph)
datmatpcc_morph = (1 - datmatpcc_morph) / 2

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

for idx, df in enumerate(
[datmatrw, datmatrw_morph, datmatpcc, datmatpcc_morph]
):
dm = pd.DataFrame(df, index=labels, columns=labels)

mask = np.triu(
np.ones_like(np.array(dm), dtype=bool)
) # make a lower triangular mask
# Plot the heatmap with the mask
plt.figure(figsize=(10, 8))
sns.heatmap(dm, annot=True, vmin=0, vmax=1)
plt.grid(False)
plt.tight_layout()

iname = None
if idx == 0:
iname = "Rw"
elif idx == 1:
iname = "Rw-morphed"
elif idx == 2:
iname = "Pcc"
elif idx == 3:
iname = "Pcc-morphed"
dm.to_csv(Path("figs/CSVs") / f"{grname}-{iname}.csv")
plt.savefig(Path("figs") / f"{grname}-{iname}.png", dpi=300)
plt.savefig(Path("figs") / f"{grname}-{iname}.svg", dpi=300)

plt.show()
47 changes: 47 additions & 0 deletions src/diffpy/similarity/convert_cif_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path

from diffpy.srreal.pdfcalculator import PDFCalculator
from diffpy.structure import Structure

Uisodefault = 0.005

typename = "Zr_O_only"
cifdir = Path(f"CIFs/{typename}")

import numpy as np
from pyobjcryst import loadCrystal

cfg = {
"qmax": 25,
"rmin": 0,
"rmax": 30,
}

pc0 = PDFCalculator(**cfg)

pdfdir = Path(f"PDFs/{typename}_grs")

for ciffile in cifdir.iterdir():
crystal = loadCrystal(ciffile)
for sc in crystal.GetScatteringComponentList():
sp = sc.mpScattPow
sp.Biso = sp.Biso or 8 * np.pi**2 * Uisodefault

r0, g0 = pc0(crystal)

crystalcomp = None
with open(ciffile, "r") as cifread:
for line in cifread:
if "_chemical_formula_structural" in line:
crystalcomp = line.split()[1]

with open(f"PDFs/{typename}_grs/{ciffile.stem}.cgr", "w") as grfile:
grfile.write("i# [PDF] Computed by diffpy-cmi\n")
grfile.write(f"composition = {crystalcomp}\n")
grfile.write(f"qmax = {cfg['qmax']}\n")
grfile.write(f"rmin = {cfg['rmin']}\n")
grfile.write(f"rmax = {cfg['rmax']}\n")

grfile.write("\n")
grfile.write(f"# Labels: r, gr\n")
np.savetxt(grfile, np.array([r0, g0]).T)
Loading