-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdffit.py
More file actions
169 lines (141 loc) · 5.63 KB
/
Copy pathpdffit.py
File metadata and controls
169 lines (141 loc) · 5.63 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
# SPDX-FileCopyrightText: 2026 EasyScience contributors <https://github.com/easyscience>
# SPDX-License-Identifier: BSD-3-Clause
"""
PDF calculation backend using diffpy.pdffit2 if available.
The class adapts the engine to EasyDiffraction calculator interface and
silences stdio on import to avoid noisy output in notebooks and logs.
"""
import os
import re
from pathlib import Path
import numpy as np
from easydiffraction.analysis.calculators.base import CalculatorBase
from easydiffraction.analysis.calculators.factory import CalculatorFactory
from easydiffraction.core.metadata import TypeInfo
from easydiffraction.datablocks.experiment.item.base import ExperimentBase
from easydiffraction.datablocks.structure.item.base import Structure
try:
from diffpy.pdffit2 import PdfFit
from diffpy.pdffit2 import redirect_stdout
from diffpy.structure.parsers.p_cif import P_cif as pdffit_cif_parser
# Silence the C++ engine output while keeping the handle open
_pdffit_devnull: object | None
with Path(os.devnull).open('w', encoding='utf-8') as _tmp_devnull:
# Duplicate file descriptor so the handle remains
# valid after the context
_pdffit_devnull = os.fdopen(os.dup(_tmp_devnull.fileno()), 'w')
redirect_stdout(_pdffit_devnull)
# TODO: Add the following print to debug mode
# print("✅ 'pdffit' calculation engine is successfully imported.")
except ImportError:
# TODO: Add the following print to debug mode
# print("⚠️ 'pdffit' module not found. This calculation engine will
# not be available.")
PdfFit = None
redirect_stdout = None
pdffit_cif_parser = None
_pdffit_devnull = None
@CalculatorFactory.register
class PdffitCalculator(CalculatorBase):
"""Wrapper for Pdffit library."""
type_info = TypeInfo(
tag='pdffit',
description='PDFfit2 for pair distribution function calculations',
)
engine_imported: bool = PdfFit is not None
@property
def name(self) -> str:
"""Short identifier of this calculator engine."""
return 'pdffit'
def calculate_structure_factors( # noqa: PLR6301
self,
structures: object,
experiments: object,
) -> list:
"""
Return an empty list; PDF does not compute structure factors.
Parameters
----------
structures : object
Unused; kept for interface consistency.
experiments : object
Unused; kept for interface consistency.
Returns
-------
list
An empty list.
"""
# PDF doesn't compute HKL but we keep interface consistent
# Intentionally unused, required by public API/signature
del structures, experiments
print('[pdffit] Calculating HKLs (not applicable)...')
return []
def calculate_pattern( # noqa: PLR6301
self,
structure: Structure,
experiment: ExperimentBase,
*,
called_by_minimizer: bool = False,
) -> None:
"""
Calculate the PDF pattern using PDFfit2.
Parameters
----------
structure : Structure
The structure object supplying atom sites and cell
parameters.
experiment : ExperimentBase
The experiment object supplying instrument and peak
parameters.
called_by_minimizer : bool, default=False
Unused; kept for interface consistency.
"""
# Intentionally unused, required by public API/signature
del called_by_minimizer
# Create PDF calculator object
calculator = PdfFit()
# ---------------------------
# Set structure parameters
# ---------------------------
# TODO: move CIF v2 -> CIF v1 conversion to a separate module
# Convert the structure to CIF supported by PDFfit
cif_string_v2 = structure.as_cif
# convert to version 1 of CIF format
# this means: replace all dots with underscores for
# cases where the dot is surrounded by letters on both sides.
pattern = r'(?<=[a-zA-Z])\.(?=[a-zA-Z])'
cif_string_v1 = re.sub(pattern, '_', cif_string_v2)
# Create the PDFit structure
pdffit_structure = pdffit_cif_parser().parse(cif_string_v1)
# Set all model parameters:
# space group, cell parameters, and atom sites (including ADPs)
calculator.add_structure(pdffit_structure)
# -------------------------
# Set experiment parameters
# -------------------------
# Set some peak-related parameters
calculator.setvar('pscale', experiment.linked_phases[structure.name].scale.value)
calculator.setvar('delta1', experiment.peak.sharp_delta_1.value)
calculator.setvar('delta2', experiment.peak.sharp_delta_2.value)
calculator.setvar('spdiameter', experiment.peak.damp_particle_diameter.value)
# Data
x = list(experiment.data.x)
y_noise = list(np.zeros_like(x))
# Assign the data to the PDFfit calculator
calculator.read_data_lists(
stype=experiment.type.radiation_probe.value[0].upper(),
qmax=experiment.peak.cutoff_q.value,
qdamp=experiment.peak.damp_q.value,
r_data=x,
Gr_data=y_noise,
)
# qbroad must be set after read_data_lists
calculator.setvar('qbroad', experiment.peak.broad_q.value)
# -----------------
# Calculate pattern
# -----------------
# Calculate the PDF pattern
calculator.calc()
# Get the calculated PDF pattern
pattern = calculator.getpdf_fit()
return np.array(pattern)