-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from scipp/write-cif
Add a CIF writer
- Loading branch information
Showing
14 changed files
with
229 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,7 @@ | |
"outputs": [], | ||
"source": [ | ||
"import scipp as sc\n", | ||
"import scippneutron as scn\n", | ||
"import scippneutron.io\n", | ||
"from scippneutron.io import cif\n", | ||
"\n", | ||
"from ess import dream, powder\n", | ||
"import ess.dream.data # noqa: F401\n", | ||
|
@@ -87,6 +86,31 @@ | |
"cell_type": "markdown", | ||
"id": "6", | ||
"metadata": {}, | ||
"source": [ | ||
"We also need some parameters to configure the output file:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"workflow[CIFAuthors] = CIFAuthors([\n", | ||
" cif.Author(\n", | ||
" name=\"Jane Doe\",\n", | ||
" email=\"[email protected]\",\n", | ||
" orcid=\"0000-0000-0000-0001\",\n", | ||
" role=\"measurement\",\n", | ||
" ),\n", | ||
"])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8", | ||
"metadata": {}, | ||
"source": [ | ||
"## Use the workflow\n", | ||
"\n", | ||
|
@@ -96,7 +120,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7", | ||
"id": "9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -105,27 +129,29 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8", | ||
"id": "10", | ||
"metadata": {}, | ||
"source": [ | ||
"We then call `compute()` to compute the result:" | ||
"We then call `compute()` to compute the result:\n", | ||
"(The `cif` object will later be used to write the result to disk.)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9", | ||
"id": "11", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"result = workflow.compute(IofDspacing)\n", | ||
"result" | ||
"results = workflow.compute([IofDspacing, ReducedDspacingCIF])\n", | ||
"result = results[IofDspacing]\n", | ||
"cif_data = results[ReducedDspacingCIF]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "10", | ||
"id": "12", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -135,28 +161,30 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "11", | ||
"id": "13", | ||
"metadata": {}, | ||
"source": [ | ||
"We can now save the result to disk:" | ||
"We can now save the result to disk:\n", | ||
"(The comment is optional but helps to identify the file later.)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "12", | ||
"id": "14", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dspacing_histogram.coords[\"dspacing\"] = sc.midpoints(\n", | ||
" dspacing_histogram.coords[\"dspacing\"]\n", | ||
")\n", | ||
"scn.io.save_xye(\"dspacing.xye\", dspacing_histogram)" | ||
"cif_data.comment = \"\"\"This file was generated with the DREAM data reduction user guide\n", | ||
"in the documentation of ESSdiffraction.\n", | ||
"See https://scipp.github.io/essdiffraction/\n", | ||
"\"\"\"\n", | ||
"cif_data.save('dspacing.cif')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "13", | ||
"id": "15", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute intermediate results\n", | ||
|
@@ -169,7 +197,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "14", | ||
"id": "16", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -186,7 +214,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "15", | ||
"id": "17", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -198,7 +226,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "16", | ||
"id": "18", | ||
"metadata": {}, | ||
"source": [ | ||
"## Grouping by scattering angle\n", | ||
|
@@ -210,7 +238,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "17", | ||
"id": "19", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -222,7 +250,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "18", | ||
"id": "20", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -233,7 +261,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "19", | ||
"id": "21", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -251,7 +279,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "20", | ||
"id": "22", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -275,7 +303,7 @@ | |
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.14" | ||
"version": "3.10.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ plopp | |
pythreejs | ||
sciline>=24.06.0 | ||
scipp>=24.09.1 | ||
scippneutron>=24.5.0 | ||
scippneutron>=24.9.0 | ||
scippnexus>=23.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) | ||
|
||
"""CIF writer for DREAM.""" | ||
|
||
import scipp as sc | ||
from scippneutron.io import cif | ||
|
||
from ess.powder.types import CIFAuthors, IofDspacing, ReducedDspacingCIF | ||
|
||
|
||
def prepare_reduced_dspacing_cif( | ||
da: IofDspacing, *, authors: CIFAuthors | ||
) -> ReducedDspacingCIF: | ||
"""Construct a CIF builder with reduced data in d-spacing. | ||
The object contains the d-spacing coordinate, intensities, | ||
and some metadata. | ||
Parameters | ||
---------- | ||
da: | ||
Reduced 1d data with a `'dspacing'` dimension and coordinate. | ||
authors: | ||
List of authors to write to the file. | ||
Returns | ||
------- | ||
: | ||
An object that contains the reduced data and metadata. | ||
Us its ``save`` method to write the CIF file. | ||
""" | ||
from .. import __version__ | ||
|
||
to_save = _prepare_data(da) | ||
return ReducedDspacingCIF( | ||
cif.CIF('reduced_dspacing') | ||
.with_reducers(f'ess.dream v{__version__}') | ||
.with_authors(*authors) | ||
.with_beamline(beamline='DREAM', facility='ESS') | ||
.with_reduced_powder_data(to_save) | ||
) | ||
|
||
|
||
def _prepare_data(da: sc.DataArray) -> sc.DataArray: | ||
hist = da.copy(deep=False) if da.bins is None else da.hist() | ||
hist.coords[hist.dim] = sc.midpoints(hist.coords[hist.dim]) | ||
return hist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.