Skip to content

Commit

Permalink
Merge pull request #89 from scipp/write-cif
Browse files Browse the repository at this point in the history
Add a CIF writer
  • Loading branch information
jl-wynen authored Sep 19, 2024
2 parents c4ee9b4 + 8380d27 commit 7772e69
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 47 deletions.
78 changes: 53 additions & 25 deletions docs/user-guide/dream/dream-data-reduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -96,7 +120,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"id": "9",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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": [
Expand All @@ -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",
Expand All @@ -169,7 +197,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"id": "16",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -186,7 +214,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"id": "17",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -198,7 +226,7 @@
},
{
"cell_type": "markdown",
"id": "16",
"id": "18",
"metadata": {},
"source": [
"## Grouping by scattering angle\n",
Expand All @@ -210,7 +238,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "17",
"id": "19",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -222,7 +250,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"id": "20",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -233,7 +261,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "19",
"id": "21",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -251,7 +279,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "20",
"id": "22",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -275,7 +303,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ dependencies = [
"plopp",
"pythreejs",
"sciline>=24.06.0",
"scipp>=24.09.1", # Fixed new hist/bin API
"scippneutron>=24.5.0",
"scipp>=24.09.1",
"scippneutron>=24.9.0",
"scippnexus>=23.12.0",
]

Expand Down
2 changes: 1 addition & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SHA1:6ce1bbccfc85ced70dedc62a2266b339f1e7c814
# SHA1:2cad3df672e12033fa21254667278ceafae15e09
#
# This file is autogenerated by pip-compile-multi
# To update, run:
Expand Down
4 changes: 2 additions & 2 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ platformdirs==4.3.6
# virtualenv
pluggy==1.5.0
# via tox
pyproject-api==1.7.2
pyproject-api==1.8.0
# via tox
requests==2.32.3
# via -r ci.in
Expand All @@ -48,7 +48,7 @@ tomli==2.0.1
# via
# pyproject-api
# tox
tox==4.19.0
tox==4.20.0
# via -r ci.in
urllib3==2.2.3
# via requests
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ sphinx==8.0.2
# sphinx-copybutton
# sphinx-design
# sphinxcontrib-bibtex
sphinx-autodoc-typehints==2.4.3
sphinx-autodoc-typehints==2.4.4
# via -r docs.in
sphinx-copybutton==0.5.2
# via -r docs.in
Expand Down
5 changes: 2 additions & 3 deletions src/ess/dream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
from .workflow import DreamGeant4Workflow, default_parameters

try:
__version__ = importlib.metadata.version(__package__ or __name__)
__version__ = importlib.metadata.version("essdiffraction")
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

del importlib

__all__ = [
'__version__',
'DreamGeant4Workflow',
'default_parameters',
'beamline',
'instrument_view',
'load_geant4_csv',
'nexus',
'providers',
]
5 changes: 4 additions & 1 deletion src/ess/dream/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@

from . import nexus
from .geant4 import load_geant4_csv
from .cif import prepare_reduced_dspacing_cif

__all__ = ["nexus", "load_geant4_csv"]
providers = (prepare_reduced_dspacing_cif,)

__all__ = ["nexus", "load_geant4_csv", "prepare_reduced_dspacing_cif", "providers"]
48 changes: 48 additions & 0 deletions src/ess/dream/io/cif.py
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
8 changes: 7 additions & 1 deletion src/ess/dream/workflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)

import itertools

import sciline
import scipp as sc

Expand All @@ -13,8 +15,11 @@
VanadiumRun,
)

from .io.cif import CIFAuthors, prepare_reduced_dspacing_cif
from .io.geant4 import LoadGeant4Workflow

_dream_providers = (prepare_reduced_dspacing_cif,)


def default_parameters() -> dict:
# Quantities not available in the simulated data
Expand All @@ -28,6 +33,7 @@ def default_parameters() -> dict:
NeXusSource[VanadiumRun]: source,
AccumulatedProtonCharge[SampleRun]: charge,
AccumulatedProtonCharge[VanadiumRun]: charge,
CIFAuthors: CIFAuthors([]),
}


Expand All @@ -36,7 +42,7 @@ def DreamGeant4Workflow() -> sciline.Pipeline:
Workflow with default parameters for the Dream Geant4 simulation.
"""
wf = LoadGeant4Workflow()
for provider in powder_providers:
for provider in itertools.chain(powder_providers, _dream_providers):
wf.insert(provider)
for key, value in default_parameters().items():
wf[key] = value
Expand Down
4 changes: 2 additions & 2 deletions src/ess/powder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .masking import with_pixel_mask_filenames

try:
__version__ = importlib.metadata.version(__package__ or __name__)
__version__ = importlib.metadata.version("essdiffraction")
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

Expand All @@ -35,12 +35,12 @@
"""Sciline providers for powder diffraction."""

__all__ = [
"__version__",
"conversion",
"correction",
"filtering",
"grouping",
"masking",
"nexus",
"transform",
"providers",
"smoothing",
Expand Down
Loading

0 comments on commit 7772e69

Please sign in to comment.