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
23 changes: 23 additions & 0 deletions news/load-data-pdfparser.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Read the PDF metadata in the data file header in ``PDFContribution.load_data``, which used the generic ``ProfileParser`` and lost the scattering type and qmax from NOMAD files with a free-text instrument comment.

**Security:**

* <news item>
14 changes: 9 additions & 5 deletions src/diffpy/cmipdf/pdfcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from diffpy.cmipdf.debyepdfgenerator import DebyePDFGenerator
from diffpy.cmipdf.pdfgenerator import PDFGenerator
from diffpy.srfit.fitbase import FitContribution, Profile, ProfileParser
from diffpy.cmipdf.pdfparser import PDFParser
from diffpy.srfit.fitbase import FitContribution, Profile


class PDFContribution(FitContribution):
Expand Down Expand Up @@ -109,14 +110,17 @@ def __init__(self, name):
# Data methods

def load_data(self, datafile):
"""Load the data from a datafile.
"""Load the data from a data file.

This uses the PDFParser to load the data and then passes it to
the built-in profile with load_parsed_data.

Parameters
----------
data : str or Path
The path to the data file.
datafile : str or Path
The path to the file that contains the data.
"""
parser = ProfileParser()
parser = PDFParser()
parser.parse_file(datafile)

# Pass it to the profile
Expand Down
23 changes: 23 additions & 0 deletions tests/test_pdfcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ def test_get_qmax_from_profile():
assert actual_qmax == expected_qmax


# load_data reads the PDF metadata in the data file header, so the
# contribution takes its scattering type and qmax from the file.
@pytest.mark.parametrize(
"input_filename, expected_settings",
[
# C1: The header holds PDFgetX3 "name = value" lines.
# Expected: The scattering type "X" and qmax 27 from the header.
("si-q27r60-xray.gr", ("X", 27.0)),
# C2: The header holds a free-text NOMAD instrument comment.
# Expected: The scattering type "N" and qmax 31.414 from the comment.
("nom-mno-neutron.gr", ("N", 31.414)),
],
)
def test_load_data_reads_metadata(datafile, input_filename, expected_settings):
contribution = PDFContribution("pdf")
contribution.load_data(datafile(input_filename))
actual_settings = (
contribution.get_scattering_type(),
contribution.get_qmax(),
)
assert actual_settings == expected_settings


def test_savetxt(datafile):
# C1: A contribution with data on r = 0 to 10 is evaluated and saved.
# Expected: The file has a header line and one line for each of the
Expand Down
Loading