Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-news-item.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/tests-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
131 changes: 131 additions & 0 deletions docs/examples/refine_pbso4_xray.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions docs/source/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <package_name>-env python=3.14 \
conda create -n <package_name>-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 <package_name>-env python=3.14 \
conda create -n <package_name>-env python=3.13 \
--file requirements/tests.txt \
--file requirements/conda.txt \
--file requirements/build.txt
Expand Down
26 changes: 26 additions & 0 deletions news/powder-diffraction-generator.rst
Original file line number Diff line number Diff line change
@@ -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:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
]
Expand Down
3 changes: 3 additions & 0 deletions requirements/conda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ diffpy.srfit
libobjcryst>=2026.3.0
libboost-devel
libboost-python
matplotlib-base
bg-mpl-stylesheets
pybaselines
4 changes: 4 additions & 0 deletions requirements/pip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
numpy
diffpy.srfit
diffpy.cmistructure
pyobjcryst
matplotlib
bg-mpl-stylesheets
pybaselines
63 changes: 61 additions & 2 deletions src/diffpy/cmirietveld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,78 @@
# (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
#
# 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

Expand Down
Loading
Loading