Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ScippNeutron's metadata utilities #124

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
29 changes: 23 additions & 6 deletions src/ess/dream/io/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
from scippneutron.io import cif

from ess.powder.calibration import OutputCalibrationData
from ess.powder.types import CIFAuthors, IofTof, ReducedTofCIF
from ess.powder.types import (
Beamline,
CIFAuthors,
IofTof,
ReducedTofCIF,
ReducerSoftwares,
Source,
)


def prepare_reduced_tof_cif(
da: IofTof, *, authors: CIFAuthors, calibration: OutputCalibrationData
da: IofTof,
*,
authors: CIFAuthors,
beamline: Beamline,
source: Source,
reducers: ReducerSoftwares,
calibration: OutputCalibrationData,
) -> ReducedTofCIF:
"""Construct a CIF builder with reduced data in d-spacing.

Expand All @@ -24,6 +37,12 @@ def prepare_reduced_tof_cif(
Reduced 1d data with a ``'tof'`` dimension and coordinate.
authors:
List of authors to write to the file.
beamline:
Information about the beamline that the data was produced at.
source:
Information about the neutron source.
reducers:
List of software pieces used to reduce the data.
calibration:
Coefficients for conversion between d-spacing and final ToF.
See :meth:`scippneutron.io.cif.CIF.with_powder_calibration`.
Expand All @@ -34,14 +53,12 @@ def prepare_reduced_tof_cif(
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 ReducedTofCIF(
cif.CIF('reduced_tof')
.with_reducers(f'ess.dream v{__version__}')
.with_reducers(*(reducer.compact_repr for reducer in reducers))
.with_authors(*authors)
.with_beamline(beamline='DREAM', facility='ESS')
.with_beamline(beamline, source)
.with_powder_calibration(calibration.to_cif_format())
.with_reduced_powder_data(to_save)
)
Expand Down
17 changes: 17 additions & 0 deletions src/ess/dream/io/geant4.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import sciline
import scipp as sc
import scippnexus as snx
from scippneutron.metadata import ESS_SOURCE

from ess.powder.types import (
Beamline,
CalibratedDetector,
CalibratedMonitor,
CalibrationData,
Expand All @@ -23,6 +25,7 @@
Position,
RunType,
SampleRun,
Source,
VanadiumRun,
)
from ess.reduce.nexus.types import CalibratedBeamline
Expand Down Expand Up @@ -269,6 +272,18 @@ def dummy_sample_position() -> Position[snx.NXsample, RunType]:
)


def dream_beamline() -> Beamline:
return Beamline(
name="DREAM",
facility="ESS",
site="ESS",
)


def ess_source() -> Source:
return ESS_SOURCE


def LoadGeant4Workflow() -> sciline.Pipeline:
"""
Workflow for loading NeXus data.
Expand All @@ -285,4 +300,6 @@ def LoadGeant4Workflow() -> sciline.Pipeline:
wf.insert(dummy_assemble_monitor_data)
wf.insert(dummy_source_position)
wf.insert(dummy_sample_position)
wf.insert(dream_beamline)
wf.insert(ess_source)
return wf
15 changes: 14 additions & 1 deletion src/ess/dream/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sciline
import scipp as sc
import scippnexus as snx
from scippneutron.metadata import Software

from ess.powder import providers as powder_providers
from ess.powder import with_pixel_mask_filenames
Expand All @@ -20,6 +21,7 @@
CaveMonitorPosition, # Should this be a DREAM-only parameter?
PixelMaskFilename,
Position,
ReducerSoftwares,
SampleRun,
TofMask,
TwoThetaMask,
Expand Down Expand Up @@ -50,14 +52,25 @@ def default_parameters() -> dict:
Position[snx.NXsource, VanadiumRun]: source_position,
AccumulatedProtonCharge[SampleRun]: charge,
AccumulatedProtonCharge[VanadiumRun]: charge,
CIFAuthors: CIFAuthors([]),
TofMask: None,
WavelengthMask: None,
TwoThetaMask: None,
CaveMonitorPosition: sc.vector([0.0, 0.0, -4220.0], unit='mm'),
CIFAuthors: CIFAuthors([]),
ReducerSoftwares: _collect_reducer_software(),
}


def _collect_reducer_software() -> ReducerSoftwares:
return ReducerSoftwares(
[
Software.from_package_metadata('essdiffraction'),
Software.from_package_metadata('scippneutron'),
Software.from_package_metadata('scipp'),
]
)


def DreamGeant4Workflow(*, run_norm: RunNormalization) -> sciline.Pipeline:
"""
Workflow with default parameters for the Dream Geant4 simulation.
Expand Down
11 changes: 10 additions & 1 deletion src/ess/powder/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sciline
import scipp as sc
from scippneutron.io import cif
from scippneutron.metadata import Person, Software

from ess.reduce.nexus import types as reduce_t
from ess.reduce.uncertainty import UncertaintyBroadcastMode as _UncertaintyBroadcastMode
Expand Down Expand Up @@ -181,8 +182,16 @@ class RawDataAndMetadata(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup):
WavelengthMask = NewType("WavelengthMask", Callable | None)
"""WavelengthMask is a callable that returns a mask for a given WavelengthData."""

Beamline = reduce_t.Beamline
"""Beamline metadata."""

CIFAuthors = NewType('CIFAuthors', list[cif.Author])
ReducerSoftwares = NewType('ReducerSoftware', list[Software])
"""Pieces of software used to reduce the data."""

Source = reduce_t.Source
"""Neutron source metadata."""

CIFAuthors = NewType('CIFAuthors', list[Person])
"""List of authors to save to output CIF files."""

ReducedTofCIF = NewType('ReducedTofCIF', cif.CIF)
Expand Down
Loading