Skip to content

Commit 7772e69

Browse files
authored
Merge pull request #89 from scipp/write-cif
Add a CIF writer
2 parents c4ee9b4 + 8380d27 commit 7772e69

File tree

14 files changed

+229
-47
lines changed

14 files changed

+229
-47
lines changed

docs/user-guide/dream/dream-data-reduction.ipynb

+53-25
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"outputs": [],
2121
"source": [
2222
"import scipp as sc\n",
23-
"import scippneutron as scn\n",
24-
"import scippneutron.io\n",
23+
"from scippneutron.io import cif\n",
2524
"\n",
2625
"from ess import dream, powder\n",
2726
"import ess.dream.data # noqa: F401\n",
@@ -87,6 +86,31 @@
8786
"cell_type": "markdown",
8887
"id": "6",
8988
"metadata": {},
89+
"source": [
90+
"We also need some parameters to configure the output file:"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"id": "7",
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"workflow[CIFAuthors] = CIFAuthors([\n",
101+
" cif.Author(\n",
102+
" name=\"Jane Doe\",\n",
103+
" email=\"[email protected]\",\n",
104+
" orcid=\"0000-0000-0000-0001\",\n",
105+
" role=\"measurement\",\n",
106+
" ),\n",
107+
"])"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"id": "8",
113+
"metadata": {},
90114
"source": [
91115
"## Use the workflow\n",
92116
"\n",
@@ -96,7 +120,7 @@
96120
{
97121
"cell_type": "code",
98122
"execution_count": null,
99-
"id": "7",
123+
"id": "9",
100124
"metadata": {},
101125
"outputs": [],
102126
"source": [
@@ -105,27 +129,29 @@
105129
},
106130
{
107131
"cell_type": "markdown",
108-
"id": "8",
132+
"id": "10",
109133
"metadata": {},
110134
"source": [
111-
"We then call `compute()` to compute the result:"
135+
"We then call `compute()` to compute the result:\n",
136+
"(The `cif` object will later be used to write the result to disk.)"
112137
]
113138
},
114139
{
115140
"cell_type": "code",
116141
"execution_count": null,
117-
"id": "9",
142+
"id": "11",
118143
"metadata": {},
119144
"outputs": [],
120145
"source": [
121-
"result = workflow.compute(IofDspacing)\n",
122-
"result"
146+
"results = workflow.compute([IofDspacing, ReducedDspacingCIF])\n",
147+
"result = results[IofDspacing]\n",
148+
"cif_data = results[ReducedDspacingCIF]"
123149
]
124150
},
125151
{
126152
"cell_type": "code",
127153
"execution_count": null,
128-
"id": "10",
154+
"id": "12",
129155
"metadata": {},
130156
"outputs": [],
131157
"source": [
@@ -135,28 +161,30 @@
135161
},
136162
{
137163
"cell_type": "markdown",
138-
"id": "11",
164+
"id": "13",
139165
"metadata": {},
140166
"source": [
141-
"We can now save the result to disk:"
167+
"We can now save the result to disk:\n",
168+
"(The comment is optional but helps to identify the file later.)"
142169
]
143170
},
144171
{
145172
"cell_type": "code",
146173
"execution_count": null,
147-
"id": "12",
174+
"id": "14",
148175
"metadata": {},
149176
"outputs": [],
150177
"source": [
151-
"dspacing_histogram.coords[\"dspacing\"] = sc.midpoints(\n",
152-
" dspacing_histogram.coords[\"dspacing\"]\n",
153-
")\n",
154-
"scn.io.save_xye(\"dspacing.xye\", dspacing_histogram)"
178+
"cif_data.comment = \"\"\"This file was generated with the DREAM data reduction user guide\n",
179+
"in the documentation of ESSdiffraction.\n",
180+
"See https://scipp.github.io/essdiffraction/\n",
181+
"\"\"\"\n",
182+
"cif_data.save('dspacing.cif')"
155183
]
156184
},
157185
{
158186
"cell_type": "markdown",
159-
"id": "13",
187+
"id": "15",
160188
"metadata": {},
161189
"source": [
162190
"## Compute intermediate results\n",
@@ -169,7 +197,7 @@
169197
{
170198
"cell_type": "code",
171199
"execution_count": null,
172-
"id": "14",
200+
"id": "16",
173201
"metadata": {},
174202
"outputs": [],
175203
"source": [
@@ -186,7 +214,7 @@
186214
{
187215
"cell_type": "code",
188216
"execution_count": null,
189-
"id": "15",
217+
"id": "17",
190218
"metadata": {},
191219
"outputs": [],
192220
"source": [
@@ -198,7 +226,7 @@
198226
},
199227
{
200228
"cell_type": "markdown",
201-
"id": "16",
229+
"id": "18",
202230
"metadata": {},
203231
"source": [
204232
"## Grouping by scattering angle\n",
@@ -210,7 +238,7 @@
210238
{
211239
"cell_type": "code",
212240
"execution_count": null,
213-
"id": "17",
241+
"id": "19",
214242
"metadata": {},
215243
"outputs": [],
216244
"source": [
@@ -222,7 +250,7 @@
222250
{
223251
"cell_type": "code",
224252
"execution_count": null,
225-
"id": "18",
253+
"id": "20",
226254
"metadata": {},
227255
"outputs": [],
228256
"source": [
@@ -233,7 +261,7 @@
233261
{
234262
"cell_type": "code",
235263
"execution_count": null,
236-
"id": "19",
264+
"id": "21",
237265
"metadata": {},
238266
"outputs": [],
239267
"source": [
@@ -251,7 +279,7 @@
251279
{
252280
"cell_type": "code",
253281
"execution_count": null,
254-
"id": "20",
282+
"id": "22",
255283
"metadata": {},
256284
"outputs": [],
257285
"source": [
@@ -275,7 +303,7 @@
275303
"name": "python",
276304
"nbconvert_exporter": "python",
277305
"pygments_lexer": "ipython3",
278-
"version": "3.10.14"
306+
"version": "3.10.13"
279307
}
280308
},
281309
"nbformat": 4,

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies = [
3737
"plopp",
3838
"pythreejs",
3939
"sciline>=24.06.0",
40-
"scipp>=24.09.1", # Fixed new hist/bin API
41-
"scippneutron>=24.5.0",
40+
"scipp>=24.09.1",
41+
"scippneutron>=24.9.0",
4242
"scippnexus>=23.12.0",
4343
]
4444

requirements/base.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ plopp
1010
pythreejs
1111
sciline>=24.06.0
1212
scipp>=24.09.1
13-
scippneutron>=24.5.0
13+
scippneutron>=24.9.0
1414
scippnexus>=23.12.0

requirements/base.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SHA1:6ce1bbccfc85ced70dedc62a2266b339f1e7c814
1+
# SHA1:2cad3df672e12033fa21254667278ceafae15e09
22
#
33
# This file is autogenerated by pip-compile-multi
44
# To update, run:

requirements/ci.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ platformdirs==4.3.6
3838
# virtualenv
3939
pluggy==1.5.0
4040
# via tox
41-
pyproject-api==1.7.2
41+
pyproject-api==1.8.0
4242
# via tox
4343
requests==2.32.3
4444
# via -r ci.in
@@ -48,7 +48,7 @@ tomli==2.0.1
4848
# via
4949
# pyproject-api
5050
# tox
51-
tox==4.19.0
51+
tox==4.20.0
5252
# via -r ci.in
5353
urllib3==2.2.3
5454
# via requests

requirements/docs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ sphinx==8.0.2
160160
# sphinx-copybutton
161161
# sphinx-design
162162
# sphinxcontrib-bibtex
163-
sphinx-autodoc-typehints==2.4.3
163+
sphinx-autodoc-typehints==2.4.4
164164
# via -r docs.in
165165
sphinx-copybutton==0.5.2
166166
# via -r docs.in

src/ess/dream/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
from .workflow import DreamGeant4Workflow, default_parameters
1313

1414
try:
15-
__version__ = importlib.metadata.version(__package__ or __name__)
15+
__version__ = importlib.metadata.version("essdiffraction")
1616
except importlib.metadata.PackageNotFoundError:
1717
__version__ = "0.0.0"
1818

1919
del importlib
2020

2121
__all__ = [
22+
'__version__',
2223
'DreamGeant4Workflow',
2324
'default_parameters',
24-
'beamline',
2525
'instrument_view',
2626
'load_geant4_csv',
2727
'nexus',
28-
'providers',
2928
]

src/ess/dream/io/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
from . import nexus
77
from .geant4 import load_geant4_csv
8+
from .cif import prepare_reduced_dspacing_cif
89

9-
__all__ = ["nexus", "load_geant4_csv"]
10+
providers = (prepare_reduced_dspacing_cif,)
11+
12+
__all__ = ["nexus", "load_geant4_csv", "prepare_reduced_dspacing_cif", "providers"]

src/ess/dream/io/cif.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
4+
"""CIF writer for DREAM."""
5+
6+
import scipp as sc
7+
from scippneutron.io import cif
8+
9+
from ess.powder.types import CIFAuthors, IofDspacing, ReducedDspacingCIF
10+
11+
12+
def prepare_reduced_dspacing_cif(
13+
da: IofDspacing, *, authors: CIFAuthors
14+
) -> ReducedDspacingCIF:
15+
"""Construct a CIF builder with reduced data in d-spacing.
16+
17+
The object contains the d-spacing coordinate, intensities,
18+
and some metadata.
19+
20+
Parameters
21+
----------
22+
da:
23+
Reduced 1d data with a `'dspacing'` dimension and coordinate.
24+
authors:
25+
List of authors to write to the file.
26+
27+
Returns
28+
-------
29+
:
30+
An object that contains the reduced data and metadata.
31+
Us its ``save`` method to write the CIF file.
32+
"""
33+
from .. import __version__
34+
35+
to_save = _prepare_data(da)
36+
return ReducedDspacingCIF(
37+
cif.CIF('reduced_dspacing')
38+
.with_reducers(f'ess.dream v{__version__}')
39+
.with_authors(*authors)
40+
.with_beamline(beamline='DREAM', facility='ESS')
41+
.with_reduced_powder_data(to_save)
42+
)
43+
44+
45+
def _prepare_data(da: sc.DataArray) -> sc.DataArray:
46+
hist = da.copy(deep=False) if da.bins is None else da.hist()
47+
hist.coords[hist.dim] = sc.midpoints(hist.coords[hist.dim])
48+
return hist

src/ess/dream/workflow.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
33

4+
import itertools
5+
46
import sciline
57
import scipp as sc
68

@@ -13,8 +15,11 @@
1315
VanadiumRun,
1416
)
1517

18+
from .io.cif import CIFAuthors, prepare_reduced_dspacing_cif
1619
from .io.geant4 import LoadGeant4Workflow
1720

21+
_dream_providers = (prepare_reduced_dspacing_cif,)
22+
1823

1924
def default_parameters() -> dict:
2025
# Quantities not available in the simulated data
@@ -28,6 +33,7 @@ def default_parameters() -> dict:
2833
NeXusSource[VanadiumRun]: source,
2934
AccumulatedProtonCharge[SampleRun]: charge,
3035
AccumulatedProtonCharge[VanadiumRun]: charge,
36+
CIFAuthors: CIFAuthors([]),
3137
}
3238

3339

@@ -36,7 +42,7 @@ def DreamGeant4Workflow() -> sciline.Pipeline:
3642
Workflow with default parameters for the Dream Geant4 simulation.
3743
"""
3844
wf = LoadGeant4Workflow()
39-
for provider in powder_providers:
45+
for provider in itertools.chain(powder_providers, _dream_providers):
4046
wf.insert(provider)
4147
for key, value in default_parameters().items():
4248
wf[key] = value

src/ess/powder/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .masking import with_pixel_mask_filenames
1919

2020
try:
21-
__version__ = importlib.metadata.version(__package__ or __name__)
21+
__version__ = importlib.metadata.version("essdiffraction")
2222
except importlib.metadata.PackageNotFoundError:
2323
__version__ = "0.0.0"
2424

@@ -35,12 +35,12 @@
3535
"""Sciline providers for powder diffraction."""
3636

3737
__all__ = [
38+
"__version__",
3839
"conversion",
3940
"correction",
4041
"filtering",
4142
"grouping",
4243
"masking",
43-
"nexus",
4444
"transform",
4545
"providers",
4646
"smoothing",

0 commit comments

Comments
 (0)