Datasets:
File size: 2,922 Bytes
03a6065 |
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 |
import os
from matminer.datasets import load_dataset
from mp_api.client import MPRester
from pymatgen.analysis.diffraction.xrd import XRDCalculator
import pymatviz as pmv
from pymatviz.enums import ElemColorScheme, Key
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tqdm import tqdm
MP_API_KEY = ""
MID = ["mp-353", "mp-661", "mp-856", "mp-1000", "mp-1479", "mp-2284", "mp-2294", "mp-10044", "mp-10086", "mp-10910", "mp-18905", "mp-23231", "mp-36526", "mp-861883", "mp-862786"]
xrd_calculator = XRDCalculator(wavelength='CuKa')
patterns = {}
for mid in tqdm(MID):
# if os.path.exists(f"{mid}-xrd.png"):
# print(f"{mid}-xrd.png already exists, skipping...")
# continue
with MPRester(MP_API_KEY) as mpr:
structure = mpr.get_structure_by_material_id(mid)
sga = SpacegroupAnalyzer(structure)
conventional_structure = sga.get_conventional_standard_structure()
xrd_pattern = xrd_calculator.get_pattern(conventional_structure, scaled=False)
patterns[mid] = xrd_pattern
plt.figure(figsize=(12, 6))
bar_width = 0.5
x = xrd_pattern.x
y = xrd_pattern.y / np.max(xrd_pattern.y) * 100
plt.bar(x, y, width=bar_width, color='black')
plt.xlabel('2 Theta (degrees)', fontsize=14)
plt.ylabel('Intensity (a.u.)', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.gca().spines['top'].set_linewidth(0.5)
plt.gca().spines['right'].set_linewidth(0.5)
plt.tight_layout()
plt.savefig(f"{mid}-xrd.png", dpi=300, bbox_inches='tight')
plt.close()
for _ in tqdm(range(5)):
mids = np.random.choice(MID, 3, replace=False)
mids = sorted(mids)
combined_pattern = {}
for mid in mids:
pattern = patterns[mid]
for two_theta, intensity in zip(pattern.x, pattern.y):
if two_theta in combined_pattern:
combined_pattern[two_theta] += intensity
else:
combined_pattern[two_theta] = intensity
combined_pattern_list = [(k, v) for k, v in combined_pattern.items()]
combined_pattern_list.sort(key=lambda x: x[0])
x = np.array([item[0] for item in combined_pattern_list])
y = np.array([item[1] for item in combined_pattern_list])
y = y / np.max(y) * 100
plt.figure(figsize=(12, 6))
bar_width = 0.5
plt.bar(x, y, width=bar_width, color='black')
plt.xlabel('2 Theta (degrees)', fontsize=14)
plt.ylabel('Intensity (a.u.)', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.gca().spines['top'].set_linewidth(0.5)
plt.gca().spines['right'].set_linewidth(0.5)
plt.tight_layout()
plt.savefig(f"{'_'.join(mids)}-xrd.png", dpi=300, bbox_inches='tight')
plt.close()
|