| 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): |
| |
| |
| |
|
|
| 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() |
| |
| |
|
|
|
|