diff --git a/.github/workflows/check-news-item.yml b/.github/workflows/check-news-item.yml index 2e36b22..4bccdde 100644 --- a/.github/workflows/check-news-item.yml +++ b/.github/workflows/check-news-item.yml @@ -3,7 +3,7 @@ name: Check for News on: pull_request_target: branches: - - main # GitHub does not evaluate expressions in trigger filters; edit this value if your base branch is not main + - main # GitHub does not evaluate expressions in trigger filters; edit this value if your base branch is not main jobs: check-news-item: diff --git a/.github/workflows/tests-on-pr.yml b/.github/workflows/tests-on-pr.yml index f4f7972..2fae32c 100644 --- a/.github/workflows/tests-on-pr.yml +++ b/.github/workflows/tests-on-pr.yml @@ -11,6 +11,8 @@ jobs: project: diffpy.cmirietveld c_extension: false headless: false - run: python -m pip install git+https://github.com/diffpy/pyobjcryst.git@d6a1279a26b9b1e6e303d103935b4469c5a62e41 --no-deps + run: | + python -m pip install git+https://github.com/diffpy/pyobjcryst.git@main --no-deps + python -m pip install diffpy.cmistructure --no-deps secrets: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/cookiecutter.json b/cookiecutter.json index 80f07a4..dcd91b6 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -14,7 +14,7 @@ "project_short_description": "A pack for diffpy.cmi that allows it to do Rietveld refinement of powder diffraction data", "project_keywords": "diffraction, PDF, X-ray, neutron, Rietveld", "minimum_supported_python_version": "3.12", - "maximum_supported_python_version": "3.14", + "maximum_supported_python_version": "3.13", "project_needs_c_code_compiled": "No", "project_has_gui_tests": "No" } diff --git a/docs/examples/refine_pbso4_xray.py b/docs/examples/refine_pbso4_xray.py new file mode 100644 index 0000000..71e70fb --- /dev/null +++ b/docs/examples/refine_pbso4_xray.py @@ -0,0 +1,131 @@ +"""Refine PbSO4 against lab x-ray data in stages. + +This is the refinement of ``tests/test_refinement.py`` as a script. Run +it from the repository root: + + python refine_pbso4_xray.py +""" + +from pathlib import Path + +from scipy.optimize import least_squares + +from diffpy.cmirietveld import ( + ChebyshevBackground, + ConstantWavelengthInstrument, + GSASDataParser, + PowderDiffractionGenerator, + PseudoVoigtProfile, +) +from diffpy.srfit import ( + FitContribution, + FitRecipe, + FitResults, + Profile, +) + +TESTDATA_DIR = Path(__file__).parents[2] / "tests" / "testdata" +DATA_FILE = TESTDATA_DIR / "PBSO4.XRA" +CIF_FILE = TESTDATA_DIR / "PbSO4-Wyckoff.cif" +STAGES = [ + ["scale", "background"], + ["cell"], + ["displacement"], + ["profile"], + ["atoms"], +] + + +def make_recipe(data_file, cif_file): + """Make the recipe for a PbSO4 x-ray refinement. + + The data are 16 to 110 degrees of a GSAS data file, with a Cu tube + and ObjCryst's polarization rate 0.4, GSAS-II's polarization of 0.7. + The background is fitted to the data first. Every variable is fixed + and tagged by the stage that frees it. + + Parameters + ---------- + data_file : pathlib.Path + The path to the GSAS data file. + cif_file : pathlib.Path + The path to the CIF file of the structure. + + Returns + ------- + FitRecipe + The recipe, with all its variables fixed. + """ + parser = GSASDataParser() + parser.parse_file(data_file) + profile = Profile() + profile.load_parsed_data(parser) + profile.set_calculation_range(xmin=16.0, xmax=110.0) + background = ChebyshevBackground("background", x_range=(16.0, 110.0)) + background.fit_to_data(profile.x, profile.y) + background.plot_fit() + instrument = ConstantWavelengthInstrument() + instrument.set_xray_source("Cu") + instrument.set_polarization(0.4) + generator = PowderDiffractionGenerator("bragg") + generator.set_structure(cif_file, name="PbSO4") + generator.set_instrument(instrument) + generator.set_peak_profile(PseudoVoigtProfile()) + contribution = FitContribution("pbso4") + contribution.set_profile(profile, xname="tth") + contribution.add_profile_generator(generator) + contribution.register_calculator(background, argnames=["tth"]) + contribution.set_equation("bragg + background") + recipe = FitRecipe() + recipe.add_contribution(contribution) + recipe.add_variable(generator.scale, 1e-5, tags=["scale"]) + for coefficient in background.coefficients: + recipe.add_variable(coefficient, tags=["background"]) + phase = generator.phase + for parameter in phase.sgpars.latpars: + recipe.add_variable(parameter, tags=["cell"]) + recipe.add_variable(instrument.displacement, tags=["displacement"]) + for name in ["U", "V", "W", "eta0", "asym0", "asym1"]: + recipe.add_variable(generator.peak_profile.get(name), tags=["profile"]) + for parameter in phase.sgpars.xyzpars: + recipe.add_variable(parameter, tags=["atoms"]) + for scatterer in phase.get_scatterers(): + recipe.add_variable( + scatterer.Biso, 0.8, name=f"Biso_{scatterer.name}", tags=["atoms"] + ) + recipe.fix("all") + return recipe + + +def refine_recipe(recipe, stages): + """Refine the recipe in stages. + + Each stage frees the variables with its tags and refines them with + the variables freed before. + + Parameters + ---------- + recipe : FitRecipe + The recipe, with all its variables fixed. + stages : list of list of str + The tags freed at each stage, in order. + """ + for tags in stages: + recipe.free(*tags) + least_squares( + recipe.residual, + recipe.get_values(), + x_scale="jac", + bounds=recipe.get_bounds_array(), + ) + + +def main(): + recipe = make_recipe(DATA_FILE, CIF_FILE) + refine_recipe(recipe, STAGES) + FitResults(recipe).print_results_table() + recipe.plot_recipe() + + +if __name__ == "__main__": + main() diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst index 6b62bf8..3f47908 100644 --- a/docs/source/getting-started.rst +++ b/docs/source/getting-started.rst @@ -38,12 +38,12 @@ Here is how you can write a block of code in the documentation. You can use the .. code-block:: bash # Create a new environment, without build dependencies (pure Python package) - conda create -n -env python=3.14 \ + conda create -n -env python=3.13 \ --file requirements/tests.txt \ --file requirements/conda.txt # Create a new environment, with build dependencies (non-pure Python package) - conda create -n -env python=3.14 \ + conda create -n -env python=3.13 \ --file requirements/tests.txt \ --file requirements/conda.txt \ --file requirements/build.txt diff --git a/news/powder-diffraction-generator.rst b/news/powder-diffraction-generator.rst new file mode 100644 index 0000000..b311f93 --- /dev/null +++ b/news/powder-diffraction-generator.rst @@ -0,0 +1,26 @@ +**Added:** + +* Add ``PowderDiffractionGenerator``, which calculates one phase's Bragg intensity with pyobjcryst from a structure set with ``set_structure`` (a pyobjcryst ``Crystal``, the path to a CIF file or a ParameterSet), a peak profile set with ``set_peak_profile`` and an instrument set with ``set_instrument``, and a refinable ``scale``. It has no default peak profile or instrument, so all three must be set before a pattern is calculated. +* Add ``PowderDiffractionGenerator.print_info``, which prints the generator's structure, instrument and peak profile with their settings and Parameter values. +* Add ``Component.get_settings``, which returns a component's settings for showing to a user, and its ``ConstantWavelengthInstrument`` version, which lists the radiation type, the x-ray source or Kalpha1/Kalpha2 wavelengths, and the polarization rate. +* Warn when ``set_structure``, ``set_peak_profile`` or ``set_instrument`` replaces a structure or component whose Parameters are used in a fit recipe. + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/pyproject.toml b/pyproject.toml index 52491de..fac3b93 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ maintainers = [ description = "A pack for diffpy.cmi that allows it to do Rietveld refinement of powder diffraction data" keywords = ['diffraction', 'PDF', 'X-ray', 'neutron', 'Rietveld'] readme = "README.rst" -requires-python = ">=3.12, <3.15" +requires-python = ">=3.12, <3.14" classifiers = [ 'Development Status :: 5 - Production/Stable', 'Environment :: Console', @@ -29,7 +29,6 @@ classifiers = [ 'Operating System :: Unix', 'Programming Language :: Python :: 3.12', 'Programming Language :: Python :: 3.13', - 'Programming Language :: Python :: 3.14', 'Topic :: Scientific/Engineering :: Physics', 'Topic :: Scientific/Engineering :: Chemistry', ] diff --git a/requirements/conda.txt b/requirements/conda.txt index 4fe1a65..322e9d3 100644 --- a/requirements/conda.txt +++ b/requirements/conda.txt @@ -3,3 +3,6 @@ diffpy.srfit libobjcryst>=2026.3.0 libboost-devel libboost-python +matplotlib-base +bg-mpl-stylesheets +pybaselines diff --git a/requirements/pip.txt b/requirements/pip.txt index f69c4a0..99b6847 100644 --- a/requirements/pip.txt +++ b/requirements/pip.txt @@ -1,3 +1,7 @@ numpy diffpy.srfit +diffpy.cmistructure pyobjcryst +matplotlib +bg-mpl-stylesheets +pybaselines diff --git a/src/diffpy/cmirietveld/__init__.py b/src/diffpy/cmirietveld/__init__.py index e916dfc..c325046 100644 --- a/src/diffpy/cmirietveld/__init__.py +++ b/src/diffpy/cmirietveld/__init__.py @@ -4,7 +4,8 @@ # (c) 2026 diffpy.cmirietveld contributors. # All rights reserved. # -# File coded by: Caden Myers, members of the Billinge group, and contributors to the diffpy.cmirietveld project. +# File coded by: Caden Myers, members of the Billinge group, and +# contributors to the diffpy.cmirietveld project. # # See GitHub contributions for a more detailed list of contributors. # https://github.com/diffpy/diffpy.cmirietveld/graphs/contributors @@ -12,11 +13,69 @@ # See LICENSE.rst for license information. # ############################################################################## -"""A pack for diffpy.cmi that allows it to do Rietveld refinement of powder diffraction data""" +"""A pack for diffpy.cmi that allows it to do Rietveld refinement of +powder diffraction data.""" + +from diffpy.cmirietveld.backgrounds import Background, ChebyshevBackground +from diffpy.cmirietveld.component import Component +from diffpy.cmirietveld.corrections import ( + IntensityCorrection, + MarchDollaseCorrection, +) +from diffpy.cmirietveld.instruments import ( + ConstantWavelengthInstrument, + Instrument, + TimeOfFlightInstrument, +) +from diffpy.cmirietveld.parsers import ( + GSASDataParser, + GSASIIInstrumentParser, + GSASInstrumentParser, + InstrumentParameterParser, + PowderDataParser, +) +from diffpy.cmirietveld.peak_profiles import ( + BackToBackExponentialProfile, + PeakProfile, + PseudoVoigtProfile, + TCHProfile, +) +from diffpy.cmirietveld.powderdiffractiongenerator import ( + PowderDiffractionGenerator, +) +from diffpy.cmirietveld.sources import ( + IntensitySource, + LeBailIntensities, + StructureIntensities, +) # package version from diffpy.cmirietveld.version import __version__ # noqa +__all__ = [ + "PowderDiffractionGenerator", + "Component", + "IntensitySource", + "StructureIntensities", + "LeBailIntensities", + "Instrument", + "ConstantWavelengthInstrument", + "TimeOfFlightInstrument", + "IntensityCorrection", + "MarchDollaseCorrection", + "PeakProfile", + "PseudoVoigtProfile", + "TCHProfile", + "BackToBackExponentialProfile", + "Background", + "ChebyshevBackground", + "PowderDataParser", + "GSASDataParser", + "InstrumentParameterParser", + "GSASInstrumentParser", + "GSASIIInstrumentParser", +] + # silence the pyflakes syntax checker assert __version__ or True diff --git a/src/diffpy/cmirietveld/backgrounds.py b/src/diffpy/cmirietveld/backgrounds.py index 4e944b4..cba2c39 100644 --- a/src/diffpy/cmirietveld/backgrounds.py +++ b/src/diffpy/cmirietveld/backgrounds.py @@ -22,8 +22,14 @@ __all__ = ["Background", "ChebyshevBackground"] +import numpy as np +from numpy.polynomial import chebyshev +from pybaselines.whittaker import arpls + from diffpy.srfit.fitbase import Calculator +N_CURVE_POINTS = 1000 + class Background(Calculator): """Base class of the backgrounds. @@ -55,30 +61,427 @@ def __call__(self, x): class ChebyshevBackground(Background): """Background described by a Chebyshev polynomial. - The range of x is mapped onto [-1, 1], so changing the calculation - range changes what the coefficients mean. + The range given at construction, ``x_range``, is mapped onto + [-1, 1], as GSAS-II maps its limits. The range is fixed, so the + coefficients keep their meaning when the calculation range of the + fit changes, and the background can be fitted to points before it is + first calculated. The background is only calculated inside the + range, ends included, since outside it the polynomial grows quickly + and would bend the fit. + + The background is put in a FitContribution's equation with + ``contribution.register_calculator(background, argnames=["tth"])``, + where "tth" is the name of the profile's x, after which its + Parameters sit under ``contribution.background``. Parameters ---------- name : str - The name of the background. It must be a valid attribute name. + The name of the background, used in the FitContribution equation, + for example "bragg + background". It must be a valid attribute + name. + x_range : tuple of float + The (xmin, xmax) range of the data axis mapped onto [-1, 1]. n_terms : int, optional The number of Chebyshev terms (default 6). + Attributes + ---------- + x_range : tuple of float + The (xmin, xmax) range of the data axis mapped onto [-1, 1]. + n_terms : int + The number of Chebyshev terms. + Managed Parameters ------------------ b0, b1, ... The Chebyshev coefficients, listed in order by ``coefficients``. + They start at 0. + + Raises + ------ + ValueError + If the start of ``x_range`` is not below its end, or ``n_terms`` + is below 1. """ - def __init__(self, name, n_terms=6): + def __init__(self, name, x_range, n_terms=6): + xmin, xmax = x_range + if not xmin < xmax: + raise ValueError( + f"ChebyshevBackground was given an x_range of {x_range}, " + "whose start isn't below its end. Please give the range as " + "(xmin, xmax), with xmin below xmax." + ) + if n_terms < 1: + raise ValueError( + f"ChebyshevBackground was given n_terms of {n_terms}, but a " + "background needs at least 1 term. Please give n_terms of 1 " + "or more." + ) Background.__init__(self, name) + self.x_range = (xmin, xmax) self.n_terms = n_terms + for index in range(n_terms): + self._new_parameter(f"b{index}", 0.0) + self._last_data = None + self._estimated_points = None + self._fixed_points = None @property def coefficients(self): """The Parameters of the Chebyshev coefficients, in order.""" - raise NotImplementedError + coefficients = [] + for index in range(self.n_terms): + coefficients.append(self.get(f"b{index}")) + return coefficients + + def __call__(self, x): + """Calculate the background. + + Parameters + ---------- + x : ndarray + The values of the data axis, for example 2theta in degrees, + within ``x_range``. + + Returns + ------- + ndarray + The background at each value of the data axis. + + Raises + ------ + ValueError + If an x value is outside ``x_range``, for example because the + profile's calculation range is wider than the background's. + """ + x = np.asarray(x, dtype=float) + xmin, xmax = self.x_range + if x.size and (x.min() < xmin or x.max() > xmax): + raise ValueError( + "The background was calculated on x values from " + f"{_format_x(x.min())} to {_format_x(x.max())}, outside its " + f"range of {self._format_range()}. Please set the profile's " + "calculation range within the background's range, or make " + "the background with a range that holds the data." + ) + background = chebyshev.chebval( + self._scale_x(x), self._get_coefficient_values() + ) + return background + + def fit_to_points(self, points): + """Fit the coefficients to the user's points. + + The points are (x, intensity) pairs chosen between the peaks, + like GSAS-II's fixed background points, and all count the same. + The coefficients are fitted to them by linear least squares and + set as the values of their Parameters, which become the + refinement's starting values. ``fit_to_data`` estimates the points + from the measured pattern instead. + + Parameters + ---------- + points : array_like + The (x, intensity) pairs, shaped (n, 2): a list of tuples, or + a two-column array such as ``np.loadtxt`` reads. The x values + are within ``x_range``. + + Raises + ------ + ValueError + If a point is not an (x, intensity) pair, there are fewer + points than terms, or a point is outside ``x_range``. + """ + points = self._read_points(points, "fit_to_points", "points", "point") + if len(points) < self.n_terms: + raise ValueError( + f"fit_to_points was given {len(points)} points, fewer than " + f"the background's {self.n_terms} terms, so the coefficients " + f"can't be determined. Please give at least {self.n_terms} " + "points, or make the background with fewer terms." + ) + self._fit_coefficients(points, np.ones(len(points))) + self._last_data = None + self._estimated_points = None + self._fixed_points = points + + def fit_to_data( + self, + x, + y, + fixed_points=None, + fixed_point_weight=1.0, + smoothness=None, + ): + """Fit the coefficients to a measured pattern. + + This is GSAS-II's automatic background. The baseline under the + pattern is estimated with pybaselines' ``arpls``, and evenly + spaced points are taken from it, every ``len(y) // 100``-th as in + GSAS-II. The user's ``fixed_points`` are added, and the + coefficients are fitted to all the points inside ``x_range`` by + weighted linear least squares and set as the values of their + Parameters, which become the refinement's starting values. + + The baseline is estimated from the data alone, so the background + can be fitted, and checked with ``plot_fit``, before any structure + is set up. + + Parameters + ---------- + x : array_like + The values of the data axis of the pattern, for example 2theta + in degrees. + y : array_like + The measured intensity at each x value. + fixed_points : array_like, optional + The user's (x, intensity) pairs, shaped (n, 2), within + ``x_range``. They are fitted with the estimated points. + fixed_point_weight : float, optional + The number of estimated points each fixed point counts as + (default 1, so every point counts the same, as in GSAS-II). A + larger weight lets the fixed points override the estimate + where it is wrong. + smoothness : float, optional + The log10 of ``arpls``'s smoothness, lambda. A larger value + gives a stiffer baseline that bends less into the peaks, and a + smaller one follows a curved background more closely. The + default, None, uses GSAS-II's value for the number of points, + min(10, (int(10 * log10(n)**1.5) - 9.5) / 10). + + Returns + ------- + ndarray + The estimated points used in the fit, inside ``x_range``, + shaped (n, 2), without the fixed points. + + Raises + ------ + ValueError + If ``x`` and ``y`` have different lengths, fewer points than + terms are inside ``x_range``, ``fixed_point_weight`` is not + above 0 (with or without fixed points), or a fixed point is + not an (x, intensity) pair or is outside ``x_range``. + """ + x, y = _read_data(x, y, "fit_to_data") + if not fixed_point_weight > 0: + raise ValueError( + "fit_to_data was given a fixed_point_weight of " + f"{fixed_point_weight}, which is not above 0. Please give a " + "weight above 0, such as 1 to count each fixed point as one " + "estimated point." + ) + if fixed_points is None: + fixed_points = np.empty((0, 2)) + else: + fixed_points = self._read_points( + fixed_points, "fit_to_data", "fixed_points", "fixed point" + ) + if smoothness is None: + smoothness = min( + 10, (int(10 * np.log10(len(y)) ** 1.5) - 9.5) / 10 + ) + baseline = arpls(y, lam=int(10**smoothness), max_iter=10)[0] + step = max(len(y) // 100, 1) + sampled_points = np.column_stack((x[::step], baseline[::step])) + xmin, xmax = self.x_range + inside_range = (sampled_points[:, 0] >= xmin) & ( + sampled_points[:, 0] <= xmax + ) + estimated_points = sampled_points[inside_range] + n_points = len(estimated_points) + len(fixed_points) + if n_points < self.n_terms: + raise ValueError( + f"fit_to_data has {n_points} points inside the background's " + f"range of {self._format_range()}, fewer than its " + f"{self.n_terms} terms, so the coefficients can't be " + "determined. Please give data that cover more of the range, " + "add fixed points, or make the background with fewer terms." + ) + all_points = np.vstack((estimated_points, fixed_points)) + weights = np.concatenate( + ( + np.ones(len(estimated_points)), + np.full(len(fixed_points), np.sqrt(fixed_point_weight)), + ) + ) + self._fit_coefficients(all_points, weights) + self._last_data = (x, y) + self._estimated_points = estimated_points + self._fixed_points = fixed_points if len(fixed_points) else None + return estimated_points + + def plot_fit(self, x=None, y=None, ax=None, show=True, return_fig=False): + """Plot the background over the data. + + The background curve is drawn from the current coefficients, so + after a refinement it shows the refined background. The plot + shows the last fit only: each ``fit_to_data`` or + ``fit_to_points`` replaces the data and points drawn before. The + data are those of the last ``fit_to_data``, unless ``x`` and + ``y`` are given. The estimated points and the fixed points are + drawn with different markers. After ``fit_to_points``, which has + no data, the curve is drawn over ``x_range`` with the points. + The data are drawn in full, and the curve only at their x values + inside ``x_range``. + + Like srfit's ``FitRecipe.plot_recipe``, the plot uses the + bg-mpl-stylesheets style, and is shown if ``show`` is True and no + ``ax`` is given. + + Parameters + ---------- + x : array_like, optional + The values of the data axis of the pattern to draw. + y : array_like, optional + The measured intensity at each x value. + ax : matplotlib.axes.Axes, optional + The axes to draw on. By default a new figure is made. + show : bool, optional + The flag to show the new figure (default True). It has no + effect when ``ax`` is given, and False suits scripts that save + the figure instead. + return_fig : bool, optional + The flag to return the figure and axes (default False). + + Returns + ------- + fig, ax : tuple + The figure and axes, returned only if ``return_fig`` is True. + + Raises + ------ + ValueError + If the background has not been fitted and no ``x`` and ``y`` + are given, only one of ``x`` and ``y`` is given, or they have + different lengths. + """ + if (x is None) != (y is None): + given_name, missing_name = ("x", "y") if y is None else ("y", "x") + raise ValueError( + f"plot_fit was given {given_name} but not {missing_name}. " + "Please give both x and y, or neither to draw the data of the " + "last fit." + ) + if x is not None: + data = _read_data(x, y, "plot_fit") + elif self._last_data is not None: + data = self._last_data + elif self._fixed_points is not None: + data = None + else: + raise ValueError( + "plot_fit has no data to draw the background on, because the " + "background hasn't been fitted yet. Please call fit_to_data " + "or fit_to_points first, or give x and y." + ) + xmin, xmax = self.x_range + if data is None: + curve_x = np.linspace(xmin, xmax, N_CURVE_POINTS) + else: + data_x = data[0] + curve_x = data_x[(data_x >= xmin) & (data_x <= xmax)] + import matplotlib.pyplot as plt + from bg_mpl_stylesheets.styles import all_styles + + with plt.style.context(all_styles["bg-style"]): + if ax is None: + fig = plt.figure() + current_ax = fig.add_subplot(111) + else: + current_ax = ax + fig = current_ax.figure + if data is not None: + current_ax.plot(data[0], data[1], "-", label="Observed") + current_ax.plot(curve_x, self(curve_x), "-", label="Background") + if self._estimated_points is not None and data is not None: + current_ax.plot( + self._estimated_points[:, 0], + self._estimated_points[:, 1], + "o", + label="Estimated points", + ) + if self._fixed_points is not None: + current_ax.plot( + self._fixed_points[:, 0], + self._fixed_points[:, 1], + "D", + label="Fixed points", + ) + current_ax.legend(frameon=True) + fig.tight_layout() + if show and ax is None: + plt.show() + if return_fig: + return fig, current_ax + + def _scale_x(self, x): + xmin, xmax = self.x_range + scaled_x = 2.0 * (x - xmin) / (xmax - xmin) - 1.0 + return scaled_x + + def _get_coefficient_values(self): + values = [] + for coefficient in self.coefficients: + values.append(coefficient.value) + return values + + def _format_range(self): + xmin, xmax = self.x_range + formatted_range = f"{_format_x(xmin)} to {_format_x(xmax)}" + return formatted_range + + def _read_points(self, points, method_name, argument_name, point_noun): + try: + points = np.asarray(points, dtype=float) + except ValueError: + points = None + if points is None or points.ndim != 2 or points.shape[1] != 2: + raise ValueError( + f"{method_name} was given {argument_name} that aren't all " + f"(x, intensity) pairs. Please give each {point_noun} as two " + "numbers, an x value and its intensity." + ) + xmin, xmax = self.x_range + outside_x = points[(points[:, 0] < xmin) | (points[:, 0] > xmax), 0] + if len(outside_x): + formatted_x = ", ".join(_format_x(value) for value in outside_x) + raise ValueError( + f"{method_name} was given {argument_name} outside the " + f"background's range of {self._format_range()}: " + f"{formatted_x}. Please give {point_noun}s within the range, " + "or make the background with a range that holds them." + ) + return points + + def _fit_coefficients(self, points, weights): + values = chebyshev.chebfit( + self._scale_x(points[:, 0]), + points[:, 1], + self.n_terms - 1, + w=weights, + ) + for coefficient, value in zip(self.coefficients, values): + coefficient.set_value(value) + + +def _read_data(x, y, method_name): + x = np.asarray(x, dtype=float) + y = np.asarray(y, dtype=float) + if len(x) != len(y): + raise ValueError( + f"{method_name} was given {len(x)} x values and {len(y)} " + "intensities. Please give one intensity for each x value." + ) + return x, y + + +def _format_x(value): + # Round off float noise, so np.arange(16, 110, 0.025) ends at 109.975 + formatted_x = str(round(float(value), 6)) + return formatted_x # End of file diff --git a/src/diffpy/cmirietveld/component.py b/src/diffpy/cmirietveld/component.py index a6892fc..aff1d5a 100644 --- a/src/diffpy/cmirietveld/component.py +++ b/src/diffpy/cmirietveld/component.py @@ -114,6 +114,23 @@ def mark_settings_changed(self): self._settings_version += 1 self._flush(other=(self,)) + def get_settings(self): + """Return the settings of the component, for showing to a user. + + A generator's ``print_info`` shows them next to the Parameters. + A component with settings overrides this to list the settings in + use, each under the name of the setter argument that sets it, so + a user sees what to pass to change it. + + Returns + ------- + dict of str to object + The value of each setting in use, by name. It is empty for a + component without settings. + """ + settings = {} + return settings + def install(self, pattern, diffraction): """Set the model up in one generator. diff --git a/src/diffpy/cmirietveld/instruments.py b/src/diffpy/cmirietveld/instruments.py index 2ee8695..632e7ca 100644 --- a/src/diffpy/cmirietveld/instruments.py +++ b/src/diffpy/cmirietveld/instruments.py @@ -365,6 +365,33 @@ def set_polarization(self, polarization_rate): self._polarization_rate = polarization_rate self.mark_settings_changed() + def get_settings(self): + """Return the settings in use, for showing to a user. + + The radiation type and the polarization rate are always listed. + A tube adds ``xray_source`` and ``kalpha2_ratio``, and explicit + wavelengths add ``kalpha1``, ``kalpha2`` and ``kalpha2_ratio``. + A single wavelength adds nothing, since it is the ``wavelength`` + Parameter. + + Returns + ------- + dict of str to object + The value of each setting in use, by the name of the setter + argument that sets it. + """ + settings = {"radiation_type": self._radiation_type} + if self._xray_source is not None: + settings["xray_source"] = self._xray_source + settings["kalpha2_ratio"] = self._kalpha2_ratio + elif self._kalpha_wavelengths is not None: + kalpha1, kalpha2 = self._kalpha_wavelengths + settings["kalpha1"] = kalpha1 + settings["kalpha2"] = kalpha2 + settings["kalpha2_ratio"] = self._kalpha2_ratio + settings["polarization_rate"] = self._polarization_rate + return settings + def install(self, pattern, diffraction): """Set the radiation, wavelengths and polarization of one generator. diff --git a/src/diffpy/cmirietveld/powderdiffractiongenerator.py b/src/diffpy/cmirietveld/powderdiffractiongenerator.py index 994f572..20ae7b1 100644 --- a/src/diffpy/cmirietveld/powderdiffractiongenerator.py +++ b/src/diffpy/cmirietveld/powderdiffractiongenerator.py @@ -22,8 +22,29 @@ __all__ = ["PowderDiffractionGenerator"] +import warnings +from pathlib import Path + +import numpy as np +from pyobjcryst import ObjCrystException +from pyobjcryst.crystal import Crystal, create_crystal_from_cif +from pyobjcryst.powderpattern import PowderPattern + +from diffpy.cmirietveld.instruments import Instrument +from diffpy.cmirietveld.peak_profiles import PeakProfile +from diffpy.cmistructure import structure_to_parameter_set +from diffpy.cmistructure.basestructureparset import BaseStructureParSet +from diffpy.srfit.exceptions import SrFitError from diffpy.srfit.fitbase import ProfileGenerator +PLACEHOLDER_TTH = np.linspace(5.0, 175.0, 1000) +# ObjCryst draws each reflection out to 5 times its full width at 4% of +# its maximum on either side of its center +PROFILE_CUTOFF_INTENSITY = 0.04 +PROFILE_EXTENT_FACTOR = 5.0 +GRID_HEADROOM = 2.0 +MAX_TTH = 180.0 + class PowderDiffractionGenerator(ProfileGenerator): """Calculate the Bragg intensity of one phase of a powder pattern @@ -33,9 +54,10 @@ class PowderDiffractionGenerator(ProfileGenerator): calculation: an intensity source, an instrument, zero or more intensity corrections and a peak profile. The Parameters of each component sit under its name, for example - ``generator.peak_profile.U`` and ``generator.instrument.zero``. With - nothing set, the generator uses a ``PseudoVoigtProfile`` and a - ``ConstantWavelengthInstrument``. + ``generator.peak_profile.U`` and ``generator.instrument.zero``. The + generator has no defaults: the structure, the instrument and the peak + profile are each set before a pattern is calculated, so a script + always shows which models it refines. The generator calculates the Bragg intensity only; a background is added in the FitContribution equation. @@ -51,10 +73,10 @@ class PowderDiffractionGenerator(ProfileGenerator): ---------- phase : ParameterSet or None The structure ParameterSet, or None until a structure is set. - peak_profile : PeakProfile - The peak profile component. - instrument : Instrument - The instrument component. + peak_profile : PeakProfile or None + The peak profile component, or None until one is set. + instrument : Instrument or None + The instrument component, or None until one is set. Managed Parameters ------------------ @@ -65,36 +87,125 @@ class PowderDiffractionGenerator(ProfileGenerator): def __init__(self, name="bragg"): ProfileGenerator.__init__(self, name) self.phase = None - - def set_structure(self, structure, name="phase"): + self._pattern = None + self._diffraction = None + self._last_tth = np.empty(0) + self._grid_top = -np.inf + self._width_ratio = 1.0 + self._width_ratio_key = None + self._installed_settings = {} + self._new_parameter("scale", 1.0) + self.instrument = None + self.peak_profile = None + + def set_structure(self, structure, name=None): """Set the structure used to calculate the pattern. + The structure is a pyobjcryst Crystal, the path to a CIF file, or a + ParameterSet that adapts a Crystal. A Crystal or a path is adapted + to a new ParameterSet, and a path is first read into a new Crystal. + A ParameterSet is used as it is, with its own name. + + Giving the same ParameterSet to several generators shares the + structure between them. Giving them the same path does not: each + call reads the file again, so each generator refines its own + structure. + Parameters ---------- - structure : pyobjcryst.crystal.Crystal - The crystal structure. + structure : Crystal, pathlib.Path, str or ObjCrystCrystalParSet + The pyobjcryst Crystal, the path to a CIF file holding it, or + the diffpy.cmistructure ParameterSet that adapts it. name : str, optional - The name of the ParameterSet that adapts the structure - (default "phase"). + The name of the ParameterSet made for a Crystal or a path + (default "phase"). With a ParameterSet, it must match the + ParameterSet's own name. + + Raises + ------ + FileNotFoundError + If ``structure`` is a path to a file that does not exist. + ValueError + If ``structure`` is a path to a file that isn't a CIF file + holding a crystal structure, a structure or ParameterSet that + ObjCryst can't calculate with, or a ParameterSet given with a + ``name`` other than its own. + + Warns + ----- + UserWarning + If a Parameter of the replaced structure is a variable of a fit + recipe or constrained in one, since the recipe would refine a + Parameter that no longer changes the pattern. """ - raise NotImplementedError + if isinstance(structure, BaseStructureParSet): + if name is not None and name != structure.name: + raise ValueError( + f"set_structure was given the name '{name}' with a " + "ParameterSet, which keeps its own name, " + f"'{structure.name}'. Please leave out the name, or give " + "it when you create the ParameterSet." + ) + crystal = structure.structure + elif isinstance(structure, (str, Path)): + crystal = _read_crystal_from_cif(Path(structure)) + else: + crystal = structure + if not isinstance(crystal, Crystal): + raise ValueError( + f"set_structure was given a {type(structure).__name__}, " + "which the powder diffraction generator can't calculate " + "with. Please give a pyobjcryst Crystal, the path to a CIF " + "file, or a ParameterSet made from a Crystal." + ) + if isinstance(structure, BaseStructureParSet): + phase = structure + else: + phase = structure_to_parameter_set(name or "phase", crystal) + self._replace_phase(phase, "set_structure") def set_structure_from_parset(self, phase): """Set the structure from an existing ParameterSet. - Passing the same ParameterSet to several generators, including - srfit's PDFGenerator, shares the structure between them. + This is the same as ``set_structure`` with a ParameterSet. It is + kept so the generator has the same methods as + diffpy.cmipdf's PDF generators. Parameters ---------- phase : ObjCrystCrystalParSet - The ParameterSet that adapts a pyobjcryst Crystal. + The diffpy.cmistructure ParameterSet that adapts a pyobjcryst + Crystal. + + Raises + ------ + ValueError + If ``phase`` does not adapt a pyobjcryst Crystal. + + Warns + ----- + UserWarning + If a Parameter of the replaced structure is a variable of a fit + recipe or constrained in one. """ - raise NotImplementedError + if not isinstance(phase, BaseStructureParSet) or not isinstance( + phase.structure, Crystal + ): + raise ValueError( + f"set_structure_from_parset was given a " + f"{type(phase).__name__}, which the powder diffraction " + "generator can't calculate with. Please give a ParameterSet " + "made from a pyobjcryst Crystal." + ) + self._replace_phase(phase, "set_structure_from_parset") def set_peak_profile(self, peak_profile): """Set the peak profile component. + The peak profile replaces the one set before, with its Parameters + under ``peak_profile``. It is installed once the generator has a + structure, so it can be set before or after the structure. + Parameters ---------- peak_profile : PeakProfile @@ -104,12 +215,28 @@ def set_peak_profile(self, peak_profile): ------ ValueError If ``peak_profile`` is not a PeakProfile. + + Warns + ----- + UserWarning + If a Parameter of the replaced peak profile is a variable of a + fit recipe or constrained in one, since the recipe would refine + a Parameter that no longer changes the pattern. """ - raise NotImplementedError + self._set_component( + "peak_profile", + peak_profile, + PeakProfile, + "PseudoVoigtProfile or TCHProfile", + ) def set_instrument(self, instrument): """Set the instrument component. + The instrument replaces the one set before, with its Parameters + under ``instrument``. It is installed once the generator has a + structure, so it can be set before or after the structure. + Parameters ---------- instrument : Instrument @@ -119,8 +246,20 @@ def set_instrument(self, instrument): ------ ValueError If ``instrument`` is not an Instrument. + + Warns + ----- + UserWarning + If a Parameter of the replaced instrument is a variable of a + fit recipe or constrained in one, since the recipe would refine + a Parameter that no longer changes the pattern. """ - raise NotImplementedError + self._set_component( + "instrument", + instrument, + Instrument, + "ConstantWavelengthInstrument", + ) def add_intensity_correction(self, correction): """Add an intensity correction component. @@ -152,9 +291,75 @@ def set_intensity_source(self, intensity_source): """ raise NotImplementedError + def print_info(self): + """Print what the generator holds. + + The scale is printed first, then the structure, the instrument + and the peak profile, each under the name its Parameters sit + under, so a user can check a setup before refining it. The + structure shows its space group and lattice parameters, with the + angles in degrees, rather than all its Parameters. Each + component shows its type, then its settings from + ``get_settings`` on one line if it has any, then the current + value of each of its Parameters. Anything that isn't set yet + shows "not set". + """ + lines = [ + f"PowderDiffractionGenerator '{self.name}'", + f" scale = {_format_value(self.scale.value)}", + ] + if self.phase is None: + lines.append(" phase: not set") + else: + crystal = self.phase.structure + lines.append( + f" {self.phase.name}: Crystal '{crystal.GetName()}', " + f"space group {crystal.GetSpaceGroup().GetName()}" + ) + for length_name in ["a", "b", "c"]: + length = self.phase.get(length_name).value + lines.append(f" {length_name} = {_format_value(length)}") + for angle_name in ["alpha", "beta", "gamma"]: + angle = np.degrees(self.phase.get(angle_name).value) + lines.append(f" {angle_name} = {_format_value(angle)}") + components = { + "instrument": self.instrument, + "peak_profile": self.peak_profile, + } + for component_name, component in components.items(): + if component is None: + lines.append(f" {component_name}: not set") + continue + lines.append(f" {component_name}: {type(component).__name__}") + settings = [] + for setting_name, value in component.get_settings().items(): + settings.append(f"{setting_name} = {_format_value(value)}") + if settings: + lines.append(f" settings: {', '.join(settings)}") + for parameter in component.iterate_over_parameters(): + lines.append( + f" {parameter.name} = {_format_value(parameter.value)}" + ) + print("\n".join(lines)) + def __call__(self, tth): """Calculate the Bragg intensity. + Each component whose settings changed since it was last installed + is installed again. Then each component pushes its Parameter + values into pyobjcryst, and the pattern is calculated and + multiplied by ``scale``. + + ObjCryst only lists the reflections up to the top of its grid, so + a reflection just above ``tth`` would leave out its tail inside + it. The pattern is therefore calculated on ``tth`` extended + upwards at its last step, as far as a reflection can reach back + into ``tth`` with the current peak width and zero shift, and only + the points of ``tth`` are returned. A peak width that depends on + hkl is measured on all the reflections listed, again whenever a + Parameter of the peak profile changes. + Reflections below ``tth`` are already drawn by ObjCryst. + Parameters ---------- tth : ndarray @@ -165,8 +370,235 @@ def __call__(self, tth): ------- ndarray The calculated Bragg intensity at each 2theta value. + + Raises + ------ + SrFitError + If no structure, instrument or peak profile is set, or the + instrument has no wavelength. + ValueError + If a component's Parameter value is outside the limits of its + ObjCryst ``RefinablePar``. """ - raise NotImplementedError + self._check_setup() + components = [self.instrument, self.peak_profile] + for component in components: + component._validate() + for component in components: + installed_component, installed_version = ( + self._installed_settings.get(component.name, (None, None)) + ) + if ( + installed_component is not component + or installed_version != component.settings_version + ): + component.install(self._pattern, self._diffraction) + self._installed_settings[component.name] = ( + component, + component.settings_version, + ) + for component in components: + component.update(self._pattern, self._diffraction) + tth = np.asarray(tth, dtype=float) + self._update_grid(tth) + self._pattern.Prepare() + used_width_ratio = self._width_ratio + self._width_ratio = self._get_width_ratio() + if self._width_ratio > used_width_ratio and self._update_grid(tth): + self._pattern.Prepare() + grid_pattern = np.array(self._pattern.GetPowderPatternCalc()) + calculated_pattern = grid_pattern[: len(tth)] + bragg_intensity = self.scale.value * calculated_pattern + return bragg_intensity + + def _validate(self): + self._check_setup() + ProfileGenerator._validate(self) + + def _check_setup(self): + if self.phase is None: + raise SrFitError( + "The generator has no structure. Please set one with " + "set_structure or set_structure_from_parset." + ) + if self.instrument is None: + raise SrFitError( + "The generator has no instrument. Please set one with " + "set_instrument, such as a ConstantWavelengthInstrument." + ) + if self.peak_profile is None: + raise SrFitError( + "The generator has no peak profile. Please set one with " + "set_peak_profile, such as a PseudoVoigtProfile or a " + "TCHProfile." + ) + + def _replace_phase(self, phase, setter_name): + if self.phase is not None: + _warn_if_parameters_used(self.phase, setter_name, "structure") + self.remove_parameter_set(self.phase) + self.phase = phase + self.add_parameter_set(phase) + self._pattern = PowderPattern() + self._set_tth(PLACEHOLDER_TTH) + self._diffraction = self._pattern.AddPowderPatternDiffraction( + phase.structure + ) + self._installed_settings = {} + self._flush(other=(self,)) + + def _set_component(self, component_name, component, base_class, examples): + setter_name = f"set_{component_name}" + kind = component_name.replace("_", " ") + if not isinstance(component, base_class): + given_name = type(component).__name__ + raise ValueError( + f"{setter_name} was given {_get_article(given_name)} " + f"{given_name}, which is not {_get_article(kind)} {kind}. " + f"Please give {_get_article(base_class.__name__)} " + f"{base_class.__name__}, such as {examples}." + ) + old_component = getattr(self, component_name) + if old_component is not None: + _warn_if_parameters_used(old_component, setter_name, kind) + self.remove_parameter_set(old_component) + setattr(self, component_name, component) + self.add_parameter_set(component) + self._flush(other=(self,)) + + def _update_grid(self, tth): + # ObjCryst leaves out the reflections above the top of its grid + needed_grid_top = self._get_needed_grid_top(tth) + grid_changed = ( + not np.array_equal(tth, self._last_tth) + or needed_grid_top > self._grid_top + ) + if grid_changed: + self._set_tth(tth, needed_grid_top) + return grid_changed + + def _get_width_ratio(self): + # The largest ratio of a reflection's width to the width without + # hkl, which depends mostly on the direction of hkl, not its angle + profile_values = [] + for parameter in self.peak_profile.iterate_over_parameters(): + profile_values.append(parameter.value) + width_ratio_key = ( + self.peak_profile, + self.peak_profile.settings_version, + tuple(profile_values), + self._diffraction.GetNbRefl(), + ) + if width_ratio_key == self._width_ratio_key: + return self._width_ratio + reflection_tth = 2 * np.array(self._diffraction.GetTheta()) + miller_h = np.array(self._diffraction.GetH()) + miller_k = np.array(self._diffraction.GetK()) + miller_l = np.array(self._diffraction.GetL()) + profile = self._diffraction.GetProfile() + width_ratio = 1.0 + for index in range(len(reflection_tth)): + center = self._pattern.X2XCorr(reflection_tth[index]) + isotropic_width = profile.GetFullProfileWidth( + PROFILE_CUTOFF_INTENSITY, center, 0, 0, 0 + ) + reflection_width = profile.GetFullProfileWidth( + PROFILE_CUTOFF_INTENSITY, + center, + miller_h[index], + miller_k[index], + miller_l[index], + ) + width_ratio = max(width_ratio, reflection_width / isotropic_width) + self._width_ratio_key = width_ratio_key + return width_ratio + + def _get_needed_grid_top(self, tth): + if len(tth) < 2: + return tth[-1] if len(tth) else -np.inf + tth_max = tth[-1] + step = tth[-1] - tth[-2] + profile = self._diffraction.GetProfile() + reach_center = tth_max + for _ in range(20): + isotropic_width = profile.GetFullProfileWidth( + PROFILE_CUTOFF_INTENSITY, np.radians(reach_center), 0, 0, 0 + ) + width = self._width_ratio * np.degrees(isotropic_width) + new_reach_center = min( + tth_max + 2 * step + PROFILE_EXTENT_FACTOR * width, MAX_TTH + ) + if abs(new_reach_center - reach_center) < 0.1 * step: + break + reach_center = max(reach_center, new_reach_center) + corrected_center = np.degrees( + self._pattern.X2XCorr(np.radians(reach_center)) + ) + needed_grid_top = 2 * reach_center - corrected_center + return needed_grid_top + + def _set_tth(self, tth, needed_grid_top=None): + self._last_tth = np.array(tth, dtype=float) + grid_tth = self._last_tth + if needed_grid_top is not None and len(tth) >= 2: + step = tth[-1] - tth[-2] + margin = GRID_HEADROOM * (needed_grid_top - tth[-1]) + n_extra_points = max(int(np.ceil(margin / step)), 0) + extra_tth = tth[-1] + step * np.arange(1, n_extra_points + 1) + extra_tth = extra_tth[extra_tth < MAX_TTH] + grid_tth = np.concatenate((self._last_tth, extra_tth)) + self._grid_top = grid_tth[-1] if len(grid_tth) else -np.inf + self._pattern.SetPowderPatternX(np.radians(grid_tth)) + self._pattern.SetPowderPatternObs(np.zeros_like(grid_tth)) + + +def _read_crystal_from_cif(cif_path): + # create_crystal_from_cif takes no Path, and raises ObjCrystException + # without the file name for a file with no structure + try: + crystal = create_crystal_from_cif(str(cif_path)) + except ObjCrystException: + raise ValueError( + f"No crystal structure could be read from {cif_path}. " + "Please give a CIF file that holds a crystal structure." + ) from None + return crystal + + +def _warn_if_parameters_used(parameter_set, setter_name, kind): + # A Parameter is observed by its ParameterSet alone, unless a recipe + # variable proxies it, which adds the recipe's observer + used_names = [] + for parameter in parameter_set.iterate_over_parameters(): + if len(parameter._observers) > 1 or parameter.constrained: + if parameter.name not in used_names: + used_names.append(parameter.name) + if not used_names: + return + warnings.warn( + f"{setter_name} replaced {_get_article(kind)} {kind} whose " + f"Parameters are used in a fit recipe: {', '.join(used_names)}. " + "The recipe would still refine them, but they no longer change the " + f"pattern. Please call {setter_name} before adding the {kind}'s " + "Parameters to the recipe.", + UserWarning, + stacklevel=4, + ) + + +def _format_value(value): + if value is None: + formatted_value = "not set" + elif isinstance(value, str): + formatted_value = value + else: + formatted_value = f"{value:g}" + return formatted_value + + +def _get_article(word): + article = "an" if word[0].lower() in "aeiou" else "a" + return article # End of file diff --git a/src/diffpy/cmirietveld/version.py b/src/diffpy/cmirietveld/version.py index 2b4865c..97d61cc 100644 --- a/src/diffpy/cmirietveld/version.py +++ b/src/diffpy/cmirietveld/version.py @@ -4,7 +4,8 @@ # (c) 2026 diffpy.cmirietveld contributors. # All rights reserved. # -# File coded by: Caden Myers, members of the Billinge group, and contributors to the diffpy.cmirietveld project. +# File coded by: Caden Myers, members of the Billinge group, and +# contributors to the diffpy.cmirietveld project. # # See GitHub contributions for a more detailed list of contributors. # https://github.com/diffpy/diffpy.cmirietveld/graphs/contributors # noqa: E501 diff --git a/tests/conftest.py b/tests/conftest.py index 505004a..37defe1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,15 +1,28 @@ import json from pathlib import Path +import matplotlib import numpy as np import pytest +from numpy.polynomial import chebyshev from pyobjcryst.crystal import create_crystal_from_cif from pyobjcryst.powderpattern import PowderPattern -from pyobjcryst.reflectionprofile import ReflectionProfilePseudoVoigt +from pyobjcryst.reflectionprofile import ( + ReflectionProfilePseudoVoigt, + ReflectionProfilePseudoVoigtAnisotropic, +) +from diffpy.cmirietveld.backgrounds import Background, ChebyshevBackground from diffpy.cmirietveld.component import Component +from diffpy.cmirietveld.instruments import ConstantWavelengthInstrument +from diffpy.cmirietveld.peak_profiles import PeakProfile, PseudoVoigtProfile +from diffpy.cmirietveld.powderdiffractiongenerator import ( + PowderDiffractionGenerator, +) +from diffpy.srfit.fitbase import FitContribution, Profile TESTDATA_DIR = Path(__file__).parent / "testdata" +PBSO4_CIF = TESTDATA_DIR / "PbSO4-Wyckoff.cif" PSEUDO_VOIGT_PARAMETER_NAMES = { "U": "U", "V": "V", @@ -20,29 +33,113 @@ "asym1": "Asym1", "asym2": "Asym2", } +PBSO4_X_RANGE = (16.0, 110.0) +PBSO4_BACKGROUND_COEFFICIENTS = [150.0, -20.0, 8.0, -3.0, 1.0, 0.5] +TOP_EDGE_EXTENSION = 20.0 + +matplotlib.use("Agg") class PbSO4Pattern: """A plain pyobjcryst pattern for PbSO4, on a 16-110 degree grid at - 1.5406 angstroms, with its diffraction component.""" + 1.5406 angstroms, with its crystal and diffraction component. + + ObjCryst leaves out reflections above the top of its grid, so the + pattern is calculated on the grid extended 20 degrees upwards at the + same step, wider than any peak in these tests, and only the points of + ``tth`` are returned. This is the pattern with every reflection that + reaches the grid, which the generator also calculates. + """ def __init__(self): - crystal = create_crystal_from_cif( - str(TESTDATA_DIR / "PbSO4-Wyckoff.cif") - ) - tth = np.arange(16.0, 110.0, 0.025) + self.crystal = create_crystal_from_cif(str(PBSO4_CIF)) self.pattern = PowderPattern() self.pattern.SetWavelength(1.5406) - self.pattern.SetPowderPatternX(np.radians(tth)) - self.pattern.SetPowderPatternObs(np.zeros_like(tth)) - self.diffraction = self.pattern.AddPowderPatternDiffraction(crystal) + self.set_tth(np.arange(16.0, 110.0, 0.025)) + self.diffraction = self.pattern.AddPowderPatternDiffraction( + self.crystal + ) + + def set_tth(self, tth): + self.tth = tth + step = tth[-1] - tth[-2] + n_extra_points = int(round(TOP_EDGE_EXTENSION / step)) + extra_tth = tth[-1] + step * np.arange(1, n_extra_points + 1) + extended_tth = np.concatenate((tth, extra_tth)) + self.pattern.SetPowderPatternX(np.radians(extended_tth)) + self.pattern.SetPowderPatternObs(np.zeros_like(extended_tth)) def calculate(self): self.pattern.Prepare() - calculated_pattern = np.array(self.pattern.GetPowderPatternCalc()) + extended_pattern = np.array(self.pattern.GetPowderPatternCalc()) + calculated_pattern = extended_pattern[: len(self.tth)] return calculated_pattern +class SyntheticPattern: + """A pattern made of a known Chebyshev background and sharp peaks. + + The background is ``PBSO4_BACKGROUND_COEFFICIENTS`` on + ``PBSO4_X_RANGE``, and 21 Gaussian peaks of 0.08 degrees standard + deviation stand between 20 and 106 degrees, so a background + estimated from the pattern can be compared with the known one. + """ + + def __init__(self, x_min=16.0, x_max=110.0): + self.x = np.arange(x_min, x_max, 0.025) + xmin, xmax = PBSO4_X_RANGE + scaled_x = 2.0 * (self.x - xmin) / (xmax - xmin) - 1.0 + self.background = chebyshev.chebval( + scaled_x, PBSO4_BACKGROUND_COEFFICIENTS + ) + peaks = np.zeros_like(self.x) + for index, center in enumerate(np.arange(20.0, 108.0, 4.3)): + height = 2000.0 / (1 + index % 3) + peaks += height * np.exp(-0.5 * ((self.x - center) / 0.08) ** 2) + self.y = self.background + peaks + + +class ExternalPeakProfile(PeakProfile): + """A peak profile that stands for one written in another package. + + The package doesn't know this class, so a generator that calculates + with it shows that a new peak profile needs no change to the + generator. It wraps pyobjcryst's anisotropic pseudo-Voigt, which the + package doesn't have yet, and maps only two of its Parameters. + """ + + def __init__(self): + PeakProfile.__init__(self) + self.create_parameters_from( + ReflectionProfilePseudoVoigtAnisotropic(), + {"W": "W", "G_HH": "G_HH"}, + ) + + def install(self, pattern, diffraction): + diffraction.SetProfile(ReflectionProfilePseudoVoigtAnisotropic()) + + def update(self, pattern, diffraction): + self.push_parameters(diffraction.GetProfile()) + + +class ExternalBackground(Background): + """A background that stands for one written in another package. + + The package doesn't know this class, so a FitContribution that + calculates with it shows that a new background needs only a subclass + of ``Background``. It is a flat background at the height of its one + Parameter, ``level``. + """ + + def __init__(self, name): + Background.__init__(self, name) + self._new_parameter("level", 0.0) + + def __call__(self, x): + background = np.full_like(x, self.level.value) + return background + + class NotificationCounter: """An observer that counts the notifications it receives.""" @@ -71,6 +168,68 @@ def other_pbso4_pattern(): return other_pbso4_pattern +@pytest.fixture +def pbso4_generator(): + pbso4_generator = PowderDiffractionGenerator() + pbso4_generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + pbso4_generator.set_instrument(instrument) + pbso4_generator.set_peak_profile(PseudoVoigtProfile()) + return pbso4_generator + + +@pytest.fixture +def other_pbso4_generator(): + other_pbso4_generator = PowderDiffractionGenerator() + other_pbso4_generator.set_structure( + create_crystal_from_cif(str(PBSO4_CIF)) + ) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + other_pbso4_generator.set_instrument(instrument) + other_pbso4_generator.set_peak_profile(PseudoVoigtProfile()) + return other_pbso4_generator + + +@pytest.fixture +def pbso4_contribution(pbso4_generator, pbso4_pattern): + profile = Profile() + profile.set_observed_profile( + pbso4_pattern.tth, np.zeros_like(pbso4_pattern.tth) + ) + pbso4_contribution = FitContribution("xray") + pbso4_contribution.set_profile(profile) + pbso4_contribution.add_profile_generator(pbso4_generator) + pbso4_contribution.set_equation("bragg") + return pbso4_contribution + + +@pytest.fixture +def synthetic_pattern(): + synthetic_pattern = SyntheticPattern() + return synthetic_pattern + + +@pytest.fixture +def pbso4_background(): + pbso4_background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + for coefficient, value in zip( + pbso4_background.coefficients, PBSO4_BACKGROUND_COEFFICIENTS + ): + coefficient.set_value(value) + return pbso4_background + + +@pytest.fixture +def pbso4_background_points(pbso4_background): + x_points = np.linspace(16.0, 110.0, 15) + pbso4_background_points = np.column_stack( + (x_points, pbso4_background(x_points)) + ) + return pbso4_background_points + + @pytest.fixture def pseudo_voigt_component(): component = Component("peak_profile") diff --git a/tests/test_backgrounds.py b/tests/test_backgrounds.py new file mode 100644 index 0000000..b3bd327 --- /dev/null +++ b/tests/test_backgrounds.py @@ -0,0 +1,876 @@ +import re + +import matplotlib.pyplot as plt +import numpy as np +import pybaselines.whittaker +import pytest +from conftest import ( + PBSO4_BACKGROUND_COEFFICIENTS, + PBSO4_X_RANGE, + ExternalBackground, + SyntheticPattern, +) +from numpy.polynomial import chebyshev + +from diffpy.cmirietveld.backgrounds import ChebyshevBackground +from diffpy.srfit.fitbase import FitContribution, FitRecipe, Profile + + +@pytest.mark.parametrize( + "background_kwargs, expected_names", + [ + # Test the coefficients a new Chebyshev background has + # C1: A user makes a background with the default number of terms. + # Expected: It has six coefficients, b0 to b5, in order, all 0. + ({}, ["b0", "b1", "b2", "b3", "b4", "b5"]), + # C2: A user makes a background with three terms. + # Expected: It has three coefficients, b0 to b2, in order, all 0. + ({"n_terms": 3}, ["b0", "b1", "b2"]), + ], +) +def test_coefficients(background_kwargs, expected_names): + background = ChebyshevBackground( + "background", x_range=PBSO4_X_RANGE, **background_kwargs + ) + actual_names = [] + actual_values = [] + for coefficient in background.coefficients: + actual_names.append(coefficient.name) + actual_values.append(coefficient.value) + expected_values = [0.0] * len(expected_names) + assert actual_names == expected_names + assert actual_values == expected_values + + +def test_coefficients_are_background_parameters(): + # Test that the coefficients are the background's own Parameters + # Expected: Each coefficient is the Parameter of the same name on the + # background, so setting one changes the background. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + actual_coefficients = background.coefficients + expected_coefficients = [background.get(f"b{i}") for i in range(6)] + assert actual_coefficients == expected_coefficients + + +@pytest.mark.parametrize( + "background_kwargs, expected_msg", + [ + # Test that a background that can't be made raises an error saying + # why and what to do + # C1: A user gives a range whose start isn't below its end. + # 1. The range is the wrong way round, 110 to 16 degrees. + # 2. The range starts and ends at 16 degrees. + # Expected: A ValueError names the range, and asks for it as + # (xmin, xmax) with xmin below xmax. + ( + {"x_range": (110.0, 16.0)}, + "ChebyshevBackground was given an x_range of (110.0, 16.0), " + "whose start isn't below its end. Please give the range as " + "(xmin, xmax), with xmin below xmax.", + ), + ( + {"x_range": (16.0, 16.0)}, + "ChebyshevBackground was given an x_range of (16.0, 16.0), " + "whose start isn't below its end. Please give the range as " + "(xmin, xmax), with xmin below xmax.", + ), + # C2: A user asks for a background with no terms. + # Expected: A ValueError asks for at least one term. + ( + {"x_range": PBSO4_X_RANGE, "n_terms": 0}, + "ChebyshevBackground was given n_terms of 0, but a background " + "needs at least 1 term. Please give n_terms of 1 or more.", + ), + ], +) +def test_init_with_bad_arguments(background_kwargs, expected_msg): + with pytest.raises(ValueError, match=re.escape(expected_msg)): + ChebyshevBackground("background", **background_kwargs) + + +@pytest.mark.parametrize( + "tth", + [ + # Test that the background is the Chebyshev polynomial on the + # background's own range, whatever range it is calculated on + # C1: A user calculates the background over its whole range. + # Expected: It equals numpy's Chebyshev polynomial, with 16 to 110 + # degrees mapped onto -1 to 1. + np.arange(16.0, 110.0, 0.025), + # C2: A user narrows the fit to 20 to 60 degrees. + # Expected: The background still maps 16 to 110 degrees onto -1 to + # 1, so at each angle it is the same as over the whole range. + np.arange(20.0, 60.0, 0.025), + # C3: A user's data start and end exactly at the background's + # range, 16 and 110 degrees. + # Expected: The background is calculated at both ends, which map + # onto -1 and 1. + np.array([16.0, 110.0]), + ], +) +def test_call(pbso4_background, tth): + actual_background = pbso4_background(tth) + xmin, xmax = PBSO4_X_RANGE + scaled_tth = 2.0 * (tth - xmin) / (xmax - xmin) - 1.0 + expected_background = chebyshev.chebval( + scaled_tth, PBSO4_BACKGROUND_COEFFICIENTS + ) + assert np.allclose(actual_background, expected_background) + + +@pytest.mark.parametrize( + "tth, tth_span", + [ + # Test that calculating the background outside its range raises an + # error saying why and what to do, since a Chebyshev polynomial + # grows quickly outside its range and would bend the fit + # C1: A user's data reach past one end of the background's range. + # 1. The data start at 10 degrees, below 16 degrees. + # 2. The data end at 120 degrees, above 110 degrees. + # Expected: A ValueError names the data's span and the range, and + # asks for a calculation range within it or a wider background. + (np.linspace(10.0, 60.0, 2001), "10.0 to 60.0"), + (np.linspace(60.0, 120.0, 2401), "60.0 to 120.0"), + # C2: A user's data reach past both ends, from 10 to 120 degrees. + # Expected: A ValueError names the data's span and the range. + (np.linspace(10.0, 120.0, 4401), "10.0 to 120.0"), + ], +) +def test_call_outside_range(pbso4_background, tth, tth_span): + expected_msg = ( + f"The background was calculated on x values from {tth_span}, " + "outside its range of 16.0 to 110.0. Please set the profile's " + "calculation range within the background's range, or make the " + "background with a range that holds the data." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + pbso4_background(tth) + + +@pytest.mark.parametrize( + "points", + [ + # Test fitting a straight background to points a user gives, which + # rises from 90 at 16 degrees to 110 at 110 degrees, so it is + # 100 + 10 T1 on the background's range + # C1: A user types three points into their script as a list of + # tuples. + # Expected: The coefficients are 100 and 10. + [(16.0, 90.0), (63.0, 100.0), (110.0, 110.0)], + # C2: A user loads four points from a two-column file. + # Expected: The coefficients are 100 and 10. + np.array([[16.0, 90.0], [39.5, 95.0], [86.5, 105.0], [110.0, 110.0]]), + # C3: A user gives exactly two points, one for each term. + # Expected: The coefficients are 100 and 10. + [(16.0, 90.0), (110.0, 110.0)], + ], +) +def test_fit_to_points(points): + background = ChebyshevBackground( + "background", x_range=PBSO4_X_RANGE, n_terms=2 + ) + background.fit_to_points(points) + actual_coefficients = [] + for coefficient in background.coefficients: + actual_coefficients.append(coefficient.value) + expected_coefficients = [100.0, 10.0] + assert np.allclose(actual_coefficients, expected_coefficients) + + +def test_fit_to_points_on_known_background(pbso4_background_points): + # Test that fitting to points on a known six-term background recovers + # it + # Expected: After a user picks 15 points on the known background, the + # coefficients are the known background's. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_points(pbso4_background_points) + actual_coefficients = [] + for coefficient in background.coefficients: + actual_coefficients.append(coefficient.value) + expected_coefficients = PBSO4_BACKGROUND_COEFFICIENTS + assert np.allclose(actual_coefficients, expected_coefficients) + + +def test_fit_to_points_as_recipe_starting_values(pbso4_background_points): + # Test that fitting to points sets the starting values of a recipe + # whose variables are the coefficients + # Expected: After a user adds the coefficients to a recipe and then + # fits the background to points, the recipe's values are the fitted + # coefficients. + # Add the coefficients of a new background to a recipe + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + recipe = FitRecipe() + for coefficient in background.coefficients: + recipe.add_variable(coefficient) + # Fit the background to 15 points on the known background + background.fit_to_points(pbso4_background_points) + actual_values = recipe.get_values() + expected_values = PBSO4_BACKGROUND_COEFFICIENTS + assert np.allclose(actual_values, expected_values) + + +@pytest.mark.parametrize( + "points, expected_msg", + [ + # Test that points the background can't be fitted to raise an error + # saying why and what to do + # C1: A user gives points that aren't (x, intensity) pairs. + # 1. Every point has three numbers. + # 2. One point among pairs has three numbers. + # 3. Only the x values are given, as a flat list. + # Expected: A ValueError asks for each point as two numbers. + ( + [ + (20.0, 150.0, 1.0), + (40.0, 140.0, 1.0), + (60.0, 130.0, 1.0), + (80.0, 120.0, 1.0), + (90.0, 115.0, 1.0), + (100.0, 110.0, 1.0), + ], + "fit_to_points was given points that aren't all (x, intensity) " + "pairs. Please give each point as two numbers, an x value and " + "its intensity.", + ), + ( + [ + (20.0, 150.0), + (40.0, 140.0), + (60.0, 130.0, 1.0), + (80.0, 120.0), + (90.0, 115.0), + (100.0, 110.0), + ], + "fit_to_points was given points that aren't all (x, intensity) " + "pairs. Please give each point as two numbers, an x value and " + "its intensity.", + ), + ( + [20.0, 40.0, 60.0, 80.0, 90.0, 100.0], + "fit_to_points was given points that aren't all (x, intensity) " + "pairs. Please give each point as two numbers, an x value and " + "its intensity.", + ), + # C2: A user picks only four points for a background of six terms. + # Expected: A ValueError says the coefficients can't be found, and + # asks for more points or fewer terms. + ( + [(20.0, 150.0), (40.0, 140.0), (60.0, 130.0), (80.0, 120.0)], + "fit_to_points was given 4 points, fewer than the background's " + "6 terms, so the coefficients can't be determined. Please give " + "at least 6 points, or make the background with fewer terms.", + ), + # C3: A user picks points below and above the background's range. + # 1. A point at 12 degrees, below 16 degrees. + # 2. Points at 12 and 115 degrees, below 16 and above 110 degrees. + # Expected: A ValueError names the points outside the range, and + # asks for points within it or a wider range. + ( + [ + (12.0, 150.0), + (30.0, 145.0), + (45.0, 140.0), + (60.0, 135.0), + (75.0, 130.0), + (90.0, 125.0), + ], + "fit_to_points was given points outside the background's range " + "of 16.0 to 110.0: 12.0. Please give points within the range, " + "or make the background with a range that holds them.", + ), + ( + [ + (12.0, 150.0), + (30.0, 145.0), + (45.0, 140.0), + (60.0, 135.0), + (75.0, 130.0), + (115.0, 125.0), + ], + "fit_to_points was given points outside the background's range " + "of 16.0 to 110.0: 12.0, 115.0. Please give points within the " + "range, or make the background with a range that holds them.", + ), + ], +) +def test_fit_to_points_with_bad_points(points, expected_msg): + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + background.fit_to_points(points) + + +def test_fit_to_data(synthetic_pattern): + # Test the automatic background on a pattern whose background is known + # Expected: After a user fits the background to a pattern of sharp + # peaks on a known background, the background is within 1% of the + # known one everywhere. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(synthetic_pattern.x, synthetic_pattern.y) + actual_background = background(synthetic_pattern.x) + expected_background = synthetic_pattern.background + assert np.allclose(actual_background, expected_background, rtol=0.01) + + +@pytest.mark.parametrize( + "fit_to_data_kwargs, expected_log_lambda", + [ + # Test that the automatic background estimates the same points as + # GSAS-II's automatic background: pybaselines' arpls with 10 + # iterations, sampled at every len(y) // 100-th point + # C1: A user leaves the smoothness at its default. + # Expected: The points are GSAS-II's, with its default log10 of + # lambda for 3760 points, 5.75. + ({}, 5.75), + # C2: A user makes the baseline stiffer with a smoothness of 7. + # Expected: The points are GSAS-II's with a log10 of lambda of 7. + ({"smoothness": 7.0}, 7.0), + ], +) +def test_fit_to_data_points_match_gsasii( + synthetic_pattern, fit_to_data_kwargs, expected_log_lambda +): + # Fit the background to the pattern + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + actual_points = background.fit_to_data( + synthetic_pattern.x, synthetic_pattern.y, **fit_to_data_kwargs + ) + # Estimate the points as GSAS-II's autoBkgCalc and calc_autobkg do + baseline = pybaselines.whittaker.arpls( + synthetic_pattern.y, lam=int(10**expected_log_lambda), max_iter=10 + )[0] + step = len(synthetic_pattern.y) // 100 + expected_points = np.column_stack( + (synthetic_pattern.x[::step], baseline[::step]) + ) + assert np.allclose(actual_points, expected_points) + + +def test_fit_to_data_wider_than_range(): + # Test the automatic background on data that extend past the + # background's range, as a measured file usually does + # Expected: The baseline is estimated from all the data from 10 to + # 120 degrees, and only its points between 16 and 110 degrees are + # fitted and returned. + wide_pattern = SyntheticPattern(x_min=10.0, x_max=120.0) + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + actual_points = background.fit_to_data(wide_pattern.x, wide_pattern.y) + # Estimate GSAS-II's points from all 4400 points of data, with its + # default log10 of lambda for them, 5.95, then keep those inside the + # range + baseline = pybaselines.whittaker.arpls( + wide_pattern.y, lam=int(10**5.95), max_iter=10 + )[0] + step = len(wide_pattern.y) // 100 + all_points = np.column_stack((wide_pattern.x[::step], baseline[::step])) + xmin, xmax = PBSO4_X_RANGE + inside_range = (all_points[:, 0] >= xmin) & (all_points[:, 0] <= xmax) + expected_points = all_points[inside_range] + assert np.allclose(actual_points, expected_points) + + +@pytest.mark.parametrize( + "fixed_point_weight", + [ + # Test that a fixed point's weight is the number of estimated + # points it counts as + # C1: A user adds a fixed point at 63 degrees with the default + # weight of 1. + # Expected: The coefficients are those fitted to the estimated + # points with the fixed point added once. + 1, + # C2: A user adds the same fixed point with a weight of 3. + # Expected: The coefficients are those fitted to the estimated + # points with the fixed point added three times. + 3, + ], +) +def test_fit_to_data_with_fixed_points(synthetic_pattern, fixed_point_weight): + fixed_points = [(63.0, 200.0)] + # Fit the background to the pattern with the fixed point + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + estimated_points = background.fit_to_data( + synthetic_pattern.x, + synthetic_pattern.y, + fixed_points=fixed_points, + fixed_point_weight=fixed_point_weight, + ) + actual_coefficients = [] + for coefficient in background.coefficients: + actual_coefficients.append(coefficient.value) + # Fit a second background to the estimated points plus copies of the + # fixed point + repeated_fixed_points = fixed_points * fixed_point_weight + all_points = np.vstack((estimated_points, repeated_fixed_points)) + expected_background = ChebyshevBackground( + "background", x_range=PBSO4_X_RANGE + ) + expected_background.fit_to_points(all_points) + expected_coefficients = [] + for coefficient in expected_background.coefficients: + expected_coefficients.append(coefficient.value) + assert np.allclose(actual_coefficients, expected_coefficients) + + +def test_fit_to_data_with_heavy_fixed_point(synthetic_pattern): + # Test that a heavily weighted fixed point overrides the estimate + # Expected: After a user puts a fixed point at 200 counts at 63 + # degrees, where the estimate is about 143, with a weight of 1000, + # the background at 63 degrees is within 1% of 200. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data( + synthetic_pattern.x, + synthetic_pattern.y, + fixed_points=[(63.0, 200.0)], + fixed_point_weight=1000, + ) + actual_background = background(np.array([63.0])) + expected_background = np.array([200.0]) + assert np.allclose(actual_background, expected_background, rtol=0.01) + + +@pytest.mark.parametrize( + "fit_to_data_kwargs, expected_msg", + [ + # Test that fixed points the automatic background can't use raise + # an error saying why and what to do + # C1: A user gives a weight that isn't above 0. + # 1. A weight of 0. + # 2. A weight of -2. + # 3. A weight of 0, with no fixed points. + # Expected: A ValueError asks for a weight above 0. + ( + {"fixed_points": [(63.0, 200.0)], "fixed_point_weight": 0.0}, + "fit_to_data was given a fixed_point_weight of 0.0, which is " + "not above 0. Please give a weight above 0, such as 1 to count " + "each fixed point as one estimated point.", + ), + ( + {"fixed_points": [(63.0, 200.0)], "fixed_point_weight": -2.0}, + "fit_to_data was given a fixed_point_weight of -2.0, which is " + "not above 0. Please give a weight above 0, such as 1 to count " + "each fixed point as one estimated point.", + ), + ( + {"fixed_point_weight": 0.0}, + "fit_to_data was given a fixed_point_weight of 0.0, which is " + "not above 0. Please give a weight above 0, such as 1 to count " + "each fixed point as one estimated point.", + ), + # C2: A user gives a fixed point that isn't an (x, intensity) pair. + # Expected: A ValueError names fixed_points, so it isn't mistaken + # for x or y, and asks for each point as two numbers. + ( + {"fixed_points": [(63.0, 200.0, 1.0)]}, + "fit_to_data was given fixed_points that aren't all " + "(x, intensity) pairs. Please give each fixed point as two " + "numbers, an x value and its intensity.", + ), + # C3: A user gives a fixed point at 115 degrees, above the + # background's range. + # Expected: A ValueError names fixed_points and the point, and asks + # for fixed points within the range or a wider range. + ( + {"fixed_points": [(63.0, 200.0), (115.0, 150.0)]}, + "fit_to_data was given fixed_points outside the background's " + "range of 16.0 to 110.0: 115.0. Please give fixed points within " + "the range, or make the background with a range that holds " + "them.", + ), + ], +) +def test_fit_to_data_with_bad_fixed_points( + synthetic_pattern, fit_to_data_kwargs, expected_msg +): + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + background.fit_to_data( + synthetic_pattern.x, synthetic_pattern.y, **fit_to_data_kwargs + ) + + +@pytest.mark.parametrize( + "x, y, expected_msg", + [ + # Test that data the automatic background can't use raise an error + # saying why and what to do + # C1: A user gives one more x value than intensities. + # Expected: A ValueError gives both counts, and asks for one + # intensity for each x value. + ( + np.linspace(16.0, 110.0, 3761), + np.full(3760, 100.0), + "fit_to_data was given 3761 x values and 3760 intensities. " + "Please give one intensity for each x value.", + ), + # C2: A user's data run from 109.5 to 130 degrees, so only three + # of the estimated points, at 109.5, 109.7 and 109.9 degrees, are + # inside the background's range. + # Expected: A ValueError says the coefficients can't be found, and + # asks for data covering more of the range, fixed points, or fewer + # terms. + ( + np.linspace(109.5, 130.0, 821), + np.full(821, 100.0), + "fit_to_data has 3 points inside the background's range of " + "16.0 to 110.0, fewer than its 6 terms, so the coefficients " + "can't be determined. Please give data that cover more of the " + "range, add fixed points, or make the background with fewer " + "terms.", + ), + ], +) +def test_fit_to_data_with_bad_data(x, y, expected_msg): + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + background.fit_to_data(x, y) + + +def test_plot_fit_after_fit_to_data(synthetic_pattern): + # Test the plot a user checks after the automatic background + # Expected: The plot shows the data, the background curve on the data's + # x, the estimated points and the fixed points, each as given. + # Fit the background with a fixed point and plot it + fixed_points = np.array([(63.0, 150.0)]) + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + estimated_points = background.fit_to_data( + synthetic_pattern.x, synthetic_pattern.y, fixed_points=fixed_points + ) + fig, ax = background.plot_fit(show=False, return_fig=True) + # Collect what was drawn, by label + actual_lines = {} + for line in ax.lines: + actual_lines[line.get_label()] = line.get_xydata() + expected_lines = { + "Observed": np.column_stack( + (synthetic_pattern.x, synthetic_pattern.y) + ), + "Background": np.column_stack( + (synthetic_pattern.x, background(synthetic_pattern.x)) + ), + "Estimated points": estimated_points, + "Fixed points": fixed_points, + } + assert list(actual_lines) == list(expected_lines) + for label, expected_xydata in expected_lines.items(): + assert np.allclose(actual_lines[label], expected_xydata) + plt.close(fig) + + +def test_plot_fit_without_fixed_points(synthetic_pattern): + # Test the plot after the automatic background with no fixed points + # Expected: The plot shows the data, the background curve and the + # estimated points, and no fixed points. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(synthetic_pattern.x, synthetic_pattern.y) + fig, ax = background.plot_fit(show=False, return_fig=True) + actual_labels = [line.get_label() for line in ax.lines] + expected_labels = ["Observed", "Background", "Estimated points"] + assert actual_labels == expected_labels + plt.close(fig) + + +def test_plot_fit_after_refinement(synthetic_pattern): + # Test that the plot shows the current coefficients, as after a + # refinement + # Expected: After a refinement changes b0 to 160, the background curve + # equals the background with b0 = 160, on the data of the last fit. + # Fit the background, then change b0 as a refinement would + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(synthetic_pattern.x, synthetic_pattern.y) + background.b0.set_value(160.0) + fig, ax = background.plot_fit(show=False, return_fig=True) + actual_curve = ax.lines[1].get_xydata() + expected_curve = np.column_stack( + (synthetic_pattern.x, background(synthetic_pattern.x)) + ) + assert np.allclose(actual_curve, expected_curve) + plt.close(fig) + + +def test_plot_fit_after_fit_to_points(pbso4_background_points): + # Test the plot after fitting to the user's points only, which gives + # no data + # Expected: The plot shows the background curve from 16 to 110 + # degrees and the user's points, and no data. + # Fit a new background to 15 points on the known background + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_points(pbso4_background_points) + fig, ax = background.plot_fit(show=False, return_fig=True) + # Check the labels, the curve's ends and values, and the points + actual_labels = [line.get_label() for line in ax.lines] + expected_labels = ["Background", "Fixed points"] + curve_x, curve_y = ax.lines[0].get_data() + actual_curve_range = (curve_x[0], curve_x[-1]) + expected_curve_range = PBSO4_X_RANGE + actual_points = ax.lines[1].get_xydata() + expected_points = pbso4_background_points + assert actual_labels == expected_labels + assert actual_curve_range == expected_curve_range + assert np.allclose(curve_y, background(curve_x)) + assert np.allclose(actual_points, expected_points) + plt.close(fig) + + +def test_plot_fit_after_fit_to_points_with_data( + pbso4_background_points, synthetic_pattern +): + # Test the plot after fitting to the user's points, with data given + # Expected: The plot shows the given data under the background curve + # and the user's points. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_points(pbso4_background_points) + fig, ax = background.plot_fit( + synthetic_pattern.x, synthetic_pattern.y, show=False, return_fig=True + ) + actual_labels = [line.get_label() for line in ax.lines] + expected_labels = ["Observed", "Background", "Fixed points"] + actual_data = ax.lines[0].get_xydata() + expected_data = np.column_stack((synthetic_pattern.x, synthetic_pattern.y)) + assert actual_labels == expected_labels + assert np.allclose(actual_data, expected_data) + plt.close(fig) + + +def test_plot_fit_after_fit_to_data_then_fit_to_points( + pbso4_background_points, synthetic_pattern +): + # Test that the plot shows only the last fit, after a user edits the + # estimated points and fits to them + # Expected: The plot shows the background curve over 16 to 110 + # degrees and the points just given, and none of the data, estimated + # points or fixed points of the earlier fit_to_data. + # Fit the background to the pattern with a fixed point + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data( + synthetic_pattern.x, synthetic_pattern.y, fixed_points=[(63.0, 150.0)] + ) + # Fit it again to 15 points on the known background, and plot it + background.fit_to_points(pbso4_background_points) + fig, ax = background.plot_fit(show=False, return_fig=True) + actual_labels = [line.get_label() for line in ax.lines] + expected_labels = ["Background", "Fixed points"] + actual_points = ax.lines[1].get_xydata() + expected_points = pbso4_background_points + assert actual_labels == expected_labels + assert np.allclose(actual_points, expected_points) + plt.close(fig) + + +def test_plot_fit_after_second_fit_to_data(synthetic_pattern): + # Test that the plot shows only the last fit, after a user refits the + # automatic background on other data without fixed points + # Expected: The plot shows the second data and their estimated points, + # and no fixed points from the first fit. + # Fit the background to the pattern with a fixed point + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data( + synthetic_pattern.x, synthetic_pattern.y, fixed_points=[(63.0, 150.0)] + ) + # Fit it again to 20 to 100 degrees of the pattern, and plot it + narrow_pattern = SyntheticPattern(x_min=20.0, x_max=100.0) + estimated_points = background.fit_to_data( + narrow_pattern.x, narrow_pattern.y + ) + fig, ax = background.plot_fit(show=False, return_fig=True) + actual_lines = {} + for line in ax.lines: + actual_lines[line.get_label()] = line.get_xydata() + expected_lines = { + "Observed": np.column_stack((narrow_pattern.x, narrow_pattern.y)), + "Background": np.column_stack( + (narrow_pattern.x, background(narrow_pattern.x)) + ), + "Estimated points": estimated_points, + } + assert list(actual_lines) == list(expected_lines) + for label, expected_xydata in expected_lines.items(): + assert np.allclose(actual_lines[label], expected_xydata) + plt.close(fig) + + +def test_plot_fit_with_data_wider_than_range(): + # Test the plot after the automatic background on data that extend + # past the background's range + # Expected: The data are drawn from 10 to 120 degrees, and the + # background curve only at the data's x values from 16 to 110 + # degrees, where it is calculated. + wide_pattern = SyntheticPattern(x_min=10.0, x_max=120.0) + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(wide_pattern.x, wide_pattern.y) + fig, ax = background.plot_fit(show=False, return_fig=True) + actual_data = ax.lines[0].get_xydata() + expected_data = np.column_stack((wide_pattern.x, wide_pattern.y)) + actual_curve = ax.lines[1].get_xydata() + xmin, xmax = PBSO4_X_RANGE + inside_range = (wide_pattern.x >= xmin) & (wide_pattern.x <= xmax) + curve_x = wide_pattern.x[inside_range] + expected_curve = np.column_stack((curve_x, background(curve_x))) + assert np.allclose(actual_data, expected_data) + assert np.allclose(actual_curve, expected_curve) + plt.close(fig) + + +def test_plot_fit_on_given_axes(synthetic_pattern): + # Test drawing the plot on axes a user made, for example next to a + # plot of the refinement + # Expected: The background is drawn on the given axes, which are + # returned. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(synthetic_pattern.x, synthetic_pattern.y) + fig, given_ax = plt.subplots() + actual_fig, actual_ax = background.plot_fit(ax=given_ax, return_fig=True) + actual_labels = [line.get_label() for line in given_ax.lines] + expected_labels = ["Observed", "Background", "Estimated points"] + assert actual_ax == given_ax + assert actual_fig == fig + assert actual_labels == expected_labels + plt.close(fig) + + +def test_plot_fit_without_return_fig(synthetic_pattern): + # Test drawing the plot on given axes without asking for the figure + # Expected: Nothing is returned, and the background is drawn on the + # given axes. + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + background.fit_to_data(synthetic_pattern.x, synthetic_pattern.y) + fig, given_ax = plt.subplots() + actual_return = background.plot_fit(ax=given_ax) + expected_return = None + actual_labels = [line.get_label() for line in given_ax.lines] + expected_labels = ["Observed", "Background", "Estimated points"] + assert actual_return == expected_return + assert actual_labels == expected_labels + plt.close(fig) + + +@pytest.mark.parametrize( + "plot_fit_kwargs, expected_msg", + [ + # Test that a plot without the data to draw raises an error saying + # why and what to do + # C1: A user plots a background that hasn't been fitted, with no + # data. + # Expected: A ValueError says there is nothing to draw on, and asks + # the user to fit the background first or give data. + ( + {}, + "plot_fit has no data to draw the background on, because the " + "background hasn't been fitted yet. Please call fit_to_data or " + "fit_to_points first, or give x and y.", + ), + # C2: A user gives only one of x and y. + # 1. Only x is given. + # 2. Only y is given. + # Expected: A ValueError names the one given, and asks for both or + # neither. + ( + {"x": np.linspace(16.0, 110.0, 3761)}, + "plot_fit was given x but not y. Please give both x and y, or " + "neither to draw the data of the last fit.", + ), + ( + {"y": np.full(3761, 100.0)}, + "plot_fit was given y but not x. Please give both x and y, or " + "neither to draw the data of the last fit.", + ), + # C3: A user gives one more x value than intensities. + # Expected: A ValueError gives both counts, and asks for one + # intensity for each x value. + ( + {"x": np.linspace(16.0, 110.0, 3761), "y": np.full(3760, 100.0)}, + "plot_fit was given 3761 x values and 3760 intensities. Please " + "give one intensity for each x value.", + ), + ], +) +def test_plot_fit_with_bad_data(plot_fit_kwargs, expected_msg): + background = ChebyshevBackground("background", x_range=PBSO4_X_RANGE) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + background.plot_fit(**plot_fit_kwargs) + + +def test_register_calculator(pbso4_background, pbso4_contribution): + # Test that registering the background puts its coefficients in the + # FitContribution, so a recipe can refine them + # Expected: The contribution holds the background's coefficients under + # its name. + pbso4_contribution.register_calculator(pbso4_background, argnames=["x"]) + actual_coefficients = [] + for index in range(6): + coefficient = pbso4_contribution.background.get(f"b{index}") + actual_coefficients.append(coefficient) + expected_coefficients = pbso4_background.coefficients + assert actual_coefficients == expected_coefficients + + +def test_evaluate_equation_background( + pbso4_background, pbso4_contribution, pbso4_pattern +): + # Test evaluating the background alone in a FitContribution, as a user + # does to look at it during a fit + # Expected: The background evaluated by the contribution equals the + # background calculated on the profile's x. + pbso4_contribution.register_calculator(pbso4_background, argnames=["x"]) + actual_background = pbso4_contribution.evaluate_equation("background") + expected_background = pbso4_background(pbso4_pattern.tth) + assert np.allclose(actual_background, expected_background) + + +def test_evaluate_with_bragg_and_background( + pbso4_background, pbso4_contribution, pbso4_generator, pbso4_pattern +): + # Test a FitContribution whose equation adds the background to the + # Bragg intensity + # Expected: The contribution's value is the generator's pattern plus + # the background, at every point of the profile. + pbso4_contribution.register_calculator(pbso4_background, argnames=["x"]) + pbso4_contribution.set_equation("bragg + background") + actual_pattern = pbso4_contribution.evaluate() + expected_pattern = pbso4_generator(pbso4_pattern.tth) + pbso4_background( + pbso4_pattern.tth + ) + assert np.allclose(actual_pattern, expected_pattern) + + +def test_evaluate_with_profile_wider_than_background(pbso4_background): + # Test a FitContribution whose profile runs past the background's + # range, as when a user forgets to set the calculation range + # Expected: Evaluating the contribution raises the background's + # ValueError, which names the profile's span and the range, before + # any refinement runs. + # Put the background in a contribution on 10 to 120 degrees + profile = Profile() + tth = np.linspace(10.0, 120.0, 4401) + profile.set_observed_profile(tth, np.zeros_like(tth)) + contribution = FitContribution("xray") + contribution.set_profile(profile) + contribution.register_calculator(pbso4_background, argnames=["x"]) + contribution.set_equation("background") + expected_msg = ( + "The background was calculated on x values from 10.0 to 120.0, " + "outside its range of 16.0 to 110.0. Please set the profile's " + "calculation range within the background's range, or make the " + "background with a range that holds the data." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + contribution.evaluate() + + +def test_evaluate_with_external_background( + pbso4_contribution, pbso4_generator, pbso4_pattern +): + # Test a FitContribution with a background written outside the + # package, as a subclass of Background + # Expected: After a user sets its level to 100, the contribution's + # value is the generator's pattern plus 100, and its level is a + # Parameter of the contribution. + external_background = ExternalBackground("background") + external_background.level.set_value(100.0) + pbso4_contribution.register_calculator(external_background, argnames=["x"]) + pbso4_contribution.set_equation("bragg + background") + actual_pattern = pbso4_contribution.evaluate() + expected_pattern = pbso4_generator(pbso4_pattern.tth) + 100.0 + actual_level = pbso4_contribution.background.level + expected_level = external_background.level + assert np.allclose(actual_pattern, expected_pattern) + assert actual_level == expected_level diff --git a/tests/test_component.py b/tests/test_component.py index 7e2b1a9..5a168f6 100644 --- a/tests/test_component.py +++ b/tests/test_component.py @@ -328,3 +328,12 @@ def test_settings_version_after_parameter_change(): actual_settings_version = component.settings_version expected_settings_version = 0 assert actual_settings_version == expected_settings_version + + +def test_get_settings(): + # Test the settings of a component that has none, such as a peak profile + # Expected: An empty dict, so print_info shows no settings for it. + component = Component("peak_profile") + actual_settings = component.get_settings() + expected_settings = {} + assert actual_settings == expected_settings diff --git a/tests/test_instruments.py b/tests/test_instruments.py index 49cf81a..bf868ee 100644 --- a/tests/test_instruments.py +++ b/tests/test_instruments.py @@ -152,6 +152,68 @@ def test_wavelength_after_setter( instrument._validate() +@pytest.mark.parametrize( + "setter_name, setter_args, expected_settings", + [ + # Test the settings an instrument reports to print_info. Only the + # settings in use are listed, each under the name of the setter + # argument that sets it + # C1: A user sets a single wavelength. + # Expected: The radiation type and the polarization rate. + ( + "set_wavelength", + [1.5406], + {"radiation_type": "X", "polarization_rate": 0.0}, + ), + # C2: A user sets a Cu tube. + # Expected: The tube and its Kalpha2/Kalpha1 ratio are listed too. + ( + "set_xray_source", + ["Cu"], + { + "radiation_type": "X", + "xray_source": "Cu", + "kalpha2_ratio": 0.5, + "polarization_rate": 0.0, + }, + ), + # C3: A user sets the Kalpha1 and Kalpha2 wavelengths from their + # instrument file, with a ratio of 0.4. + # Expected: The two wavelengths and the ratio are listed. + ( + "set_xray_wavelengths", + [CU_KALPHA1, CU_KALPHA2, 0.4], + { + "radiation_type": "X", + "kalpha1": CU_KALPHA1, + "kalpha2": CU_KALPHA2, + "kalpha2_ratio": 0.4, + "polarization_rate": 0.0, + }, + ), + # C4: A user switches to neutrons. + # Expected: The radiation type is N. + ( + "set_radiation_type", + ["N"], + {"radiation_type": "N", "polarization_rate": 0.0}, + ), + # C5: A user sets a polarization rate of 0.95. + # Expected: The polarization rate is 0.95. + ( + "set_polarization", + [0.95], + {"radiation_type": "X", "polarization_rate": 0.95}, + ), + ], +) +def test_get_settings(setter_name, setter_args, expected_settings): + instrument = ConstantWavelengthInstrument() + getattr(instrument, setter_name)(*setter_args) + actual_settings = instrument.get_settings() + assert actual_settings == expected_settings + + @pytest.mark.parametrize( "setter_name, setter_args", [ diff --git a/tests/test_powderdiffractiongenerator.py b/tests/test_powderdiffractiongenerator.py new file mode 100644 index 0000000..d04c2d7 --- /dev/null +++ b/tests/test_powderdiffractiongenerator.py @@ -0,0 +1,1259 @@ +import re +import warnings + +import numpy as np +import pytest +from conftest import PBSO4_CIF, TESTDATA_DIR, ExternalPeakProfile +from pyobjcryst.crystal import Crystal, create_crystal_from_cif +from pyobjcryst.reflectionprofile import ( + ReflectionProfilePseudoVoigtAnisotropic, + ReflectionProfilePseudoVoigtTCH, +) + +from diffpy.cmirietveld.instruments import ConstantWavelengthInstrument +from diffpy.cmirietveld.peak_profiles import PseudoVoigtProfile, TCHProfile +from diffpy.cmirietveld.powderdiffractiongenerator import ( + PowderDiffractionGenerator, +) +from diffpy.cmistructure import structure_to_parameter_set +from diffpy.cmistructure.objcrystparset import ObjCrystCrystalParSet +from diffpy.srfit.exceptions import SrFitError +from diffpy.srfit.fitbase import FitContribution, FitRecipe, Profile +from diffpy.structure import load_structure + + +def test_init(): + # Test a new generator before anything is set + # Expected: It is named bragg, has no structure, no instrument and no + # peak profile, since a user chooses each of them, and its scale is 1. + generator = PowderDiffractionGenerator() + actual_name = generator.name + actual_phase = generator.phase + actual_instrument = generator.instrument + actual_peak_profile = generator.peak_profile + actual_scale = generator.scale.value + expected_name = "bragg" + expected_phase = None + expected_instrument = None + expected_peak_profile = None + expected_scale = 1.0 + assert actual_name == expected_name + assert actual_phase == expected_phase + assert actual_instrument == expected_instrument + assert actual_peak_profile == expected_peak_profile + assert actual_scale == expected_scale + + +@pytest.mark.parametrize( + "set_structure_kwargs, expected_phase_name", + [ + # Test setting the structure from a pyobjcryst Crystal + # C1: A user sets the PbSO4 crystal without naming it. + # Expected: The structure's Parameters are under generator.phase. + ({}, "phase"), + # C2: A user sets the PbSO4 crystal and names it pbso4. + # Expected: The structure's Parameters are under generator.pbso4. + ({"name": "pbso4"}, "pbso4"), + ], +) +def test_set_structure_with_crystal(set_structure_kwargs, expected_phase_name): + crystal = create_crystal_from_cif(str(PBSO4_CIF)) + generator = PowderDiffractionGenerator() + generator.set_structure(crystal, **set_structure_kwargs) + actual_phase_type = type(generator.phase) + actual_phase_name = generator.phase.name + actual_managed_phase = generator.get(expected_phase_name) + actual_structure = generator.phase.structure + expected_phase_type = ObjCrystCrystalParSet + expected_managed_phase = generator.phase + expected_structure = crystal + assert actual_phase_type == expected_phase_type + assert actual_phase_name == expected_phase_name + assert actual_managed_phase == expected_managed_phase + assert actual_structure == expected_structure + + +@pytest.mark.parametrize( + "cif_path", + [ + # Test setting the structure from the path to a CIF file + # C1: A user gives the path to the PbSO4 CIF as a pathlib.Path. + # Expected: The generator reads the file into a pyobjcryst Crystal, + # and the pattern equals plain pyobjcryst's for that CIF. + PBSO4_CIF, + # C2: A user gives the same path as a string. + # Expected: The same Crystal type and pattern as for the Path. + str(PBSO4_CIF), + ], +) +def test_set_structure_with_cif_path(pbso4_pattern, cif_path): + generator = PowderDiffractionGenerator() + generator.set_structure(cif_path) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + generator.set_peak_profile(PseudoVoigtProfile()) + actual_structure_type = type(generator.phase.structure) + actual_pattern = generator(pbso4_pattern.tth) + expected_structure_type = Crystal + expected_pattern = pbso4_pattern.calculate() + assert actual_structure_type == expected_structure_type + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_with_cif_path_given_to_two_generators(pbso4_pattern): + # Test giving the same CIF path to two generators + # Expected: Each generator reads its own structure, so after the lattice + # parameter a of one is set to 8.50 angstroms, the other's pattern is + # still plain pyobjcryst's for the CIF. Sharing one structure needs one + # ParameterSet given to both generators. + # Give two generators the same path, instrument and peak profile + generator = PowderDiffractionGenerator() + other_generator = PowderDiffractionGenerator() + generator.set_structure(PBSO4_CIF) + other_generator.set_structure(PBSO4_CIF) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + peak_profile = PseudoVoigtProfile() + generator.set_instrument(instrument) + other_generator.set_instrument(instrument) + generator.set_peak_profile(peak_profile) + other_generator.set_peak_profile(peak_profile) + # Change a on the first generator only + generator.phase.a.set_value(8.50) + actual_other_pattern = other_generator(pbso4_pattern.tth) + expected_other_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_other_pattern, expected_other_pattern) + + +@pytest.mark.parametrize( + "file_path", + [ + # Test setting the structure from a file that isn't a CIF file + # holding a crystal structure + # C1: A user gives the path to their data file by mistake. + # Expected: A ValueError names the file and says to give a CIF file + # with a crystal structure. + TESTDATA_DIR / "PBSO4.XRA", + # C2: A user gives the .stru file from their PDF fits, which + # diffpy.cmipdf's PDF generators read. + # Expected: The same ValueError, since the generator reads CIF + # files only. + TESTDATA_DIR / "pbso4.stru", + ], +) +def test_set_structure_with_non_cif_file(file_path): + generator = PowderDiffractionGenerator() + expected_msg = ( + f"No crystal structure could be read from {file_path}. " + "Please give a CIF file that holds a crystal structure." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + generator.set_structure(file_path) + + +@pytest.mark.parametrize( + "phase_name, set_structure_kwargs", + [ + # Test setting the structure from an existing ParameterSet, as a + # user does to share one structure between datasets + # C1: A user makes a ParameterSet named phase from the PbSO4 crystal + # and gives it to set_structure. + # Expected: The generator manages that same ParameterSet, under + # generator.phase. + ("phase", {}), + # C2: A user makes the ParameterSet with the name pbso4. + # Expected: The ParameterSet keeps its name, so it is under + # generator.pbso4, and generator.phase is it too. + ("pbso4", {}), + # C3: A user makes the ParameterSet with the name pbso4 and gives + # set_structure the same name. + # Expected: The same as without the name, since the names match. + ("pbso4", {"name": "pbso4"}), + ], +) +def test_set_structure_with_parameter_set(phase_name, set_structure_kwargs): + phase = structure_to_parameter_set( + phase_name, create_crystal_from_cif(str(PBSO4_CIF)) + ) + generator = PowderDiffractionGenerator() + generator.set_structure(phase, **set_structure_kwargs) + actual_phase = generator.phase + actual_managed_phase = generator.get(phase_name) + expected_phase = phase + assert actual_phase == expected_phase + assert actual_managed_phase == expected_phase + + +def test_set_structure_with_parameter_set_and_other_name(): + # Test giving set_structure a name with a ParameterSet that has another + # name of its own + # Expected: A ValueError says the name can't be used and how to name + # the ParameterSet instead. + phase = structure_to_parameter_set( + "phase", create_crystal_from_cif(str(PBSO4_CIF)) + ) + generator = PowderDiffractionGenerator() + expected_msg = ( + "set_structure was given the name 'pbso4' with a ParameterSet, " + "which keeps its own name, 'phase'. Please leave out the name, or " + "give it when you create the ParameterSet." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + generator.set_structure(phase, name="pbso4") + + +def test_set_structure_with_diffpy_structure(): + # Test giving set_structure the diffpy.structure Structure a user has + # from their PDF fits, which ObjCryst can't calculate with + # Expected: A ValueError names the type and lists what to give instead. + diffpy_structure = load_structure(str(PBSO4_CIF)) + generator = PowderDiffractionGenerator() + expected_msg = ( + "set_structure was given a Structure, which the powder diffraction " + "generator can't calculate with. Please give a pyobjcryst Crystal, " + "the path to a CIF file, or a ParameterSet made from a Crystal." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + generator.set_structure(diffpy_structure) + + +def test_set_structure_from_parset(): + # Test setting the structure with set_structure_from_parset, which + # srfit's and diffpy.cmipdf's PDF generators also have + # Expected: The generator manages that same ParameterSet. + phase = structure_to_parameter_set( + "phase", create_crystal_from_cif(str(PBSO4_CIF)) + ) + generator = PowderDiffractionGenerator() + generator.set_structure_from_parset(phase) + actual_phase = generator.phase + expected_phase = phase + assert actual_phase == expected_phase + + +def test_call_after_set_structure_twice(pbso4_pattern): + # Test setting a second structure, as when a user reruns the setup cell + # of their script with another CIF + # Expected: The generator holds only the second structure, and the + # pattern equals plain pyobjcryst's for it alone, not the sum of both. + # Set a first structure, with a changed lattice, and calculate once + generator = PowderDiffractionGenerator() + first_crystal = create_crystal_from_cif(str(PBSO4_CIF)) + first_crystal.GetPar("a").SetHumanValue(8.50) + generator.set_structure(first_crystal) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + generator.set_peak_profile(PseudoVoigtProfile()) + generator(pbso4_pattern.tth) + # Replace it with the CIF's structure and calculate again + second_crystal = create_crystal_from_cif(str(PBSO4_CIF)) + generator.set_structure(second_crystal) + actual_structure = generator.phase.structure + actual_pattern = generator(pbso4_pattern.tth) + expected_structure = second_crystal + expected_pattern = pbso4_pattern.calculate() + assert actual_structure == expected_structure + assert np.allclose(actual_pattern, expected_pattern) + + +def test_get_after_set_structure_with_new_name(pbso4_generator): + # Test setting a second structure under a new name, as when a user + # renames their phase and reruns the setup cell of their script + # Expected: The first structure, named phase, is no longer under the + # generator, and the second one is under generator.pbso4. + pbso4_generator.set_structure( + create_crystal_from_cif(str(PBSO4_CIF)), name="pbso4" + ) + actual_old_phase = pbso4_generator.get("phase") + actual_new_phase = pbso4_generator.get("pbso4") + expected_old_phase = None + expected_new_phase = pbso4_generator.phase + assert actual_old_phase == expected_old_phase + assert actual_new_phase == expected_new_phase + + +def test_call_with_pseudo_voigt_profile(pbso4_generator, pbso4_pattern): + # Test calculating a pattern with a pseudo-Voigt peak profile and a + # constant-wavelength instrument + # Expected: The pattern equals the one plain pyobjcryst calculates for + # the same structure, grid and wavelength, whose own default profile is + # pseudo-Voigt. + actual_pattern = pbso4_generator(pbso4_pattern.tth) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_set_wavelength(pbso4_generator, pbso4_pattern): + # Test that a wavelength set after a calculation is used in the next one + # Expected: After a user sets a single wavelength of 1.909 angstroms, + # the pattern equals plain pyobjcryst's at 1.909 angstroms. + # Calculate once, so the new wavelength has to be installed again + pbso4_generator(pbso4_pattern.tth) + # Set the new wavelength and calculate again + pbso4_generator.instrument.set_wavelength(1.909) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same wavelength directly in pyobjcryst + pbso4_pattern.pattern.SetWavelength(1.909) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_set_xray_source(pbso4_generator, pbso4_pattern): + # Test that an x-ray tube set after a calculation is used in the next one + # Expected: After a user sets a Cu tube, the pattern equals plain + # pyobjcryst's for a Cu tube with a Kalpha2/Kalpha1 ratio of 0.5. + # Calculate once, so the tube has to be installed again + pbso4_generator(pbso4_pattern.tth) + # Set the Cu tube and calculate again + pbso4_generator.instrument.set_xray_source("Cu") + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same tube directly in pyobjcryst + pbso4_pattern.pattern.SetWavelength("Cu", 0.5) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +@pytest.mark.parametrize( + "peak_profile_class, pyobjcryst_class, parameter_name, new_value", + [ + # Test that the generator calculates with the peak profile it is + # given + # C1: A user switches to a TCH profile and sets its Lorentzian + # width X. + # Expected: The pattern equals plain pyobjcryst's with a TCH profile + # and the same X. + (TCHProfile, ReflectionProfilePseudoVoigtTCH, "X", 0.02), + # C2: A user writes their own peak profile in another package, here + # one for pyobjcryst's anisotropic pseudo-Voigt, and sets its G_HH. + # Expected: The pattern equals plain pyobjcryst's with that profile + # and the same G_HH, so a new profile needs no change to the + # generator. + ( + ExternalPeakProfile, + ReflectionProfilePseudoVoigtAnisotropic, + "G_HH", + 0.0005, + ), + ], +) +def test_call_after_set_peak_profile( + pbso4_generator, + pbso4_pattern, + peak_profile_class, + pyobjcryst_class, + parameter_name, + new_value, +): + # Give the generator the new profile and change one of its Parameters + pbso4_generator.set_peak_profile(peak_profile_class()) + pbso4_generator.peak_profile.get(parameter_name).set_value(new_value) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Install the same profile and value directly in pyobjcryst + pbso4_pattern.diffraction.SetProfile(pyobjcryst_class()) + installed_profile = pbso4_pattern.diffraction.GetProfile() + installed_profile.GetPar(parameter_name).SetHumanValue(new_value) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_set_components_before_set_structure(pbso4_pattern): + # Test setting up the instrument and the peak profile before the + # structure, as a user may do at the top of a script + # Expected: The pattern equals plain pyobjcryst's with a TCH profile, + # because the instrument and the profile are installed once the + # structure is set. + # Set the instrument and the profile first, then the structure + generator = PowderDiffractionGenerator() + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + tch_profile = TCHProfile() + tch_profile.X.set_value(0.02) + generator.set_peak_profile(tch_profile) + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + actual_pattern = generator(pbso4_pattern.tth) + # Install the same profile directly in pyobjcryst + pbso4_pattern.diffraction.SetProfile(ReflectionProfilePseudoVoigtTCH()) + installed_profile = pbso4_pattern.diffraction.GetProfile() + installed_profile.GetPar("X").SetHumanValue(0.02) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +@pytest.mark.parametrize( + "setter_name, component_name, component_class", + [ + # Test that a setter replaces the component and its Parameters + # C1: A user switches to a TCH profile. + # Expected: generator.peak_profile is the TCH profile, and the + # generator manages it, so a change to its Parameters reaches a + # FitContribution. + ("set_peak_profile", "peak_profile", TCHProfile), + # C2: A user gives the generator an instrument they set up. + # Expected: generator.instrument is that instrument, and the + # generator manages it. + ("set_instrument", "instrument", ConstantWavelengthInstrument), + ], +) +def test_component_after_setter( + pbso4_generator, setter_name, component_name, component_class +): + new_component = component_class() + getattr(pbso4_generator, setter_name)(new_component) + actual_component = getattr(pbso4_generator, component_name) + actual_managed_component = pbso4_generator.get(component_name) + expected_component = new_component + assert actual_component == expected_component + assert actual_managed_component == expected_component + + +def test_call_after_peak_profile_parameter_change( + pbso4_generator, pbso4_pattern +): + # Test that a changed peak profile Parameter is used in the next + # calculation + # Expected: After W is set to 0.05, the pattern equals plain + # pyobjcryst's with W = 0.05. + # Calculate once, then change W and calculate again + pbso4_generator(pbso4_pattern.tth) + pbso4_generator.peak_profile.W.set_value(0.05) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same W directly in pyobjcryst + installed_profile = pbso4_pattern.diffraction.GetProfile() + installed_profile.GetPar("W").SetHumanValue(0.05) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_instrument_parameter_change( + pbso4_generator, pbso4_pattern +): + # Test that a changed instrument Parameter is used in the next + # calculation + # Expected: After zero is set to 0.1 degrees, the pattern equals plain + # pyobjcryst's with a zero shift of 0.1 degrees. + # Calculate once, then change zero and calculate again + pbso4_generator(pbso4_pattern.tth) + pbso4_generator.instrument.zero.set_value(0.1) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same zero shift directly in pyobjcryst + pbso4_pattern.pattern.GetPar("Zero").SetHumanValue(0.1) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_wavelength_change(pbso4_generator, pbso4_pattern): + # Test that a refined wavelength is used in the next calculation + # Expected: After the wavelength Parameter is set to 1.55 angstroms, as + # a refinement would, the pattern equals plain pyobjcryst's at 1.55 + # angstroms. + # Calculate once, then change the wavelength and calculate again + pbso4_generator(pbso4_pattern.tth) + pbso4_generator.instrument.wavelength.set_value(1.55) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same wavelength directly in pyobjcryst + pbso4_pattern.pattern.SetWavelength(1.55) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_structure_parameter_change(pbso4_generator, pbso4_pattern): + # Test that a changed structure Parameter is used in the next + # calculation + # Expected: After the lattice parameter a is set to 8.50 angstroms, the + # pattern equals plain pyobjcryst's with a = 8.50 angstroms. + # Calculate once, then change a and calculate again + pbso4_generator(pbso4_pattern.tth) + pbso4_generator.phase.a.set_value(8.50) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + # Set the same lattice parameter directly in pyobjcryst + pbso4_pattern.crystal.GetPar("a").SetHumanValue(8.50) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_scale_change(pbso4_generator, pbso4_pattern): + # Test that the scale is applied to the calculated pattern + # Expected: Doubling the scale doubles the pattern. + initial_pattern = pbso4_generator(pbso4_pattern.tth) + pbso4_generator.scale.set_value(2.0) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + expected_pattern = 2 * initial_pattern + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_after_grid_change(pbso4_generator, pbso4_pattern): + # Test calculating on a new 2theta grid, as when a user changes the + # calculation range + # Expected: The pattern equals plain pyobjcryst's on the new 20-60 + # degree grid, with one value per new point. + # Calculate once on the full grid, then on the new one + pbso4_generator(pbso4_pattern.tth) + new_tth = np.arange(20.0, 60.0, 0.02) + actual_pattern = pbso4_generator(new_tth) + # Calculate on the same grid directly in pyobjcryst + pbso4_pattern.set_tth(new_tth) + expected_pattern = pbso4_pattern.calculate() + actual_number_of_points = len(actual_pattern) + expected_number_of_points = len(new_tth) + assert actual_number_of_points == expected_number_of_points + assert np.allclose(actual_pattern, expected_pattern) + + +@pytest.mark.parametrize( + "tth_min, tth_max, peak_width_w", + [ + # Test that reflections just outside the calculation range still + # draw their tails inside it, as in GSAS-II. PbSO4's strongest + # reflection is at 29.675 degrees. + # C1: A user's range ends 0.3 degrees below the strongest + # reflection. + # Expected: The pattern equals plain pyobjcryst's on a grid running + # 30 degrees past the range, at the range's points, so the + # reflection's tail is drawn. + (16.0, 29.375, 0.01), + # C2: A user's range ends 0.3 degrees below the strongest + # reflection, and the peaks are five times as broad, with W = 0.05. + # Expected: The pattern equals plain pyobjcryst's on the wider grid, + # so the margin the generator adds grows with the peak width. + (16.0, 29.375, 0.05), + # C3: A user's range starts 0.3 degrees above the strongest + # reflection. + # Expected: The pattern equals plain pyobjcryst's on the wider grid, + # as ObjCryst already draws reflections below its grid. + (29.975, 60.0, 0.01), + ], +) +def test_call_with_reflection_outside_range( + pbso4_generator, pbso4_pattern, tth_min, tth_max, peak_width_w +): + # Calculate with the generator on the user's range + wide_tth = np.round(np.arange(10.0, tth_max + 30.0, 0.025), 6) + in_range = (wide_tth >= tth_min - 1e-9) & (wide_tth <= tth_max + 1e-9) + tth = wide_tth[in_range] + pbso4_generator.peak_profile.W.set_value(peak_width_w) + actual_pattern = pbso4_generator(tth) + # Calculate directly in pyobjcryst on the wide grid, and keep the + # range's points + pbso4_pattern.set_tth(wide_tth) + pbso4_pattern.diffraction.GetProfile().GetPar("W").SetHumanValue( + peak_width_w + ) + expected_pattern = pbso4_pattern.calculate()[in_range] + assert np.allclose(actual_pattern, expected_pattern) + + +def test_evaluate_with_two_phases_and_background( + pbso4_pattern, pbso4_background +): + # Test a FitContribution with two phases that share one instrument, + # as for a two-phase sample measured in one dataset + # Expected: The contribution's value is the sum of both phases' + # patterns and the background, and after a fit steps the shared zero + # to 0.1 degrees, both phases' peaks move: each phase equals plain + # pyobjcryst's pattern with a zero shift of 0.1 degrees. + # Make two phases with one instrument, and halve the second phase's + # scale so the two can be told apart + shared_instrument = ConstantWavelengthInstrument() + shared_instrument.set_wavelength(1.5406) + phase_1 = PowderDiffractionGenerator("phase_1") + phase_2 = PowderDiffractionGenerator("phase_2") + for phase in [phase_1, phase_2]: + phase.set_structure(PBSO4_CIF) + phase.set_instrument(shared_instrument) + phase.set_peak_profile(PseudoVoigtProfile()) + phase_2.scale.set_value(0.5) + # Put both phases and the background in one contribution + profile = Profile() + profile.set_observed_profile( + pbso4_pattern.tth, np.zeros_like(pbso4_pattern.tth) + ) + contribution = FitContribution("two_phases") + contribution.set_profile(profile) + contribution.add_profile_generator(phase_1) + contribution.add_profile_generator(phase_2) + contribution.register_calculator(pbso4_background, argnames=["x"]) + contribution.set_equation("phase_1 + phase_2 + background") + # Evaluate once, then step the shared zero and evaluate again + contribution.evaluate() + shared_instrument.zero.set_value(0.1) + actual_pattern = contribution.evaluate() + actual_phase_1_pattern = contribution.evaluate_equation("phase_1") + actual_phase_2_pattern = contribution.evaluate_equation("phase_2") + # Calculate one phase with the same zero shift directly in pyobjcryst + pbso4_pattern.pattern.GetPar("Zero").SetHumanValue(0.1) + shifted_phase_pattern = pbso4_pattern.calculate() + expected_phase_1_pattern = shifted_phase_pattern + expected_phase_2_pattern = 0.5 * shifted_phase_pattern + expected_pattern = ( + expected_phase_1_pattern + + expected_phase_2_pattern + + pbso4_background(pbso4_pattern.tth) + ) + assert np.allclose(actual_phase_1_pattern, expected_phase_1_pattern) + assert np.allclose(actual_phase_2_pattern, expected_phase_2_pattern) + assert np.allclose(actual_pattern, expected_pattern) + + +def test_call_with_shared_peak_profile( + pbso4_generator, other_pbso4_generator, pbso4_pattern +): + # Test one peak profile given to two generators, as a user does for one + # phase measured in two datasets + # Expected: After W is set to 0.05, both patterns equal plain + # pyobjcryst's with W = 0.05. + # Give both generators the same profile and calculate once + shared_profile = PseudoVoigtProfile() + pbso4_generator.set_peak_profile(shared_profile) + other_pbso4_generator.set_peak_profile(shared_profile) + pbso4_generator(pbso4_pattern.tth) + other_pbso4_generator(pbso4_pattern.tth) + # Change W once and calculate both again + shared_profile.W.set_value(0.05) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + actual_other_pattern = other_pbso4_generator(pbso4_pattern.tth) + # Set the same W directly in pyobjcryst + installed_profile = pbso4_pattern.diffraction.GetProfile() + installed_profile.GetPar("W").SetHumanValue(0.05) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + assert np.allclose(actual_other_pattern, expected_pattern) + + +def test_call_with_shared_instrument_after_parameter_change( + pbso4_generator, other_pbso4_generator, pbso4_pattern +): + # Test one instrument given to two generators, as a user does for two + # phases in one dataset + # Expected: After zero is set to 0.1 degrees, both patterns equal plain + # pyobjcryst's with a zero shift of 0.1 degrees. + # Give both generators the same instrument and calculate once + shared_instrument = ConstantWavelengthInstrument() + shared_instrument.set_wavelength(1.5406) + pbso4_generator.set_instrument(shared_instrument) + other_pbso4_generator.set_instrument(shared_instrument) + pbso4_generator(pbso4_pattern.tth) + other_pbso4_generator(pbso4_pattern.tth) + # Change zero once and calculate both again + shared_instrument.zero.set_value(0.1) + actual_pattern = pbso4_generator(pbso4_pattern.tth) + actual_other_pattern = other_pbso4_generator(pbso4_pattern.tth) + # Set the same zero shift directly in pyobjcryst + pbso4_pattern.pattern.GetPar("Zero").SetHumanValue(0.1) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + assert np.allclose(actual_other_pattern, expected_pattern) + + +def test_call_with_shared_instrument_after_setting_change( + pbso4_generator, other_pbso4_generator, pbso4_pattern +): + # Test one instrument given to two generators when a setting changes + # Expected: After a user sets a Cu tube, both patterns equal plain + # pyobjcryst's for a Cu tube, because both generators install the + # instrument again. + # Give both generators the same instrument and calculate once + shared_instrument = ConstantWavelengthInstrument() + shared_instrument.set_wavelength(1.5406) + pbso4_generator.set_instrument(shared_instrument) + other_pbso4_generator.set_instrument(shared_instrument) + pbso4_generator(pbso4_pattern.tth) + other_pbso4_generator(pbso4_pattern.tth) + # Set a Cu tube once and calculate both again + shared_instrument.set_xray_source("Cu") + actual_pattern = pbso4_generator(pbso4_pattern.tth) + actual_other_pattern = other_pbso4_generator(pbso4_pattern.tth) + # Set the same tube directly in pyobjcryst + pbso4_pattern.pattern.SetWavelength("Cu", 0.5) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + assert np.allclose(actual_other_pattern, expected_pattern) + + +def test_call_with_shared_structure(pbso4_pattern): + # Test one structure ParameterSet given to two generators, as a user + # does to refine one structure against two datasets + # Expected: After the lattice parameter a is set to 8.50 angstroms, + # both patterns equal plain pyobjcryst's with a = 8.50 angstroms. + # Give two generators the same structure, instrument and peak profile, + # and calculate once + shared_phase = structure_to_parameter_set( + "phase", create_crystal_from_cif(str(PBSO4_CIF)) + ) + generator = PowderDiffractionGenerator() + other_generator = PowderDiffractionGenerator() + generator.set_structure(shared_phase) + other_generator.set_structure(shared_phase) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + peak_profile = PseudoVoigtProfile() + generator.set_instrument(instrument) + other_generator.set_instrument(instrument) + generator.set_peak_profile(peak_profile) + other_generator.set_peak_profile(peak_profile) + generator(pbso4_pattern.tth) + other_generator(pbso4_pattern.tth) + # Change a once and calculate both again + shared_phase.a.set_value(8.50) + actual_pattern = generator(pbso4_pattern.tth) + actual_other_pattern = other_generator(pbso4_pattern.tth) + # Set the same lattice parameter directly in pyobjcryst + pbso4_pattern.crystal.GetPar("a").SetHumanValue(8.50) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + assert np.allclose(actual_other_pattern, expected_pattern) + + +@pytest.mark.parametrize( + "component_name, parameter_name, new_value", + [ + # Test that a FitContribution recalculates after a Parameter + # changes, as it does at every step of a fit + # C1: A fit steps the peak profile's W to 0.05. + # Expected: The contribution's value equals the generator's pattern + # with W = 0.05, not the cached pattern. + ("peak_profile", "W", 0.05), + # C2: A fit steps the instrument's zero to 0.1 degrees. + # Expected: The contribution's value equals the generator's pattern + # with a zero shift of 0.1 degrees. + ("instrument", "zero", 0.1), + # C3: A fit steps the wavelength to 1.55 angstroms. + # Expected: The contribution's value equals the generator's pattern + # at 1.55 angstroms. + ("instrument", "wavelength", 1.55), + # C4: A fit steps the lattice parameter a to 8.50 angstroms. + # Expected: The contribution's value equals the generator's pattern + # with a = 8.50 angstroms. + ("phase", "a", 8.50), + ], +) +def test_evaluate_after_parameter_change( + pbso4_generator, + pbso4_contribution, + pbso4_pattern, + component_name, + parameter_name, + new_value, +): + # Evaluate the contribution once, then change the Parameter and + # evaluate again + pbso4_contribution.evaluate() + component = getattr(pbso4_generator, component_name) + component.get(parameter_name).set_value(new_value) + actual_pattern = pbso4_contribution.evaluate() + # Calculate with the generator directly, which has no cache + expected_pattern = pbso4_generator(pbso4_pattern.tth) + assert np.allclose(actual_pattern, expected_pattern) + + +def test_evaluate_after_instrument_setting_change( + pbso4_generator, pbso4_contribution, pbso4_pattern +): + # Test that a FitContribution recalculates after an instrument setting + # changes, as it does after a Parameter changes + # Expected: After a user sets a Cu tube, the contribution's value equals + # plain pyobjcryst's pattern for a Cu tube, not the cached pattern. + # Evaluate the contribution once + pbso4_contribution.evaluate() + # Set a Cu tube and evaluate again + pbso4_generator.instrument.set_xray_source("Cu") + actual_pattern = pbso4_contribution.evaluate() + # Set the same tube directly in pyobjcryst + pbso4_pattern.pattern.SetWavelength("Cu", 0.5) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + + +def test_evaluate_after_set_peak_profile( + pbso4_generator, pbso4_contribution, pbso4_pattern +): + # Test that a FitContribution recalculates after the peak profile is + # replaced, and that the replaced profile no longer counts + # Expected: After a user switches to a TCH profile, the contribution's + # value equals plain pyobjcryst's with a TCH profile, and it stays so + # after the old profile's W is changed. + # Evaluate the contribution once + pbso4_contribution.evaluate() + # Switch to a TCH profile and evaluate again + old_profile = pbso4_generator.peak_profile + pbso4_generator.set_peak_profile(TCHProfile()) + actual_pattern = pbso4_contribution.evaluate() + # Change the old profile's W and evaluate once more + old_profile.W.set_value(0.05) + actual_pattern_after_old_change = pbso4_contribution.evaluate() + # Install a TCH profile directly in pyobjcryst + pbso4_pattern.diffraction.SetProfile(ReflectionProfilePseudoVoigtTCH()) + expected_pattern = pbso4_pattern.calculate() + assert np.allclose(actual_pattern, expected_pattern) + assert np.allclose(actual_pattern_after_old_change, expected_pattern) + + +def test_peak_profile_after_add_profile_generator(pbso4_generator): + # Test that a FitContribution doesn't replace the peak profile, since + # srfit gives each generator a data Profile named profile + # Expected: generator.peak_profile is still the peak profile, and + # generator.profile is the contribution's data Profile. + expected_peak_profile = pbso4_generator.peak_profile + expected_profile = Profile() + contribution = FitContribution("xray") + contribution.set_profile(expected_profile) + contribution.add_profile_generator(pbso4_generator) + actual_peak_profile = pbso4_generator.peak_profile + actual_profile = pbso4_generator.profile + assert actual_peak_profile == expected_peak_profile + assert actual_profile == expected_profile + + +@pytest.mark.parametrize( + "component_name, parameter_name, new_value, limits, used_value", + [ + # Test that a value outside ObjCryst's limits stops the calculation, + # since ObjCryst would silently use another value + # C1: A fit steps the peak profile's eta0 to 1.5, past its limits of + # 0 to 1. + # Expected: A ValueError says ObjCryst would use 1.0, and how to + # keep the value within the limits. + ("peak_profile", "eta0", 1.5, "[0.0, 1.0]", "1.0"), + # C2: A fit steps the instrument's zero to 3 degrees, past its + # limits of +-0.05 rad. + # Expected: A ValueError says ObjCryst would use 0.05 rad in + # degrees, and how to keep the value within the limits. + ( + "instrument", + "zero", + 3.0, + "[-2.8647889756541165, 2.8647889756541165]", + "2.8647889756541165", + ), + ], +) +def test_call_with_value_outside_limits( + pbso4_generator, + pbso4_pattern, + component_name, + parameter_name, + new_value, + limits, + used_value, +): + component = getattr(pbso4_generator, component_name) + component.get(parameter_name).set_value(new_value) + expected_msg = ( + f"The value of '{parameter_name}', {new_value}, is outside its " + f"limits {limits}. ObjCryst would silently use {used_value} " + "instead, so the refined value would not be the one used to " + "calculate the pattern. Please keep the value within the limits, " + "for example by passing the recipe's bounds to the optimizer: " + "least_squares(..., bounds=recipe.get_bounds_array())." + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): + pbso4_generator(pbso4_pattern.tth) + + +@pytest.mark.parametrize( + "setter_name, wrong_component_class, expected_msg", + [ + # Test that a setter rejects the wrong kind of component + # C1: A user passes an instrument to set_peak_profile. + # Expected: A ValueError says it is not a peak profile and names the + # peak profiles to use. + ( + "set_peak_profile", + ConstantWavelengthInstrument, + "set_peak_profile was given a ConstantWavelengthInstrument, " + "which is not a peak profile. Please give a PeakProfile, such as " + "PseudoVoigtProfile or TCHProfile.", + ), + # C2: A user passes a peak profile to set_instrument. + # Expected: A ValueError says it is not an instrument and names the + # instrument to use. + ( + "set_instrument", + PseudoVoigtProfile, + "set_instrument was given a PseudoVoigtProfile, which is not an " + "instrument. Please give an Instrument, such as " + "ConstantWavelengthInstrument.", + ), + ], +) +def test_setter_with_wrong_component( + pbso4_generator, setter_name, wrong_component_class, expected_msg +): + with pytest.raises(ValueError, match=re.escape(expected_msg)): + getattr(pbso4_generator, setter_name)(wrong_component_class()) + + +def test_call_before_set_structure(pbso4_pattern): + # Test calculating a pattern before a structure is set + # Expected: An SrFitError says that the structure is missing and how to + # set it. + # Set everything but the structure + generator = PowderDiffractionGenerator() + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + generator.set_peak_profile(PseudoVoigtProfile()) + expected_msg = ( + "The generator has no structure. Please set one with " + "set_structure or set_structure_from_parset." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + generator(pbso4_pattern.tth) + + +def test_call_before_set_instrument(pbso4_pattern): + # Test calculating a pattern before an instrument is set, as when a user + # forgets it in their script + # Expected: An SrFitError says that the instrument is missing and how to + # set it. + # Set everything but the instrument + generator = PowderDiffractionGenerator() + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + generator.set_peak_profile(PseudoVoigtProfile()) + expected_msg = ( + "The generator has no instrument. Please set one with " + "set_instrument, such as a ConstantWavelengthInstrument." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + generator(pbso4_pattern.tth) + + +def test_call_before_set_peak_profile(pbso4_pattern): + # Test calculating a pattern before a peak profile is set, as when a + # user forgets it in their script + # Expected: An SrFitError says that the peak profile is missing and how + # to set it. + # Set everything but the peak profile + generator = PowderDiffractionGenerator() + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + expected_msg = ( + "The generator has no peak profile. Please set one with " + "set_peak_profile, such as a PseudoVoigtProfile or a TCHProfile." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + generator(pbso4_pattern.tth) + + +def test_residual_before_set_structure(pbso4_pattern): + # Test starting a fit with a generator that has no structure + # Expected: The recipe's first residual raises the same SrFitError as + # calculating the pattern, before anything is calculated. + # Set everything but the structure, and build a recipe on the generator + generator = PowderDiffractionGenerator() + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + generator.set_peak_profile(PseudoVoigtProfile()) + profile = Profile() + profile.set_observed_profile( + pbso4_pattern.tth, np.zeros_like(pbso4_pattern.tth) + ) + contribution = FitContribution("xray") + contribution.set_profile(profile) + contribution.add_profile_generator(generator) + contribution.set_equation("bragg") + recipe = FitRecipe() + recipe.add_contribution(contribution) + recipe.add_variable(generator.scale) + # Start the fit + expected_msg = ( + "The generator has no structure. Please set one with " + "set_structure or set_structure_from_parset." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + recipe.residual() + + +def test_residual_before_set_instrument(pbso4_pattern): + # Test starting a fit with a generator that has no instrument + # Expected: The recipe's first residual raises the same SrFitError as + # calculating the pattern, before anything is calculated. + # Set everything but the instrument, and build a recipe on the + # generator + generator = PowderDiffractionGenerator() + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + generator.set_peak_profile(PseudoVoigtProfile()) + profile = Profile() + profile.set_observed_profile( + pbso4_pattern.tth, np.zeros_like(pbso4_pattern.tth) + ) + contribution = FitContribution("xray") + contribution.set_profile(profile) + contribution.add_profile_generator(generator) + contribution.set_equation("bragg") + recipe = FitRecipe() + recipe.add_contribution(contribution) + recipe.add_variable(generator.scale) + # Start the fit + expected_msg = ( + "The generator has no instrument. Please set one with " + "set_instrument, such as a ConstantWavelengthInstrument." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + recipe.residual() + + +def test_residual_before_set_peak_profile(pbso4_pattern): + # Test starting a fit with a generator that has no peak profile + # Expected: The recipe's first residual raises the same SrFitError as + # calculating the pattern, before anything is calculated. + # Set everything but the peak profile, and build a recipe on the + # generator + generator = PowderDiffractionGenerator() + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + instrument = ConstantWavelengthInstrument() + instrument.set_wavelength(1.5406) + generator.set_instrument(instrument) + profile = Profile() + profile.set_observed_profile( + pbso4_pattern.tth, np.zeros_like(pbso4_pattern.tth) + ) + contribution = FitContribution("xray") + contribution.set_profile(profile) + contribution.add_profile_generator(generator) + contribution.set_equation("bragg") + recipe = FitRecipe() + recipe.add_contribution(contribution) + recipe.add_variable(generator.scale) + # Start the fit + expected_msg = ( + "The generator has no peak profile. Please set one with " + "set_peak_profile, such as a PseudoVoigtProfile or a TCHProfile." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + recipe.residual() + + +def test_call_before_set_wavelength(pbso4_pattern): + # Test calculating a pattern before the instrument has a wavelength + # Expected: An SrFitError says that the wavelength is missing and how to + # set it. + # Set everything, with an instrument that has no wavelength + generator = PowderDiffractionGenerator() + generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + generator.set_instrument(ConstantWavelengthInstrument()) + generator.set_peak_profile(PseudoVoigtProfile()) + expected_msg = ( + "The instrument has no wavelength. Please set one with " + "set_wavelength, set_xray_source or set_xray_wavelengths." + ) + with pytest.raises(SrFitError, match=re.escape(expected_msg)): + generator(pbso4_pattern.tth) + + +@pytest.mark.parametrize( + "setter_name, component_name, parameter_name, new_component_class, " + "expected_msg", + [ + # Test that a setter warns when the component it replaces has + # Parameters in a fit recipe, since the recipe would refine + # Parameters that no longer change the pattern + # C1: A user adds the peak profile's W to their recipe, then + # switches to a TCH profile. + # Expected: A UserWarning names W and says to set the profile before + # adding its Parameters to the recipe. + ( + "set_peak_profile", + "peak_profile", + "W", + TCHProfile, + "set_peak_profile replaced a peak profile whose Parameters are " + "used in a fit recipe: W. The recipe would still refine them, " + "but they no longer change the pattern. Please call " + "set_peak_profile before adding the peak profile's Parameters " + "to the recipe.", + ), + # C2: A user adds the instrument's zero to their recipe, then gives + # the generator an instrument they set up. + # Expected: A UserWarning names zero and says to set the instrument + # before adding its Parameters to the recipe. + ( + "set_instrument", + "instrument", + "zero", + ConstantWavelengthInstrument, + "set_instrument replaced an instrument whose Parameters are used " + "in a fit recipe: zero. The recipe would still refine them, but " + "they no longer change the pattern. Please call set_instrument " + "before adding the instrument's Parameters to the recipe.", + ), + ], +) +def test_setter_warns_recipe_variable_replaced( + pbso4_generator, + setter_name, + component_name, + parameter_name, + new_component_class, + expected_msg, +): + # Add one of the current component's Parameters to a recipe + recipe = FitRecipe() + component = getattr(pbso4_generator, component_name) + recipe.add_variable(component.get(parameter_name)) + # Replace the component + with pytest.warns(UserWarning, match=re.escape(expected_msg)): + getattr(pbso4_generator, setter_name)(new_component_class()) + + +def test_set_peak_profile_warns_constrained_parameter_replaced( + pbso4_generator, +): + # Test replacing a peak profile whose Parameter is constrained in a fit + # recipe, as when a user ties the peak width of two datasets to one + # recipe variable + # Expected: The same UserWarning as for a recipe variable, naming W, + # since the recipe would still set W, but W no longer changes the + # pattern. + # Constrain the peak profile's W to a recipe variable + recipe = FitRecipe() + recipe.create_new_variable("width", 0.01) + recipe.constrain(pbso4_generator.peak_profile.W, "width") + # Switch to a TCH profile + expected_msg = ( + "set_peak_profile replaced a peak profile whose Parameters are " + "used in a fit recipe: W. The recipe would still refine them, " + "but they no longer change the pattern. Please call " + "set_peak_profile before adding the peak profile's Parameters " + "to the recipe." + ) + with pytest.warns(UserWarning, match=re.escape(expected_msg)): + pbso4_generator.set_peak_profile(TCHProfile()) + + +def test_set_structure_warns_recipe_variable_replaced(pbso4_generator): + # Test setting a second structure after the first one's Parameters are + # in a fit recipe, as when a user reruns the setup cell of their script + # after building their recipe + # Expected: A UserWarning names a and says to set the structure before + # adding its Parameters to the recipe. + # Add the first structure's lattice parameter a to a recipe + recipe = FitRecipe() + recipe.add_variable(pbso4_generator.phase.a) + # Set a second structure + expected_msg = ( + "set_structure replaced a structure whose Parameters are used in a " + "fit recipe: a. The recipe would still refine them, but they no " + "longer change the pattern. Please call set_structure before " + "adding the structure's Parameters to the recipe." + ) + with pytest.warns(UserWarning, match=re.escape(expected_msg)): + pbso4_generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + + +@pytest.mark.parametrize( + "setter_name, new_component_class", + [ + # Test that the setters stay silent when none of the replaced + # component's Parameters is in a recipe, even with the generator in + # a FitContribution of a recipe that refines other Parameters + # C1: A user builds a recipe that refines the scale, then switches + # to a TCH profile. + # Expected: No warning. + ("set_peak_profile", TCHProfile), + # C2: A user builds a recipe that refines the scale, then gives the + # generator an instrument they set up. + # Expected: No warning. + ("set_instrument", ConstantWavelengthInstrument), + ], +) +def test_setter_without_recipe_variables( + pbso4_generator, pbso4_contribution, setter_name, new_component_class +): + # Put the contribution in a recipe that refines only the scale + recipe = FitRecipe() + recipe.add_contribution(pbso4_contribution) + recipe.add_variable(pbso4_generator.scale) + # Replace the component, with warnings turned into errors + with warnings.catch_warnings(): + warnings.simplefilter("error", UserWarning) + getattr(pbso4_generator, setter_name)(new_component_class()) + + +def test_set_structure_without_recipe_variables( + pbso4_generator, pbso4_contribution +): + # Test that set_structure stays silent when none of the replaced + # structure's Parameters is in a recipe + # Expected: After a user builds a recipe that refines the scale and + # then sets a second structure, no warning. + # Put the contribution in a recipe that refines only the scale + recipe = FitRecipe() + recipe.add_contribution(pbso4_contribution) + recipe.add_variable(pbso4_generator.scale) + # Set a second structure, with warnings turned into errors + with warnings.catch_warnings(): + warnings.simplefilter("error", UserWarning) + pbso4_generator.set_structure(create_crystal_from_cif(str(PBSO4_CIF))) + + +def test_print_info(pbso4_generator, capsys): + # Test printing what a fully set up generator holds, as a user does to + # check their setup before refining + # Expected: The scale, then the structure, the instrument and the peak + # profile under the names their Parameters sit under. The structure + # shows its space group and lattice parameters, with the angles in + # degrees. Each component shows its type, then its settings, if it + # has any, on one line, then its Parameter values. + pbso4_generator.print_info() + actual_info = capsys.readouterr().out + expected_info = ( + "PowderDiffractionGenerator 'bragg'\n" + " scale = 1\n" + " phase: Crystal 'O4 Pb S', space group P n m a\n" + " a = 8.48\n" + " b = 5.398\n" + " c = 6.958\n" + " alpha = 90\n" + " beta = 90\n" + " gamma = 90\n" + " instrument: ConstantWavelengthInstrument\n" + " settings: radiation_type = X, polarization_rate = 0\n" + " zero = 0\n" + " displacement = 0\n" + " transparency = 0\n" + " mu_r = 0\n" + " wavelength = 1.5406\n" + " peak_profile: PseudoVoigtProfile\n" + " U = 0\n" + " V = 0\n" + " W = 0.01\n" + " eta0 = 0.5\n" + " eta1 = 0\n" + " asym0 = 1\n" + " asym1 = 0\n" + " asym2 = 0\n" + ) + assert actual_info == expected_info + + +def test_print_info_before_setup_is_complete(capsys): + # Test printing what a generator holds while a user is still setting it + # up + # Expected: The structure and the peak profile, which aren't set, show + # "not set", and so does the wavelength of the instrument, which has no + # wavelength yet. + # Set only an instrument, without a wavelength + generator = PowderDiffractionGenerator() + generator.set_instrument(ConstantWavelengthInstrument()) + generator.print_info() + actual_info = capsys.readouterr().out + expected_info = ( + "PowderDiffractionGenerator 'bragg'\n" + " scale = 1\n" + " phase: not set\n" + " instrument: ConstantWavelengthInstrument\n" + " settings: radiation_type = X, polarization_rate = 0\n" + " zero = 0\n" + " displacement = 0\n" + " transparency = 0\n" + " mu_r = 0\n" + " wavelength = not set\n" + " peak_profile: not set\n" + ) + assert actual_info == expected_info diff --git a/tests/test_refinement.py b/tests/test_refinement.py new file mode 100644 index 0000000..77a1d0c --- /dev/null +++ b/tests/test_refinement.py @@ -0,0 +1,172 @@ +import numpy as np +import pytest +from conftest import PBSO4_CIF, TESTDATA_DIR +from scipy.optimize import least_squares + +from diffpy.cmirietveld import ( + ChebyshevBackground, + ConstantWavelengthInstrument, + GSASDataParser, + PowderDiffractionGenerator, + PseudoVoigtProfile, +) +from diffpy.srfit.fitbase import ( + FitContribution, + FitRecipe, + FitResults, + Profile, +) + +RW_MARGIN = 0.01 + + +class PbSO4XrayRefinement: + """The PbSO4 lab x-ray refinement, set up as in the simple example. + + The data are 16 to 110 degrees of ``PBSO4.XRA`` with a Cu tube and + ObjCryst's polarization rate 0.4, GSAS-II's polarization of 0.7. The + background is fitted to the data first. Every variable is fixed and + tagged by the stage that frees it. + """ + + def __init__(self): + parser = GSASDataParser() + parser.parse_file(TESTDATA_DIR / "PBSO4.XRA") + profile = Profile() + profile.load_parsed_data(parser) + profile.set_calculation_range(xmin=16.0, xmax=110.0) + self.background = ChebyshevBackground( + "background", x_range=(16.0, 110.0) + ) + self.background.fit_to_data(profile.x, profile.y) + instrument = ConstantWavelengthInstrument() + instrument.set_xray_source("Cu") + instrument.set_polarization(0.4) + self.generator = PowderDiffractionGenerator("bragg") + self.generator.set_structure(PBSO4_CIF, name="PbSO4") + self.generator.set_instrument(instrument) + self.generator.set_peak_profile(PseudoVoigtProfile()) + contribution = FitContribution("pbso4") + contribution.set_profile(profile, xname="tth") + contribution.add_profile_generator(self.generator) + contribution.register_calculator(self.background, argnames=["tth"]) + contribution.set_equation("bragg + background") + self.recipe = FitRecipe() + self.recipe.add_contribution(contribution) + self.recipe.add_variable(self.generator.scale, 1e-5, tags=["scale"]) + for coefficient in self.background.coefficients: + self.recipe.add_variable(coefficient, tags=["background"]) + phase = self.generator.phase + for parameter in phase.sgpars.latpars: + self.recipe.add_variable(parameter, tags=["cell"]) + self.recipe.add_variable( + instrument.displacement, tags=["displacement"] + ) + for name in ["U", "V", "W", "eta0", "asym0", "asym1"]: + self.recipe.add_variable( + self.generator.peak_profile.get(name), tags=["profile"] + ) + for parameter in phase.sgpars.xyzpars: + self.recipe.add_variable(parameter, tags=["atoms"]) + for scatterer in phase.get_scatterers(): + self.recipe.add_variable( + scatterer.Biso, + 0.8, + name=f"Biso_{scatterer.name}", + tags=["atoms"], + ) + self.recipe.fix("all") + + def refine_in_stages(self, stages): + for tags in stages: + self.recipe.free(*tags) + least_squares( + self.recipe.residual, + self.recipe.get_values(), + x_scale="jac", + bounds=self.recipe.get_bounds_array(), + ) + + def get_rw(self): + rw = FitResults(self.recipe).rw + return rw + + def get_cell(self): + cell = [] + for name in ["a", "b", "c"]: + cell.append(self.generator.phase.get(name).value) + return cell + + +@pytest.mark.parametrize( + "stages, gsasii_rwp, gsasii_cell, cell_tolerance", + [ + # Test that a staged PbSO4 x-ray refinement approaches GSAS-II's + # reference refinement of the same data, stage by stage. Each case + # refines from the start up to its stage. The peak shapes differ + # from GSAS-II's, so Rw is only checked not to exceed GSAS-II's Rwp + # by more than 0.01, and refined values change slightly from run to + # run (ObjCryst sums |F| in pointer order), so later stages get + # wider cell tolerances. + # C1: A user refines the scale and the background. + # Expected: Rw is at most GSAS-II's 0.458 plus 0.01, and the cell + # is still the CIF's. + ( + [["scale", "background"]], + 0.45812, + [8.48000, 5.39800, 6.95800], + 1e-9, + ), + # C2: A user then refines the cell. + # Expected: Rw is at most GSAS-II's 0.321 plus 0.01, and the cell + # is within 0.001 angstroms of GSAS-II's. + ( + [["scale", "background"], ["cell"]], + 0.32066, + [8.48578, 5.40226, 6.96410], + 0.001, + ), + # C3: A user then refines the sample displacement. + # Expected: Rw is at most GSAS-II's 0.274 plus 0.01, and the cell + # is within 0.001 angstroms of GSAS-II's. + ( + [["scale", "background"], ["cell"], ["displacement"]], + 0.27398, + [8.48144, 5.39943, 6.96057], + 0.001, + ), + # C4: A user then refines the peak profile. + # Expected: Rw is at most GSAS-II's 0.135 plus 0.01, and the cell + # is within 0.003 angstroms of GSAS-II's. + ( + [["scale", "background"], ["cell"], ["displacement"], ["profile"]], + 0.13462, + [8.47997, 5.39854, 6.95982], + 0.003, + ), + # C5: A user then refines the atom positions and ADPs. + # Expected: Rw is at most GSAS-II's 0.101 plus 0.01, and the cell + # is within 0.003 angstroms of GSAS-II's. + ( + [ + ["scale", "background"], + ["cell"], + ["displacement"], + ["profile"], + ["atoms"], + ], + 0.10070, + [8.48035, 5.39862, 6.96002], + 0.003, + ), + ], +) +def test_refine_in_stages(stages, gsasii_rwp, gsasii_cell, cell_tolerance): + refinement = PbSO4XrayRefinement() + refinement.refine_in_stages(stages) + actual_rw = refinement.get_rw() + expected_max_rw = gsasii_rwp + RW_MARGIN + actual_cell = refinement.get_cell() + expected_cell = gsasii_cell + assert actual_rw <= expected_max_rw + assert np.allclose(actual_cell, expected_cell, atol=cell_tolerance) diff --git a/tests/testdata/README.rst b/tests/testdata/README.rst index a103367..5b9659d 100644 --- a/tests/testdata/README.rst +++ b/tests/testdata/README.rst @@ -10,6 +10,10 @@ Laboratory. All rights reserved. This product includes software produced by UChicago Argonne, LLC under Contract No. DE-AC02-06CH11357 with the Department of Energy. +``pbso4.stru`` is ``PbSO4-Wyckoff.cif`` written in the PDFfit format by +diffpy.structure (``load_structure(...).write(..., "pdffit")``), with trailing +whitespace removed. + ``pbso4-esd.xra`` is ``PBSO4.XRA`` written in the GSAS ESD format, with the square root of each count as its uncertainty, to three decimals. Each line holds five (count, uncertainty) pairs of eight characters, and the last line diff --git a/tests/testdata/pbso4.stru b/tests/testdata/pbso4.stru new file mode 100644 index 0000000..be85039 --- /dev/null +++ b/tests/testdata/pbso4.stru @@ -0,0 +1,153 @@ +title +format pdffit +scale 1.000000 +sharp 0.000000, 0.000000, 1.000000, 0.000000 +spcgr P1 +cell 8.480000, 5.398000, 6.958000, 90.000000, 90.000000, 90.000000 +dcell 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000 +ncell 1, 1, 1, 24 +atoms +PB 0.18820000 0.25000000 0.16700000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +PB 0.31180000 0.75000000 0.66700000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +PB 0.81180000 0.75000000 0.83300000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +PB 0.68820000 0.25000000 0.33300000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +S 0.06300000 0.25000000 0.68600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +S 0.43700000 0.75000000 0.18600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +S 0.93700000 0.75000000 0.31400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +S 0.56300000 0.25000000 0.81400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.90500000 0.25000000 0.60000000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.59500000 0.75000000 0.10000000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.09500000 0.75000000 0.40000000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.40500000 0.25000000 0.90000000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.18100000 0.25000000 0.54300000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.31900000 0.75000000 0.04300000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.81900000 0.75000000 0.45700000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.68100000 0.25000000 0.95700000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.08500000 0.02600000 0.80600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.41500000 0.52600000 0.30600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.08500000 0.47400000 0.80600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.41500000 0.97400000 0.30600000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.91500000 0.97400000 0.19400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.58500000 0.47400000 0.69400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.91500000 0.52600000 0.19400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 +O 0.58500000 0.02600000 0.69400000 1.0000 + 0.00000000 0.00000000 0.00000000 0.0000 + 0.01000000 0.01000000 0.01000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000 + 0.00000000 0.00000000 0.00000000