Skip to content

Commit fe89263

Browse files
jokasimrjl-wynen
andauthored
feat: expose NC options in peak reader (#234)
* feat: expose NC options in peak reader * Update src/ess/diffraction/peaks.py Co-authored-by: Jan-Lukas Wynen <jan-lukas.wynen@ess.eu> * docs: add example * fix * fix * fix --------- Co-authored-by: Jan-Lukas Wynen <jan-lukas.wynen@ess.eu>
1 parent 199db57 commit fe89263

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

src/ess/beer/peakfinding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from ..diffraction.peaks import dspacing_peak_positions_from_cif
1+
from ..diffraction.peaks import dspacing_peaks_from_cif
22
from .types import CIFIdentifierForPeakPositions, CIFPeaksMinIntensity, DHKLList
33

44

55
def dhkl_peaks_from_cif(
66
cif: CIFIdentifierForPeakPositions, intensity_threshold: CIFPeaksMinIntensity
77
) -> DHKLList:
88
'''Gets the list of expected peak positions from a CIF file/identifier.'''
9-
return dspacing_peak_positions_from_cif(cif, intensity_threshold)
9+
return dspacing_peaks_from_cif(cif, intensity_threshold).coords['dspacing']

src/ess/diffraction/peaks.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import scipp as sc
33

44

5-
def dspacing_peak_positions_from_cif(cif, intensity_threshold=None) -> sc.Variable:
5+
def dspacing_peaks_from_cif(cif, intensity_threshold=None, **kwargs) -> sc.DataArray:
66
"""
7-
Retrieves a list of the peak positions for the given material.
7+
Retrieves a data array representing the bragg peaks of the given material.
88
99
The material is represented by a cif file or a codid or similar.
1010
The number of peaks retrieved can be controlled by setting the intensity
@@ -27,20 +27,58 @@ def dspacing_peak_positions_from_cif(cif, intensity_threshold=None) -> sc.Variab
2727
times the multiplicity of the peak.
2828
The ``intensity_threshold`` must be convertible to unit ``barn``.
2929
30+
kwargs:
31+
Can be anything that :py:`NCrystal.NCMATComposer.from_cif` supports.
32+
For example: ``uiso_temperature`` or ``override_spacegroup``.
33+
3034
Returns
3135
------------
32-
Array containing the peak positions in `dspacing`, with unit ``angstrom``.
33-
"""
34-
info = NC.NCMATComposer.from_cif(cif).load('comp=bragg').info
36+
Data array representing peak amplitudes and peak positions in ``dspacing``.
37+
The full NCrystal information object is added as a coordinate with the name ``info``.
38+
The input arguments are added as scalar coordinates.
39+
40+
Examples
41+
--------
42+
>>> from ess.diffraction.peaks import dspacing_peaks_from_cif
43+
>>> dspacing_peaks_from_cif(
44+
... 'codid::9008460',
45+
... uiso_temperature=400,
46+
... override_spacegroup='F d -3 m:1',
47+
... )
48+
<scipp.DataArray>
49+
Dimensions: Sizes[peaks:162, ]
50+
Coordinates:
51+
* cif string <no unit> () "codid::9008460"
52+
* dspacing float64 [Å] (peaks) [2.33803, 2.02479, ..., 0.243756, 0.243756]
53+
* info PyObject <no unit> () <NCrystal.core.Info object at ...>
54+
* override_spacegroup string <no unit> () "F d -3 m:1"
55+
* uiso_temperature int64 [dimensionless] () 400
56+
Data:
57+
float64 [barn] (peaks) [13.3631, 9.59562, ..., 0.000556426, 0.000556426]
58+
59+
""" # noqa: E501
60+
info = NC.NCMATComposer.from_cif(cif, **kwargs).load('comp=bragg').info
3561
min_intensity = (
3662
intensity_threshold.to(unit='barn').value
3763
if intensity_threshold is not None
3864
else 0
3965
)
40-
return sc.array(
41-
dims=['peaks'],
42-
values=[
43-
hkl.d for hkl in info.hklObjects() if (hkl.f2 * hkl.mult) >= min_intensity
44-
],
66+
dims = ['peaks']
67+
peaks = [hkl for hkl in info.hklObjects() if (hkl.f2 * hkl.mult) >= min_intensity]
68+
dspacing = sc.array(
69+
dims=dims,
70+
values=[hkl.d for hkl in peaks],
4571
unit='angstrom',
4672
)
73+
out = sc.DataArray(
74+
sc.array(dims=dims, values=[hkl.f2 * hkl.mult for hkl in peaks], unit='barn'),
75+
coords={
76+
'dspacing': dspacing,
77+
'cif': sc.scalar(cif),
78+
'info': sc.scalar(info),
79+
**{name: sc.scalar(value) for name, value in kwargs.items()},
80+
},
81+
)
82+
if intensity_threshold is not None:
83+
out.coords['intensity_threshold'] = intensity_threshold
84+
return out

tests/diffraction/test_peaks.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
import scipp as sc
22

3-
from ess.diffraction.peaks import dspacing_peak_positions_from_cif
3+
from ess.diffraction.peaks import dspacing_peaks_from_cif
44

55

66
def test_retreived_peak_list_has_expected_units():
7-
d = dspacing_peak_positions_from_cif(
7+
d = dspacing_peaks_from_cif(
88
'codid::9011998',
99
sc.scalar(50, unit='barn'),
1010
)
11-
assert d.unit == 'angstrom'
11+
assert d.coords['dspacing'].unit == 'angstrom'
12+
assert d.unit == 'barn'
1213

1314

1415
def test_intensity_threshold_is_taken_into_account():
15-
d = dspacing_peak_positions_from_cif(
16+
d = dspacing_peaks_from_cif(
1617
'codid::9011998',
1718
sc.scalar(50, unit='barn'),
1819
)
1920
assert len(d) > 0
2021

21-
d = dspacing_peak_positions_from_cif(
22+
d = dspacing_peaks_from_cif(
2223
'codid::9011998',
2324
sc.scalar(50, unit='Mbarn'),
2425
)
2526
assert len(d) == 0
27+
28+
29+
def test_retreived_peak_list_with_temperature_kwarg():
30+
d = dspacing_peaks_from_cif(
31+
'codid::9011998', sc.scalar(50, unit='barn'), uiso_temperature=300
32+
)
33+
assert d.coords['uiso_temperature'] == 300

0 commit comments

Comments
 (0)