Skip to content
Merged
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
1 change: 0 additions & 1 deletion packages/essdiffraction/docs/api-reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
.. autosummary::
:toctree: ../generated/functions

with_pixel_mask_filenames
```

### Submodules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"workflow[KeepEvents[VanadiumRun]] = KeepEvents(True)\n",
"workflow[KeepEvents[EmptyCanRun]] = KeepEvents(True)\n",
"\n",
"workflow[MaskedDetectorIDs] = MaskedDetectorIDs({})\n",
"workflow[PixelMasks] = PixelMasks({})\n",
"workflow[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop"
]
},
Expand Down Expand Up @@ -260,9 +260,7 @@
"unfiltered_detector = workflow.compute(CorrectedDetector[SampleRun])\n",
"filtered_detector = thresholded_workflow.compute(CorrectedDetector[SampleRun])\n",
"\n",
"two_theta_bins = sc.linspace(\"two_theta\", 76.0, 104.0, 201, unit=\"deg\").to(\n",
" unit=\"rad\"\n",
")\n",
"two_theta_bins = sc.linspace(\"two_theta\", 76.0, 104.0, 201, unit=\"deg\").to(unit=\"rad\")\n",
"wavelength_bins = sc.linspace(\"wavelength\", 0.8, 3.2, 241, unit=\"angstrom\")\n",
"\n",
"unfiltered_map = unfiltered_detector.hist(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"import scippneutron as scn\n",
"import scippneutron.peaks\n",
"\n",
"from ess import powder\n",
"from ess.snspowder import powgen\n",
"from ess.powder.types import *"
]
Expand Down Expand Up @@ -78,7 +77,7 @@
"workflow[TwoThetaMask] = None\n",
"workflow[WavelengthMask] = None\n",
"\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.3, 2.3434, 201, unit=\"angstrom\")\n",
"\n",
"# Do not mask any pixels / voxels:\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down Expand Up @@ -194,7 +194,7 @@
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.3, 2.3434, 201, unit=\"angstrom\")\n",
"\n",
"# Do not mask any pixels / voxels:\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down Expand Up @@ -269,7 +269,7 @@
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.3, 2.3434, 201, unit=\"angstrom\")\n",
"\n",
"# Do not mask any pixels / voxels:\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down Expand Up @@ -381,7 +381,7 @@
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.3, 2.3434, 201, unit=\"angstrom\")\n",
"\n",
"# Do not mask any pixels / voxels:\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.3, 2.3434, 201, unit=\"angstrom\")\n",
"\n",
"# Do not mask any pixels / voxels:\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"import scippneutron as scn\n",
"import scippneutron.io\n",
"\n",
"from ess import powder\n",
"from ess.snspowder import powgen\n",
"import ess.snspowder.powgen.data # noqa: F401\n",
"from ess.powder.types import *"
Expand Down Expand Up @@ -84,7 +83,7 @@
"workflow[TwoThetaMask] = None\n",
"workflow[WavelengthMask] = None\n",
"# No pixel masks\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow[PixelMaskFilenames] = []"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions packages/essdiffraction/src/ess/dream/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
IntensityDspacingTwoTheta,
MonitorFilename,
NeXusDetectorName,
PixelMaskFilename,
PixelMaskFilenames,
ReducedTofCIF,
SampleRun,
UncertaintyBroadcastMode,
Expand Down Expand Up @@ -52,8 +52,8 @@
parameter_registry[MonitorFilename[VanadiumRun]] = FilenameParameter.from_type(
MonitorFilename[VanadiumRun]
)
parameter_registry[PixelMaskFilename] = MultiFilenameParameter.from_type(
PixelMaskFilename,
parameter_registry[PixelMaskFilenames] = MultiFilenameParameter.from_type(
PixelMaskFilenames,
)
parameter_registry[NeXusDetectorName] = StringParameter.from_type(
NeXusDetectorName, default="mantle"
Expand Down
5 changes: 0 additions & 5 deletions packages/essdiffraction/src/ess/dream/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import scipp as sc
import scippnexus as snx
from ess.powder import providers as powder_providers
from ess.powder import with_pixel_mask_filenames
from ess.powder.correction import RunNormalization, insert_run_normalization
from ess.powder.types import (
AccumulatedProtonCharge,
Expand All @@ -19,7 +18,6 @@
LookupTableFilename,
LookupTableRelativeErrorThreshold,
Measurement,
PixelMaskFilename,
Position,
ReducerSoftware,
SampleRun,
Expand All @@ -32,7 +30,6 @@
from scippneutron.metadata import Software

from ess.reduce.nexus.types import DetectorBankSizes, NeXusName
from ess.reduce.parameter import parameter_mappers
from ess.reduce.unwrap import GenericUnwrapWorkflow, WavelengthLutMode
from ess.reduce.workflow import register_workflow

Expand Down Expand Up @@ -149,8 +146,6 @@ def DreamWorkflow(
prepare_reduced_empty_can_subtracted_tof_cif,
)

parameter_mappers[PixelMaskFilename] = with_pixel_mask_filenames


def default_parameters() -> dict:
"""Return the default reduction parameters shared by DREAM powder workflows."""
Expand Down
2 changes: 0 additions & 2 deletions packages/essdiffraction/src/ess/powder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
workflow,
)
from .correction import RunNormalization
from .masking import with_pixel_mask_filenames

try:
__version__ = importlib.metadata.version("essdiffraction")
Expand Down Expand Up @@ -51,6 +50,5 @@
"masking",
"providers",
"smoothing",
"with_pixel_mask_filenames",
"workflow",
]
58 changes: 6 additions & 52 deletions packages/essdiffraction/src/ess/powder/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
Masking functions for the powder workflow.
"""

from collections.abc import Iterable

import numpy as np
import sciline
import scipp as sc

from ess.reduce.masking import read_pixel_masks, to_detector_masks

from .types import (
MaskedDetectorIDs,
PixelMaskFilename,
PixelMasks,
RunType,
TofMask,
TwoThetaMask,
Expand All @@ -21,35 +18,20 @@
)


def read_pixel_masks(filename: PixelMaskFilename) -> MaskedDetectorIDs:
"""Read a pixel mask from a Scipp hdf5 file.

Parameters
----------
filename:
Path to the hdf5 file.
"""
return MaskedDetectorIDs({filename: sc.io.load_hdf5(filename)})


def apply_masks(
data: WavelengthDetector[RunType],
masked_pixel_ids: MaskedDetectorIDs,
pixel_masks: PixelMasks,
tof_mask_func: TofMask,
wavelength_mask_func: WavelengthMask,
two_theta_mask_func: TwoThetaMask,
) -> sc.DataArray:
""" """
out = data.copy(deep=False)
if len(masked_pixel_ids) > 0:
if len(pixel_masks) > 0:
key = (
set(out.coords.keys()) & {"detector_number", "detector_id", "spectrum"}
).pop()
ids = out.coords[key]
for name, masked in masked_pixel_ids.items():
mask = sc.zeros(sizes=ids.sizes, dtype="bool")
mask.values[np.isin(ids.values, masked.values)] = True
out.masks[name] = mask
out = out.assign_masks(to_detector_masks(pixel_masks, out, id_coord=key))

for dim, mask in {
"tof": tof_mask_func,
Expand All @@ -71,31 +53,3 @@ def apply_masks(


providers = (read_pixel_masks,)


def _merge(*dicts: dict) -> dict:
return {key: value for d in dicts for key, value in d.items()}


def with_pixel_mask_filenames(
workflow: sciline.Pipeline, masks: Iterable[str]
) -> sciline.Pipeline:
"""
Return modified workflow with pixel mask filenames set.

Parameters
----------
workflow:
Workflow to modify.
masks:
List or tuple of pixel mask filenames to set.
"""
workflow = workflow.copy()
# Workaround bug in Cyclebane, which does not allow empty maps
if len(masks) == 0:
workflow[MaskedDetectorIDs] = MaskedDetectorIDs({})
return workflow
workflow[MaskedDetectorIDs] = (
workflow[MaskedDetectorIDs].map({PixelMaskFilename: masks}).reduce(func=_merge)
)
return workflow
8 changes: 3 additions & 5 deletions packages/essdiffraction/src/ess/powder/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from scippneutron.io import cif
from scippneutron.metadata import Person, Software

from ess.reduce import masking as masking_t
from ess.reduce.nexus import types as reduce_t
from ess.reduce.uncertainty import UncertaintyBroadcastMode as _UncertaintyBroadcastMode
from ess.reduce.unwrap import types as unwrap_t
Expand Down Expand Up @@ -177,9 +178,6 @@ class CorrectedDetector(sciline.Scope[RunType, sc.DataArray], sc.DataArray):
"""


MaskedDetectorIDs = NewType("MaskedDetectorIDs", dict[str, sc.Variable])
"""1-D variable listing all masked detector IDs."""

CaveMonitorPosition = NewType("CaveMonitorPosition", sc.Variable)
"""Position of DREAM's cave monitor."""

Expand All @@ -204,8 +202,8 @@ class NormalizedDspacing(sciline.Scope[RunType, sc.DataArray], sc.DataArray):
"""Detector data in d-spacing - 2theta space normalized by monitor/proton charge."""


PixelMaskFilename = NewType("PixelMaskFilename", str)
"""Filename of a pixel mask."""
PixelMaskFilenames = masking_t.PixelMaskFilenames
PixelMasks = masking_t.PixelMasks


class RawDataAndMetadata(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup):
Expand Down
6 changes: 3 additions & 3 deletions packages/essdiffraction/src/ess/powder/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .types import (
CorrectedDetector,
DspacingDetector,
MaskedDetectorIDs,
PixelMasks,
RunType,
TofMask,
TwoThetaMask,
Expand All @@ -13,14 +13,14 @@

def add_masks_and_corrections(
da: DspacingDetector[RunType],
masked_pixel_ids: MaskedDetectorIDs,
pixel_masks: PixelMasks,
tof_mask_func: TofMask,
wavelength_mask_func: WavelengthMask,
two_theta_mask_func: TwoThetaMask,
) -> CorrectedDetector[RunType]:
masked = apply_masks(
data=da,
masked_pixel_ids=masked_pixel_ids,
pixel_masks=pixel_masks,
tof_mask_func=tof_mask_func,
wavelength_mask_func=wavelength_mask_func,
two_theta_mask_func=two_theta_mask_func,
Expand Down
4 changes: 2 additions & 2 deletions packages/essdiffraction/tests/beer/mcstas_reduction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
ElasticCoordTransformGraph,
EmptyDetector,
KeepEvents,
MaskedDetectorIDs,
NormalizedDspacing,
PixelMasks,
QDetector,
SampleRun,
TofMask,
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_modulation_workflows_can_normalize(factory):
wf[DetectorBank] = DetectorBank.north
wf[DHKLList] = silicon_peaks_array()
wf[DspacingBins] = sc.linspace('dspacing', 0.8, 2.2, 31, unit='angstrom')
wf[MaskedDetectorIDs] = MaskedDetectorIDs({})
wf[PixelMasks] = PixelMasks({})
wf[KeepEvents[SampleRun]] = KeepEvents[SampleRun](True)
wf[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop
wf[TofMask] = None
Expand Down
Loading
Loading